diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml new file mode 100644 index 0000000..aa14250 --- /dev/null +++ b/.github/workflows/coding-standards.yml @@ -0,0 +1,12 @@ +name: Coding Standards + +on: + push: + pull_request: + workflow_dispatch: + +jobs: + cs: + uses: ray-di/.github/.github/workflows/coding-standards.yml@v1 + with: + php_version: 8.1 diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..19eda0c --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,13 @@ +name: Static Analysis + +on: + push: + pull_request: + workflow_dispatch: + +jobs: + sa: + uses: ray-di/.github/.github/workflows/static-analysis.yml@v1 + with: + php_version: 8.1 + has_crc_config: true diff --git a/.github/workflows/update-copyright-years-in-license-file.yml b/.github/workflows/update-copyright-years-in-license-file.yml index fc00479..f432106 100644 --- a/.github/workflows/update-copyright-years-in-license-file.yml +++ b/.github/workflows/update-copyright-years-in-license-file.yml @@ -1,4 +1,4 @@ -name: Update copyright year(s) in license file +name: Update copyright year in license file on: workflow_dispatch: diff --git a/.gitignore b/.gitignore index 5a3f380..b75d795 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /composer.lock /.php_cs.cache /.phpunit.result.cache +/.phpcs-cache diff --git a/composer-require-checker.json b/composer-require-checker.json new file mode 100644 index 0000000..ad6a80c --- /dev/null +++ b/composer-require-checker.json @@ -0,0 +1,18 @@ +{ + "symbol-whitelist" : [ + "Mobile_Detect" + ], + "php-core-extensions" : [ + "Core", + "date", + "json", + "hash", + "pcre", + "Phar", + "Reflection", + "SPL", + "random", + "standard" + ], + "scan-files" : [] +} \ No newline at end of file diff --git a/composer.json b/composer.json index f5f2cf8..f9bfd70 100644 --- a/composer.json +++ b/composer.json @@ -15,22 +15,25 @@ ], "require": { "php": "^8.0", - "bear/resource": "^1.15.2", + "bear/resource": "^1.20", "bear/sunday": "^1.2", - "bear/app-meta": "^1.1", + "bear/app-meta": "^1.8", "twig/twig": "^2.15.3 || ^v3.4.3", - "mobiledetect/mobiledetectlib": "^2.7", + "mobiledetect/mobiledetectlib": "^2.7 || ^3.74", "ray/di": "^2.7", - "doctrine/annotations": "^1.13.3" + "ray/aop": "^2.13", + "psr/log": "^3.0" }, "require-dev": { + "doctrine/annotations": "^1.13.3 || ^2.0", "phpunit/phpunit": "^9.5.21", "squizlabs/php_codesniffer": "^3.7", "phpmd/phpmd": "^2.6", "rector/rector": "^0.15.3", "ray/rector-ray": "^1.0", - "doctrine/coding-standard": "^11.1", - "vimeo/psalm": "^5.4" + "doctrine/coding-standard": "^12.0", + "vimeo/psalm": "^5.4", + "phpstan/phpstan": "^1.10" }, "autoload":{ "psr-4":{ @@ -46,13 +49,12 @@ } }, "scripts": { - "test": ["@setup", "vendor/bin/phpunit"], - "test74": ["/usr/local/Cellar/php@7.4/7.4.14/bin/php vendor/bin/phpunit"], - "tests": ["@cs", "phpstan analyse -l max src tests -c phpstan.neon --no-progress", "@test"], - "coverage": ["php -dzend_extension=xdebug.so -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"], - "cs": ["php-cs-fixer fix -v --dry-run", "phpcs --standard=phpcs.xml src;"], + "test": ["phpunit"], + "tests": ["@cs", "@sa", "@test"], + "coverage": ["php -dzend_extension=xdebug.so -dxdebug.mode=coverage phpunit --coverage-text --coverage-html=build/coverage"], + "cs": ["phpcs --standard=phpcs.xml src"], "cs-fix": ["phpcbf src tests"], - "sa": "psalm" + "sa": ["psalm", "phpstan"] }, "config": { "allow-plugins": { diff --git a/phpstan.neon b/phpstan.neon index e69de29..6acfdee 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -0,0 +1,9 @@ +parameters: + level: max + paths: + - src + - tests + excludePaths: + - */tests/tmp/* + - */tests/Fake/* + ignoreErrors: diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 68% rename from phpunit.xml rename to phpunit.xml.dist index 839e7d3..b3cf599 100644 --- a/phpunit.xml +++ b/phpunit.xml.dist @@ -1,12 +1,12 @@ - + src - tests + tests diff --git a/psalm.xml b/psalm.xml index b8a42df..50b16d2 100644 --- a/psalm.xml +++ b/psalm.xml @@ -5,6 +5,13 @@ xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" > + + + + + + + diff --git a/src/AppPathProvider.php b/src/AppPathProvider.php index 91e2bd1..51fa85b 100644 --- a/src/AppPathProvider.php +++ b/src/AppPathProvider.php @@ -16,7 +16,7 @@ public function __construct( } /** - * {@inheritdoc} + * {@inheritDoc} */ public function get(): array { diff --git a/src/ErrorPagerRenderer.php b/src/ErrorPagerRenderer.php index facb77d..e4fa4ff 100644 --- a/src/ErrorPagerRenderer.php +++ b/src/ErrorPagerRenderer.php @@ -12,6 +12,9 @@ use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; +use function assert; +use function is_array; + class ErrorPagerRenderer implements RenderInterface { public function __construct( @@ -28,6 +31,7 @@ public function __construct( */ public function render(ResourceObject $ro): string { + assert(is_array($ro->body)); $ro->view = $this->twig->render($this->errorPage, $ro->body); return $ro->view; diff --git a/src/MobileTemplateFinder.php b/src/MobileTemplateFinder.php index 14acc92..227c754 100644 --- a/src/MobileTemplateFinder.php +++ b/src/MobileTemplateFinder.php @@ -4,32 +4,41 @@ namespace Madapaja\TwigModule; +use Detection\MobileDetect; use Madapaja\TwigModule\Annotation\TwigPaths; use Mobile_Detect; +use Ray\Di\Di\Named; +use function class_alias; +use function class_exists; use function file_exists; use function sprintf; use function str_replace; class MobileTemplateFinder implements TemplateFinderInterface { - private TemplateFinder $templateFinder; - + /** @param array $paths */ public function __construct( - private mixed $userAgent = '', #[TwigPaths] - private array $paths = [], + private array $paths, + #[Named('original')] + private TemplateFinderInterface $templateFinder, ) { - $this->templateFinder = new TemplateFinder(); } /** - * {@inheritdoc} + * {@inheritDoc} */ public function __invoke(string $name): string { + if (! class_exists(MobileDetect::class)) { + // @codeCoverageIgnoreStart + class_alias(Mobile_Detect::class, MobileDetect::class); // @phpstan-ignore-line + // @codeCoverageIgnoreEnd + } + + $detect = new MobileDetect(); $templatePath = ($this->templateFinder)($name); - $detect = new Mobile_Detect(null, $this->userAgent); $isMobile = $detect->isMobile() && ! $detect->isTablet(); if ($isMobile) { $mobilePath = str_replace(TwigRenderer::EXT, '.mobile.twig', $templatePath); diff --git a/src/MobileTwigModule.php b/src/MobileTwigModule.php index 95491f1..de88467 100644 --- a/src/MobileTwigModule.php +++ b/src/MobileTwigModule.php @@ -9,10 +9,11 @@ class MobileTwigModule extends AbstractModule { /** - * {@inheritdoc} + * {@inheritDoc} */ protected function configure() { $this->bind(TemplateFinderInterface::class)->to(MobileTemplateFinder::class); + $this->bind(TemplateFinderInterface::class)->annotatedWith('original')->to(TemplateFinder::class); } } diff --git a/src/OptionProvider.php b/src/OptionProvider.php index e55da26..4f00718 100644 --- a/src/OptionProvider.php +++ b/src/OptionProvider.php @@ -24,7 +24,7 @@ public function __construct( } /** - * {@inheritdoc} + * {@inheritDoc} */ public function get() { diff --git a/src/TemplateFinder.php b/src/TemplateFinder.php index 3763899..273e69d 100644 --- a/src/TemplateFinder.php +++ b/src/TemplateFinder.php @@ -4,6 +4,8 @@ namespace Madapaja\TwigModule; +use function assert; +use function is_int; use function str_replace; use function strpos; use function substr; @@ -11,12 +13,13 @@ class TemplateFinder implements TemplateFinderInterface { /** - * {@inheritdoc} + * {@inheritDoc} */ - public function __invoke(string $resourceFilePath): string + public function __invoke(string $name): string { - $pos = strpos($resourceFilePath, '/Resource/'); - $relativePath = substr($resourceFilePath, $pos + 10); + $pos = strpos($name, '/Resource/'); + assert(is_int($pos)); + $relativePath = substr($name, $pos + 10); return str_replace('.php', TwigRenderer::EXT, $relativePath); } diff --git a/src/TwigErrorHandler.php b/src/TwigErrorHandler.php index 4d09c3f..3776925 100644 --- a/src/TwigErrorHandler.php +++ b/src/TwigErrorHandler.php @@ -26,7 +26,7 @@ public function __construct( } /** - * {@inheritdoc} + * {@inheritDoc} */ public function handle(Throwable $e, Request $request) { @@ -56,7 +56,7 @@ public function handle(Throwable $e, Request $request) } /** - * {@inheritdoc} + * {@inheritDoc} */ public function transfer() { diff --git a/src/TwigErrorPage.php b/src/TwigErrorPage.php index 4637762..6cd469b 100644 --- a/src/TwigErrorPage.php +++ b/src/TwigErrorPage.php @@ -11,8 +11,10 @@ class TwigErrorPage extends ResourceObject { - /** @var array */ + /** @var array */ public $headers = ['content-type' => 'text/html; charset=utf-8']; + + /** @var RenderInterface */ protected $renderer; public function __sleep() @@ -23,12 +25,14 @@ public function __sleep() /** * @Inject * @Named("error_page") + * {@inheritDoc} */ #[Inject] - #[Named('error_page')] - public function setRenderer(RenderInterface $renderer): void + public function setRenderer(RenderInterface $renderer) { $this->renderer = $renderer; + + return $this; } } diff --git a/src/TwigErrorPageHandler.php b/src/TwigErrorPageHandler.php deleted file mode 100644 index f3cf3f6..0000000 --- a/src/TwigErrorPageHandler.php +++ /dev/null @@ -1,73 +0,0 @@ -isCodeExists($e) ? $e->getCode() : 503; - if ($code >= 500) { - $eStr = (string) $e; - $this->logger->error(sprintf('logref:%s %s', crc32($eStr), $eStr)); - } - - $this->errorPage->code = $code; - $this->errorPage->body = [ - 'status' => [ - 'code' => $code, - 'message' => (new Code())->statusText[$code], - ], - 'e' => [ - 'code' => $e->getCode(), - 'message' => $e->getMessage(), - ], - ]; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function transfer() - { - ($this->transfer)($this->errorPage, []); - } - - private function isCodeExists(Throwable $e): bool - { - if (! ($e instanceof NotFound) && ! ($e instanceof BadRequest) && ! ($e instanceof ServerError)) { - return false; - } - - return array_key_exists($e->getCode(), (new Code())->statusText); - } -} diff --git a/src/TwigErrorPageModule.php b/src/TwigErrorPageModule.php index 36919a7..e64d8b9 100644 --- a/src/TwigErrorPageModule.php +++ b/src/TwigErrorPageModule.php @@ -12,7 +12,7 @@ class TwigErrorPageModule extends AbstractModule { /** - * {@inheritdoc} + * {@inheritDoc} */ protected function configure() { diff --git a/src/TwigModule.php b/src/TwigModule.php index c4e5721..c166a9d 100644 --- a/src/TwigModule.php +++ b/src/TwigModule.php @@ -17,11 +17,12 @@ use function is_array; +/** @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class TwigModule extends AbstractModule { /** - * @param array $paths Twig template paths - * @param array $options Twig_Environment options + * @param array $paths Twig template paths + * @param array $options Twig_Environment options * @param AbstractModule|null $module * * @see http://twig.sensiolabs.org/api/master/Twig_Environment.html @@ -35,7 +36,7 @@ public function __construct( } /** - * {@inheritdoc} + * {@inheritDoc} */ protected function configure() { @@ -116,7 +117,7 @@ private function bindTwigRedirectPath(): void $this->bind()->annotatedWith(TwigRedirectPath::class)->toInstance('/redirect/redirect.html.twig'); } - private function isNotEmpty($var) + private function isNotEmpty(mixed $var): bool { return is_array($var) && ! empty($var); } diff --git a/src/TwigRenderer.php b/src/TwigRenderer.php index 2317180..e2d63d4 100644 --- a/src/TwigRenderer.php +++ b/src/TwigRenderer.php @@ -40,7 +40,7 @@ public function __construct( } /** - * {@inheritdoc} + * {@inheritDoc} */ public function render(ResourceObject $ro) { @@ -72,14 +72,14 @@ private function setContentType(ResourceObject $ro): void $ro->headers['Content-Type'] = 'text/html; charset=utf-8'; } - private function renderView(ResourceObject $ro) + private function renderView(ResourceObject $ro): string { $template = $this->load($ro); return $template ? $template->render($this->buildBody($ro)) : ''; } - private function renderRedirectView(ResourceObject $ro) + private function renderRedirectView(ResourceObject $ro): string { try { return $this->twig->render($this->redirectPage, ['url' => $ro->headers['Location']]); @@ -121,7 +121,7 @@ private function loadTemplate(ResourceObject $ro): TemplateWrapper { $loader = $this->twig->getLoader(); if ($loader instanceof FilesystemLoader) { - $classFile = $this->getReflection($ro)->getFileName(); + $classFile = (string) $this->getReflection($ro)->getFileName(); $templateFile = ($this->templateFinder)($classFile); return $this->twig->load($templateFile); @@ -130,15 +130,20 @@ private function loadTemplate(ResourceObject $ro): TemplateWrapper return $this->twig->load($this->getReflection($ro)->name . self::EXT); } + /** @return ReflectionClass */ private function getReflection(ResourceObject $ro): ReflectionClass { if ($ro instanceof WeavedInterface) { - return (new ReflectionClass($ro))->getParentClass(); + /** @var ReflectionClass $ref */ + $ref = (new ReflectionClass($ro))->getParentClass(); + + return $ref; } return new ReflectionClass($ro); } + /** @return array */ private function buildBody(ResourceObject $ro): array { $body = is_array($ro->body) ? $ro->body : []; diff --git a/tests/AppPathProviderTestModule.php b/tests/AppPathProviderTestModule.php index 873312d..8e65012 100644 --- a/tests/AppPathProviderTestModule.php +++ b/tests/AppPathProviderTestModule.php @@ -5,18 +5,18 @@ namespace Madapaja\TwigModule; use BEAR\AppMeta\AbstractAppMeta; -use BEAR\AppMeta\AppMeta; +use BEAR\AppMeta\Meta; use Madapaja\TwigModule\Annotation\TwigPaths; use Ray\Di\AbstractModule; class AppPathProviderTestModule extends AbstractModule { /** - * {@inheritdoc} + * {@inheritDoc} */ protected function configure() { - $this->bind(AbstractAppMeta::class)->toInstance(new AppMeta('Madapaja\TwigModule')); + $this->bind(AbstractAppMeta::class)->toInstance(new Meta('Madapaja\TwigModule')); $this->install(new TwigModule()); $this->bind()->annotatedWith(TwigPaths::class)->toProvider(AppPathProvider::class); } diff --git a/tests/ArrayLoaderTest.php b/tests/ArrayLoaderTest.php index 5a59a90..bd9939d 100644 --- a/tests/ArrayLoaderTest.php +++ b/tests/ArrayLoaderTest.php @@ -12,6 +12,8 @@ use Ray\Di\Injector; use ReflectionClass; +use function assert; + class ArrayLoaderTest extends TestCase { private Injector $injector; @@ -50,7 +52,9 @@ public function testTemplateNotFoundException(): void $ro = $this->injector->getInstance(NoTemplate::class); $prop = (new ReflectionClass($ro))->getProperty('renderer'); $prop->setAccessible(true); - $prop->getValue($ro)->render($ro); + $roValue = $prop->getValue($ro); + assert($roValue instanceof TwigRenderer); + $roValue->render($ro); } public function testPage(): void diff --git a/tests/ExtensionTest.php b/tests/ExtensionTest.php index 5aa2a40..353568c 100644 --- a/tests/ExtensionTest.php +++ b/tests/ExtensionTest.php @@ -17,7 +17,7 @@ class ExtensionTest extends TestCase public function setUp(): void { - $this->injector = new Injector(new TwigExtensionTestModule([$_ENV['TEST_DIR'] . '/Fake/src/Resource/'])); + $this->injector = new Injector(new TwigExtensionTestModule([__DIR__ . '/Fake/src/Resource/'])); } public function testTwigFilter(): void diff --git a/tests/Fake/src/FakeTransfer.php b/tests/Fake/src/FakeTransfer.php new file mode 100644 index 0000000..7a565f3 --- /dev/null +++ b/tests/Fake/src/FakeTransfer.php @@ -0,0 +1,15 @@ + 'text/html; charset=SJIS' + ]; + + public function onPost() + { + $this->code = Code::FOUND; + $this->headers['Location'] = '/path/to/baz'; + + return $this; + } +} diff --git a/tests/FileLoaderTest.php b/tests/FileLoaderTest.php index 1cbc1ec..61664dd 100644 --- a/tests/FileLoaderTest.php +++ b/tests/FileLoaderTest.php @@ -10,17 +10,19 @@ use Madapaja\TwigModule\Resource\Page\NoTemplate; use Madapaja\TwigModule\Resource\Page\Page; use Madapaja\TwigModule\Resource\Page\Redirect; +use Madapaja\TwigModule\ResourceNoTemplate\Page\RedirectNoTemplate; use PHPUnit\Framework\TestCase; use Ray\Di\Injector; use ReflectionClass; +use function assert; use function trim; class FileLoaderTest extends TestCase { public function getInjector(): Injector { - return new Injector(new TwigFileLoaderTestModule([$_ENV['TEST_DIR'] . '/Fake/src/Resource'])); + return new Injector(new TwigFileLoaderTestModule([__DIR__ . '/Fake/src/Resource'])); } public function testRenderer(): void @@ -36,11 +38,11 @@ public function testRenderer(): void public function testTwigOptions(): void { /** @var TwigRenderer $renderer */ - $renderer = (new Injector(new TwigFileLoaderTestModule([$_ENV['TEST_DIR'] . '/Fake/src/Resource'], ['debug' => true])))->getInstance(TwigRenderer::class); + $renderer = (new Injector(new TwigFileLoaderTestModule([__DIR__ . '/Fake/src/Resource'], ['debug' => true])))->getInstance(TwigRenderer::class); $this->assertTrue($renderer->twig->isDebug()); /** @var TwigRenderer $renderer */ - $renderer = (new Injector(new TwigFileLoaderTestModule([$_ENV['TEST_DIR'] . '/Fake/src/Resource'], ['debug' => false])))->getInstance(TwigRenderer::class); + $renderer = (new Injector(new TwigFileLoaderTestModule([__DIR__ . '/Fake/src/Resource'], ['debug' => false])))->getInstance(TwigRenderer::class); $this->assertFalse($renderer->twig->isDebug()); } @@ -75,7 +77,9 @@ public function testTemplateNotFoundException(): void $ro = $injector->getInstance(NoTemplate::class); $prop = (new ReflectionClass($ro))->getProperty('renderer'); $prop->setAccessible(true); - $prop->getValue($ro)->render($ro); + $renderer = $prop->getValue($ro); + assert($renderer instanceof TwigRenderer); + $renderer->render($ro); } public function testNoViewWhenCode301(): void @@ -85,7 +89,9 @@ public function testNoViewWhenCode301(): void $ro->code = 303; $prop = (new ReflectionClass($ro))->getProperty('renderer'); $prop->setAccessible(true); - $view = $prop->getValue($ro)->render($ro); + $renderer = $prop->getValue($ro); + assert($renderer instanceof TwigRenderer); + $view = $renderer->render($ro); $this->assertSame('', $view); } @@ -96,7 +102,9 @@ public function testNoContent(): void $ro->code = Code::NO_CONTENT; $prop = (new ReflectionClass($ro))->getProperty('renderer'); $prop->setAccessible(true); - $view = $prop->getValue($ro)->render($ro); + $renderer = $prop->getValue($ro); + assert($renderer instanceof TwigRenderer); + $view = $renderer->render($ro); $this->assertSame('', $view); } @@ -144,4 +152,16 @@ public function testRedirectOnPost(): void $this->assertSame('/path/to/baz', $ro->headers['Location']); $this->assertMatchesRegularExpression('#^.*Redirecting to /path/to/baz.*$#s', trim((string) $ro)); } + + public function testRedirectOnPostNoRedirectTemplate(): void + { + $injector = new Injector(new TwigFileLoaderTestModule([__DIR__ . '/Fake/src/ResourceNoTemplate'])); + $ro = $injector->getInstance(RedirectNoTemplate::class); + $ro->onPost(); + $view = (string) $ro; + $this->assertSame(Code::FOUND, $ro->code); + $this->assertSame('/path/to/baz', $ro->headers['Location']); + $this->assertSame('', $ro->view); + $this->assertSame('', $view); + } } diff --git a/tests/MobileTemplateFinderTest.php b/tests/MobileTemplateFinderTest.php index cb464a0..635bd3c 100644 --- a/tests/MobileTemplateFinderTest.php +++ b/tests/MobileTemplateFinderTest.php @@ -4,7 +4,10 @@ namespace Madapaja\TwigModule; +use BEAR\AppMeta\AbstractAppMeta; +use BEAR\AppMeta\Meta; use PHPUnit\Framework\TestCase; +use Ray\Di\AbstractModule; use Ray\Di\Injector; class MobileTemplateFinderTest extends TestCase @@ -13,25 +16,32 @@ class MobileTemplateFinderTest extends TestCase public function setUp(): void { - $this->injector = new Injector(new MobileTwigModule()); + $module = new MobileTwigModule(new TwigModule()); + $module->install(new class extends AbstractModule{ + protected function configure(): void + { + $this->bind(AbstractAppMeta::class)->toInstance(new Meta(__NAMESPACE__)); + } + }); + $this->injector = new Injector($module); } public function testMobileTemplate(): void { - $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)($_ENV['TEST_DIR'] . '/Resource/Page/Index.php'); + $_SERVER['HTTP_USER_AGENT'] = '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 = [__DIR__ . '/Fake/src/Resource']; + $templateFinder = new MobileTemplateFinder($paths, new TemplateFinder()); + $file = ($templateFinder)(__DIR__ . '/Resource/Page/Index.php'); $expected = 'Page/Index.mobile.twig'; $this->assertSame($expected, $file); } public function testPcTemplate(): void { - $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)($_ENV['TEST_DIR'] . '/Resource/Page/Index.php'); + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)'; + $paths = [__DIR__ . '/Fake/src/Resource']; + $templateFinder = new MobileTemplateFinder($paths, new TemplateFinder()); + $file = ($templateFinder)(__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 9abc26e..3039a5f 100644 --- a/tests/OptionProviderTest.php +++ b/tests/OptionProviderTest.php @@ -12,7 +12,7 @@ class OptionProviderTest extends TestCase { - public $tmpDir; + public string $tmpDir; public function setUp(): void { diff --git a/tests/TwigErrorPageHandlerTest.php b/tests/TwigErrorPageHandlerTest.php index ff669b2..c24e070 100644 --- a/tests/TwigErrorPageHandlerTest.php +++ b/tests/TwigErrorPageHandlerTest.php @@ -32,7 +32,7 @@ public function setUp(): void ); } - public function testHandle() + public function testHandle(): void { $request = new RouterMatch(); $request->method = 'get'; @@ -40,8 +40,6 @@ public function testHandle() $request->query = []; $handler = $this->handler->handle(new NotFound(), $request); $this->assertInstanceOf(TwigErrorHandler::class, $handler); - - return $handler; } /** @depends testHandle */ diff --git a/tests/TwigErrorPageModuleTest.php b/tests/TwigErrorPageModuleTest.php new file mode 100644 index 0000000..61b379b --- /dev/null +++ b/tests/TwigErrorPageModuleTest.php @@ -0,0 +1,49 @@ +install(new class extends AbstractModule { + protected function configure(): void + { + $this->bind(LoggerInterface::class)->to(NullLogger::class); + $this->bind(TransferInterface::class)->to(FakeTransfer::class); + } + }); + $injector = (new Injector($module)); + $error = $injector->getInstance(ErrorInterface::class); + assert($error instanceof ErrorInterface); + $error->handle(new RuntimeException(), new RouterMatch())->transfer(); + assert(is_array(FakeTransfer::$ro->body)); + $this->assertArrayHasKey('status', FakeTransfer::$ro->body); + $this->assertArrayHasKey('e', FakeTransfer::$ro->body); + $this->assertArrayHasKey('logref', FakeTransfer::$ro->body); + } + + public function testSerializeTwigErrorPage(): void + { + $errorPage = unserialize(serialize(new TwigErrorPage())); + $this->assertInstanceOf(TwigErrorPage::class, $errorPage); + } +} diff --git a/tests/WeavedResourceTest.php b/tests/WeavedResourceTest.php index bf7298e..64b5a6f 100644 --- a/tests/WeavedResourceTest.php +++ b/tests/WeavedResourceTest.php @@ -17,7 +17,7 @@ class WeavedResourceTest extends TestCase public function setUp(): void { - $this->injector = new Injector(new TwigWeavedResourceTestModule([$_ENV['TEST_DIR'] . '/Fake/src/Resource'])); + $this->injector = new Injector(new TwigWeavedResourceTestModule([__DIR__ . '/Fake/src/Resource'])); } public function testRenderer(): void diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index 3002bb2..0000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,20 +0,0 @@ -addPsr4('Madapaja\TwigModule\\', [__DIR__]); -$_ENV['TEST_DIR'] = __DIR__; -$_ENV['TMP_DIR'] = __DIR__ . '/tmp'; -// no annotation in PHP 8 -if (PHP_MAJOR_VERSION >= 8) { - ServiceLocator::setReader(new AttributeReader()); -}