From c75c12d1e361b4b34e509802b3030d880ff02b96 Mon Sep 17 00:00:00 2001 From: Andrea Sprega Date: Mon, 26 Aug 2024 11:54:30 +0200 Subject: [PATCH] Bump dependencies to PHP 8.1 and Symfony 7.1. Update attribute handling logic to leverage work already done by the framework. --- .github/workflows/test.yml | 2 +- .gitignore | 2 +- composer.json | 15 +++--- phpunit.xml.dist | 21 ++++---- src/EventListener/BreadcrumbListener.php | 20 +------- tests/Unit/Service/BreadcrumbBuilderTest.php | 15 ++---- .../Service/BreadcrumbItemFactoryTest.php | 12 ++--- .../Service/BreadcrumbItemProcessorTest.php | 49 +++---------------- 8 files changed, 40 insertions(+), 96 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6023051..70e376b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,6 @@ jobs: strategy: matrix: php-version: - - "8.0" - "8.1" - "8.2" - "8.3" @@ -29,6 +28,7 @@ jobs: uses: "shivammathur/setup-php@v2" with: php-version: "${{ matrix.php-version }}" + coverage: xdebug ini-values: "zend.assertions=1" - name: "Cache dependencies installed with composer" diff --git a/.gitignore b/.gitignore index 1aa32b1..93305c2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ /var phpunit.xml composer.lock -.phpunit.result.cache +.phpunit.cache diff --git a/composer.json b/composer.json index b4e2bfd..71fd6d2 100644 --- a/composer.json +++ b/composer.json @@ -19,17 +19,18 @@ } ], "require": { - "php": ">=8.0.0", - "symfony/framework-bundle": "^5.4|^6.3|^7.0|^7.1", - "symfony/property-access": "^5.4|^6.3|^7.0|^7.1", - "symfony/translation": "^5.4|^6.3|^7.0|^7.1", - "symfony/yaml": "^5.4|^6.3|^7.0|^7.1", + "php": ">=8.1.0", + "symfony/framework-bundle": "^6.4|^7.0|^7.1", + "symfony/property-access": "^6.4|^7.0|^7.1", + "symfony/translation": "^6.4|^7.0|^7.1", + "symfony/yaml": "^6.4|^7.0|^7.1", "twig/twig": "^2.10|^3.0" }, "require-dev": { "mockery/mockery": "^1.3", - "phpunit/phpunit": "8.5.15", - "symfony/dependency-injection": "^5.4|^6.3|^7.0|^7.1", + "phpunit/php-code-coverage": "10.1.16", + "phpunit/phpunit": "10.5.30", + "symfony/dependency-injection": "^6.4|^7.0|^7.1", "symfony/monolog-bundle": "^3.7" }, "autoload": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index fbdad9e..31fb507 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,14 +1,17 @@ - - - tests - - + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" + cacheDirectory=".phpunit.cache"> + + + tests + + + + + src + + diff --git a/src/EventListener/BreadcrumbListener.php b/src/EventListener/BreadcrumbListener.php index c7f0a0f..09bd586 100644 --- a/src/EventListener/BreadcrumbListener.php +++ b/src/EventListener/BreadcrumbListener.php @@ -17,25 +17,7 @@ public function __construct(BreadcrumbBuilder $breadcrumbBuilder) public function onKernelController(ControllerEvent $event): void { - // In case controller is not an array (e.g. a closure or an invokable class), we can't do anything. - if (!is_array($event->getController())) { - return; - } - - list($controller, $action) = $event->getController(); - - $class = new \ReflectionClass($controller); - $method = new \ReflectionMethod($controller, $action); - - $breadcrumbs = []; - if (($classAttribute = $class->getAttributes(Breadcrumb::class)[0] ?? null)) { - $breadcrumbs[] = $classAttribute->newInstance(); - } - if (($methodAttribute = $method->getAttributes(Breadcrumb::class)[0] ?? null)) { - $breadcrumbs[] = $methodAttribute->newInstance(); - } - - foreach ($breadcrumbs as $breadcrumb) { + foreach ($event->getAttributes(Breadcrumb::class) as $breadcrumb) { foreach ($breadcrumb->items as $item) { $this->breadcrumbBuilder->addItem( $item['label'], diff --git a/tests/Unit/Service/BreadcrumbBuilderTest.php b/tests/Unit/Service/BreadcrumbBuilderTest.php index 5febf6e..1559963 100644 --- a/tests/Unit/Service/BreadcrumbBuilderTest.php +++ b/tests/Unit/Service/BreadcrumbBuilderTest.php @@ -3,6 +3,7 @@ namespace SlopeIt\Tests\BreadcrumbBundle\Unit\Service; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; +use Mockery\MockInterface; use PHPUnit\Framework\TestCase; use SlopeIt\BreadcrumbBundle\Model\BreadcrumbItem; use SlopeIt\BreadcrumbBundle\Service\BreadcrumbBuilder; @@ -15,15 +16,9 @@ class BreadcrumbBuilderTest extends TestCase { use MockeryPHPUnitIntegration; - /** - * @var BreadcrumbBuilder - */ - private $builder; + private BreadcrumbBuilder $builder; - /** - * @var BreadcrumbItemFactory|\Mockery\MockInterface - */ - private $itemFactory; + private BreadcrumbItemFactory|MockInterface $itemFactory; protected function setUp(): void { @@ -48,9 +43,9 @@ public function test_getItems_emptyBreadcrumb() public function test_getItems_nonEmptyBreadcrumb() { $item1 = \Mockery::mock(BreadcrumbItem::class); - $this->itemFactory->shouldReceive('create')->with('aLabel', 'aRoute', null, null)->andReturn($item1); + $this->itemFactory->expects('create')->with('aLabel', 'aRoute', null, null)->andReturn($item1); $item2 = \Mockery::mock(BreadcrumbItem::class); - $this->itemFactory->shouldReceive('create')->with('anotherLabel', 'anotherRoute', [ 'a' => 'b' ], null) + $this->itemFactory->expects('create')->with('anotherLabel', 'anotherRoute', [ 'a' => 'b' ], null) ->andReturn($item2); $this->builder->addItem('aLabel', 'aRoute'); diff --git a/tests/Unit/Service/BreadcrumbItemFactoryTest.php b/tests/Unit/Service/BreadcrumbItemFactoryTest.php index 30dc5a1..ff39f17 100644 --- a/tests/Unit/Service/BreadcrumbItemFactoryTest.php +++ b/tests/Unit/Service/BreadcrumbItemFactoryTest.php @@ -3,26 +3,22 @@ namespace SlopeIt\Tests\BreadcrumbBundle\Unit\Service; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -use SlopeIt\BreadcrumbBundle\Model\BreadcrumbItem; use SlopeIt\BreadcrumbBundle\Service\BreadcrumbItemFactory; -/** - * @coversDefaultClass \SlopeIt\BreadcrumbBundle\Service\BreadcrumbItemFactory - */ +#[CoversClass(BreadcrumbItemFactory::class)] class BreadcrumbItemFactoryTest extends TestCase { use MockeryPHPUnitIntegration; - /** - * @covers ::create - */ public function test_create() { $factory = new BreadcrumbItemFactory(); $item = $factory->create('aLabel', 'aRoute'); - $this->assertTrue($item instanceof BreadcrumbItem); + $this->assertSame('aLabel', $item->getLabel()); + $this->assertSame('aRoute', $item->getRoute()); } } diff --git a/tests/Unit/Service/BreadcrumbItemProcessorTest.php b/tests/Unit/Service/BreadcrumbItemProcessorTest.php index 140e739..9b87927 100644 --- a/tests/Unit/Service/BreadcrumbItemProcessorTest.php +++ b/tests/Unit/Service/BreadcrumbItemProcessorTest.php @@ -3,6 +3,8 @@ namespace SlopeIt\Tests\BreadcrumbBundle\Unit\Service; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; +use Mockery\MockInterface; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use SlopeIt\BreadcrumbBundle\Model\BreadcrumbItem; use SlopeIt\BreadcrumbBundle\Service\BreadcrumbItemProcessor; @@ -14,37 +16,20 @@ use Symfony\Component\Routing\RequestContext; use Symfony\Contracts\Translation\TranslatorInterface; -/** - * @coversDefaultClass \SlopeIt\BreadcrumbBundle\Service\BreadcrumbItemProcessor - */ +#[CoversClass(BreadcrumbItemProcessor::class)] class BreadcrumbItemProcessorTest extends TestCase { use MockeryPHPUnitIntegration; - /** - * @var BreadcrumbItemProcessor - */ - private $SUT; + private BreadcrumbItemProcessor $SUT; - /** - * @var PropertyAccessorInterface|\Mockery\MockInterface - */ - private $propertyAccessor; + private PropertyAccessorInterface|MockInterface $propertyAccessor; - /** - * @var RequestStack|\Mockery\MockInterface - */ - private $requestStack; + private RequestStack|MockInterface $requestStack; - /** - * @var UrlGeneratorInterface|\Mockery\MockInterface - */ - private $urlGenerator; + private UrlGeneratorInterface|MockInterface $urlGenerator; - /** - * @var TranslatorInterface|\Mockery\MockInterface - */ - private $translator; + private TranslatorInterface|MockInterface $translator; protected function setUp(): void { @@ -70,9 +55,6 @@ protected function setUp(): void ); } - /** - * @covers ::process - */ public function test_process_item_with_label_as_simple_variable() { $item = new BreadcrumbItem('$variableName'); @@ -82,9 +64,6 @@ public function test_process_item_with_label_as_simple_variable() $this->assertSame('variableValue', $processedItems[0]->getTranslatedLabel()); } - /** - * @covers ::process - */ public function test_process_item_with_label_as_variable_with_property_path() { $item = new BreadcrumbItem('$variableName.property.nestedProperty'); @@ -99,9 +78,6 @@ public function test_process_item_with_label_as_variable_with_property_path() $this->assertSame('propertyValue', $processedItems[0]->getTranslatedLabel()); } - /** - * @covers ::process - */ public function test_process_item_with_label_not_to_be_translated() { $item = new BreadcrumbItem('Already translated label', null, null, false); @@ -111,9 +87,6 @@ public function test_process_item_with_label_not_to_be_translated() $this->assertSame('Already translated label', $processedItems[0]->getTranslatedLabel()); } - /** - * @covers ::process - */ public function test_process_item_with_label_to_be_translated_with_default_translation_domain() { $item = new BreadcrumbItem('translatable_key'); @@ -126,9 +99,6 @@ public function test_process_item_with_label_to_be_translated_with_default_trans $this->assertSame('Translated label', $processedItems[0]->getTranslatedLabel()); } - /** - * @covers ::process - */ public function test_process_item_with_label_to_be_translated_with_specific_translation_domain() { $item = new BreadcrumbItem('translatable_key', null, null, 'custom_domain'); @@ -141,9 +111,6 @@ public function test_process_item_with_label_to_be_translated_with_specific_tran $this->assertSame('Translated label', $processedItems[0]->getTranslatedLabel()); } - /** - * @covers ::process - */ public function test_process_item_with_null_label() { $item = new BreadcrumbItem();