diff --git a/src/Component/spec/Grid/State/RequestGridProviderSpec.php b/src/Component/spec/Grid/State/RequestGridProviderSpec.php deleted file mode 100644 index 41c70bf83..000000000 --- a/src/Component/spec/Grid/State/RequestGridProviderSpec.php +++ /dev/null @@ -1,209 +0,0 @@ -beConstructedWith($gridViewFactory, $gridProvider); - } - - function it_is_initializable(): void - { - $this->shouldHaveType(RequestGridProvider::class); - } - - function it_provides_a_grid_view( - Request $request, - GridViewFactoryInterface $gridViewFactory, - GridProviderInterface $gridProvider, - Grid $gridDefinition, - GridView $gridView, - ): void { - $context = new Context(new RequestOption($request->getWrappedObject())); - - $operation = new Index(grid: 'app_book'); - - $request->query = new InputBag(); - - $gridProvider->get('app_book')->willReturn($gridDefinition); - $gridDefinition->getDriverConfiguration()->willReturn([]); - - $gridViewFactory->create($gridDefinition, $context, new Parameters(), [])->willReturn($gridView); - - $this->provide($operation, $context) - ->shouldReturn($gridView) - ; - } - - function it_sets_current_page_from_request( - Request $request, - GridViewFactoryInterface $gridViewFactory, - GridProviderInterface $gridProvider, - Grid $gridDefinition, - GridView $gridView, - Pagerfanta $pagerfanta, - ): void { - $context = new Context(new RequestOption($request->getWrappedObject())); - - $operation = new Index(grid: 'app_book'); - - $request->query = new InputBag(['page' => 42]); - - $gridProvider->get('app_book')->willReturn($gridDefinition); - $gridDefinition->getLimits()->willReturn([]); - $gridDefinition->getDriverConfiguration()->willReturn([]); - - $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->provide($operation, $context) - ->shouldReturn($gridView) - ; - } - - function it_sets_max_per_page_from_request( - Request $request, - GridViewFactoryInterface $gridViewFactory, - GridProviderInterface $gridProvider, - Grid $gridDefinition, - GridView $gridView, - Pagerfanta $pagerfanta, - ): void { - $context = new Context(new RequestOption($request->getWrappedObject())); - - $operation = new Index(grid: 'app_book'); - - $request->query = new InputBag(['limit' => 25]); - - $gridProvider->get('app_book')->willReturn($gridDefinition); - $gridDefinition->getDriverConfiguration()->willReturn([]); - $gridDefinition->getLimits()->willReturn([10, 25]); - - $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->provide($operation, $context) - ->shouldReturn($gridView) - ; - } - - function it_sets_max_per_page_from_grid_configuration( - Request $request, - GridViewFactoryInterface $gridViewFactory, - GridProviderInterface $gridProvider, - Grid $gridDefinition, - GridView $gridView, - Pagerfanta $pagerfanta, - ): void { - $context = new Context(new RequestOption($request->getWrappedObject())); - - $operation = new Index(grid: 'app_book'); - - $request->query = new InputBag(); - - $gridProvider->get('app_book')->willReturn($gridDefinition); - $gridDefinition->getDriverConfiguration()->willReturn([]); - $gridDefinition->getLimits()->willReturn([15, 30]); - - $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->provide($operation, $context) - ->shouldReturn($gridView) - ; - } - - function it_limits_max_per_page_with_max_grid_configuration_limit( - Request $request, - GridViewFactoryInterface $gridViewFactory, - GridProviderInterface $gridProvider, - Grid $gridDefinition, - GridView $gridView, - Pagerfanta $pagerfanta, - ): void { - $context = new Context(new RequestOption($request->getWrappedObject())); - - $operation = new Index(grid: 'app_book'); - - $request->query = new InputBag(['limit' => 40]); - - $gridProvider->get('app_book')->willReturn($gridDefinition); - $gridDefinition->getDriverConfiguration()->willReturn([]); - $gridDefinition->getLimits()->willReturn([15, 30]); - - $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->provide($operation, $context) - ->shouldReturn($gridView) - ; - } - - function it_throws_an_exception_when_operation_has_no_grid( - Request $request, - ): void { - $operation = new Index(name: 'app_book'); - - $this->shouldThrow(new \RuntimeException('Operation has no grid, so you cannot use this provider for operation "app_book"')) - ->during('provide', [ - $operation, - new Context(new RequestOption($request->getWrappedObject())), - ]) - ; - } - - function it_throws_an_exception_when_operation_does_not_implement_the_grid_aware_interface( - Request $request, - ): void { - $operation = new Create(name: 'app_book'); - - $this->shouldThrow(new \LogicException('You can not use a grid if your operation does not implement "Sylius\Resource\Metadata\GridAwareOperationInterface".')) - ->during('provide', [ - $operation, - new Context(new RequestOption($request->getWrappedObject())), - ]) - ; - } -} 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()))); + } +}