|
| 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 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Functional\Parameters; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +//use ApiPlatform\Tests\Fixtures\TestBundle\Document\DummyAuthorExact as DummyAuthorExactDocument; |
| 18 | +//use ApiPlatform\Tests\Fixtures\TestBundle\Document\DummyBookExact as DummyBookExactDocument; |
| 19 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyAuthorExact; |
| 20 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyBookExact; |
| 21 | +use ApiPlatform\Tests\RecreateSchemaTrait; |
| 22 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 23 | +use Doctrine\ODM\MongoDB\MongoDBException; |
| 24 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 25 | +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; |
| 26 | +use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; |
| 27 | +use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; |
| 28 | +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; |
| 29 | +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 30 | + |
| 31 | +final class ExactFilterTest extends ApiTestCase |
| 32 | +{ |
| 33 | + use RecreateSchemaTrait; |
| 34 | + use SetupClassResourcesTrait; |
| 35 | + |
| 36 | + /** |
| 37 | + * @return class-string[] |
| 38 | + */ |
| 39 | + public static function getResources(): array |
| 40 | + { |
| 41 | + return [DummyBookExact::class, DummyAuthorExact::class]; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @throws MongoDBException |
| 46 | + * @throws \Throwable |
| 47 | + */ |
| 48 | + protected function setUp(): void |
| 49 | + { |
| 50 | + // TODO: implement ODM classes |
| 51 | + $authorEntityClass = $this->isMongoDB() ? /*DummyAuthorExactDocument::class*/ : DummyAuthorExact::class; |
| 52 | + $bookEntityClass = $this->isMongoDB() ? /*DummyBookExactDocument::class*/ : DummyBookExact::class; |
| 53 | + |
| 54 | + $this->recreateSchema([$authorEntityClass, $bookEntityClass]); |
| 55 | + $this->loadFixtures($authorEntityClass, $bookEntityClass); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @throws ServerExceptionInterface |
| 60 | + * @throws RedirectionExceptionInterface |
| 61 | + * @throws DecodingExceptionInterface |
| 62 | + * @throws ClientExceptionInterface |
| 63 | + * @throws TransportExceptionInterface |
| 64 | + */ |
| 65 | + #[DataProvider('exactSearchFilterProvider')] |
| 66 | + public function testExactSearchFilter(string $url, int $expectedCount, array $expectedTitles): void |
| 67 | + { |
| 68 | + $response = self::createClient()->request('GET', $url); |
| 69 | + $this->assertResponseIsSuccessful(); |
| 70 | + |
| 71 | + $responseData = $response->toArray(); |
| 72 | + $filteredItems = $responseData['hydra:member']; |
| 73 | + |
| 74 | + $this->assertCount($expectedCount, $filteredItems, \sprintf('Expected %d items for URL %s', $expectedCount, $url)); |
| 75 | + |
| 76 | + $titles = array_map(fn ($book) => $book['title'], $filteredItems); |
| 77 | + sort($titles); |
| 78 | + sort($expectedTitles); |
| 79 | + |
| 80 | + $this->assertSame($expectedTitles, $titles, 'The titles do not match the expected values.'); |
| 81 | + } |
| 82 | + |
| 83 | + public static function exactSearchFilterProvider(): \Generator |
| 84 | + { |
| 85 | + yield 'filter_by_author_exact_id_1' => [ |
| 86 | + '/dummy_book_exacts?dummyAuthorExact=1', |
| 87 | + 2, |
| 88 | + ['Book 1', 'Book 2'], |
| 89 | + ]; |
| 90 | + yield 'filter_by_author_exact_id_1_and_title_book_1' => [ |
| 91 | + '/dummy_book_exacts?dummyAuthorExact=1&title=Book 1', |
| 92 | + 1, |
| 93 | + ['Book 1'], |
| 94 | + ]; |
| 95 | + yield 'filter_by_author_exact_id_1_and_title_book_3' => [ |
| 96 | + '/dummy_book_exacts?dummyAuthorExact=1&title=Book 3', |
| 97 | + 0, |
| 98 | + [], |
| 99 | + ]; |
| 100 | + yield 'filter_by_author_exact_id_3_and_title_book_3' => [ |
| 101 | + '/dummy_book_exacts?dummyAuthorExact=2&title=Book 3', |
| 102 | + 1, |
| 103 | + ['Book 3'], |
| 104 | + ]; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @throws \Throwable |
| 109 | + * @throws MongoDBException |
| 110 | + */ |
| 111 | + private function loadFixtures(string $authorEntityClass, string $bookEntityClass): void |
| 112 | + { |
| 113 | + $manager = $this->getManager(); |
| 114 | + |
| 115 | + $authors = []; |
| 116 | + foreach ([['name' => 'Author 1'], ['name' => 'Author 2']] as $authorData) { |
| 117 | + $author = new $authorEntityClass(name: $authorData['name']); |
| 118 | + $manager->persist($author); |
| 119 | + $authors[] = $author; |
| 120 | + } |
| 121 | + |
| 122 | + $books = [ |
| 123 | + ['title' => 'Book 1', 'isbn' => '1234567890123', 'author' => $authors[0]], |
| 124 | + ['title' => 'Book 2', 'isbn' => '1234567890124', 'author' => $authors[0]], |
| 125 | + ['title' => 'Book 3', 'isbn' => '1234567890125', 'author' => $authors[1]], |
| 126 | + ]; |
| 127 | + |
| 128 | + foreach ($books as $bookData) { |
| 129 | + $book = new $bookEntityClass( |
| 130 | + title: $bookData['title'], |
| 131 | + isbn: $bookData['isbn'], |
| 132 | + dummyAuthorExact: $bookData['author'] |
| 133 | + ); |
| 134 | + |
| 135 | + $author->dummyBookExacts->add($book); |
| 136 | + $manager->persist($book); |
| 137 | + } |
| 138 | + |
| 139 | + $manager->flush(); |
| 140 | + } |
| 141 | +} |
0 commit comments