|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 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 | +namespace ApiPlatform\Core\Tests\Doctrine\Orm\Filter; |
| 13 | + |
| 14 | +use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter; |
| 15 | +use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator; |
| 16 | +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; |
| 17 | +use Doctrine\Common\Persistence\ManagerRegistry; |
| 18 | +use Doctrine\ORM\EntityRepository; |
| 19 | +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; |
| 20 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 21 | +use Symfony\Component\HttpFoundation\Request; |
| 22 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Antoine Bluchet <soyuka@gmail.com> |
| 26 | + */ |
| 27 | +class ExistsFilterTest extends KernelTestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var ManagerRegistry |
| 31 | + */ |
| 32 | + private $managerRegistry; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var EntityRepository |
| 36 | + */ |
| 37 | + private $repository; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var string |
| 41 | + */ |
| 42 | + protected $resourceClass; |
| 43 | + |
| 44 | + /** |
| 45 | + * {@inheritdoc} |
| 46 | + */ |
| 47 | + protected function setUp() |
| 48 | + { |
| 49 | + self::bootKernel(); |
| 50 | + $manager = DoctrineTestHelper::createTestEntityManager(); |
| 51 | + $this->managerRegistry = self::$kernel->getContainer()->get('doctrine'); |
| 52 | + $this->repository = $manager->getRepository(Dummy::class); |
| 53 | + $this->resourceClass = Dummy::class; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @dataProvider provideApplyTestData |
| 58 | + */ |
| 59 | + public function testApply($properties, array $filterParameters, string $expected) |
| 60 | + { |
| 61 | + $request = Request::create('/api/dummies', 'GET', $filterParameters); |
| 62 | + |
| 63 | + $requestStack = new RequestStack(); |
| 64 | + $requestStack->push($request); |
| 65 | + |
| 66 | + $queryBuilder = $this->repository->createQueryBuilder('o'); |
| 67 | + |
| 68 | + $filter = new ExistsFilter( |
| 69 | + $this->managerRegistry, |
| 70 | + $requestStack, |
| 71 | + null, |
| 72 | + $properties |
| 73 | + ); |
| 74 | + |
| 75 | + $filter->apply($queryBuilder, new QueryNameGenerator(), $this->resourceClass); |
| 76 | + $actual = $queryBuilder->getQuery()->getDQL(); |
| 77 | + |
| 78 | + $this->assertEquals($expected, $actual); |
| 79 | + } |
| 80 | + |
| 81 | + public function testGetDescription() |
| 82 | + { |
| 83 | + $filter = new ExistsFilter( |
| 84 | + $this->managerRegistry, |
| 85 | + new RequestStack(), |
| 86 | + null, |
| 87 | + [ |
| 88 | + 'name' => null, |
| 89 | + 'description' => null, |
| 90 | + ] |
| 91 | + ); |
| 92 | + |
| 93 | + $this->assertEquals([ |
| 94 | + 'exists[description]' => [ |
| 95 | + 'property' => 'description', |
| 96 | + 'type' => 'bool', |
| 97 | + 'required' => false, |
| 98 | + ], |
| 99 | + ], $filter->getDescription($this->resourceClass)); |
| 100 | + } |
| 101 | + |
| 102 | + public function testGetDescriptionDefaultFields() |
| 103 | + { |
| 104 | + $filter = new ExistsFilter( |
| 105 | + $this->managerRegistry, |
| 106 | + new RequestStack() |
| 107 | + ); |
| 108 | + |
| 109 | + $this->assertEquals([ |
| 110 | + 'exists[alias]' => [ |
| 111 | + 'property' => 'alias', |
| 112 | + 'type' => 'bool', |
| 113 | + 'required' => false, |
| 114 | + ], |
| 115 | + 'exists[description]' => [ |
| 116 | + 'property' => 'description', |
| 117 | + 'type' => 'bool', |
| 118 | + 'required' => false, |
| 119 | + ], |
| 120 | + 'exists[dummy]' => [ |
| 121 | + 'property' => 'dummy', |
| 122 | + 'type' => 'bool', |
| 123 | + 'required' => false, |
| 124 | + ], |
| 125 | + 'exists[dummyDate]' => [ |
| 126 | + 'property' => 'dummyDate', |
| 127 | + 'type' => 'bool', |
| 128 | + 'required' => false, |
| 129 | + ], |
| 130 | + 'exists[dummyFloat]' => [ |
| 131 | + 'property' => 'dummyFloat', |
| 132 | + 'type' => 'bool', |
| 133 | + 'required' => false, |
| 134 | + ], |
| 135 | + 'exists[dummyPrice]' => [ |
| 136 | + 'property' => 'dummyPrice', |
| 137 | + 'type' => 'bool', |
| 138 | + 'required' => false, |
| 139 | + ], |
| 140 | + 'exists[jsonData]' => [ |
| 141 | + 'property' => 'jsonData', |
| 142 | + 'type' => 'bool', |
| 143 | + 'required' => false, |
| 144 | + ], |
| 145 | + 'exists[nameConverted]' => [ |
| 146 | + 'property' => 'nameConverted', |
| 147 | + 'type' => 'bool', |
| 148 | + 'required' => false, |
| 149 | + ], |
| 150 | + 'exists[dummyBoolean]' => [ |
| 151 | + 'property' => 'dummyBoolean', |
| 152 | + 'type' => 'bool', |
| 153 | + 'required' => false, |
| 154 | + ], |
| 155 | + ], $filter->getDescription($this->resourceClass)); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Provides test data. |
| 160 | + * |
| 161 | + * Provides 4 parameters: |
| 162 | + * - order parameter name |
| 163 | + * - configuration of filterable properties |
| 164 | + * - filter parameters |
| 165 | + * - expected DQL query |
| 166 | + * |
| 167 | + * @return array |
| 168 | + */ |
| 169 | + public function provideApplyTestData(): array |
| 170 | + { |
| 171 | + return [ |
| 172 | + 'valid values' => [ |
| 173 | + [ |
| 174 | + 'description' => null, |
| 175 | + //'relatedDummy.name' => null, |
| 176 | + ], |
| 177 | + [ |
| 178 | + 'exists' => [ |
| 179 | + 'description' => 'true', |
| 180 | + ], |
| 181 | + ], |
| 182 | + sprintf('SELECT o FROM %s o WHERE o.description IS NOT NULL', Dummy::class), |
| 183 | + ], |
| 184 | + |
| 185 | + 'valid values (empty for true)' => [ |
| 186 | + [ |
| 187 | + 'description' => null, |
| 188 | + ], |
| 189 | + [ |
| 190 | + 'exists' => [ |
| 191 | + 'description' => '', |
| 192 | + ], |
| 193 | + ], |
| 194 | + sprintf('SELECT o FROM %s o WHERE o.description IS NOT NULL', Dummy::class), |
| 195 | + ], |
| 196 | + |
| 197 | + 'valid values (1 for true)' => [ |
| 198 | + [ |
| 199 | + 'description' => null, |
| 200 | + ], |
| 201 | + [ |
| 202 | + 'exists' => [ |
| 203 | + 'description' => '1', |
| 204 | + ], |
| 205 | + ], |
| 206 | + sprintf('SELECT o FROM %s o WHERE o.description IS NOT NULL', Dummy::class), |
| 207 | + ], |
| 208 | + |
| 209 | + 'invalid values' => [ |
| 210 | + [ |
| 211 | + 'description' => null, |
| 212 | + ], |
| 213 | + [ |
| 214 | + 'exists' => [ |
| 215 | + 'description' => 'invalid', |
| 216 | + ], |
| 217 | + ], |
| 218 | + sprintf('SELECT o FROM %s o', Dummy::class), |
| 219 | + ], |
| 220 | + |
| 221 | + 'negative values' => [ |
| 222 | + [ |
| 223 | + 'description' => null, |
| 224 | + ], |
| 225 | + [ |
| 226 | + 'exists' => [ |
| 227 | + 'description' => 'false', |
| 228 | + ], |
| 229 | + ], |
| 230 | + sprintf('SELECT o FROM %s o WHERE o.description IS NULL', Dummy::class), |
| 231 | + ], |
| 232 | + |
| 233 | + 'negative values (0)' => [ |
| 234 | + [ |
| 235 | + 'description' => null, |
| 236 | + ], |
| 237 | + [ |
| 238 | + 'exists' => [ |
| 239 | + 'description' => '0', |
| 240 | + ], |
| 241 | + ], |
| 242 | + sprintf('SELECT o FROM %s o WHERE o.description IS NULL', Dummy::class), |
| 243 | + ], |
| 244 | + |
| 245 | + 'related values' => [ |
| 246 | + [ |
| 247 | + 'description' => null, |
| 248 | + 'relatedDummy.name' => null, |
| 249 | + ], |
| 250 | + [ |
| 251 | + 'exists' => [ |
| 252 | + 'description' => '1', |
| 253 | + 'relatedDummy.name' => '1', |
| 254 | + ], |
| 255 | + ], |
| 256 | + sprintf('SELECT o FROM %s o INNER JOIN o.relatedDummy relatedDummy_a1 WHERE o.description IS NOT NULL AND relatedDummy_a1.name IS NOT NULL', Dummy::class), |
| 257 | + ], |
| 258 | + |
| 259 | + 'not nullable values' => [ |
| 260 | + [ |
| 261 | + 'description' => null, |
| 262 | + 'name' => null, |
| 263 | + ], |
| 264 | + [ |
| 265 | + 'exists' => [ |
| 266 | + 'description' => '1', |
| 267 | + 'name' => '0', |
| 268 | + ], |
| 269 | + ], |
| 270 | + sprintf('SELECT o FROM %s o WHERE o.description IS NOT NULL', Dummy::class), |
| 271 | + ], |
| 272 | + ]; |
| 273 | + } |
| 274 | +} |
0 commit comments