diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a04383d..908027f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -15,15 +15,10 @@ jobs: strategy: fail-fast: false matrix: - php: ['8.1'] - symfony: ['~5.4.0'] + php: ['8.3'] + symfony: ['~7.3.0'] phpunit: ['phpunit.xml'] deps: ['normal'] - include: - - php: '8.1' - symfony: '~5.4.0' - phpunit: 'phpunit.xml' - deps: 'low' steps: - uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 4f3f0d5..a60bd7d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ vendor composer.lock .idea build +.phpunit.cache diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index f6a8302..775f131 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -29,6 +29,7 @@ 'single_line_comment_style' => false, 'trailing_comma_in_multiline' => ['elements' => ['arrays', 'arguments', 'match', 'parameters']], 'yoda_style' => false, + 'fully_qualified_strict_types' => false, // Additional rules 'date_time_immutable' => true, @@ -39,7 +40,7 @@ 'import_functions' => true, ], 'heredoc_indentation' => ['indentation' => 'same_as_start'], - 'mb_str_functions' => true, + // 'mb_str_functions' => true, 'native_constant_invocation' => true, 'nullable_type_declaration_for_default_null_value' => true, 'static_lambda' => true, diff --git a/bundle/Handler/FieldType/Handler.php b/bundle/Handler/FieldType/Handler.php index 749c309..11be254 100644 --- a/bundle/Handler/FieldType/Handler.php +++ b/bundle/Handler/FieldType/Handler.php @@ -15,12 +15,9 @@ abstract class Handler extends BaseHandler { - protected FieldHelper $fieldHelper; - - public function __construct(FieldHelper $fieldHelper) - { - $this->fieldHelper = $fieldHelper; - } + public function __construct( + protected FieldHelper $fieldHelper, + ) {} public function getMetaTags(string $tagName, array $params = []): array { diff --git a/bundle/Handler/FieldType/Image.php b/bundle/Handler/FieldType/Image.php index 267562f..e9284b0 100644 --- a/bundle/Handler/FieldType/Image.php +++ b/bundle/Handler/FieldType/Image.php @@ -17,27 +17,19 @@ use Symfony\Component\HttpFoundation\RequestStack; use function ltrim; -use function mb_strpos; +use function str_starts_with; final class Image extends Handler { - private VariationHandler $imageVariationService; - - private RequestStack $requestStack; - - private LoggerInterface $logger; - public function __construct( FieldHelper $fieldHelper, - VariationHandler $imageVariationService, - RequestStack $requestStack, - ?LoggerInterface $logger = null, + private readonly VariationHandler $imageVariationService, + private readonly RequestStack $requestStack, + private ?LoggerInterface $logger = null, ) { parent::__construct($fieldHelper); - $this->imageVariationService = $imageVariationService; - $this->requestStack = $requestStack; - $this->logger = $logger ?? new NullLogger(); + $this->logger ??= new NullLogger(); } protected function getFieldValue(Field $field, string $tagName, array $params = []): string @@ -48,7 +40,7 @@ protected function getFieldValue(Field $field, string $tagName, array $params = try { $variationUri = $this->imageVariationService->getVariation($field, $this->content->getVersionInfo(), $variationName)->uri; - if (mb_strpos($variationUri, '/') === 0 && ($request = $this->requestStack->getCurrentRequest()) !== null) { + if (str_starts_with($variationUri, '/') && ($request = $this->requestStack->getCurrentRequest()) !== null) { $variationUri = $request->getUriForPath('/' . ltrim($variationUri, '/')); } diff --git a/bundle/Handler/Literal/CanonicalUrl.php b/bundle/Handler/Literal/CanonicalUrl.php index b63e659..ebf8cf2 100644 --- a/bundle/Handler/Literal/CanonicalUrl.php +++ b/bundle/Handler/Literal/CanonicalUrl.php @@ -15,15 +15,10 @@ final class CanonicalUrl implements HandlerInterface { - private RequestStack $requestStack; - - private UrlGeneratorInterface $urlGenerator; - - public function __construct(RequestStack $requestStack, UrlGeneratorInterface $urlGenerator) - { - $this->requestStack = $requestStack; - $this->urlGenerator = $urlGenerator; - } + public function __construct( + private readonly RequestStack $requestStack, + private readonly UrlGeneratorInterface $urlGenerator, + ) {} public function getMetaTags($tagName, array $params = []): array { diff --git a/bundle/Handler/Literal/Url.php b/bundle/Handler/Literal/Url.php index dd1548a..1501603 100644 --- a/bundle/Handler/Literal/Url.php +++ b/bundle/Handler/Literal/Url.php @@ -15,12 +15,9 @@ final class Url implements HandlerInterface { - private RequestStack $requestStack; - - public function __construct(RequestStack $requestStack) - { - $this->requestStack = $requestStack; - } + public function __construct( + private readonly RequestStack $requestStack, + ) {} public function getMetaTags(string $tagName, array $params = []): array { diff --git a/bundle/MetaTag/Collector.php b/bundle/MetaTag/Collector.php index c825457..91d17d5 100644 --- a/bundle/MetaTag/Collector.php +++ b/bundle/MetaTag/Collector.php @@ -16,31 +16,24 @@ final class Collector implements CollectorInterface { - private Registry $metaTagHandlers; - - private ContentTypeService $contentTypeService; - - private ConfigResolverInterface $configResolver; - - public function __construct(Registry $metaTagHandlers, ContentTypeService $contentTypeService, ConfigResolverInterface $configResolver) - { - $this->metaTagHandlers = $metaTagHandlers; - $this->contentTypeService = $contentTypeService; - $this->configResolver = $configResolver; - } + public function __construct( + private readonly Registry $metaTagHandlers, + private readonly ContentTypeService $contentTypeService, + private readonly ConfigResolverInterface $configResolver, + ) {} public function collect(Content $content): array { $metaTags = []; - $allHandlers = $this->configResolver->hasParameter('global_handlers', 'netgen_open_graph') ? - $this->configResolver->getParameter('global_handlers', 'netgen_open_graph') : - []; + $allHandlers = $this->configResolver->hasParameter('global_handlers', 'netgen_open_graph') + ? $this->configResolver->getParameter('global_handlers', 'netgen_open_graph') + : []; $contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId); - $contentTypeHandlers = $this->configResolver->hasParameter('content_type_handlers', 'netgen_open_graph') ? - $this->configResolver->getParameter('content_type_handlers', 'netgen_open_graph') : - []; + $contentTypeHandlers = $this->configResolver->hasParameter('content_type_handlers', 'netgen_open_graph') + ? $this->configResolver->getParameter('content_type_handlers', 'netgen_open_graph') + : []; if (isset($contentTypeHandlers[$contentType->identifier])) { $allHandlers = array_merge( @@ -61,8 +54,8 @@ public function collect(Content $content): array foreach ($newMetaTags as $metaTag) { if (!$metaTag instanceof Item) { throw new LogicException( - '\'' . $handler['handler'] . '\' handler returned wrong value.' . - ' Expected \'Netgen\Bundle\OpenGraphBundle\MetaTag\Item\', got \'' . get_class($metaTag) . '\'.', + '\'' . $handler['handler'] . '\' handler returned wrong value.' + . ' Expected \'Netgen\Bundle\OpenGraphBundle\MetaTag\Item\', got \'' . get_class($metaTag) . '\'.', ); } diff --git a/bundle/MetaTag/Item.php b/bundle/MetaTag/Item.php index 0268279..f17416a 100644 --- a/bundle/MetaTag/Item.php +++ b/bundle/MetaTag/Item.php @@ -6,15 +6,10 @@ final class Item { - private string $tagName; - - private string $tagValue; - - public function __construct(string $tagName, string $tagValue) - { - $this->tagName = $tagName; - $this->tagValue = $tagValue; - } + public function __construct( + private readonly string $tagName, + private readonly string $tagValue, + ) {} /** * Returns tag name. diff --git a/bundle/Templating/Twig/Extension/NetgenOpenGraphRuntime.php b/bundle/Templating/Twig/Extension/NetgenOpenGraphRuntime.php index 12a07db..38898a9 100644 --- a/bundle/Templating/Twig/Extension/NetgenOpenGraphRuntime.php +++ b/bundle/Templating/Twig/Extension/NetgenOpenGraphRuntime.php @@ -13,26 +13,18 @@ final class NetgenOpenGraphRuntime { - private CollectorInterface $tagCollector; - - private RendererInterface $tagRenderer; - - private LoggerInterface $logger; - private bool $throwExceptions = true; public function __construct( - CollectorInterface $tagCollector, - RendererInterface $tagRenderer, - ?LoggerInterface $logger = null, + private readonly CollectorInterface $tagCollector, + private readonly RendererInterface $tagRenderer, + private ?LoggerInterface $logger = null, ) { - $this->tagCollector = $tagCollector; - $this->tagRenderer = $tagRenderer; - $this->logger = $logger ?? new NullLogger(); + $this->logger ??= new NullLogger(); } /** - * Sets the flag that determines if the exceptions will thrown instead of logged. + * Sets the flag that determines if the exceptions will be thrown instead of logged. */ public function setThrowExceptions(bool $throwExceptions = true): void { diff --git a/composer.json b/composer.json index 9d818b5..c9c7193 100644 --- a/composer.json +++ b/composer.json @@ -18,15 +18,15 @@ } ], "require": { - "php": "^8.1", - "ibexa/core": "^4.4", + "php": "^8.3", + "ibexa/core": "^5.0", "twig/twig": "^3.0" }, "require-dev": { - "ibexa/fieldtype-richtext": "^4.4", - "phpunit/phpunit": "^9.6", - "matthiasnoback/symfony-config-test": "^5.0", - "matthiasnoback/symfony-dependency-injection-test": "^5.0" + "ibexa/fieldtype-richtext": "^5.0", + "phpunit/phpunit": "^10.0", + "matthiasnoback/symfony-config-test": "^6.0", + "matthiasnoback/symfony-dependency-injection-test": "^6.0" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/phpunit.xml b/phpunit.xml index 148ef7c..cd47c66 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,12 +1,10 @@ - @@ -15,18 +13,17 @@ tests - - + + + bundle - - bundle/Resources - - - + + + bundle/Resources + + + - - - - + diff --git a/tests/DependencyInjection/NetgenOpenGraphExtensionTest.php b/tests/DependencyInjection/NetgenOpenGraphExtensionTest.php index dd361cf..19a25d4 100644 --- a/tests/DependencyInjection/NetgenOpenGraphExtensionTest.php +++ b/tests/DependencyInjection/NetgenOpenGraphExtensionTest.php @@ -6,12 +6,11 @@ use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; use Netgen\Bundle\OpenGraphBundle\DependencyInjection\NetgenOpenGraphExtension; +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; final class NetgenOpenGraphExtensionTest extends AbstractExtensionTestCase { - /** - * @doesNotPerformAssertions - */ + #[DoesNotPerformAssertions] public function testItSetsValidContainerParameters(): void { $this->container->setParameter('ibexa.site_access.list', []); diff --git a/tests/Handler/FieldType/ImageTest.php b/tests/Handler/FieldType/ImageTest.php index 89dddba..9586332 100644 --- a/tests/Handler/FieldType/ImageTest.php +++ b/tests/Handler/FieldType/ImageTest.php @@ -129,12 +129,23 @@ public function testGettingTagsWithExceptionThrownByVariationHandler(): void ->method('getField') ->willReturn($this->field); + $this->fieldHelper->expects(self::once()) + ->method('isFieldEmpty') + ->willReturn(false); + + $this->variationHandler->expects(self::once()) + ->method('getVariation') + ->willThrowException(new \Exception('Variation handler error')); + $request = Request::create('/'); $this->requestStack->expects(self::once()) ->method('getCurrentRequest') ->willReturn($request); + $this->logger->expects(self::once()) + ->method('error'); + $this->image->getMetaTags('some_tag', ['some_value', 'some_value_2', 'some_value_3']); } @@ -221,6 +232,27 @@ public function testGettingTagsWithMultipleArgumentsInArray(): void ->method('getField') ->willReturn($this->field); - $this->image->getMetaTags('some_tag', ['some_value', 'some_value_2']); + $this->fieldHelper->expects(self::once()) + ->method('isFieldEmpty') + ->willReturn(false); + + $variation = new Variation(['uri' => '/some/uri']); + + $this->variationHandler->expects(self::once()) + ->method('getVariation') + ->willReturn($variation); + + $request = Request::create('/'); + + $this->requestStack->expects(self::exactly(1)) + ->method('getCurrentRequest') + ->willReturn($request); + + $result = $this->image->getMetaTags('some_tag', ['some_value', 'some_value_2']); + + self::assertCount(1, $result); + self::assertInstanceOf('Netgen\Bundle\OpenGraphBundle\MetaTag\Item', $result[0]); + self::assertSame('some_tag', $result[0]->getTagName()); + self::assertSame('http://localhost/some/uri', $result[0]->getTagValue()); } } diff --git a/tests/Handler/Literal/UrlTest.php b/tests/Handler/Literal/UrlTest.php index 73b61ab..29a6fc6 100644 --- a/tests/Handler/Literal/UrlTest.php +++ b/tests/Handler/Literal/UrlTest.php @@ -8,6 +8,7 @@ use Netgen\Bundle\OpenGraphBundle\Handler\HandlerInterface; use Netgen\Bundle\OpenGraphBundle\Handler\Literal\Url; use Netgen\Bundle\OpenGraphBundle\MetaTag\Item; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; @@ -37,9 +38,7 @@ public function testGettingTagsWithEmptyParams(): void $this->url->getMetaTags('some_tag'); } - /** - * @dataProvider validResultProvider - */ + #[DataProvider('validResultProvider')] public function testGettingTagsWithValidResult(string $input, string $output): void { $request = Request::create('https://domain.com'); @@ -54,7 +53,7 @@ public function testGettingTagsWithValidResult(string $input, string $output): v self::assertSame($output, $result[0]->getTagValue()); } - public function validResultProvider(): iterable + public static function validResultProvider(): iterable { return [ ['https://other.domain.com/some/path', 'https://other.domain.com/some/path'], @@ -66,9 +65,7 @@ public function validResultProvider(): iterable ]; } - /** - * @dataProvider validResultProviderWithoutRequest - */ + #[DataProvider('validResultProviderWithoutRequest')] public function testGettingTagsWithValidResultAndWithoutRequest(string $input, string $output): void { $result = $this->url->getMetaTags('some_tag', [$input]); @@ -79,7 +76,7 @@ public function testGettingTagsWithValidResultAndWithoutRequest(string $input, s self::assertSame($output, $result[0]->getTagValue()); } - public function validResultProviderWithoutRequest(): iterable + public static function validResultProviderWithoutRequest(): iterable { return [ ['https://other.domain.com/some/path', 'https://other.domain.com/some/path'], diff --git a/tests/Handler/RegistryTest.php b/tests/Handler/RegistryTest.php index 3d529ce..0094af7 100644 --- a/tests/Handler/RegistryTest.php +++ b/tests/Handler/RegistryTest.php @@ -20,7 +20,7 @@ protected function setUp(): void public function testAddingHandlers(): void { - $handler = $this->getMockForAbstractClass(HandlerInterface::class); + $handler = self::createStub(HandlerInterface::class); $this->registry->addHandler('some_handler', $handler); self::assertSame($this->registry->getHandler('some_handler'), $handler); @@ -28,7 +28,7 @@ public function testAddingHandlers(): void public function testGettingHandlers(): void { - $handler = $this->getMockForAbstractClass(HandlerInterface::class); + $handler = self::createStub(HandlerInterface::class); $this->registry->addHandler('some_handler', $handler); $returnedHandler = $this->registry->getHandler('some_handler'); diff --git a/tests/MetaTag/CollectorTest.php b/tests/MetaTag/CollectorTest.php index 26b7ce1..7d22f33 100644 --- a/tests/MetaTag/CollectorTest.php +++ b/tests/MetaTag/CollectorTest.php @@ -48,17 +48,9 @@ protected function setUp(): void public function testCollect(): void { - $this->config->expects(self::at(0)) + $this->config ->method('hasParameter') - ->willReturn(true); - - $this->config->expects(self::at(1)) - ->method('getParameter') - ->willReturn([]); - - $this->config->expects(self::at(2)) - ->method('hasParameter') - ->willReturn(true); + ->willReturnOnConsecutiveCalls(true, true); $handlers = [ 'article' => [ @@ -72,9 +64,9 @@ public function testCollect(): void ], ]; - $this->config->expects(self::at(3)) + $this->config ->method('getParameter') - ->willReturn($handlers); + ->willReturnOnConsecutiveCalls([], $handlers); $versionInfo = new VersionInfo( [ @@ -100,7 +92,7 @@ public function testCollect(): void 'fieldDefinitions' => [ new FieldDefinition( [ - 'id' => 'id', + 'id' => 42, 'identifier' => 'name', 'fieldTypeIdentifier' => 'eztext', ], @@ -136,18 +128,14 @@ public function testCollectWithLogicException(): void $handlers = ['all_content_types' => $handlerArray]; - $this->config->expects(self::at(0)) + $this->config ->method('hasParameter') - ->willReturn(true); + ->willReturnOnConsecutiveCalls(true, false); - $this->config->expects(self::at(1)) + $this->config ->method('getParameter') ->willReturn($handlers); - $this->config->expects(self::at(2)) - ->method('hasParameter') - ->willReturn(false); - $versionInfo = new VersionInfo( [ 'contentInfo' => new ContentInfo( @@ -172,7 +160,7 @@ public function testCollectWithLogicException(): void 'fieldDefinitions' => [ new FieldDefinition( [ - 'id' => 'id', + 'id' => 42, 'identifier' => 'name', 'fieldTypeIdentifier' => 'eztext', ], diff --git a/tests/NetgenOpenGraphBundleTest.php b/tests/NetgenOpenGraphBundleTest.php index e7c2e55..c3c2462 100644 --- a/tests/NetgenOpenGraphBundleTest.php +++ b/tests/NetgenOpenGraphBundleTest.php @@ -18,7 +18,7 @@ public function testItAddsCompilerPass(): void ->onlyMethods(['addCompilerPass']) ->getMock(); - $container->expects(self::at(0)) + $container->expects(self::exactly(1)) ->method('addCompilerPass') ->with(new MetaTagHandlersCompilerPass());