|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Core\Tests\Filter; |
| 15 | + |
| 16 | +use ApiPlatform\Core\Api\FilterInterface; |
| 17 | +use ApiPlatform\Core\Exception\FilterValidationException; |
| 18 | +use ApiPlatform\Core\Filter\QueryParameterValidateListener; |
| 19 | +use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
| 20 | +use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
| 21 | +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | +use Psr\Container\ContainerInterface; |
| 24 | +use Symfony\Component\HttpFoundation\Request; |
| 25 | +use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
| 26 | + |
| 27 | +class QueryParameterValidateListenerTest extends TestCase |
| 28 | +{ |
| 29 | + private $testedInstance; |
| 30 | + |
| 31 | + private $filterLocatorProphecy; |
| 32 | + |
| 33 | + /** |
| 34 | + * unsafe method should not use filter validations. |
| 35 | + */ |
| 36 | + public function testOnKernelRequestWithUnsafeMethod() |
| 37 | + { |
| 38 | + $this->setUpWithFilters(); |
| 39 | + |
| 40 | + $request = new Request(); |
| 41 | + $request->setMethod('POST'); |
| 42 | + |
| 43 | + $eventProphecy = $this->prophesize(GetResponseEvent::class); |
| 44 | + $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); |
| 45 | + |
| 46 | + $this->assertNull( |
| 47 | + $this->testedInstance->onKernelRequest($eventProphecy->reveal()) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * If the tested filter is non-existant, then nothing should append. |
| 53 | + */ |
| 54 | + public function testOnKernelRequestWithWrongFilter() |
| 55 | + { |
| 56 | + $this->setUpWithFilters(['some_inexistent_filter']); |
| 57 | + |
| 58 | + $request = new Request([], [], ['_api_resource_class' => Dummy::class, '_api_collection_operation_name' => 'get']); |
| 59 | + $request->setMethod('GET'); |
| 60 | + |
| 61 | + $eventProphecy = $this->prophesize(GetResponseEvent::class); |
| 62 | + $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); |
| 63 | + |
| 64 | + $this->filterLocatorProphecy->has('some_inexistent_filter')->shouldBeCalled(); |
| 65 | + $this->filterLocatorProphecy->get('some_inexistent_filter')->shouldNotBeCalled(); |
| 66 | + |
| 67 | + $this->assertNull( |
| 68 | + $this->testedInstance->onKernelRequest($eventProphecy->reveal()) |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * if the required parameter is not set, throw an FilterValidationException. |
| 74 | + */ |
| 75 | + public function testOnKernelRequestWithRequiredFilterNotSet() |
| 76 | + { |
| 77 | + $this->setUpWithFilters(['some_filter']); |
| 78 | + |
| 79 | + $request = new Request([], [], ['_api_resource_class' => Dummy::class, '_api_collection_operation_name' => 'get']); |
| 80 | + $request->setMethod('GET'); |
| 81 | + |
| 82 | + $eventProphecy = $this->prophesize(GetResponseEvent::class); |
| 83 | + $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); |
| 84 | + |
| 85 | + $this->filterLocatorProphecy |
| 86 | + ->has('some_filter') |
| 87 | + ->shouldBeCalled() |
| 88 | + ->willReturn(true) |
| 89 | + ; |
| 90 | + $filterProphecy = $this->prophesize(FilterInterface::class); |
| 91 | + $filterProphecy |
| 92 | + ->getDescription(Dummy::class) |
| 93 | + ->shouldBeCalled() |
| 94 | + ->willReturn([ |
| 95 | + 'required' => [ |
| 96 | + 'required' => true, |
| 97 | + ], |
| 98 | + ]) |
| 99 | + ; |
| 100 | + $this->filterLocatorProphecy |
| 101 | + ->get('some_filter') |
| 102 | + ->shouldBeCalled() |
| 103 | + ->willReturn($filterProphecy->reveal()) |
| 104 | + ; |
| 105 | + |
| 106 | + $this->expectException(FilterValidationException::class); |
| 107 | + $this->expectExceptionMessage('Query parameter "required" is required'); |
| 108 | + $this->testedInstance->onKernelRequest($eventProphecy->reveal()); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * if the required parameter is set, no exception should be throwned. |
| 113 | + */ |
| 114 | + public function testOnKernelRequestWithRequiredFilter() |
| 115 | + { |
| 116 | + $this->setUpWithFilters(['some_filter']); |
| 117 | + |
| 118 | + $request = new Request( |
| 119 | + ['required' => 'foo'], |
| 120 | + [], |
| 121 | + ['_api_resource_class' => Dummy::class, '_api_collection_operation_name' => 'get'] |
| 122 | + ); |
| 123 | + $request->setMethod('GET'); |
| 124 | + |
| 125 | + $eventProphecy = $this->prophesize(GetResponseEvent::class); |
| 126 | + $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); |
| 127 | + |
| 128 | + $this->filterLocatorProphecy |
| 129 | + ->has('some_filter') |
| 130 | + ->shouldBeCalled() |
| 131 | + ->willReturn(true) |
| 132 | + ; |
| 133 | + $filterProphecy = $this->prophesize(FilterInterface::class); |
| 134 | + $filterProphecy |
| 135 | + ->getDescription(Dummy::class) |
| 136 | + ->shouldBeCalled() |
| 137 | + ->willReturn([ |
| 138 | + 'required' => [ |
| 139 | + 'required' => true, |
| 140 | + ], |
| 141 | + ]) |
| 142 | + ; |
| 143 | + $this->filterLocatorProphecy |
| 144 | + ->get('some_filter') |
| 145 | + ->shouldBeCalled() |
| 146 | + ->willReturn($filterProphecy->reveal()) |
| 147 | + ; |
| 148 | + |
| 149 | + $this->assertNull( |
| 150 | + $this->testedInstance->onKernelRequest($eventProphecy->reveal()) |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + private function setUpWithFilters(array $filters = []) |
| 155 | + { |
| 156 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 157 | + $resourceMetadataFactoryProphecy |
| 158 | + ->create(Dummy::class) |
| 159 | + ->willReturn( |
| 160 | + (new ResourceMetadata('dummy')) |
| 161 | + ->withAttributes([ |
| 162 | + 'filters' => $filters, |
| 163 | + ]) |
| 164 | + ) |
| 165 | + ; |
| 166 | + |
| 167 | + $this->filterLocatorProphecy = $this->prophesize(ContainerInterface::class); |
| 168 | + |
| 169 | + $this->testedInstance = new QueryParameterValidateListener( |
| 170 | + $resourceMetadataFactoryProphecy->reveal(), |
| 171 | + $this->filterLocatorProphecy->reveal() |
| 172 | + ); |
| 173 | + } |
| 174 | +} |
0 commit comments