|
15 | 15 |
|
16 | 16 | use ApiPlatform\Metadata\Get;
|
17 | 17 | use ApiPlatform\Metadata\GetCollection;
|
| 18 | +use ApiPlatform\Metadata\Link; |
18 | 19 | use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
|
19 | 20 | use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\BackedEnumIntegerResource;
|
20 | 21 | use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\BackedEnumStringResource;
|
@@ -43,4 +44,65 @@ public function testOnlyGetOperationsAddedWhenNonSpecified(string $resourceClass
|
43 | 44 |
|
44 | 45 | $this->assertInstanceOf($operationClass, $operations[$operationName]);
|
45 | 46 | }
|
| 47 | + |
| 48 | + public function testEnumsAreAssignedValuePropertyAsIdentifierByDefault(): void |
| 49 | + { |
| 50 | + $linkFactory = self::getContainer()->get('api_platform.metadata.resource.link_factory'); |
| 51 | + $result = $linkFactory->completeLink(new Link(fromClass: BackedEnumIntegerResource::class)); |
| 52 | + $identifiers = $result->getIdentifiers(); |
| 53 | + |
| 54 | + $this->assertCount(1, $identifiers); |
| 55 | + $this->assertNotContains('id', $identifiers); |
| 56 | + $this->assertContains('value', $identifiers); |
| 57 | + } |
| 58 | + |
| 59 | + public function testCollection(): void |
| 60 | + { |
| 61 | + self::createClient()->request('GET', '/backed_enum_integer_resources', ['headers' => ['Accept' => 'application/json']]); |
| 62 | + |
| 63 | + $this->assertResponseIsSuccessful(); |
| 64 | + $this->assertJsonEquals([ |
| 65 | + [ |
| 66 | + 'name' => 'Yes', |
| 67 | + 'value' => 1, |
| 68 | + 'description' => 'We say yes', |
| 69 | + ], |
| 70 | + [ |
| 71 | + 'name' => 'No', |
| 72 | + 'value' => 2, |
| 73 | + 'description' => 'Computer says no', |
| 74 | + ], |
| 75 | + [ |
| 76 | + 'name' => 'Maybe', |
| 77 | + 'value' => 3, |
| 78 | + 'description' => 'Let me think about it', |
| 79 | + ], |
| 80 | + ]); |
| 81 | + } |
| 82 | + |
| 83 | + public function testItem(): void |
| 84 | + { |
| 85 | + self::createClient()->request('GET', '/backed_enum_integer_resources/1', ['headers' => ['Accept' => 'application/json']]); |
| 86 | + |
| 87 | + $this->assertResponseIsSuccessful(); |
| 88 | + $this->assertJsonEquals([ |
| 89 | + 'name' => 'Yes', |
| 90 | + 'value' => 1, |
| 91 | + 'description' => 'We say yes', |
| 92 | + ]); |
| 93 | + } |
| 94 | + |
| 95 | + public static function provider404s(): iterable |
| 96 | + { |
| 97 | + yield ['/backed_enum_integer_resources/42']; |
| 98 | + yield ['/backed_enum_integer_resources/fortytwo']; |
| 99 | + } |
| 100 | + |
| 101 | + /** @dataProvider provider404s */ |
| 102 | + public function testItem404(string $uri): void |
| 103 | + { |
| 104 | + self::createClient()->request('GET', $uri); |
| 105 | + |
| 106 | + $this->assertResponseStatusCodeSame(404); |
| 107 | + } |
46 | 108 | }
|
0 commit comments