From 6aefd520caee375c45551b35416922456accc644 Mon Sep 17 00:00:00 2001 From: core23 Date: Sat, 6 Jun 2020 12:35:16 +0200 Subject: [PATCH 1/2] Add support for KnpMenu 3 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1d5805f7..7af6754a 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ ], "require": { "php": "^7.3", - "knplabs/knp-menu": "^2.1", + "knplabs/knp-menu": "^2.1 || ^3.1", "sonata-project/block-bundle": "^4.0", "symfony/config": "^4.2 || ^5.0", "symfony/dependency-injection": "^4.2 || ^5.0", From 58bc1442e78c59a6e53b8e2d969318d379e9a12d Mon Sep 17 00:00:00 2001 From: core23 Date: Sat, 6 Jun 2020 15:12:22 +0200 Subject: [PATCH 2/2] Refactor prophecy to native phpunit test --- phpstan-baseline.neon | 10 - tests/Menu/ConfigBuilderTest.php | 390 ++++++++++++++++--------------- 2 files changed, 203 insertions(+), 197 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 69a56e17..aea224d6 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -140,16 +140,6 @@ parameters: count: 1 path: tests/BundleIntegrationTest.php - - - message: "#^Parameter \\#1 \\$factory of class Core23\\\\MenuBundle\\\\Menu\\\\ConfigBuilder constructor expects Knp\\\\Menu\\\\FactoryInterface, object given\\.$#" - count: 7 - path: tests/Menu/ConfigBuilderTest.php - - - - message: "#^Parameter \\#2 \\$translator of class Core23\\\\MenuBundle\\\\Menu\\\\ConfigBuilder constructor expects Symfony\\\\Contracts\\\\Translation\\\\TranslatorInterface, object given\\.$#" - count: 7 - path: tests/Menu/ConfigBuilderTest.php - - message: "#^Parameter \\#1 \\$builder of class Core23\\\\MenuBundle\\\\Provider\\\\ConfigProvider constructor expects Core23\\\\MenuBundle\\\\Menu\\\\ConfigBuilderInterface, object given\\.$#" count: 4 diff --git a/tests/Menu/ConfigBuilderTest.php b/tests/Menu/ConfigBuilderTest.php index 6448aaa2..1052083b 100644 --- a/tests/Menu/ConfigBuilderTest.php +++ b/tests/Menu/ConfigBuilderTest.php @@ -14,73 +14,75 @@ use Core23\MenuBundle\Menu\ConfigBuilder; use Knp\Menu\FactoryInterface; use Knp\Menu\ItemInterface; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\Prophecy\ObjectProphecy; use Symfony\Contracts\Translation\TranslatorInterface; final class ConfigBuilderTest extends TestCase { /** - * @var ObjectProphecy + * @var MockObject&FactoryInterface */ private $factory; /** - * @var ObjectProphecy + * @var MockObject&TranslatorInterface */ private $translator; protected function setUp(): void { - $this->factory = $this->prophesize(FactoryInterface::class); - $this->translator = $this->prophesize(TranslatorInterface::class); + $this->factory = $this->createMock(FactoryInterface::class); + $this->translator = $this->createMock(TranslatorInterface::class); } public function testBuildMenuWithoutItems(): void { $builder = new ConfigBuilder( - $this->factory->reveal(), - $this->translator->reveal() + $this->factory, + $this->translator ); - $mainMenu = $this->prophesize(ItemInterface::class); + $mainMenu = $this->createMock(ItemInterface::class); - $this->factory->createItem('main', [ - 'attributes' => [ - 'class' => 'nav', - ], - 'childrenAttributes' => [ - 'class' => 'nav nav-pills', - ], - ]) - ->willReturn($mainMenu) + $this->factory->method('createItem') + ->with('main', [ + 'attributes' => [ + 'class' => 'nav', + ], + 'childrenAttributes' => [ + 'class' => 'nav nav-pills', + ], + ]) + ->willReturn($mainMenu) ; - static::assertSame($mainMenu->reveal(), $builder->buildMenu([], [])); + static::assertSame($mainMenu, $builder->buildMenu([], [])); } public function testBuildMenuWithOptions(): void { $builder = new ConfigBuilder( - $this->factory->reveal(), - $this->translator->reveal() + $this->factory, + $this->translator ); - $mainMenu = $this->prophesize(ItemInterface::class); + $mainMenu = $this->createMock(ItemInterface::class); - $this->factory->createItem('main', [ - 'attributes' => [ - 'class' => 'nav', - ], - 'childrenAttributes' => [ - 'class' => 'nav nav-pills', - ], - 'foo' => 'bar', - ]) - ->willReturn($mainMenu) + $this->factory->method('createItem') + ->with('main', [ + 'attributes' => [ + 'class' => 'nav', + ], + 'childrenAttributes' => [ + 'class' => 'nav nav-pills', + ], + 'foo' => 'bar', + ]) + ->willReturn($mainMenu) ; - static::assertSame($mainMenu->reveal(), $builder->buildMenu([], [ + static::assertSame($mainMenu, $builder->buildMenu([], [ 'foo' => 'bar', ])); } @@ -88,24 +90,25 @@ public function testBuildMenuWithOptions(): void public function testBuildMenuWithMenuAttributes(): void { $builder = new ConfigBuilder( - $this->factory->reveal(), - $this->translator->reveal() + $this->factory, + $this->translator ); - $mainMenu = $this->prophesize(ItemInterface::class); + $mainMenu = $this->createMock(ItemInterface::class); - $this->factory->createItem('main', [ - 'attributes' => [ - 'class' => 'nav', - ], - 'childrenAttributes' => [ - 'attr-foo' => 'custom', - ], - ]) - ->willReturn($mainMenu) - ; + $this->factory->method('createItem') + ->with('main', [ + 'attributes' => [ + 'class' => 'nav', + ], + 'childrenAttributes' => [ + 'attr-foo' => 'custom', + ], + ]) + ->willReturn($mainMenu) + ; - static::assertSame($mainMenu->reveal(), $builder->buildMenu([ + static::assertSame($mainMenu, $builder->buildMenu([ 'attributes' => [ 'attr-foo' => 'custom', ], @@ -115,43 +118,45 @@ public function testBuildMenuWithMenuAttributes(): void public function testBuildMenuWithItems(): void { $builder = new ConfigBuilder( - $this->factory->reveal(), - $this->translator->reveal() + $this->factory, + $this->translator ); - $item = $this->prophesize(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); - $mainMenu = $this->prophesize(ItemInterface::class); - $mainMenu->addChild($item) - ->shouldBeCalled() + $mainMenu = $this->createMock(ItemInterface::class); + $mainMenu->expects(static::once())->method('addChild') + ->with($item) ; - $this->factory->createItem('main', [ - 'attributes' => [ - 'class' => 'nav', - ], - 'childrenAttributes' => [ - 'class' => 'nav nav-pills', - ], - ]) - ->willReturn($mainMenu) - ; - - $this->factory->createItem('my-label', [ - 'route' => 'my-route', - 'routeParameters' => [ - 'paramKey' => 'value', - ], - 'linkAttributes' => ['class' => 'my-class'], - 'extras' => [ - 'safe_label' => true, - 'translation_domain' => false, - ], - ]) - ->willReturn($item) - ; + $this->factory->expects(static::at(0))->method('createItem') + ->with('main', [ + 'attributes' => [ + 'class' => 'nav', + ], + 'childrenAttributes' => [ + 'class' => 'nav nav-pills', + ], + ]) + ->willReturn($mainMenu) + ; + + $this->factory->expects(static::at(1))->method('createItem') + ->with('my-label', [ + 'route' => 'my-route', + 'routeParameters' => [ + 'paramKey' => 'value', + ], + 'linkAttributes' => ['class' => 'my-class'], + 'extras' => [ + 'safe_label' => true, + 'translation_domain' => false, + ], + ]) + ->willReturn($item) + ; - static::assertSame($mainMenu->reveal(), $builder->buildMenu([ + static::assertSame($mainMenu, $builder->buildMenu([ 'items' => [ [ 'label' => 'my-label', @@ -168,45 +173,48 @@ public function testBuildMenuWithItems(): void public function testBuildMenuWithTranslation(): void { $builder = new ConfigBuilder( - $this->factory->reveal(), - $this->translator->reveal() + $this->factory, + $this->translator ); - $item = $this->prophesize(ItemInterface::class); - - $mainMenu = $this->prophesize(ItemInterface::class); - $mainMenu->addChild($item) - ->shouldBeCalled() - ; + $item = $this->createMock(ItemInterface::class); - $this->factory->createItem('main', [ - 'attributes' => [ - 'class' => 'nav', - ], - 'childrenAttributes' => [ - 'class' => 'nav nav-pills', - ], - ]) - ->willReturn($mainMenu) + $mainMenu = $this->createMock(ItemInterface::class); + $mainMenu->expects(static::once())->method('addChild') + ->with($item) ; - $this->factory->createItem('My label', [ - 'route' => 'my-route', - 'routeParameters' => [], - 'linkAttributes' => [], - 'extras' => [ - 'safe_label' => true, - 'translation_domain' => false, - ], - ]) - ->willReturn($item) - ; + $this->factory->expects(static::at(0))->method('createItem') + ->with('main', [ + 'attributes' => [ + 'class' => 'nav', + ], + 'childrenAttributes' => [ + 'class' => 'nav nav-pills', + ], + ]) + ->willReturn($mainMenu) + ; + + $this->factory->expects(static::at(1))->method('createItem') + ->with('My label', [ + 'route' => 'my-route', + 'routeParameters' => [], + 'linkAttributes' => [], + 'extras' => [ + 'safe_label' => true, + 'translation_domain' => false, + ], + ]) + ->willReturn($item) + ; - $this->translator->trans('my-label', [], 'App') + $this->translator->method('trans') + ->with('my-label', [], 'App') ->willReturn('My label') ; - static::assertSame($mainMenu->reveal(), $builder->buildMenu([ + static::assertSame($mainMenu, $builder->buildMenu([ 'items' => [ [ 'label' => 'my-label', @@ -220,41 +228,43 @@ public function testBuildMenuWithTranslation(): void public function testBuildMenuWithIcon(): void { $builder = new ConfigBuilder( - $this->factory->reveal(), - $this->translator->reveal() + $this->factory, + $this->translator ); - $item = $this->prophesize(ItemInterface::class); + $item = $this->createMock(ItemInterface::class); - $mainMenu = $this->prophesize(ItemInterface::class); - $mainMenu->addChild($item) - ->shouldBeCalled() - ; - - $this->factory->createItem('main', [ - 'attributes' => [ - 'class' => 'nav', - ], - 'childrenAttributes' => [ - 'class' => 'nav nav-pills', - ], - ]) - ->willReturn($mainMenu) + $mainMenu = $this->createMock(ItemInterface::class); + $mainMenu->expects(static::once())->method('addChild') + ->with($item) ; - $this->factory->createItem(' my-label', [ - 'route' => 'my-route', - 'routeParameters' => [], - 'linkAttributes' => [], - 'extras' => [ - 'safe_label' => true, - 'translation_domain' => false, - ], - ]) - ->willReturn($item) - ; + $this->factory->expects(static::at(0))->method('createItem') + ->with('main', [ + 'attributes' => [ + 'class' => 'nav', + ], + 'childrenAttributes' => [ + 'class' => 'nav nav-pills', + ], + ]) + ->willReturn($mainMenu) + ; + + $this->factory->expects(static::at(1))->method('createItem') + ->with(' my-label', [ + 'route' => 'my-route', + 'routeParameters' => [], + 'linkAttributes' => [], + 'extras' => [ + 'safe_label' => true, + 'translation_domain' => false, + ], + ]) + ->willReturn($item) + ; - static::assertSame($mainMenu->reveal(), $builder->buildMenu([ + static::assertSame($mainMenu, $builder->buildMenu([ 'items' => [ [ 'label' => 'my-label', @@ -268,64 +278,70 @@ public function testBuildMenuWithIcon(): void public function testBuildMenuWithChildren(): void { $builder = new ConfigBuilder( - $this->factory->reveal(), - $this->translator->reveal() + $this->factory, + $this->translator ); - $item = $this->prophesize(ItemInterface::class); - - $mainMenu = $this->prophesize(ItemInterface::class); - $mainMenu->addChild($item) - ->shouldBeCalled() + $item = $this->createMock(ItemInterface::class); + $item->expects(static::once())->method('addChild') + ->with($item) ; - $this->factory->createItem('main', [ - 'attributes' => [ - 'class' => 'nav', - ], - 'childrenAttributes' => [ - 'class' => 'nav nav-pills', - ], - ]) - ->willReturn($mainMenu) + $mainMenu = $this->createMock(ItemInterface::class); + $mainMenu->expects(static::once())->method('addChild') + ->with($item) ; - $this->factory->createItem('my-label ', [ - 'route' => 'my-route', - 'routeParameters' => [], - 'linkAttributes' => [ - 'class' => 'dropdown-toggle', - 'data-toggle' => 'dropdown', - 'data-target' => '#', - ], - 'extras' => [ - 'safe_label' => true, - 'translation_domain' => false, - ], - 'attributes' => [ - 'class' => 'dropdown', - ], - 'childrenAttributes' => [ - 'class' => 'dropdown-menu', - ], - 'label' => 'my-label ', - ]) - ->willReturn($item) - ; - - $this->factory->createItem('my-sub-label', [ - 'route' => 'my-sub-route', - 'routeParameters' => [], - 'linkAttributes' => [], - 'extras' => [ - 'safe_label' => true, - 'translation_domain' => false, - ], - ]) - ->willReturn($item) - ; + $this->factory->expects(static::at(0))->method('createItem') + ->with('main', [ + 'attributes' => [ + 'class' => 'nav', + ], + 'childrenAttributes' => [ + 'class' => 'nav nav-pills', + ], + ]) + ->willReturn($mainMenu) + ; + + $this->factory->expects(static::at(1))->method('createItem') + ->with('my-label ', [ + 'route' => 'my-route', + 'routeParameters' => [], + 'linkAttributes' => [ + 'class' => 'dropdown-toggle', + 'data-toggle' => 'dropdown', + 'data-target' => '#', + ], + 'extras' => [ + 'safe_label' => true, + 'translation_domain' => false, + ], + 'attributes' => [ + 'class' => 'dropdown', + ], + 'childrenAttributes' => [ + 'class' => 'dropdown-menu', + ], + 'label' => 'my-label ', + ]) + ->willReturn($item) + ; + + $this->factory->expects(static::at(2))->method('createItem') + ->with('my-sub-label', [ + 'route' => 'my-sub-route', + 'routeParameters' => [], + 'linkAttributes' => [], + 'extras' => [ + 'safe_label' => true, + 'translation_domain' => false, + ], + ]) + ->willReturn($item) + ; - static::assertSame($mainMenu->reveal(), $builder->buildMenu([ + static::assertSame($mainMenu, $builder->buildMenu([ 'items' => [ [ 'label' => 'my-label',