From 3a009fe87b6349812ea994ed8321012d36b423a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 10 Sep 2024 12:05:54 +0200 Subject: [PATCH] [phpspec-2-phpunit] First migrated tests (Grid) --- .../Grid/State/RequestGridProviderTest.php | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 src/Component/tests/Grid/State/RequestGridProviderTest.php diff --git a/src/Component/tests/Grid/State/RequestGridProviderTest.php b/src/Component/tests/Grid/State/RequestGridProviderTest.php new file mode 100644 index 000000000..cfdfb19a7 --- /dev/null +++ b/src/Component/tests/Grid/State/RequestGridProviderTest.php @@ -0,0 +1,188 @@ +gridViewFactory = $this->prophesize(GridViewFactoryInterface::class); + $this->gridProvider = $this->prophesize(GridProviderInterface::class); + $this->provider = new RequestGridProvider( + $this->gridViewFactory->reveal(), + $this->gridProvider->reveal(), + ); + } + + public function testItIsInitializable(): void + { + $this->assertInstanceOf(RequestGridProvider::class, $this->provider); + } + + public function testItProvidesAGridView(): void + { + $request = $this->prophesize(Request::class); + $context = new Context(new RequestOption($request->reveal())); + $operation = new Index(grid: 'app_book'); + + $request->query = new InputBag(); + $gridDefinition = $this->prophesize(Grid::class); + $gridView = $this->prophesize(GridView::class); + + $this->gridProvider->get('app_book')->willReturn($gridDefinition); + $gridDefinition->getDriverConfiguration()->willReturn([]); + $this->gridViewFactory->create($gridDefinition, $context, new Parameters(), [])->willReturn($gridView); + + $this->assertSame($gridView->reveal(), $this->provider->provide($operation, $context)); + } + + public function testItSetsCurrentPageFromRequest(): void + { + $request = $this->prophesize(Request::class); + $context = new Context(new RequestOption($request->reveal())); + $operation = new Index(grid: 'app_book'); + + $request->query = new InputBag(['page' => 42]); + $gridDefinition = $this->prophesize(Grid::class); + $gridView = $this->prophesize(GridView::class); + $pagerfanta = $this->prophesize(Pagerfanta::class); + + $this->gridProvider->get('app_book')->willReturn($gridDefinition); + $gridDefinition->getLimits()->willReturn([]); + $gridDefinition->getDriverConfiguration()->willReturn([]); + $this->gridViewFactory->create($gridDefinition, $context, new Parameters(['page' => 42]), [])->willReturn($gridView); + + $gridView->getData()->willReturn($pagerfanta); + $pagerfanta->setCurrentPage(42)->willReturn($pagerfanta)->shouldBeCalled(); + $pagerfanta->setMaxPerPage(10)->willReturn($pagerfanta)->shouldBeCalled(); + + $this->assertSame($gridView->reveal(), $this->provider->provide($operation, $context)); + } + + public function testItSetsMaxPerPageFromRequest(): void + { + $request = $this->prophesize(Request::class); + $context = new Context(new RequestOption($request->reveal())); + $operation = new Index(grid: 'app_book'); + + $request->query = new InputBag(['limit' => 25]); + $gridDefinition = $this->prophesize(Grid::class); + $gridView = $this->prophesize(GridView::class); + $pagerfanta = $this->prophesize(Pagerfanta::class); + + $this->gridProvider->get('app_book')->willReturn($gridDefinition); + $gridDefinition->getDriverConfiguration()->willReturn([]); + $gridDefinition->getLimits()->willReturn([10, 25]); + $this->gridViewFactory->create($gridDefinition, $context, new Parameters(['limit' => 25]), [])->willReturn($gridView); + + $gridView->getData()->willReturn($pagerfanta); + $pagerfanta->setCurrentPage(1)->willReturn($pagerfanta)->shouldBeCalled(); + $pagerfanta->setMaxPerPage(25)->willReturn($pagerfanta)->shouldBeCalled(); + + $this->assertSame($gridView->reveal(), $this->provider->provide($operation, $context)); + } + + public function testItSetsMaxPerPageFromGridConfiguration(): void + { + $request = $this->prophesize(Request::class); + $context = new Context(new RequestOption($request->reveal())); + $operation = new Index(grid: 'app_book'); + + $request->query = new InputBag(); + $gridDefinition = $this->prophesize(Grid::class); + $gridView = $this->prophesize(GridView::class); + $pagerfanta = $this->prophesize(Pagerfanta::class); + + $this->gridProvider->get('app_book')->willReturn($gridDefinition); + $gridDefinition->getDriverConfiguration()->willReturn([]); + $gridDefinition->getLimits()->willReturn([15, 30]); + $this->gridViewFactory->create($gridDefinition, $context, new Parameters([]), [])->willReturn($gridView); + + $gridView->getData()->willReturn($pagerfanta); + $pagerfanta->setCurrentPage(1)->willReturn($pagerfanta)->shouldBeCalled(); + $pagerfanta->setMaxPerPage(15)->willReturn($pagerfanta)->shouldBeCalled(); + + $this->assertSame($gridView->reveal(), $this->provider->provide($operation, $context)); + } + + public function testItLimitsMaxPerPageWithMaxGridConfigurationLimit(): void + { + $request = $this->prophesize(Request::class); + $context = new Context(new RequestOption($request->reveal())); + $operation = new Index(grid: 'app_book'); + + $request->query = new InputBag(['limit' => 40]); + $gridDefinition = $this->prophesize(Grid::class); + $gridView = $this->prophesize(GridView::class); + $pagerfanta = $this->prophesize(Pagerfanta::class); + + $this->gridProvider->get('app_book')->willReturn($gridDefinition); + $gridDefinition->getDriverConfiguration()->willReturn([]); + $gridDefinition->getLimits()->willReturn([15, 30]); + $this->gridViewFactory->create($gridDefinition, $context, new Parameters(['limit' => 40]), [])->willReturn($gridView); + + $gridView->getData()->willReturn($pagerfanta); + $pagerfanta->setCurrentPage(1)->willReturn($pagerfanta)->shouldBeCalled(); + $pagerfanta->setMaxPerPage(30)->willReturn($pagerfanta)->shouldBeCalled(); + + $this->assertSame($gridView->reveal(), $this->provider->provide($operation, $context)); + } + + public function testItThrowsAnExceptionWhenOperationHasNoGrid(): void + { + $request = $this->prophesize(Request::class); + $operation = new Index(name: 'app_book'); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Operation has no grid, so you cannot use this provider for operation "app_book"'); + + $this->provider->provide($operation, new Context(new RequestOption($request->reveal()))); + } + + public function testItThrowsAnExceptionWhenOperationDoesNotImplementTheGridAwareInterface(): void + { + $request = $this->prophesize(Request::class); + $operation = new Create(name: 'app_book'); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('You can not use a grid if your operation does not implement "Sylius\Resource\Metadata\GridAwareOperationInterface".'); + + $this->provider->provide($operation, new Context(new RequestOption($request->reveal()))); + } +}