Skip to content

Commit

Permalink
Merge pull request #5 from MarcusSchwarz/TAL-4
Browse files Browse the repository at this point in the history
make tests passing on 7.2 as well. fixes #4 and #1
  • Loading branch information
usox authored May 15, 2018
2 parents 8e7d695 + b1e17cd commit 6ee72dd
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 16 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ php:
- 5.6
- 7.2

matrix:
allow_failures:
- php: 7.2

before_script:
# Make sure the locales used in the tests are installed
- sudo locale-gen en_GB
Expand Down
2 changes: 1 addition & 1 deletion classes/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function fillSlot($key, $content)

public function fillSlotCallback($key, $callback, $_thistpl, $tpl)
{
assert('is_callable($callback)');
assert(is_callable($callback));
$this->_slots[$key] = array($callback, $_thistpl, $tpl);
if ($this->_parentContext) {
// Works around bug with tal:define popping context after fillslot
Expand Down
21 changes: 16 additions & 5 deletions classes/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace PhpTal;

use PhpTal\Exception\TemplateException;

class ExceptionHandler
{
private $encoding;
Expand All @@ -28,21 +30,30 @@ function __construct($encoding)
*
* Doesn't change exception handler if non-default one is set.
*
* @param \Exception e exception to re-throw and display
* @param \Exception|\Throwable $e exception to re-throw and display
* @param string $encoding
*
* @return void
* @throws \Exception
* @throws TemplateException
* @throws \Throwable
*/
public static function handleException(\Exception $e, $encoding)
public static function handleException($e, $encoding) // todo 7.2 -> $e should be \Throwable
{
// PHPTAL's handler is only useful on fresh HTTP response
if (PHP_SAPI !== 'cli' && !headers_sent()) {
$old_exception_handler = set_exception_handler(array(new ExceptionHandler($encoding), '_defaultExceptionHandler'));
$old_exception_handler = set_exception_handler([
new ExceptionHandler($encoding),
'_defaultExceptionHandler'
]);

if ($old_exception_handler !== NULL) {
if ($old_exception_handler !== null) {
restore_exception_handler(); // if there's user's exception handler, let it work
}
}

if (PHP_VERSION_ID >= 70000 && get_class($e) === \ParseError::class) {
$e = new TemplateException($e->getMessage());
}
throw $e; // throws instead of outputting immediately to support user's try/catch
}

Expand Down
19 changes: 16 additions & 3 deletions classes/PHPTAL.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,12 @@ public function execute()
$templateFunction($this, $this->_context);
$res = ob_get_clean();
}
catch (Exception $e)
catch (\Throwable $e)
{
ob_end_clean();
throw $e;
}
catch (\Exception $e)
{
ob_end_clean();
throw $e;
Expand All @@ -704,7 +709,11 @@ public function execute()
return $this->_postfilter->filter($res);
}
}
catch (Exception $e)
catch (\Throwable $e)
{
PhpTal\ExceptionHandler::handleException($e, $this->getEncoding());
}
catch (\Exception $e)
{
PhpTal\ExceptionHandler::handleException($e, $this->getEncoding());
}
Expand Down Expand Up @@ -867,7 +876,11 @@ public function prepare()
try {
eval("?>\n".$result);
}
catch(Exception $e) {
catch(\Throwable $e) {
ob_end_clean();
throw $e;
}
catch(\Exception $e) {
ob_end_clean();
throw $e;
}
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
"PhpTal\\": "classes/"
}
},
"autoload-dev": {
"classmap": [
"classes/", "tests/"
]
},
"config": {
"platform": {
"php": "5.6"
Expand Down
6 changes: 5 additions & 1 deletion tests/PhptalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ function testPHPParseErrorDoesNotStopPHPTAL2()
try {
@$tpl->execute(); // if test dies for no apparent reason, the reason is '@'
}
catch(Exception $e) {
catch(\Throwable $e) {
ob_end_clean();
throw $e;
}
catch(\Exception $e) {
ob_end_clean();
throw $e;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TalDefineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function testEmpty()
{
$tal = $this->newPHPTAL();
$tal->setSource('<div class="blank_bg" tal:define="book relative/book" tal:condition="php: count(book)>0"></div>');
$tal->relative = array('book'=>1);
$tal->relative = array('book'=>[1]);

$this->assertEquals($tal->execute(), '<div class="blank_bg"></div>');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
error_reporting( E_ALL | E_STRICT );
assert_options(ASSERT_ACTIVE, 1);

require_once 'vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';

abstract class PHPTAL_TestCase extends PHPUnit_Framework_TestCase
{
Expand Down

0 comments on commit 6ee72dd

Please sign in to comment.