diff --git a/.travis.yml b/.travis.yml index c34fdde..9734eb0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: php sudo: false dist: trusty php: - - 7.0 + - 7 - 7.1 - 7.2 cache: @@ -16,9 +16,11 @@ env: before_script: - composer self-update - composer update $DEPENDENCIES + - if [[ $TRAVIS_PHP_VERSION = '7.1' && DEPENDENCIES = '' ]]; then COVERAGE="--coverage-clover=coverage.clover"; else phpenv config-rm xdebug.ini; fi + - if [[ $TRAVIS_PHP_VERSION = '7.1' && DEPENDENCIES = '' ]]; then composer require --dev phpstan/phpstan-shim friendsofphp/php-cs-fixer; fi script: - - ./vendor/bin/phpunit --coverage-clover=coverage.clover; + - ./vendor/bin/phpunit $COVERAGE; - if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.4.0/php-cs-fixer.phar -O php-cs-fixer && php php-cs-fixer fix --config=.php_cs -v --dry-run --using-cache=no --path-mode=intersection `git diff --name-only --diff-filter=ACMRTUXB $TRAVIS_COMMIT_RANGE`; fi - + - if [[ $TRAVIS_PHP_VERSION = '7.1' && DEPENDENCIES = '' ]]; then ./vendor/bin/phpstan analyse -l max src tests --no-progress --no-interaction; fi after_script: - if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi diff --git a/composer.json b/composer.json index 13a92ae..0c82c9b 100644 --- a/composer.json +++ b/composer.json @@ -32,8 +32,11 @@ } }, "autoload-dev":{ - "psr-4":{ - "Madapaja\\TwigModule\\": "tests/Fake/src" + "psr-4": { + "Madapaja\\TwigModule\\": [ + "tests", + "tests/Fake/src" + ] } }, "scripts": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..e69de29 diff --git a/src/MobileTemplateFinder.php b/src/MobileTemplateFinder.php index 27fca44..548b3b3 100644 --- a/src/MobileTemplateFinder.php +++ b/src/MobileTemplateFinder.php @@ -42,7 +42,7 @@ public function __construct($userAgent = '', array $paths = []) */ public function __invoke(string $name) : string { - $templatePath = $this->templateFinder->__invoke($name); + $templatePath = ($this->templateFinder)($name); $detect = new \Mobile_Detect(null, $this->userAgent); $isMobile = $detect->isMobile() && ! $detect->isTablet(); if ($isMobile) { diff --git a/src/TwigRenderer.php b/src/TwigRenderer.php index ce718ae..1b6a660 100644 --- a/src/TwigRenderer.php +++ b/src/TwigRenderer.php @@ -70,12 +70,10 @@ private function load(ResourceObject $ro) try { return $this->loadTemplate($ro); } catch (\Twig_Error_Loader $e) { - if ($ro->code !== 200) { - return; + if ($ro->code === 200) { + throw new Exception\TemplateNotFound($e->getMessage(), 500, $e); } } - - throw new Exception\TemplateNotFound($e->getMessage(), 500, $e); } private function isNoContent(ResourceObject $ro) : bool @@ -88,7 +86,7 @@ private function loadTemplate(ResourceObject $ro) : \Twig_TemplateWrapper $loader = $this->twig->getLoader(); if ($loader instanceof \Twig_Loader_Filesystem) { $classFile = $this->getReflection($ro)->getFileName(); - $templateFile = $this->templateFinder->__invoke($classFile); + $templateFile = ($this->templateFinder)($classFile); return $this->twig->load($templateFile); } diff --git a/tests/AppMetaTest.php b/tests/AppMetaTest.php index 17199ec..e5fb457 100644 --- a/tests/AppMetaTest.php +++ b/tests/AppMetaTest.php @@ -4,11 +4,9 @@ * * @license http://opensource.org/licenses/MIT MIT */ -namespace Madapaja\TwigModule\Resource\Page; +namespace Madapaja\TwigModule; use BEAR\Resource\RenderInterface; -use Madapaja\TwigModule\TwigAppMetaTestModule; -use Madapaja\TwigModule\TwigRenderer; use PHPUnit_Framework_TestCase; use Ray\Di\Injector; @@ -16,7 +14,7 @@ class AppMetaTest extends PHPUnit_Framework_TestCase { public function testRenderer() { - /** @var $renderer TwigRenderer */ + /** @var TwigRenderer $renderer */ $renderer = (new Injector(new TwigAppMetaTestModule()))->getInstance(RenderInterface::class); $this->assertInstanceOf(TwigRenderer::class, $renderer); $this->assertFalse($renderer->twig->isDebug()); diff --git a/tests/AppPathProviderTest.php b/tests/AppPathProviderTest.php index d22bab0..a4f46d7 100644 --- a/tests/AppPathProviderTest.php +++ b/tests/AppPathProviderTest.php @@ -25,8 +25,11 @@ public function testAppPathProvider() } } - /** @var $renderer TwigRenderer */ + /** @var TwigRenderer $renderer */ $renderer = (new Injector(new AppPathProviderTestModule($appDir)))->getInstance(TwigRenderer::class); - $this->assertSame($paths, $renderer->twig->getLoader()->getPaths()); + /** @var \Twig_Loader_Filesystem $loader */ + $loader = $renderer->twig->getLoader(); + $this->assertInstanceOf(\Twig_Loader_Filesystem::class, $loader); + $this->assertSame($paths, $loader->getPaths()); } } diff --git a/tests/ArrayLoaderTest.php b/tests/ArrayLoaderTest.php index 935965c..a5898d0 100644 --- a/tests/ArrayLoaderTest.php +++ b/tests/ArrayLoaderTest.php @@ -4,10 +4,11 @@ * * @license http://opensource.org/licenses/MIT MIT */ -namespace Madapaja\TwigModule\Resource\Page; +namespace Madapaja\TwigModule; -use Madapaja\TwigModule\TwigArrayLoaderTestModule; -use Madapaja\TwigModule\TwigRenderer; +use Madapaja\TwigModule\Resource\Page\Index; +use Madapaja\TwigModule\Resource\Page\NoTemplate; +use Madapaja\TwigModule\Resource\Page\Page; use PHPUnit_Framework_TestCase; use Ray\Di\Injector; diff --git a/tests/ExtensionTest.php b/tests/ExtensionTest.php index e125a06..e6e5615 100644 --- a/tests/ExtensionTest.php +++ b/tests/ExtensionTest.php @@ -4,9 +4,9 @@ * * @license http://opensource.org/licenses/MIT MIT */ -namespace Madapaja\TwigModule\Resource\Page; +namespace Madapaja\TwigModule; -use Madapaja\TwigModule\TwigExtensionTestModule; +use Madapaja\TwigModule\Resource\Page\TwigFilter; use PHPUnit_Framework_TestCase; use Ray\Di\Injector; diff --git a/tests/Fake/src/Resource/Page/Index.php b/tests/Fake/src/Resource/Page/Index.php index f75ebdc..df704ef 100644 --- a/tests/Fake/src/Resource/Page/Index.php +++ b/tests/Fake/src/Resource/Page/Index.php @@ -10,6 +10,8 @@ class Index extends ResourceObject { + public $templatePath; + public function onGet($name = 'BEAR.Sunday') { $this['name'] = $name; diff --git a/tests/FileLoaderTest.php b/tests/FileLoaderTest.php index 609275d..7cfa4e6 100644 --- a/tests/FileLoaderTest.php +++ b/tests/FileLoaderTest.php @@ -4,12 +4,12 @@ * * @license http://opensource.org/licenses/MIT MIT */ -namespace Madapaja\TwigModule\Resource\Page; +namespace Madapaja\TwigModule; use BEAR\Resource\Code; -use Madapaja\TwigModule\TwigAppMetaTestModule; -use Madapaja\TwigModule\TwigFileLoaderTestModule; -use Madapaja\TwigModule\TwigRenderer; +use Madapaja\TwigModule\Resource\Page\Index; +use Madapaja\TwigModule\Resource\Page\NoTemplate; +use Madapaja\TwigModule\Resource\Page\Page; use PHPUnit_Framework_TestCase; use Ray\Di\Injector; @@ -32,11 +32,11 @@ public function testRenderer() public function testTwigOptions() { - /** @var $renderer TwigRenderer */ + /** @var TwigRenderer $renderer */ $renderer = (new Injector(new TwigFileLoaderTestModule([$_ENV['TEST_DIR'] . '/Fake/src/Resource'], ['debug' => true])))->getInstance(TwigRenderer::class); $this->assertTrue($renderer->twig->isDebug()); - /** @var $renderer TwigRenderer */ + /** @var TwigRenderer $renderer */ $renderer = (new Injector(new TwigFileLoaderTestModule([$_ENV['TEST_DIR'] . '/Fake/src/Resource'], ['debug' => false])))->getInstance(TwigRenderer::class); $this->assertFalse($renderer->twig->isDebug()); } diff --git a/tests/MobileTemplateFinderTest.php b/tests/MobileTemplateFinderTest.php index 6beaf86..9f8ab5d 100644 --- a/tests/MobileTemplateFinderTest.php +++ b/tests/MobileTemplateFinderTest.php @@ -25,7 +25,7 @@ public function testMobileTemplate() $iphone = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A523 Safari/8536.25'; $paths = [$_ENV['TEST_DIR'] . '/Fake/src/Resource']; $templateFinder = new MobileTemplateFinder($iphone, $paths); - $file = $templateFinder->__invoke($_ENV['TEST_DIR'] . '/Resource/Page/Index.php', $iphone); + $file = ($templateFinder)($_ENV['TEST_DIR'] . '/Resource/Page/Index.php'); $expected = 'Page/Index.mobile.twig'; $this->assertSame($expected, $file); } @@ -35,7 +35,7 @@ public function testPcTemplate() $pc = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)'; $paths = [$_ENV['TEST_DIR'] . '/Fake/src/Resource']; $templateFinder = new MobileTemplateFinder($pc, $paths); - $file = $templateFinder->__invoke($_ENV['TEST_DIR'] . '/Resource/Page/Index.php', $pc); + $file = ($templateFinder)($_ENV['TEST_DIR'] . '/Resource/Page/Index.php'); $expected = 'Page/Index.html.twig'; $this->assertSame($expected, $file); } diff --git a/tests/OptionProviderTest.php b/tests/OptionProviderTest.php index c33b2e1..9629378 100644 --- a/tests/OptionProviderTest.php +++ b/tests/OptionProviderTest.php @@ -23,7 +23,7 @@ public function setUp() public function testOptionProvider() { - /** @var $renderer TwigRenderer */ + /** @var TwigRenderer $renderer */ $renderer = (new Injector(new OptionProviderTestModule($this->tmpDir, true)))->getInstance(TwigRenderer::class); $this->assertSame($this->tmpDir . '/twig', $renderer->twig->getCache()); @@ -32,7 +32,7 @@ public function testOptionProvider() public function testOptionProviderDebugFalse() { - /** @var $renderer TwigRenderer */ + /** @var TwigRenderer $renderer */ $renderer = (new Injector(new OptionProviderTestModule($this->tmpDir, false)))->getInstance(TwigRenderer::class); $this->assertFalse($renderer->twig->isDebug()); } diff --git a/tests/WeavedResourceTest.php b/tests/WeavedResourceTest.php index a57a95f..e144c1e 100644 --- a/tests/WeavedResourceTest.php +++ b/tests/WeavedResourceTest.php @@ -4,10 +4,9 @@ * * @license http://opensource.org/licenses/MIT MIT */ -namespace Madapaja\TwigModule\Resource\Page; +namespace Madapaja\TwigModule; -use Madapaja\TwigModule\TwigRenderer; -use Madapaja\TwigModule\TwigWeavedResourceTestModule; +use Madapaja\TwigModule\Resource\Page\Index; use PHPUnit_Framework_TestCase; use Ray\Di\Injector;