From cc6cb76c0596be1b3243bed788ccf4381fe806e7 Mon Sep 17 00:00:00 2001 From: Claudiu Pintiuta Date: Mon, 10 Jul 2023 10:32:58 +0300 Subject: [PATCH 1/6] refactoring, added phpunit, laminas coding standard, psalm and tests --- composer.json | 27 ++++-- phpcs.xml | 20 +++++ psalm-baseline.xml | 9 ++ psalm.xml | 18 ++++ src/ConfigProvider.php | 52 +++++------ src/Extension/AuthenticationExtension.php | 22 ++--- src/Extension/AuthorizationExtension.php | 23 +---- src/Extension/DateExtension.php | 38 +++----- src/Extension/FlashMessengerExtension.php | 35 ++------ src/Extension/FormElementsExtension.php | 86 ++++++++----------- src/Extension/NavigationExtension.php | 25 +----- src/Extension/Translation/TransNode.php | 57 +++++------- .../Translation/TransTokenParser.php | 36 +++----- src/Extension/TranslationExtension.php | 18 ++-- .../AuthenticationExtensionFactory.php | 29 ++++--- src/Factory/AuthorizationExtensionFactory.php | 29 ++++--- .../FlashMessengerExtensionFactory.php | 27 +++--- src/Factory/NavigationExtensionFactory.php | 30 ++++--- .../View/HelperPluginManagerFactory.php | 19 ++-- src/TwigEnvironmentDelegator.php | 29 +++---- test/ConfigProviderTest.php | 49 +++++++++++ .../Extension/AuthenticationExtensionTest.php | 37 ++++++++ test/Extension/AuthorizationExtensionTest.php | 37 ++++++++ test/Extension/DateExtensionTest.php | 65 ++++++++++++++ .../Extension/FlashMessengerExtensionTest.php | 46 ++++++++++ test/Extension/FormElementsExtensionTest.php | 21 +++++ test/Extension/NavigationExtensionTest.php | 51 +++++++++++ test/Extension/TranslationExtensionTest.php | 23 +++++ .../AuthenticationExtensionFactoryTest.php | 59 +++++++++++++ .../AuthorizationExtensionFactoryTest.php | 65 ++++++++++++++ .../FlashMessengerExtensionFactoryTest.php | 65 ++++++++++++++ .../NavigationExtensionFactoryTest.php | 65 ++++++++++++++ 32 files changed, 859 insertions(+), 353 deletions(-) create mode 100644 phpcs.xml create mode 100644 psalm-baseline.xml create mode 100644 psalm.xml create mode 100644 test/ConfigProviderTest.php create mode 100644 test/Extension/AuthenticationExtensionTest.php create mode 100644 test/Extension/AuthorizationExtensionTest.php create mode 100644 test/Extension/DateExtensionTest.php create mode 100644 test/Extension/FlashMessengerExtensionTest.php create mode 100644 test/Extension/FormElementsExtensionTest.php create mode 100644 test/Extension/NavigationExtensionTest.php create mode 100644 test/Extension/TranslationExtensionTest.php create mode 100644 test/Factory/AuthenticationExtensionFactoryTest.php create mode 100644 test/Factory/AuthorizationExtensionFactoryTest.php create mode 100644 test/Factory/FlashMessengerExtensionFactoryTest.php create mode 100644 test/Factory/NavigationExtensionFactoryTest.php diff --git a/composer.json b/composer.json index dd11487..604fd03 100644 --- a/composer.json +++ b/composer.json @@ -16,20 +16,24 @@ } ], "require": { - "php": "~7.4.0 || ~8.0.0 || ~8.1.0", + "php": "~8.1.0 || ~8.2.0", "laminas/laminas-servicemanager": "^3.11", "mezzio/mezzio-twigrenderer": "^2.9", "laminas/laminas-view": "^2.20", - "laminas/laminas-authentication": "^2.10" - }, - "require-dev": { - "phpunit/phpunit": "^9.5", - "squizlabs/php_codesniffer": "^3.6", + "laminas/laminas-authentication": "^2.10", "dotkernel/dot-navigation": "^3.1", "dotkernel/dot-flashmessenger": "^3.1.1", "laminas/laminas-form": "^3.1.1", "doctrine/doctrine-module": "^5.1", - "mezzio/mezzio-authorization": "^1.4" + "mezzio/mezzio-authorization": "^1.4", + "dotkernel/dot-authorization": "^3.3" + }, + "require-dev": { + "phpunit/phpunit": "^10.2", + "squizlabs/php_codesniffer": "^3.6", + "symfony/var-dumper": "^6.3", + "laminas/laminas-coding-standard": "^2.5", + "vimeo/psalm": "^5.13" }, "autoload": { "psr-4": { @@ -40,5 +44,14 @@ "psr-4": { "DotTest\\Twig\\": "test/" } + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + }, + "scripts": { + "cs-check": "phpcs", + "cs-fix": "phpcbf" } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..1efe663 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + src + test + + + + diff --git a/psalm-baseline.xml b/psalm-baseline.xml new file mode 100644 index 0000000..7c53d4a --- /dev/null +++ b/psalm-baseline.xml @@ -0,0 +1,9 @@ + + + + + twig_date_converter($env, $date) + twig_date_converter($env, $now) + + + diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..e6f472e --- /dev/null +++ b/psalm.xml @@ -0,0 +1,18 @@ + + + + + + + + + diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index b565f50..5cd1dc0 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -1,11 +1,10 @@ $this->getDependencyConfig(), - 'view_helpers' => [], ]; } @@ -47,43 +41,37 @@ public function __invoke(): array public function getDependencyConfig(): array { return [ - 'factories' => [ - HelperPluginManager::class => HelperPluginManagerFactory::class, - + 'factories' => [ + HelperPluginManager::class => HelperPluginManagerFactory::class, AuthenticationExtension::class => AuthenticationExtensionFactory::class, - AuthorizationExtension::class => AuthorizationExtensionFactory::class, - NavigationExtension::class => NavigationExtensionFactory::class, + AuthorizationExtension::class => AuthorizationExtensionFactory::class, + NavigationExtension::class => NavigationExtensionFactory::class, FlashMessengerExtension::class => FlashMessengerExtensionFactory::class, - FormElementsExtension::class => InvokableFactory::class, - AuthenticationService::class => AuthenticationServiceFactory::class + FormElementsExtension::class => InvokableFactory::class, + AuthenticationService::class => AuthenticationServiceFactory::class, ], - - 'delegators' => [ - Environment::class => [ + 'delegators' => [ + Environment::class => [ TwigEnvironmentDelegator::class, ], - FlashMessengerRenderer::class => [ LazyServiceFactory::class, ], - NavigationRenderer::class => [ + NavigationRenderer::class => [ LazyServiceFactory::class, - ] + ], ], - 'lazy_services' => [ 'class_map' => [ - NavigationRenderer::class => - NavigationRenderer::class, - - FlashMessengerRenderer::class => - FlashMessengerRenderer::class, - ] + NavigationRenderer::class + => NavigationRenderer::class, + FlashMessengerRenderer::class + => FlashMessengerRenderer::class, + ], ], - - 'aliases' => [ - 'ViewHelperManager' => HelperPluginManager::class, - AuthenticationServiceInterface::class => AuthenticationService::class + 'aliases' => [ + 'ViewHelperManager' => HelperPluginManager::class, + AuthenticationServiceInterface::class => AuthenticationService::class, ], ]; } diff --git a/src/Extension/AuthenticationExtension.php b/src/Extension/AuthenticationExtension.php index 226fffb..9d7bf71 100644 --- a/src/Extension/AuthenticationExtension.php +++ b/src/Extension/AuthenticationExtension.php @@ -1,45 +1,33 @@ authentication = $authentication; } - /** - * @return string - */ public function getName(): string { return 'dot-authentication'; } - public function getFunctions() + public function getFunctions(): array { return [ new TwigFunction('hasIdentity', [$this, 'hasIdentity']), @@ -52,7 +40,7 @@ public function hasIdentity(): bool return $this->authentication->hasIdentity(); } - public function getIdentity() + public function getIdentity(): mixed { return $this->authentication->getIdentity(); } diff --git a/src/Extension/AuthorizationExtension.php b/src/Extension/AuthorizationExtension.php index 7881dd3..0c17dfd 100644 --- a/src/Extension/AuthorizationExtension.php +++ b/src/Extension/AuthorizationExtension.php @@ -1,28 +1,20 @@ authorization->isGranted($permission); } diff --git a/src/Extension/DateExtension.php b/src/Extension/DateExtension.php index 187dbde..a811398 100644 --- a/src/Extension/DateExtension.php +++ b/src/Extension/DateExtension.php @@ -1,20 +1,18 @@ 'year', 'm' => 'month', @@ -24,10 +22,7 @@ class DateExtension extends AbstractExtension 's' => 'second', ]; - /** - * @return array|TwigFilter[] - */ - public function getFilters() + public function getFilters(): array { return [ new TwigFilter('time_diff', [$this, 'diff'], ['needs_environment' => true]), @@ -37,18 +32,16 @@ public function getFilters() /** * Filters for converting dates to a time ago string like Facebook and Twitter has. * - * @param Environment $env - * @param string|\DateTime $date a string or DateTime object to convert - * @param string|\DateTime $now A string or DateTime object to compare with. - * If none given, the current time will be used. + * @param string|DateTime $date a string or DateTime object to convert + * @param string|DateTime $now A string or DateTime object to compare with. * - * @return string the converted time + * If none given, the current time will be used. */ - public function diff(Environment $env, $date, $now = null) + public function diff(Environment $env, $date, $now = null): string { // Convert both dates to DateTime instances. $date = twig_date_converter($env, $date); - $now = twig_date_converter($env, $now); + $now = twig_date_converter($env, $now); // Get the difference between the two DateTime objects. $diff = $date->diff($now); @@ -65,15 +58,8 @@ public function diff(Environment $env, $date, $now = null) return ''; } - /** - * @param $count - * @param $invert - * @param $unit - * @return string - */ - private function getPluralizedInterval($count, $invert, $unit) + public function getPluralizedInterval(mixed $count, int $invert, string $unit): string { - if (1 !== $count) { $unit .= 's'; } diff --git a/src/Extension/FlashMessengerExtension.php b/src/Extension/FlashMessengerExtension.php index ccf1d5d..4b3ec5d 100644 --- a/src/Extension/FlashMessengerExtension.php +++ b/src/Extension/FlashMessengerExtension.php @@ -1,11 +1,10 @@ renderer = $renderer; } - /** - * @return string - */ public function getName(): string { return 'dot-messenger'; } - /** - * @return array - */ public function getFunctions(): array { return [ @@ -59,29 +43,20 @@ public function getFunctions(): array ]; } - /** - * @param string|null $type - * @param string $channel - * @return string - */ public function renderMessages( - string $type = null, + ?string $type = null, string $channel = FlashMessengerInterface::DEFAULT_CHANNEL ): string { return $this->renderer->render($type, $channel); } /** - * @param string $partial * @param array $params - * @param string|null $type - * @param string $channel - * @return string */ public function renderMessagesPartial( string $partial, array $params = [], - string $type = null, + ?string $type = null, string $channel = FlashMessengerInterface::DEFAULT_CHANNEL ): string { return $this->renderer->renderPartial($partial, $params, $type, $channel); diff --git a/src/Extension/FormElementsExtension.php b/src/Extension/FormElementsExtension.php index 21555a5..d21daae 100644 --- a/src/Extension/FormElementsExtension.php +++ b/src/Extension/FormElementsExtension.php @@ -1,16 +1,13 @@ Button::class, - 'Captcha' => Captcha::class, - 'Checkbox' => Checkbox::class, - 'Collection' => Collection::class, - 'Color' => Color::class, - 'Csrf' => Csrf::class, - 'Date' => Date::class, - 'DateSelect' => DateSelect::class, - 'DateTime' => DateTime::class, - 'DateTimeLocal' => DateTimeLocal::class, + protected array $formElements = [ + 'Button' => Button::class, + 'Captcha' => Captcha::class, + 'Checkbox' => Checkbox::class, + 'Collection' => Collection::class, + 'Color' => Color::class, + 'Csrf' => Csrf::class, + 'Date' => Date::class, + 'DateSelect' => DateSelect::class, + 'DateTime' => DateTime::class, + 'DateTimeLocal' => DateTimeLocal::class, 'DateTimeSelect' => DateTimeSelect::class, - 'Email' => Email::class, - 'File' => File::class, - 'Fieldset' => Fieldset::class, - 'Hidden' => Hidden::class, - 'Image' => Image::class, - 'Month' => Month::class, - 'MonthSelect' => MonthSelect::class, - 'MultiCheckbox' => MultiCheckbox::class, - 'Number' => Number::class, - 'Password' => Password::class, - 'Radio' => Radio::class, - 'Range' => Range::class, - 'Search' => Search::class, - 'Select' => Select::class, - 'Submit' => Submit::class, - 'Tel' => Tel::class, - 'Text' => Text::class, - 'Textarea' => Textarea::class, - 'Time' => Time::class, - 'Url' => Url::class, - 'Week' => Week::class + 'Email' => Email::class, + 'File' => File::class, + 'Fieldset' => Fieldset::class, + 'Hidden' => Hidden::class, + 'Image' => Image::class, + 'Month' => Month::class, + 'MonthSelect' => MonthSelect::class, + 'MultiCheckbox' => MultiCheckbox::class, + 'Number' => Number::class, + 'Password' => Password::class, + 'Radio' => Radio::class, + 'Range' => Range::class, + 'Search' => Search::class, + 'Select' => Select::class, + 'Submit' => Submit::class, + 'Tel' => Tel::class, + 'Text' => Text::class, + 'Textarea' => Textarea::class, + 'Time' => Time::class, + 'Url' => Url::class, + 'Week' => Week::class, ]; - /** - * @return string - */ public function getName(): string { return 'dot-form'; } - /** - * @return array - */ public function getTests(): array { $tests = []; foreach ($this->formElements as $element => $class) { $tests[] = new TwigTest($element, function ($value) use ($class) { - return ($value instanceof $class); + return $value instanceof $class; }); } diff --git a/src/Extension/NavigationExtension.php b/src/Extension/NavigationExtension.php index 3269074..f84b669 100644 --- a/src/Extension/NavigationExtension.php +++ b/src/Extension/NavigationExtension.php @@ -1,11 +1,10 @@ navigationRenderer = $navigationRenderer; @@ -49,10 +37,6 @@ public function getFunctions(): array ]; } - /** - * @param Page $page - * @return mixed - */ public function htmlAttributes(Page $page): string { return $this->navigationRenderer->htmlAttributes($page->getAttributes()); @@ -60,7 +44,6 @@ public function htmlAttributes(Page $page): string /** * @param string|NavigationContainer $container - * @return string */ public function renderMenu($container): string { @@ -69,9 +52,7 @@ public function renderMenu($container): string /** * @param string|NavigationContainer $container - * @param string $partial * @param array $params - * @return string */ public function renderMenuPartial($container, string $partial, array $params = []): string { diff --git a/src/Extension/Translation/TransNode.php b/src/Extension/Translation/TransNode.php index d0aaa2f..55bc306 100644 --- a/src/Extension/Translation/TransNode.php +++ b/src/Extension/Translation/TransNode.php @@ -1,5 +1,7 @@ $body]; if (null !== $count) { @@ -77,8 +72,7 @@ public function compile(Compiler $compiler) if ($vars) { $compiler ->write('echo strtr(' . $function . '(') - ->subcompile($msg) - ; + ->subcompile($msg); if ($this->hasNode('plural')) { $compiler @@ -86,8 +80,7 @@ public function compile(Compiler $compiler) ->subcompile($msg1) ->raw(', abs(') ->subcompile($this->hasNode('count') ? $this->getNode('count') : null) - ->raw(')') - ; + ->raw(')'); } $compiler->raw('), array('); @@ -98,15 +91,13 @@ public function compile(Compiler $compiler) ->string('%count%') ->raw(' => abs(') ->subcompile($this->hasNode('count') ? $this->getNode('count') : null) - ->raw('), ') - ; + ->raw('), '); } else { $compiler ->string('%' . $var->getAttribute('name') . '%') ->raw(' => ') ->subcompile($var) - ->raw(', ') - ; + ->raw(', '); } } @@ -114,8 +105,7 @@ public function compile(Compiler $compiler) } else { $compiler ->write('echo ' . $function . '(') - ->subcompile($msg) - ; + ->subcompile($msg); if ($this->hasNode('plural')) { $compiler @@ -123,8 +113,7 @@ public function compile(Compiler $compiler) ->subcompile($msg1) ->raw(', abs(') ->subcompile($this->hasNode('count') ? $this->getNode('count') : null) - ->raw(')') - ; + ->raw(')'); } $compiler->raw(");\n"); @@ -133,7 +122,6 @@ public function compile(Compiler $compiler) /** * @param Node $body A Twig_Node instance - * * @return array */ protected function compileString(Node $body) @@ -152,8 +140,8 @@ protected function compileString(Node $body) /** @var Node $node */ foreach ($body as $node) { - if (get_class($node) === 'Node' && $node->getNode(0) instanceof TempNameExpression) { - $node = $node->getNode(1); + if ($node::class === Node::class && $node->getNode("0") instanceof TempNameExpression) { + $node = $node->getNode("1"); } if ($node instanceof PrintNode) { @@ -161,7 +149,7 @@ protected function compileString(Node $body) while ($n instanceof FilterExpression) { $n = $n->getNode('node'); } - $msg .= sprintf('%%%s%%', $n->getAttribute('name')); + $msg .= sprintf('%%%s%%', $n->getAttribute('name')); $vars[] = new NameExpression($n->getAttribute('name'), $n->getTemplateLine()); } else { $msg .= $node->getAttribute('data'); @@ -176,7 +164,6 @@ protected function compileString(Node $body) /** * @param bool $plural Return plural or singular function to use - * * @return string */ protected function getTransFunction($plural) diff --git a/src/Extension/Translation/TransTokenParser.php b/src/Extension/Translation/TransTokenParser.php index 8a42db1..321a95b 100644 --- a/src/Extension/Translation/TransTokenParser.php +++ b/src/Extension/Translation/TransTokenParser.php @@ -1,5 +1,7 @@ getLine(); $stream = $this->parser->getStream(); - $count = null; + $count = null; $plural = null; - $notes = null; + $notes = null; - if (!$stream->test(Token::BLOCK_END_TYPE)) { + if (! $stream->test(Token::BLOCK_END_TYPE)) { $body = $this->parser->getExpressionParser()->parseExpression(); } else { $stream->expect(Token::BLOCK_END_TYPE); @@ -58,38 +55,25 @@ public function parse(Token $token) return new TransNode($body, $plural, $count, $notes, $lineno, $this->getTag()); } - /** - * @param Token $token - * @return bool - */ - public function decideForFork(Token $token) + public function decideForFork(Token $token): bool { return $token->test(['plural', 'notes', 'endtrans']); } - /** - * @param Token $token - * @return bool - */ - public function decideForEnd(Token $token) + public function decideForEnd(Token $token): bool { return $token->test('endtrans'); } - /** - * @return string - */ - public function getTag() + public function getTag(): string { return 'trans'; } /** - * @param Node $body - * @param $lineno * @throws SyntaxError */ - private function checkTransString(Node $body, $lineno) + private function checkTransString(Node $body, mixed $lineno): void { foreach ($body as $i => $node) { if ( @@ -101,7 +85,7 @@ private function checkTransString(Node $body, $lineno) } throw new SyntaxError( - sprintf('The text to be translated with "trans" can only contain references to simple variables'), + 'The text to be translated with "trans" can only contain references to simple variables', $lineno ); } diff --git a/src/Extension/TranslationExtension.php b/src/Extension/TranslationExtension.php index 6183727..23e69f1 100644 --- a/src/Extension/TranslationExtension.php +++ b/src/Extension/TranslationExtension.php @@ -1,29 +1,21 @@ get(AuthenticationServiceInterface::class)); + $service = $container->has(AuthenticationServiceInterface::class) ? + $container->get(AuthenticationServiceInterface::class) : null; + + if (! $service instanceof AuthenticationServiceInterface) { + throw new Exception(sprintf('Unable to find %s', AuthenticationServiceInterface::class)); + } + + return new AuthenticationExtension($service); } } diff --git a/src/Factory/AuthorizationExtensionFactory.php b/src/Factory/AuthorizationExtensionFactory.php index 34686e4..057bc0a 100644 --- a/src/Factory/AuthorizationExtensionFactory.php +++ b/src/Factory/AuthorizationExtensionFactory.php @@ -1,30 +1,37 @@ get(AuthorizationInterface::class)); + $authorizationInterface = $container->has(AuthorizationInterface::class) ? + $container->get(AuthorizationInterface::class) : null; + + if (! $authorizationInterface instanceof AuthorizationInterface) { + throw new Exception(sprintf('Unable to find %s', AuthorizationInterface::class)); + } + + return new AuthorizationExtension($authorizationInterface); } } diff --git a/src/Factory/FlashMessengerExtensionFactory.php b/src/Factory/FlashMessengerExtensionFactory.php index e2633be..bfc9020 100644 --- a/src/Factory/FlashMessengerExtensionFactory.php +++ b/src/Factory/FlashMessengerExtensionFactory.php @@ -1,30 +1,31 @@ get(RendererInterface::class)); + $renderInterface = $container->has(RendererInterface::class) ? + $container->get(RendererInterface::class) : null; + + if (! $renderInterface instanceof RendererInterface) { + throw new Exception(sprintf('Unable to find %s', RendererInterface::class)); + } + + return new FlashMessengerExtension($renderInterface); } } diff --git a/src/Factory/NavigationExtensionFactory.php b/src/Factory/NavigationExtensionFactory.php index 470cf02..dddd6c8 100644 --- a/src/Factory/NavigationExtensionFactory.php +++ b/src/Factory/NavigationExtensionFactory.php @@ -1,31 +1,37 @@ get(RendererInterface::class); - return new NavigationExtension($navigationMenu); + $renderInterface = $container->has(RendererInterface::class) ? + $container->get(RendererInterface::class) : null; + + if (! $renderInterface instanceof RendererInterface) { + throw new Exception(sprintf('Unable to find %s', RendererInterface::class)); + } + + return new NavigationExtension($renderInterface); } } diff --git a/src/Laminas/View/HelperPluginManagerFactory.php b/src/Laminas/View/HelperPluginManagerFactory.php index bbf2ea8..f78ca51 100644 --- a/src/Laminas/View/HelperPluginManagerFactory.php +++ b/src/Laminas/View/HelperPluginManagerFactory.php @@ -1,28 +1,25 @@ get('config')['view_helpers']; return new HelperPluginManager($container, $config); diff --git a/src/TwigEnvironmentDelegator.php b/src/TwigEnvironmentDelegator.php index 11ae231..5f26cb1 100644 --- a/src/TwigEnvironmentDelegator.php +++ b/src/TwigEnvironmentDelegator.php @@ -1,11 +1,10 @@ get('ViewHelperManager'); - $zfRenderer = new PhpRenderer(); + $zfRenderer = new PhpRenderer(); $zfRenderer->setHelperPluginManager($viewHelperManager); $environment->registerUndefinedFunctionCallback( function ($name) use ($viewHelperManager, $zfRenderer) { - if (!$viewHelperManager->has($name)) { + if (! $viewHelperManager->has($name)) { return false; } $callable = [$zfRenderer->plugin($name), '__invoke']; - $options = ['is_safe' => ['html']]; + $options = ['is_safe' => ['html']]; return new TwigFunction($name, $callable, $options); } ); diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php new file mode 100644 index 0000000..ea1925e --- /dev/null +++ b/test/ConfigProviderTest.php @@ -0,0 +1,49 @@ +config = (new ConfigProvider())(); + } + + public function testHasDependencies(): void + { + self::assertArrayHasKey('dependencies', $this->config); + } + + public function testDependenciesHasFactories(): void + { + $this->assertArrayHasKey('factories', $this->config['dependencies']); + $this->assertArrayHasKey(HelperPluginManager::class, $this->config['dependencies']['factories']); + $this->assertArrayHasKey(AuthenticationExtension::class, $this->config['dependencies']['factories']); + $this->assertArrayHasKey(AuthorizationExtension::class, $this->config['dependencies']['factories']); + $this->assertArrayHasKey(NavigationExtension::class, $this->config['dependencies']['factories']); + $this->assertArrayHasKey(FlashMessengerExtension::class, $this->config['dependencies']['factories']); + $this->assertArrayHasKey(FormElementsExtension::class, $this->config['dependencies']['factories']); + $this->assertArrayHasKey(AuthenticationService::class, $this->config['dependencies']['factories']); + } + + public function testDependenciesHasAliases(): void + { + $this->assertArrayHasKey('aliases', $this->config['dependencies']); + $this->assertArrayHasKey(AuthenticationServiceInterface::class, $this->config['dependencies']['aliases']); + } +} diff --git a/test/Extension/AuthenticationExtensionTest.php b/test/Extension/AuthenticationExtensionTest.php new file mode 100644 index 0000000..0dedb34 --- /dev/null +++ b/test/Extension/AuthenticationExtensionTest.php @@ -0,0 +1,37 @@ +interfaceMock = $this->createMock(AuthenticationServiceInterface::class); + $this->extension = new AuthenticationExtension($this->interfaceMock); + } + + public function testCreate() + { + assertInstanceOf(AuthenticationExtension::class, $this->extension); + } + + public function testFunctions() + { + foreach ($this->extension->getFunctions() as $function) { + assertInstanceOf(TwigFunction::class, $function); + } + } +} diff --git a/test/Extension/AuthorizationExtensionTest.php b/test/Extension/AuthorizationExtensionTest.php new file mode 100644 index 0000000..9af2940 --- /dev/null +++ b/test/Extension/AuthorizationExtensionTest.php @@ -0,0 +1,37 @@ +interfaceMock = $this->createMock(AuthorizationInterface::class); + $this->extension = new AuthorizationExtension($this->interfaceMock); + } + + public function testCreate() + { + self::assertInstanceOf(AuthorizationExtension::class, $this->extension); + } + + public function testFunctions() + { + foreach ($this->extension->getFunctions() as $function) { + assertInstanceOf(TwigFunction::class, $function); + } + } +} diff --git a/test/Extension/DateExtensionTest.php b/test/Extension/DateExtensionTest.php new file mode 100644 index 0000000..a585a82 --- /dev/null +++ b/test/Extension/DateExtensionTest.php @@ -0,0 +1,65 @@ +extension = new DateExtension(); + $coreExtension = new CoreExtension(); + $this->env = $this->createMock(Environment::class); + $this->env->method('getExtension')->willReturn($coreExtension); + } + + public function testFilters() + { + foreach ($this->extension->getFilters() as $filter) { + self::assertInstanceOf(TwigFilter::class, $filter); + } + } + + public function testDiffWillReturnString() + { + assertIsString($this->extension->diff($this->env, "2023-07-31")); + } + + public function testDiffWillReturnExceptionUnexpectedCharacters() + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Unexpected character'); + $this->extension->diff($this->env, '2023-23-3322'); + } + + public function testDiffWillReturnExceptionNotFoundMessage() + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('The timezone could not be found in the database'); + $this->extension->diff($this->env, 'asdas'); + } + + public function testPluralizedInterval() + { + $month = 'month'; //can be any word + self::assertSame('in 1 month', $this->extension->getPluralizedInterval(1, 1, $month)); + self::assertSame('in 2 months', $this->extension->getPluralizedInterval(2, 1, $month)); + self::assertSame('1 month ago', $this->extension->getPluralizedInterval(1, 0, $month)); + self::assertSame('2 months ago', $this->extension->getPluralizedInterval(2, 0, $month)); + } +} diff --git a/test/Extension/FlashMessengerExtensionTest.php b/test/Extension/FlashMessengerExtensionTest.php new file mode 100644 index 0000000..fde5c76 --- /dev/null +++ b/test/Extension/FlashMessengerExtensionTest.php @@ -0,0 +1,46 @@ +createMock(RendererInterface::class); + $this->extension = new FlashMessengerExtension($interfaceMock); + } + + public function testCreate() + { + self::assertInstanceOf(FlashMessengerExtension::class, $this->extension); + } + + public function testFunctions() + { + foreach ($this->extension->getFunctions() as $function) { + assertInstanceOf(TwigFunction::class, $function); + } + } + + public function testWillRenderMessages() + { + assertIsString($this->extension->renderMessages()); + } + + public function testWillRenderMessagesPartial() + { + assertIsString($this->extension->renderMessagesPartial('partial string')); + } +} diff --git a/test/Extension/FormElementsExtensionTest.php b/test/Extension/FormElementsExtensionTest.php new file mode 100644 index 0000000..1c1824a --- /dev/null +++ b/test/Extension/FormElementsExtensionTest.php @@ -0,0 +1,21 @@ +getTests(); + foreach ($twigTests as $element) { + self::assertInstanceOf(TwigTest::class, $element); + } + } +} diff --git a/test/Extension/NavigationExtensionTest.php b/test/Extension/NavigationExtensionTest.php new file mode 100644 index 0000000..7f61a57 --- /dev/null +++ b/test/Extension/NavigationExtensionTest.php @@ -0,0 +1,51 @@ +createMock(RendererInterface::class); + $this->extension = new NavigationExtension($interfaceMock); + } + + public function testCreate() + { + self::assertInstanceOf(NavigationExtension::class, $this->extension); + } + + public function testFunctions() + { + foreach ($this->extension->getFunctions() as $function) { + self::assertInstanceOf(TwigFunction::class, $function); + } + } + + public function testHtmlAttributes() + { + $page = new Page(); + self::assertIsString($this->extension->htmlAttributes($page)); + } + + public function testRenderMenu() + { + self::assertIsString($this->extension->renderMenu(new NavigationContainer())); + } + + public function testRenderMenuPartial() + { + self::assertIsString($this->extension->renderMenuPartial(new NavigationContainer(), 'partial')); + } +} diff --git a/test/Extension/TranslationExtensionTest.php b/test/Extension/TranslationExtensionTest.php new file mode 100644 index 0000000..38b8186 --- /dev/null +++ b/test/Extension/TranslationExtensionTest.php @@ -0,0 +1,23 @@ +extension = new TranslationExtension(); + } + + public function testTokenParsersIsArray() + { + self::assertIsArray($this->extension->getTokenParsers()); + } +} diff --git a/test/Factory/AuthenticationExtensionFactoryTest.php b/test/Factory/AuthenticationExtensionFactoryTest.php new file mode 100644 index 0000000..ea7fce2 --- /dev/null +++ b/test/Factory/AuthenticationExtensionFactoryTest.php @@ -0,0 +1,59 @@ +container = $this->createMock(ContainerInterface::class); + } + + /** + * @throws \Exception + */ + public function testWillNotInstantiateWithoutInterface() + { + $this->container->expects(self::once()) + ->method('has') + ->with(AuthenticationServiceInterface::class) + ->willReturn(false); + $this->expectExceptionMessage(sprintf('Unable to find %s', AuthenticationServiceInterface::class)); + (new AuthenticationExtensionFactory())($this->container); + } + + /** + * @throws \Exception|Exception + */ + public function testWillInstantiateWithInterface() + { + $this->container->expects(self::once()) + ->method('has') + ->with(AuthenticationServiceInterface::class) + ->willReturn(true); + + $interface = $this->createMock(AuthenticationServiceInterface::class); + $this->container->method('get') + ->willReturnMap([ + [AuthenticationServiceInterface::class, $interface], + ]); + + $factory = (new AuthenticationExtensionFactory())($this->container); + + assertInstanceOf(AuthenticationExtension::class, $factory); + } +} diff --git a/test/Factory/AuthorizationExtensionFactoryTest.php b/test/Factory/AuthorizationExtensionFactoryTest.php new file mode 100644 index 0000000..131f324 --- /dev/null +++ b/test/Factory/AuthorizationExtensionFactoryTest.php @@ -0,0 +1,65 @@ +container = $this->createMock(ContainerInterface::class); + } + + /** + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function testWillNotInstantiateWithoutInterface() + { + $this->container->expects(self::once()) + ->method('has') + ->with(AuthorizationInterface::class) + ->willReturn(false); + $this->expectExceptionMessage(sprintf('Unable to find %s', AuthorizationInterface::class)); + (new AuthorizationExtensionFactory())($this->container); + } + + /** + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws Exception + */ + public function testWillInstantiateWithInterface() + { + $this->container->expects(self::once()) + ->method('has') + ->with(AuthorizationInterface::class) + ->willReturn(true); + + $interface = $this->createMock(AuthorizationInterface::class); + $this->container->method('get') + ->willReturnMap([ + [AuthorizationInterface::class, $interface], + ]); + + $factory = (new AuthorizationExtensionFactory())($this->container); + + self::assertInstanceOf(AuthorizationExtension::class, $factory); + } +} diff --git a/test/Factory/FlashMessengerExtensionFactoryTest.php b/test/Factory/FlashMessengerExtensionFactoryTest.php new file mode 100644 index 0000000..8e569f8 --- /dev/null +++ b/test/Factory/FlashMessengerExtensionFactoryTest.php @@ -0,0 +1,65 @@ +container = $this->createMock(ContainerInterface::class); + } + + /** + * @return void + * @throws \Exception + */ + public function testWillNotInstantiateWithoutInterface() + { + $this->container->expects(self::once()) + ->method('has') + ->with(RendererInterface::class) + ->willReturn(false); + + $this->expectExceptionMessage(sprintf('Unable to find %s', RendererInterface::class)); + (new FlashMessengerExtensionFactory())($this->container); + } + + /** + * @return void + * @throws Exception + * @throws \Exception + */ + public function testWillInstantiateWithInterface() + { + $this->container->expects(self::once()) + ->method('has') + ->with(RendererInterface::class) + ->willReturn(true); + + $interface = $this->createMock(RendererInterface::class); + $this->container->method('get') + ->willReturnMap([ + [RendererInterface::class, $interface], + ]); + + $factory = (new FlashMessengerExtensionFactory())($this->container); + + self::assertInstanceOf(FlashMessengerExtension::class, $factory); + } +} diff --git a/test/Factory/NavigationExtensionFactoryTest.php b/test/Factory/NavigationExtensionFactoryTest.php new file mode 100644 index 0000000..0bf2324 --- /dev/null +++ b/test/Factory/NavigationExtensionFactoryTest.php @@ -0,0 +1,65 @@ +container = $this->createMock(ContainerInterface::class); + } + + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function testWillNotInstantiateWithoutInterface() + { + $this->container->expects(self::once()) + ->method('has') + ->with(RendererInterface::class) + ->willReturn(false); + + $this->expectExceptionMessage(sprintf('Unable to find %s', RendererInterface::class)); + (new NavigationExtensionFactory())($this->container); + } + + /** + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws Exception + */ + public function testWillInstantiateWithInterface() + { + $this->container->expects(self::once()) + ->method('has') + ->with(RendererInterface::class) + ->willReturn(true); + + $interface = $this->createMock(RendererInterface::class); + $this->container->method('get') + ->willReturnMap([ + [RendererInterface::class, $interface], + ]); + + $factory = (new NavigationExtensionFactory())($this->container); + + self::assertInstanceOf(NavigationExtension::class, $factory); + } +} From 1a4ab22a2db3c2480f600f3e509fd682eb718e31 Mon Sep 17 00:00:00 2001 From: Claudiu Pintiuta Date: Mon, 10 Jul 2023 12:55:46 +0300 Subject: [PATCH 2/6] added phpunit config --- .gitignore | 1 - phpunit.xml | 12 ++++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 phpunit.xml diff --git a/.gitignore b/.gitignore index c4a2a74..2cfa69b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ clover.xml coveralls-upload.json -phpunit.xml # Created by .ignore support plugin (hsz.mobi) ### JetBrains template diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..01ff4e1 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,12 @@ + + + + + ./test + + + \ No newline at end of file From 256e19eb0e8dc04e65c246521b10c3c1c6aabeb6 Mon Sep 17 00:00:00 2001 From: Claudiu Pintiuta Date: Tue, 11 Jul 2023 14:08:32 +0300 Subject: [PATCH 3/6] fixes from git review --- .gitignore | 2 + README.md | 6 +- composer.json | 13 ++- phpunit.xml | 2 +- src/Extension/AuthenticationExtension.php | 3 +- src/Extension/AuthorizationExtension.php | 3 +- src/Extension/DateExtension.php | 14 +-- src/Extension/FlashMessengerExtension.php | 3 - src/Extension/NavigationExtension.php | 16 ++-- src/Extension/Translation/TransNode.php | 6 +- .../Translation/TransTokenParser.php | 6 +- .../AuthenticationExtensionFactory.php | 7 ++ src/Factory/AuthorizationExtensionFactory.php | 1 + .../FlashMessengerExtensionFactory.php | 7 ++ src/Factory/NavigationExtensionFactory.php | 1 + .../View/HelperPluginManagerFactory.php | 17 +++- test/ConfigProviderTest.php | 28 +++++- .../Extension/AuthenticationExtensionTest.php | 16 ++-- test/Extension/AuthorizationExtensionTest.php | 16 ++-- test/Extension/DateExtensionTest.php | 17 ++-- .../Extension/FlashMessengerExtensionTest.php | 15 ++-- test/Extension/FormElementsExtensionTest.php | 2 +- test/Extension/NavigationExtensionTest.php | 14 +-- test/Extension/TranslationExtensionTest.php | 2 +- .../AuthenticationExtensionFactoryTest.php | 16 +++- .../AuthorizationExtensionFactoryTest.php | 9 +- .../FlashMessengerExtensionFactoryTest.php | 14 +-- .../NavigationExtensionFactoryTest.php | 8 +- .../View/HelperPluginManagerFactoryTest.php | 89 +++++++++++++++++++ 29 files changed, 254 insertions(+), 99 deletions(-) create mode 100644 test/Laminas/View/HelperPluginManagerFactoryTest.php diff --git a/.gitignore b/.gitignore index 2cfa69b..4d3fdcb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ clover.xml coveralls-upload.json +.phpcs-cache +.phpunit.result.cache # Created by .ignore support plugin (hsz.mobi) ### JetBrains template diff --git a/README.md b/README.md index d2918bd..83f84b3 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,11 @@ DotKernel component providing twig extensions and customizations ![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-twigrenderer) -![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-twigrenderer/3.2.0) +![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-twigrenderer/3.4.0) [![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-form)](https://github.com/dotkernel/dot-twigrenderer/issues) [![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-form)](https://github.com/dotkernel/dot-twigrenderer/network) [![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-form)](https://github.com/dotkernel/dot-twigrenderer/stargazers) -[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-form)](https://github.com/dotkernel/dot-twigrenderer/blob/3.2.0/LICENSE.md) +[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-form)](https://github.com/dotkernel/dot-twigrenderer/blob/3.0/LICENSE.md) + +[![SymfonyInsight](https://insight.symfony.com/projects/b9a7d75d-d00a-44a9-b1c0-aea8670681cc/big.svg)](https://insight.symfony.com/projects/b9a7d75d-d00a-44a9-b1c0-aea8670681cc) diff --git a/composer.json b/composer.json index 604fd03..7b26cd0 100644 --- a/composer.json +++ b/composer.json @@ -25,13 +25,10 @@ "dotkernel/dot-flashmessenger": "^3.1.1", "laminas/laminas-form": "^3.1.1", "doctrine/doctrine-module": "^5.1", - "mezzio/mezzio-authorization": "^1.4", "dotkernel/dot-authorization": "^3.3" }, "require-dev": { "phpunit/phpunit": "^10.2", - "squizlabs/php_codesniffer": "^3.6", - "symfony/var-dumper": "^6.3", "laminas/laminas-coding-standard": "^2.5", "vimeo/psalm": "^5.13" }, @@ -46,12 +43,20 @@ } }, "config": { + "sort-packages": true, "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } }, "scripts": { + "check": [ + "@cs-check", + "@test" + ], "cs-check": "phpcs", - "cs-fix": "phpcbf" + "cs-fix": "phpcbf", + "test": "phpunit --colors=always", + "test-coverage": "phpunit --colors=always --coverage-clover clover.xml", + "static-analysis": "psalm --shepherd --stats" } } diff --git a/phpunit.xml b/phpunit.xml index 01ff4e1..87aab12 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -9,4 +9,4 @@ ./test - \ No newline at end of file + diff --git a/src/Extension/AuthenticationExtension.php b/src/Extension/AuthenticationExtension.php index 9d7bf71..d652a2f 100644 --- a/src/Extension/AuthenticationExtension.php +++ b/src/Extension/AuthenticationExtension.php @@ -14,8 +14,7 @@ class AuthenticationExtension extends AbstractExtension { - /** @var AuthenticationServiceInterface */ - protected $authentication; + protected AuthenticationServiceInterface $authentication; public function __construct(AuthenticationServiceInterface $authentication) { diff --git a/src/Extension/AuthorizationExtension.php b/src/Extension/AuthorizationExtension.php index 0c17dfd..d903ef2 100644 --- a/src/Extension/AuthorizationExtension.php +++ b/src/Extension/AuthorizationExtension.php @@ -14,8 +14,7 @@ class AuthorizationExtension extends AbstractExtension { - /** @var AuthorizationInterface */ - protected $authorization; + protected AuthorizationInterface $authorization; public function __construct(AuthorizationInterface $authorization) { diff --git a/src/Extension/DateExtension.php b/src/Extension/DateExtension.php index a811398..27f9bd2 100644 --- a/src/Extension/DateExtension.php +++ b/src/Extension/DateExtension.php @@ -4,7 +4,8 @@ namespace Dot\Twig\Extension; -use DateTime; +use DateTimeInterface; +use DateTimeZone; use Twig\Environment; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; @@ -31,14 +32,13 @@ public function getFilters(): array /** * Filters for converting dates to a time ago string like Facebook and Twitter has. - * - * @param string|DateTime $date a string or DateTime object to convert - * @param string|DateTime $now A string or DateTime object to compare with. - * * If none given, the current time will be used. */ - public function diff(Environment $env, $date, $now = null): string - { + public function diff( + Environment $env, + string|DateTimeInterface|null $date, + string|DateTimeZone|null $now = null + ): string { // Convert both dates to DateTime instances. $date = twig_date_converter($env, $date); $now = twig_date_converter($env, $now); diff --git a/src/Extension/FlashMessengerExtension.php b/src/Extension/FlashMessengerExtension.php index 4b3ec5d..cc827a3 100644 --- a/src/Extension/FlashMessengerExtension.php +++ b/src/Extension/FlashMessengerExtension.php @@ -50,9 +50,6 @@ public function renderMessages( return $this->renderer->render($type, $channel); } - /** - * @param array $params - */ public function renderMessagesPartial( string $partial, array $params = [], diff --git a/src/Extension/NavigationExtension.php b/src/Extension/NavigationExtension.php index f84b669..e575590 100644 --- a/src/Extension/NavigationExtension.php +++ b/src/Extension/NavigationExtension.php @@ -42,20 +42,16 @@ public function htmlAttributes(Page $page): string return $this->navigationRenderer->htmlAttributes($page->getAttributes()); } - /** - * @param string|NavigationContainer $container - */ - public function renderMenu($container): string + public function renderMenu(NavigationContainer|string $container): string { return $this->navigationRenderer->render($container); } - /** - * @param string|NavigationContainer $container - * @param array $params - */ - public function renderMenuPartial($container, string $partial, array $params = []): string - { + public function renderMenuPartial( + NavigationContainer|string $container, + string $partial, + array $params = [] + ): string { return $this->navigationRenderer->renderPartial($container, $partial, $params); } } diff --git a/src/Extension/Translation/TransNode.php b/src/Extension/Translation/TransNode.php index 55bc306..487319b 100644 --- a/src/Extension/Translation/TransNode.php +++ b/src/Extension/Translation/TransNode.php @@ -120,11 +120,7 @@ public function compile(Compiler $compiler) } } - /** - * @param Node $body A Twig_Node instance - * @return array - */ - protected function compileString(Node $body) + protected function compileString(Node $body): array { if ( $body instanceof NameExpression || diff --git a/src/Extension/Translation/TransTokenParser.php b/src/Extension/Translation/TransTokenParser.php index 321a95b..343e428 100644 --- a/src/Extension/Translation/TransTokenParser.php +++ b/src/Extension/Translation/TransTokenParser.php @@ -14,11 +14,7 @@ class TransTokenParser extends AbstractTokenParser { - /** - * @return TransNode|Node - * @throws SyntaxError - */ - public function parse(Token $token) + public function parse(Token $token): TransNode { $lineno = $token->getLine(); $stream = $this->parser->getStream(); diff --git a/src/Factory/AuthenticationExtensionFactory.php b/src/Factory/AuthenticationExtensionFactory.php index d71ea4d..f5626a4 100644 --- a/src/Factory/AuthenticationExtensionFactory.php +++ b/src/Factory/AuthenticationExtensionFactory.php @@ -11,12 +11,19 @@ use Dot\Twig\Extension\AuthenticationExtension; use Exception; use Laminas\Authentication\AuthenticationServiceInterface; +use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; use function sprintf; class AuthenticationExtensionFactory { + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws Exception + */ public function __invoke(ContainerInterface $container): AuthenticationExtension { $service = $container->has(AuthenticationServiceInterface::class) ? diff --git a/src/Factory/AuthorizationExtensionFactory.php b/src/Factory/AuthorizationExtensionFactory.php index 057bc0a..2df33d9 100644 --- a/src/Factory/AuthorizationExtensionFactory.php +++ b/src/Factory/AuthorizationExtensionFactory.php @@ -22,6 +22,7 @@ class AuthorizationExtensionFactory /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface + * @throws Exception */ public function __invoke(ContainerInterface $container): AuthorizationExtension { diff --git a/src/Factory/FlashMessengerExtensionFactory.php b/src/Factory/FlashMessengerExtensionFactory.php index bfc9020..11ac906 100644 --- a/src/Factory/FlashMessengerExtensionFactory.php +++ b/src/Factory/FlashMessengerExtensionFactory.php @@ -11,12 +11,19 @@ use Dot\FlashMessenger\View\RendererInterface; use Dot\Twig\Extension\FlashMessengerExtension; use Exception; +use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; use function sprintf; class FlashMessengerExtensionFactory { + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws Exception + */ public function __invoke(ContainerInterface $container): FlashMessengerExtension { $renderInterface = $container->has(RendererInterface::class) ? diff --git a/src/Factory/NavigationExtensionFactory.php b/src/Factory/NavigationExtensionFactory.php index dddd6c8..333447c 100644 --- a/src/Factory/NavigationExtensionFactory.php +++ b/src/Factory/NavigationExtensionFactory.php @@ -22,6 +22,7 @@ class NavigationExtensionFactory /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface + * @throws Exception */ public function __invoke(ContainerInterface $container): NavigationExtension { diff --git a/src/Laminas/View/HelperPluginManagerFactory.php b/src/Laminas/View/HelperPluginManagerFactory.php index f78ca51..5717efc 100644 --- a/src/Laminas/View/HelperPluginManagerFactory.php +++ b/src/Laminas/View/HelperPluginManagerFactory.php @@ -8,20 +8,33 @@ namespace Dot\Twig\Laminas\View; +use Exception; use Laminas\View\HelperPluginManager; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; +use function array_key_exists; + class HelperPluginManagerFactory { /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface + * @throws Exception */ public function __invoke(ContainerInterface $container): HelperPluginManager { - $config = $container->get('config')['view_helpers']; - return new HelperPluginManager($container, $config); + if (! $container->has('config')) { + throw new Exception('Unable to find config'); + } + + $config = $container->get('config'); + + if (! array_key_exists('view_helpers', $config)) { + throw new Exception('Unable to find view_helpers config'); + } + + return new HelperPluginManager($container, $config['view_helpers']); } } diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php index ea1925e..bc17e75 100644 --- a/test/ConfigProviderTest.php +++ b/test/ConfigProviderTest.php @@ -4,6 +4,8 @@ namespace DotTest\Twig; +use Dot\FlashMessenger\View\FlashMessengerRenderer; +use Dot\Navigation\View\NavigationRenderer; use Dot\Twig\ConfigProvider; use Dot\Twig\Extension\AuthenticationExtension; use Dot\Twig\Extension\AuthorizationExtension; @@ -14,6 +16,7 @@ use Laminas\Authentication\AuthenticationServiceInterface; use Laminas\View\HelperPluginManager; use PHPUnit\Framework\TestCase; +use Twig\Environment; class ConfigProviderTest extends TestCase { @@ -26,7 +29,7 @@ protected function setUp(): void public function testHasDependencies(): void { - self::assertArrayHasKey('dependencies', $this->config); + $this->assertArrayHasKey('dependencies', $this->config); } public function testDependenciesHasFactories(): void @@ -44,6 +47,29 @@ public function testDependenciesHasFactories(): void public function testDependenciesHasAliases(): void { $this->assertArrayHasKey('aliases', $this->config['dependencies']); + $this->assertArrayHasKey('ViewHelperManager', $this->config['dependencies']['aliases']); $this->assertArrayHasKey(AuthenticationServiceInterface::class, $this->config['dependencies']['aliases']); } + + public function testDependenciesHasLazyServices() + { + $this->assertArrayHasKey('lazy_services', $this->config['dependencies']); + $this->assertArrayHasKey('class_map', $this->config['dependencies']['lazy_services']); + $this->assertArrayHasKey( + NavigationRenderer::class, + $this->config['dependencies']['lazy_services']['class_map'] + ); + $this->assertArrayHasKey( + FlashMessengerRenderer::class, + $this->config['dependencies']['lazy_services']['class_map'] + ); + } + + public function testDependenciesHasDelegators() + { + $this->assertArrayHasKey('delegators', $this->config['dependencies']); + $this->assertArrayHasKey(Environment::class, $this->config['dependencies']['delegators']); + $this->assertArrayHasKey(FlashMessengerRenderer::class, $this->config['dependencies']['delegators']); + $this->assertArrayHasKey(NavigationRenderer::class, $this->config['dependencies']['delegators']); + } } diff --git a/test/Extension/AuthenticationExtensionTest.php b/test/Extension/AuthenticationExtensionTest.php index 0dedb34..b03ac96 100644 --- a/test/Extension/AuthenticationExtensionTest.php +++ b/test/Extension/AuthenticationExtensionTest.php @@ -6,32 +6,32 @@ use Dot\Twig\Extension\AuthenticationExtension; use Laminas\Authentication\AuthenticationServiceInterface; -use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; use Twig\TwigFunction; -use function PHPUnit\Framework\assertInstanceOf; - class AuthenticationExtensionTest extends TestCase { - private AuthenticationServiceInterface|MockObject $interfaceMock; private AuthenticationExtension $extension; + /** + * @throws Exception + */ protected function setUp(): void { - $this->interfaceMock = $this->createMock(AuthenticationServiceInterface::class); - $this->extension = new AuthenticationExtension($this->interfaceMock); + $interfaceMock = $this->createMock(AuthenticationServiceInterface::class); + $this->extension = new AuthenticationExtension($interfaceMock); } public function testCreate() { - assertInstanceOf(AuthenticationExtension::class, $this->extension); + $this->assertInstanceOf(AuthenticationExtension::class, $this->extension); } public function testFunctions() { foreach ($this->extension->getFunctions() as $function) { - assertInstanceOf(TwigFunction::class, $function); + $this->assertInstanceOf(TwigFunction::class, $function); } } } diff --git a/test/Extension/AuthorizationExtensionTest.php b/test/Extension/AuthorizationExtensionTest.php index 9af2940..b8ed6df 100644 --- a/test/Extension/AuthorizationExtensionTest.php +++ b/test/Extension/AuthorizationExtensionTest.php @@ -6,32 +6,32 @@ use Dot\Authorization\AuthorizationInterface; use Dot\Twig\Extension\AuthorizationExtension; -use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; use Twig\TwigFunction; -use function PHPUnit\Framework\assertInstanceOf; - class AuthorizationExtensionTest extends TestCase { - private AuthorizationInterface|MockObject $interfaceMock; private AuthorizationExtension $extension; + /** + * @throws Exception + */ protected function setUp(): void { - $this->interfaceMock = $this->createMock(AuthorizationInterface::class); - $this->extension = new AuthorizationExtension($this->interfaceMock); + $interfaceMock = $this->createMock(AuthorizationInterface::class); + $this->extension = new AuthorizationExtension($interfaceMock); } public function testCreate() { - self::assertInstanceOf(AuthorizationExtension::class, $this->extension); + $this->assertInstanceOf(AuthorizationExtension::class, $this->extension); } public function testFunctions() { foreach ($this->extension->getFunctions() as $function) { - assertInstanceOf(TwigFunction::class, $function); + $this->assertInstanceOf(TwigFunction::class, $function); } } } diff --git a/test/Extension/DateExtensionTest.php b/test/Extension/DateExtensionTest.php index a585a82..af9307c 100644 --- a/test/Extension/DateExtensionTest.php +++ b/test/Extension/DateExtensionTest.php @@ -12,14 +12,15 @@ use Twig\Extension\CoreExtension; use Twig\TwigFilter; -use function PHPUnit\Framework\assertIsString; - class DateExtensionTest extends TestCase { private DateExtension $extension; private MockObject|Environment $env; + /** + * @throws \PHPUnit\Framework\MockObject\Exception + */ protected function setUp(): void { $this->extension = new DateExtension(); @@ -31,13 +32,13 @@ protected function setUp(): void public function testFilters() { foreach ($this->extension->getFilters() as $filter) { - self::assertInstanceOf(TwigFilter::class, $filter); + $this->assertInstanceOf(TwigFilter::class, $filter); } } public function testDiffWillReturnString() { - assertIsString($this->extension->diff($this->env, "2023-07-31")); + $this->assertIsString($this->extension->diff($this->env, "2023-07-31")); } public function testDiffWillReturnExceptionUnexpectedCharacters() @@ -57,9 +58,9 @@ public function testDiffWillReturnExceptionNotFoundMessage() public function testPluralizedInterval() { $month = 'month'; //can be any word - self::assertSame('in 1 month', $this->extension->getPluralizedInterval(1, 1, $month)); - self::assertSame('in 2 months', $this->extension->getPluralizedInterval(2, 1, $month)); - self::assertSame('1 month ago', $this->extension->getPluralizedInterval(1, 0, $month)); - self::assertSame('2 months ago', $this->extension->getPluralizedInterval(2, 0, $month)); + $this->assertSame('in 1 month', $this->extension->getPluralizedInterval(1, 1, $month)); + $this->assertSame('in 2 months', $this->extension->getPluralizedInterval(2, 1, $month)); + $this->assertSame('1 month ago', $this->extension->getPluralizedInterval(1, 0, $month)); + $this->assertSame('2 months ago', $this->extension->getPluralizedInterval(2, 0, $month)); } } diff --git a/test/Extension/FlashMessengerExtensionTest.php b/test/Extension/FlashMessengerExtensionTest.php index fde5c76..0baee72 100644 --- a/test/Extension/FlashMessengerExtensionTest.php +++ b/test/Extension/FlashMessengerExtensionTest.php @@ -6,16 +6,17 @@ use Dot\FlashMessenger\View\RendererInterface; use Dot\Twig\Extension\FlashMessengerExtension; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; use Twig\TwigFunction; -use function PHPUnit\Framework\assertInstanceOf; -use function PHPUnit\Framework\assertIsString; - class FlashMessengerExtensionTest extends TestCase { private FlashMessengerExtension $extension; + /** + * @throws Exception + */ protected function setUp(): void { $interfaceMock = $this->createMock(RendererInterface::class); @@ -24,23 +25,23 @@ protected function setUp(): void public function testCreate() { - self::assertInstanceOf(FlashMessengerExtension::class, $this->extension); + $this->assertInstanceOf(FlashMessengerExtension::class, $this->extension); } public function testFunctions() { foreach ($this->extension->getFunctions() as $function) { - assertInstanceOf(TwigFunction::class, $function); + $this->assertInstanceOf(TwigFunction::class, $function); } } public function testWillRenderMessages() { - assertIsString($this->extension->renderMessages()); + $this->assertIsString($this->extension->renderMessages()); } public function testWillRenderMessagesPartial() { - assertIsString($this->extension->renderMessagesPartial('partial string')); + $this->assertIsString($this->extension->renderMessagesPartial('partial string')); } } diff --git a/test/Extension/FormElementsExtensionTest.php b/test/Extension/FormElementsExtensionTest.php index 1c1824a..f6d11ea 100644 --- a/test/Extension/FormElementsExtensionTest.php +++ b/test/Extension/FormElementsExtensionTest.php @@ -15,7 +15,7 @@ public function testWillGetTests() $extension = new FormElementsExtension(); $twigTests = $extension->getTests(); foreach ($twigTests as $element) { - self::assertInstanceOf(TwigTest::class, $element); + $this->assertInstanceOf(TwigTest::class, $element); } } } diff --git a/test/Extension/NavigationExtensionTest.php b/test/Extension/NavigationExtensionTest.php index 7f61a57..5125d1c 100644 --- a/test/Extension/NavigationExtensionTest.php +++ b/test/Extension/NavigationExtensionTest.php @@ -8,6 +8,7 @@ use Dot\Navigation\Page; use Dot\Navigation\View\RendererInterface; use Dot\Twig\Extension\NavigationExtension; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; use Twig\TwigFunction; @@ -15,6 +16,9 @@ class NavigationExtensionTest extends TestCase { private NavigationExtension $extension; + /** + * @throws Exception + */ protected function setUp(): void { $interfaceMock = $this->createMock(RendererInterface::class); @@ -23,29 +27,29 @@ protected function setUp(): void public function testCreate() { - self::assertInstanceOf(NavigationExtension::class, $this->extension); + $this->assertInstanceOf(NavigationExtension::class, $this->extension); } public function testFunctions() { foreach ($this->extension->getFunctions() as $function) { - self::assertInstanceOf(TwigFunction::class, $function); + $this->assertInstanceOf(TwigFunction::class, $function); } } public function testHtmlAttributes() { $page = new Page(); - self::assertIsString($this->extension->htmlAttributes($page)); + $this->assertIsString($this->extension->htmlAttributes($page)); } public function testRenderMenu() { - self::assertIsString($this->extension->renderMenu(new NavigationContainer())); + $this->assertIsString($this->extension->renderMenu(new NavigationContainer())); } public function testRenderMenuPartial() { - self::assertIsString($this->extension->renderMenuPartial(new NavigationContainer(), 'partial')); + $this->assertIsString($this->extension->renderMenuPartial(new NavigationContainer(), 'partial')); } } diff --git a/test/Extension/TranslationExtensionTest.php b/test/Extension/TranslationExtensionTest.php index 38b8186..758e2fd 100644 --- a/test/Extension/TranslationExtensionTest.php +++ b/test/Extension/TranslationExtensionTest.php @@ -18,6 +18,6 @@ protected function setUp(): void public function testTokenParsersIsArray() { - self::assertIsArray($this->extension->getTokenParsers()); + $this->assertIsArray($this->extension->getTokenParsers()); } } diff --git a/test/Factory/AuthenticationExtensionFactoryTest.php b/test/Factory/AuthenticationExtensionFactoryTest.php index ea7fce2..c6c78c0 100644 --- a/test/Factory/AuthenticationExtensionFactoryTest.php +++ b/test/Factory/AuthenticationExtensionFactoryTest.php @@ -9,7 +9,9 @@ use Laminas\Authentication\AuthenticationServiceInterface; use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; +use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; use function PHPUnit\Framework\assertInstanceOf; use function sprintf; @@ -18,17 +20,21 @@ class AuthenticationExtensionFactoryTest extends TestCase { private ContainerInterface $container; + /** + * @throws Exception + */ protected function setUp(): void { $this->container = $this->createMock(ContainerInterface::class); } /** - * @throws \Exception + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function testWillNotInstantiateWithoutInterface() { - $this->container->expects(self::once()) + $this->container->expects($this->once()) ->method('has') ->with(AuthenticationServiceInterface::class) ->willReturn(false); @@ -37,11 +43,13 @@ public function testWillNotInstantiateWithoutInterface() } /** - * @throws \Exception|Exception + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws Exception */ public function testWillInstantiateWithInterface() { - $this->container->expects(self::once()) + $this->container->expects($this->once()) ->method('has') ->with(AuthenticationServiceInterface::class) ->willReturn(true); diff --git a/test/Factory/AuthorizationExtensionFactoryTest.php b/test/Factory/AuthorizationExtensionFactoryTest.php index 131f324..0ed4702 100644 --- a/test/Factory/AuthorizationExtensionFactoryTest.php +++ b/test/Factory/AuthorizationExtensionFactoryTest.php @@ -19,19 +19,21 @@ class AuthorizationExtensionFactoryTest extends TestCase { private ContainerInterface $container; + /** + * @throws Exception + */ protected function setUp(): void { $this->container = $this->createMock(ContainerInterface::class); } /** - * @return void * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ public function testWillNotInstantiateWithoutInterface() { - $this->container->expects(self::once()) + $this->container->expects($this->once()) ->method('has') ->with(AuthorizationInterface::class) ->willReturn(false); @@ -40,14 +42,13 @@ public function testWillNotInstantiateWithoutInterface() } /** - * @return void * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @throws Exception */ public function testWillInstantiateWithInterface() { - $this->container->expects(self::once()) + $this->container->expects($this->once()) ->method('has') ->with(AuthorizationInterface::class) ->willReturn(true); diff --git a/test/Factory/FlashMessengerExtensionFactoryTest.php b/test/Factory/FlashMessengerExtensionFactoryTest.php index 8e569f8..c633c72 100644 --- a/test/Factory/FlashMessengerExtensionFactoryTest.php +++ b/test/Factory/FlashMessengerExtensionFactoryTest.php @@ -9,7 +9,9 @@ use Dot\Twig\Factory\FlashMessengerExtensionFactory; use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; +use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; use function sprintf; @@ -26,12 +28,12 @@ protected function setUp(): void } /** - * @return void - * @throws \Exception + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function testWillNotInstantiateWithoutInterface() { - $this->container->expects(self::once()) + $this->container->expects($this->once()) ->method('has') ->with(RendererInterface::class) ->willReturn(false); @@ -41,13 +43,13 @@ public function testWillNotInstantiateWithoutInterface() } /** - * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface * @throws Exception - * @throws \Exception */ public function testWillInstantiateWithInterface() { - $this->container->expects(self::once()) + $this->container->expects($this->once()) ->method('has') ->with(RendererInterface::class) ->willReturn(true); diff --git a/test/Factory/NavigationExtensionFactoryTest.php b/test/Factory/NavigationExtensionFactoryTest.php index 0bf2324..af69c04 100644 --- a/test/Factory/NavigationExtensionFactoryTest.php +++ b/test/Factory/NavigationExtensionFactoryTest.php @@ -19,6 +19,9 @@ class NavigationExtensionFactoryTest extends TestCase { private ContainerInterface $container; + /** + * @throws Exception + */ protected function setUp(): void { $this->container = $this->createMock(ContainerInterface::class); @@ -30,7 +33,7 @@ protected function setUp(): void */ public function testWillNotInstantiateWithoutInterface() { - $this->container->expects(self::once()) + $this->container->expects($this->once()) ->method('has') ->with(RendererInterface::class) ->willReturn(false); @@ -40,14 +43,13 @@ public function testWillNotInstantiateWithoutInterface() } /** - * @return void * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @throws Exception */ public function testWillInstantiateWithInterface() { - $this->container->expects(self::once()) + $this->container->expects($this->once()) ->method('has') ->with(RendererInterface::class) ->willReturn(true); diff --git a/test/Laminas/View/HelperPluginManagerFactoryTest.php b/test/Laminas/View/HelperPluginManagerFactoryTest.php new file mode 100644 index 0000000..2810ce9 --- /dev/null +++ b/test/Laminas/View/HelperPluginManagerFactoryTest.php @@ -0,0 +1,89 @@ +container = $this->createMock(ContainerInterface::class); + } + + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function testWillNotInstantiateWithoutConfig() + { + $this->container->expects($this->once()) + ->method('has') + ->with('config') + ->willReturn(false); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Unable to find config'); + (new HelperPluginManagerFactory())($this->container); + } + + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function testWillNotInstantiateWithoutViewHelpers() + { + $this->container->expects($this->once()) + ->method('has') + ->with('config') + ->willReturn(true); + + $this->container->expects($this->once()) + ->method('get') + ->with('config') + ->willReturn([ + 'test', + ]); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Unable to find view_helpers config'); + (new HelperPluginManagerFactory())($this->container); + } + + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function testWillInstantiate() + { + $this->container->expects($this->once()) + ->method('has') + ->with('config') + ->willReturn(true); + + $this->container->expects($this->once()) + ->method('get') + ->with('config') + ->willReturn([ + 'view_helpers' => [], + ]); + + $factory = (new HelperPluginManagerFactory())($this->container); + + $this->assertInstanceOf(HelperPluginManager::class, $factory); + } +} From fe6899b9dfe599ea36718df52f6cb69da7e59421 Mon Sep 17 00:00:00 2001 From: Claudiu Pintiuta Date: Tue, 11 Jul 2023 16:40:17 +0300 Subject: [PATCH 4/6] github actions files --- .github/workflows/cs-tests.yml | 46 +++++++++++++++++++++++++++ .github/workflows/static-analysis.yml | 46 +++++++++++++++++++++++++++ .github/workflows/unit-tests.yml | 46 +++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 .github/workflows/cs-tests.yml create mode 100644 .github/workflows/static-analysis.yml create mode 100644 .github/workflows/unit-tests.yml diff --git a/.github/workflows/cs-tests.yml b/.github/workflows/cs-tests.yml new file mode 100644 index 0000000..a04c904 --- /dev/null +++ b/.github/workflows/cs-tests.yml @@ -0,0 +1,46 @@ +on: + - push + +name: Run phpcs checks + +jobs: + mutation: + name: PHP ${{ matrix.php }}-${{ matrix.os }} + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - "8.1" + - "8.2" + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php }}" + tools: composer:v2, cs2pr + coverage: none + + - name: Determine composer cache directory + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v3 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + - name: Install dependencies with composer + run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run phpcs checks + run: vendor/bin/phpcs \ No newline at end of file diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..e5dfb02 --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,46 @@ +on: + - push + +name: Run static analysis + +jobs: + mutation: + name: PHP ${{ matrix.php }}-${{ matrix.os }} + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - "8.1" + - "8.2" + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php }}" + tools: composer:v2, cs2pr + coverage: none + + - name: Determine composer cache directory + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v3 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + - name: Install dependencies with composer + run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run static analysis + run: vendor/bin/psalm --no-cache --output-format=github --show-info=false --threads=4 \ No newline at end of file diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..a8ecec8 --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,46 @@ +on: + - push + +name: Run PHPUnit tests + +jobs: + mutation: + name: PHP ${{ matrix.php }}-${{ matrix.os }} + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - "8.1" + - "8.2" + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php }}" + tools: composer:v2, cs2pr + coverage: none + + - name: Determine composer cache directory + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v3 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + - name: Install dependencies with composer + run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run PHPUnit tests + run: vendor/bin/phpunit --colors=always \ No newline at end of file From 456040b6eb86257878a5394771fe548beb3ed3b7 Mon Sep 17 00:00:00 2001 From: Claudiu Pintiuta Date: Tue, 11 Jul 2023 16:50:16 +0300 Subject: [PATCH 5/6] fixes from review --- .github/workflows/cs-tests.yml | 2 +- .github/workflows/static-analysis.yml | 2 +- .github/workflows/unit-tests.yml | 2 +- src/ConfigProvider.php | 4 ---- src/Extension/AuthenticationExtension.php | 4 ---- src/Extension/AuthorizationExtension.php | 4 ---- src/Extension/FlashMessengerExtension.php | 4 ---- src/Extension/FormElementsExtension.php | 4 ---- src/Extension/NavigationExtension.php | 4 ---- src/Factory/AuthenticationExtensionFactory.php | 4 ---- src/Factory/AuthorizationExtensionFactory.php | 4 ---- src/Factory/FlashMessengerExtensionFactory.php | 4 ---- src/Factory/NavigationExtensionFactory.php | 4 ---- src/Laminas/View/HelperPluginManagerFactory.php | 4 ---- src/TwigEnvironmentDelegator.php | 4 ---- test/Extension/AuthenticationExtensionTest.php | 4 ++-- test/Extension/AuthorizationExtensionTest.php | 4 ++-- test/Extension/DateExtensionTest.php | 10 +++++----- test/Extension/FlashMessengerExtensionTest.php | 8 ++++---- test/Extension/FormElementsExtensionTest.php | 2 +- test/Extension/NavigationExtensionTest.php | 10 +++++----- test/Extension/TranslationExtensionTest.php | 2 +- test/Factory/AuthenticationExtensionFactoryTest.php | 7 ++++--- test/Factory/AuthorizationExtensionFactoryTest.php | 7 ++++--- test/Factory/FlashMessengerExtensionFactoryTest.php | 7 ++++--- test/Factory/NavigationExtensionFactoryTest.php | 7 ++++--- test/Laminas/View/HelperPluginManagerFactoryTest.php | 9 +++++---- 27 files changed, 44 insertions(+), 87 deletions(-) diff --git a/.github/workflows/cs-tests.yml b/.github/workflows/cs-tests.yml index a04c904..3da9965 100644 --- a/.github/workflows/cs-tests.yml +++ b/.github/workflows/cs-tests.yml @@ -43,4 +43,4 @@ jobs: run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi - name: Run phpcs checks - run: vendor/bin/phpcs \ No newline at end of file + run: vendor/bin/phpcs diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index e5dfb02..74550fc 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -43,4 +43,4 @@ jobs: run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi - name: Run static analysis - run: vendor/bin/psalm --no-cache --output-format=github --show-info=false --threads=4 \ No newline at end of file + run: vendor/bin/psalm --no-cache --output-format=github --show-info=false --threads=4 diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index a8ecec8..593bed9 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -43,4 +43,4 @@ jobs: run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi - name: Run PHPUnit tests - run: vendor/bin/phpunit --colors=always \ No newline at end of file + run: vendor/bin/phpunit --colors=always diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 5cd1dc0..5a22162 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -1,9 +1,5 @@ extension = new AuthenticationExtension($interfaceMock); } - public function testCreate() + public function testCreate(): void { $this->assertInstanceOf(AuthenticationExtension::class, $this->extension); } - public function testFunctions() + public function testFunctions(): void { foreach ($this->extension->getFunctions() as $function) { $this->assertInstanceOf(TwigFunction::class, $function); diff --git a/test/Extension/AuthorizationExtensionTest.php b/test/Extension/AuthorizationExtensionTest.php index b8ed6df..3d7c88e 100644 --- a/test/Extension/AuthorizationExtensionTest.php +++ b/test/Extension/AuthorizationExtensionTest.php @@ -23,12 +23,12 @@ protected function setUp(): void $this->extension = new AuthorizationExtension($interfaceMock); } - public function testCreate() + public function testCreate(): void { $this->assertInstanceOf(AuthorizationExtension::class, $this->extension); } - public function testFunctions() + public function testFunctions(): void { foreach ($this->extension->getFunctions() as $function) { $this->assertInstanceOf(TwigFunction::class, $function); diff --git a/test/Extension/DateExtensionTest.php b/test/Extension/DateExtensionTest.php index af9307c..2467403 100644 --- a/test/Extension/DateExtensionTest.php +++ b/test/Extension/DateExtensionTest.php @@ -29,33 +29,33 @@ protected function setUp(): void $this->env->method('getExtension')->willReturn($coreExtension); } - public function testFilters() + public function testFilters(): void { foreach ($this->extension->getFilters() as $filter) { $this->assertInstanceOf(TwigFilter::class, $filter); } } - public function testDiffWillReturnString() + public function testDiffWillReturnString(): void { $this->assertIsString($this->extension->diff($this->env, "2023-07-31")); } - public function testDiffWillReturnExceptionUnexpectedCharacters() + public function testDiffWillReturnExceptionUnexpectedCharacters(): void { $this->expectException(Exception::class); $this->expectExceptionMessage('Unexpected character'); $this->extension->diff($this->env, '2023-23-3322'); } - public function testDiffWillReturnExceptionNotFoundMessage() + public function testDiffWillReturnExceptionNotFoundMessage(): void { $this->expectException(Exception::class); $this->expectExceptionMessage('The timezone could not be found in the database'); $this->extension->diff($this->env, 'asdas'); } - public function testPluralizedInterval() + public function testPluralizedInterval(): void { $month = 'month'; //can be any word $this->assertSame('in 1 month', $this->extension->getPluralizedInterval(1, 1, $month)); diff --git a/test/Extension/FlashMessengerExtensionTest.php b/test/Extension/FlashMessengerExtensionTest.php index 0baee72..c40507c 100644 --- a/test/Extension/FlashMessengerExtensionTest.php +++ b/test/Extension/FlashMessengerExtensionTest.php @@ -23,24 +23,24 @@ protected function setUp(): void $this->extension = new FlashMessengerExtension($interfaceMock); } - public function testCreate() + public function testCreate(): void { $this->assertInstanceOf(FlashMessengerExtension::class, $this->extension); } - public function testFunctions() + public function testFunctions(): void { foreach ($this->extension->getFunctions() as $function) { $this->assertInstanceOf(TwigFunction::class, $function); } } - public function testWillRenderMessages() + public function testWillRenderMessages(): void { $this->assertIsString($this->extension->renderMessages()); } - public function testWillRenderMessagesPartial() + public function testWillRenderMessagesPartial(): void { $this->assertIsString($this->extension->renderMessagesPartial('partial string')); } diff --git a/test/Extension/FormElementsExtensionTest.php b/test/Extension/FormElementsExtensionTest.php index f6d11ea..0b05487 100644 --- a/test/Extension/FormElementsExtensionTest.php +++ b/test/Extension/FormElementsExtensionTest.php @@ -10,7 +10,7 @@ class FormElementsExtensionTest extends TestCase { - public function testWillGetTests() + public function testWillGetTests(): void { $extension = new FormElementsExtension(); $twigTests = $extension->getTests(); diff --git a/test/Extension/NavigationExtensionTest.php b/test/Extension/NavigationExtensionTest.php index 5125d1c..c7ce71c 100644 --- a/test/Extension/NavigationExtensionTest.php +++ b/test/Extension/NavigationExtensionTest.php @@ -25,30 +25,30 @@ protected function setUp(): void $this->extension = new NavigationExtension($interfaceMock); } - public function testCreate() + public function testCreate(): void { $this->assertInstanceOf(NavigationExtension::class, $this->extension); } - public function testFunctions() + public function testFunctions(): void { foreach ($this->extension->getFunctions() as $function) { $this->assertInstanceOf(TwigFunction::class, $function); } } - public function testHtmlAttributes() + public function testHtmlAttributes(): void { $page = new Page(); $this->assertIsString($this->extension->htmlAttributes($page)); } - public function testRenderMenu() + public function testRenderMenu(): void { $this->assertIsString($this->extension->renderMenu(new NavigationContainer())); } - public function testRenderMenuPartial() + public function testRenderMenuPartial(): void { $this->assertIsString($this->extension->renderMenuPartial(new NavigationContainer(), 'partial')); } diff --git a/test/Extension/TranslationExtensionTest.php b/test/Extension/TranslationExtensionTest.php index 758e2fd..8069675 100644 --- a/test/Extension/TranslationExtensionTest.php +++ b/test/Extension/TranslationExtensionTest.php @@ -16,7 +16,7 @@ protected function setUp(): void $this->extension = new TranslationExtension(); } - public function testTokenParsersIsArray() + public function testTokenParsersIsArray(): void { $this->assertIsArray($this->extension->getTokenParsers()); } diff --git a/test/Factory/AuthenticationExtensionFactoryTest.php b/test/Factory/AuthenticationExtensionFactoryTest.php index c6c78c0..f7a8918 100644 --- a/test/Factory/AuthenticationExtensionFactoryTest.php +++ b/test/Factory/AuthenticationExtensionFactoryTest.php @@ -8,6 +8,7 @@ use Dot\Twig\Factory\AuthenticationExtensionFactory; use Laminas\Authentication\AuthenticationServiceInterface; use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -18,7 +19,7 @@ class AuthenticationExtensionFactoryTest extends TestCase { - private ContainerInterface $container; + private ContainerInterface|MockObject $container; /** * @throws Exception @@ -32,7 +33,7 @@ protected function setUp(): void * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function testWillNotInstantiateWithoutInterface() + public function testWillNotInstantiateWithoutInterface(): void { $this->container->expects($this->once()) ->method('has') @@ -47,7 +48,7 @@ public function testWillNotInstantiateWithoutInterface() * @throws NotFoundExceptionInterface * @throws Exception */ - public function testWillInstantiateWithInterface() + public function testWillInstantiateWithInterface(): void { $this->container->expects($this->once()) ->method('has') diff --git a/test/Factory/AuthorizationExtensionFactoryTest.php b/test/Factory/AuthorizationExtensionFactoryTest.php index 0ed4702..49ea028 100644 --- a/test/Factory/AuthorizationExtensionFactoryTest.php +++ b/test/Factory/AuthorizationExtensionFactoryTest.php @@ -8,6 +8,7 @@ use Dot\Twig\Extension\AuthorizationExtension; use Dot\Twig\Factory\AuthorizationExtensionFactory; use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -17,7 +18,7 @@ class AuthorizationExtensionFactoryTest extends TestCase { - private ContainerInterface $container; + private ContainerInterface|MockObject $container; /** * @throws Exception @@ -31,7 +32,7 @@ protected function setUp(): void * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function testWillNotInstantiateWithoutInterface() + public function testWillNotInstantiateWithoutInterface(): void { $this->container->expects($this->once()) ->method('has') @@ -46,7 +47,7 @@ public function testWillNotInstantiateWithoutInterface() * @throws NotFoundExceptionInterface * @throws Exception */ - public function testWillInstantiateWithInterface() + public function testWillInstantiateWithInterface(): void { $this->container->expects($this->once()) ->method('has') diff --git a/test/Factory/FlashMessengerExtensionFactoryTest.php b/test/Factory/FlashMessengerExtensionFactoryTest.php index c633c72..9c5449f 100644 --- a/test/Factory/FlashMessengerExtensionFactoryTest.php +++ b/test/Factory/FlashMessengerExtensionFactoryTest.php @@ -8,6 +8,7 @@ use Dot\Twig\Extension\FlashMessengerExtension; use Dot\Twig\Factory\FlashMessengerExtensionFactory; use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -17,7 +18,7 @@ class FlashMessengerExtensionFactoryTest extends TestCase { - private ContainerInterface $container; + private ContainerInterface|MockObject $container; /** * @throws Exception @@ -31,7 +32,7 @@ protected function setUp(): void * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function testWillNotInstantiateWithoutInterface() + public function testWillNotInstantiateWithoutInterface(): void { $this->container->expects($this->once()) ->method('has') @@ -47,7 +48,7 @@ public function testWillNotInstantiateWithoutInterface() * @throws NotFoundExceptionInterface * @throws Exception */ - public function testWillInstantiateWithInterface() + public function testWillInstantiateWithInterface(): void { $this->container->expects($this->once()) ->method('has') diff --git a/test/Factory/NavigationExtensionFactoryTest.php b/test/Factory/NavigationExtensionFactoryTest.php index af69c04..06b83c8 100644 --- a/test/Factory/NavigationExtensionFactoryTest.php +++ b/test/Factory/NavigationExtensionFactoryTest.php @@ -8,6 +8,7 @@ use Dot\Twig\Extension\NavigationExtension; use Dot\Twig\Factory\NavigationExtensionFactory; use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -17,7 +18,7 @@ class NavigationExtensionFactoryTest extends TestCase { - private ContainerInterface $container; + private ContainerInterface|MockObject $container; /** * @throws Exception @@ -31,7 +32,7 @@ protected function setUp(): void * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function testWillNotInstantiateWithoutInterface() + public function testWillNotInstantiateWithoutInterface(): void { $this->container->expects($this->once()) ->method('has') @@ -47,7 +48,7 @@ public function testWillNotInstantiateWithoutInterface() * @throws NotFoundExceptionInterface * @throws Exception */ - public function testWillInstantiateWithInterface() + public function testWillInstantiateWithInterface(): void { $this->container->expects($this->once()) ->method('has') diff --git a/test/Laminas/View/HelperPluginManagerFactoryTest.php b/test/Laminas/View/HelperPluginManagerFactoryTest.php index 2810ce9..9e3ceb4 100644 --- a/test/Laminas/View/HelperPluginManagerFactoryTest.php +++ b/test/Laminas/View/HelperPluginManagerFactoryTest.php @@ -7,6 +7,7 @@ use Dot\Twig\Laminas\View\HelperPluginManagerFactory; use Exception; use Laminas\View\HelperPluginManager; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -14,7 +15,7 @@ class HelperPluginManagerFactoryTest extends TestCase { - private ContainerInterface $container; + private ContainerInterface|MockObject $container; /** * @throws Exception @@ -29,7 +30,7 @@ protected function setUp(): void * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function testWillNotInstantiateWithoutConfig() + public function testWillNotInstantiateWithoutConfig(): void { $this->container->expects($this->once()) ->method('has') @@ -45,7 +46,7 @@ public function testWillNotInstantiateWithoutConfig() * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function testWillNotInstantiateWithoutViewHelpers() + public function testWillNotInstantiateWithoutViewHelpers(): void { $this->container->expects($this->once()) ->method('has') @@ -68,7 +69,7 @@ public function testWillNotInstantiateWithoutViewHelpers() * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function testWillInstantiate() + public function testWillInstantiate(): void { $this->container->expects($this->once()) ->method('has') From e8815fb41d6da168c35e998934afffa6bdd8e7e5 Mon Sep 17 00:00:00 2001 From: Claudiu Pintiuta Date: Wed, 12 Jul 2023 12:07:46 +0300 Subject: [PATCH 6/6] fixes from reviews --- test/ConfigProviderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php index bc17e75..f425e78 100644 --- a/test/ConfigProviderTest.php +++ b/test/ConfigProviderTest.php @@ -51,7 +51,7 @@ public function testDependenciesHasAliases(): void $this->assertArrayHasKey(AuthenticationServiceInterface::class, $this->config['dependencies']['aliases']); } - public function testDependenciesHasLazyServices() + public function testDependenciesHasLazyServices(): void { $this->assertArrayHasKey('lazy_services', $this->config['dependencies']); $this->assertArrayHasKey('class_map', $this->config['dependencies']['lazy_services']); @@ -65,7 +65,7 @@ public function testDependenciesHasLazyServices() ); } - public function testDependenciesHasDelegators() + public function testDependenciesHasDelegators(): void { $this->assertArrayHasKey('delegators', $this->config['dependencies']); $this->assertArrayHasKey(Environment::class, $this->config['dependencies']['delegators']);