From d287f5a386d65b881aba9cb3a92e4115fe00c580 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 30 May 2018 12:29:38 +0900 Subject: [PATCH 1/9] add phpstan.neon --- phpstan.neon | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 phpstan.neon diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..e69de29 From 1f30395478e5b90073762bae214b54df2903868a Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 30 May 2018 12:29:58 +0900 Subject: [PATCH 2/9] avoid "return null" --- src/TwigRenderer.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/TwigRenderer.php b/src/TwigRenderer.php index ce718ae..8362b28 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 From 667b20505ae621e022cfdc9ae748481a5953fa0c Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 30 May 2018 12:37:16 +0900 Subject: [PATCH 3/9] make tests class loadable --- composer.json | 7 +++++-- tests/AppMetaTest.php | 6 ++---- tests/ArrayLoaderTest.php | 7 ++++--- tests/ExtensionTest.php | 4 ++-- tests/FileLoaderTest.php | 8 ++++---- tests/WeavedResourceTest.php | 5 ++--- 6 files changed, 19 insertions(+), 18 deletions(-) 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/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/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/FileLoaderTest.php b/tests/FileLoaderTest.php index 609275d..cea9b2b 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; 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; From 2efa53742e68f7123580ae727d197925d40c03ac Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 30 May 2018 12:39:11 +0900 Subject: [PATCH 4/9] fix @var order --- tests/AppPathProviderTest.php | 2 +- tests/FileLoaderTest.php | 4 ++-- tests/OptionProviderTest.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/AppPathProviderTest.php b/tests/AppPathProviderTest.php index d22bab0..d30495d 100644 --- a/tests/AppPathProviderTest.php +++ b/tests/AppPathProviderTest.php @@ -25,7 +25,7 @@ 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()); } diff --git a/tests/FileLoaderTest.php b/tests/FileLoaderTest.php index cea9b2b..7cfa4e6 100644 --- a/tests/FileLoaderTest.php +++ b/tests/FileLoaderTest.php @@ -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/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()); } From b512409596d13da516171d9ce6abc9994d5e4492 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 30 May 2018 12:39:33 +0900 Subject: [PATCH 5/9] explicity public property --- tests/Fake/src/Resource/Page/Index.php | 2 ++ 1 file changed, 2 insertions(+) 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; From c9a9a20444ef7d322a6f91d79c92c40845852849 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 30 May 2018 12:41:43 +0900 Subject: [PATCH 6/9] remove redundant third parameter --- tests/MobileTemplateFinderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/MobileTemplateFinderTest.php b/tests/MobileTemplateFinderTest.php index 6beaf86..d746ca3 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->__invoke($_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->__invoke($_ENV['TEST_DIR'] . '/Resource/Page/Index.php'); $expected = 'Page/Index.html.twig'; $this->assertSame($expected, $file); } From 1d5a591db9e9c314c6ef7e96bda8f7808232c43d Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 30 May 2018 12:43:14 +0900 Subject: [PATCH 7/9] function call --- src/MobileTemplateFinder.php | 2 +- src/TwigRenderer.php | 2 +- tests/MobileTemplateFinderTest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) 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 8362b28..1b6a660 100644 --- a/src/TwigRenderer.php +++ b/src/TwigRenderer.php @@ -86,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/MobileTemplateFinderTest.php b/tests/MobileTemplateFinderTest.php index d746ca3..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'); + $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'); + $file = ($templateFinder)($_ENV['TEST_DIR'] . '/Resource/Page/Index.php'); $expected = 'Page/Index.html.twig'; $this->assertSame($expected, $file); } From d4a9fd8d2592907cabeb36b70beb9206db72e76f Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 30 May 2018 12:47:19 +0900 Subject: [PATCH 8/9] soothe phpstan --- tests/AppPathProviderTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/AppPathProviderTest.php b/tests/AppPathProviderTest.php index d30495d..a4f46d7 100644 --- a/tests/AppPathProviderTest.php +++ b/tests/AppPathProviderTest.php @@ -27,6 +27,9 @@ public function testAppPathProvider() /** @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()); } } From 640a8b081bb10ba76e2fc8dc0e22d1971d5e625b Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 30 May 2018 12:50:23 +0900 Subject: [PATCH 9/9] update .travis.yml --- .travis.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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