|
29 | 29 | use ApiPlatform\Core\Tests\Fixtures\DummyFilter;
|
30 | 30 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
|
31 | 31 | use Symfony\Component\PropertyInfo\Type;
|
| 32 | +use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
32 | 33 |
|
33 | 34 | /**
|
34 | 35 | * @author Amrouche Hamza <[email protected]>
|
@@ -235,6 +236,103 @@ public function testNormalize()
|
235 | 236 | $this->assertEquals($expected, $normalizer->normalize($documentation));
|
236 | 237 | }
|
237 | 238 |
|
| 239 | + public function testNormalizeWithNameConverter() |
| 240 | + { |
| 241 | + $documentation = new Documentation(new ResourceNameCollection([Dummy::class]), 'Dummy API', 'This is a dummy API', '1.2.3', ['jsonld' => ['application/ld+json']]); |
| 242 | + |
| 243 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 244 | + $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['name', 'nameConverted'])); |
| 245 | + |
| 246 | + $dummyMetadata = new ResourceMetadata('Dummy', 'This is a dummy.', null, ['get' => ['method' => 'GET']], [], []); |
| 247 | + |
| 248 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 249 | + $resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn($dummyMetadata); |
| 250 | + |
| 251 | + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 252 | + $propertyMetadataFactoryProphecy->create(Dummy::class, 'name')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'This is a name.', true, true, null, null, false)); |
| 253 | + $propertyMetadataFactoryProphecy->create(Dummy::class, 'nameConverted')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'This is a converted name.', true, true, null, null, false)); |
| 254 | + |
| 255 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 256 | + $resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true); |
| 257 | + |
| 258 | + $operationMethodResolverProphecy = $this->prophesize(OperationMethodResolverInterface::class); |
| 259 | + $operationMethodResolverProphecy->getItemOperationMethod(Dummy::class, 'get')->shouldBeCalled()->willReturn('GET'); |
| 260 | + |
| 261 | + $urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class); |
| 262 | + $urlGeneratorProphecy->generate('api_entrypoint')->willReturn('/app_dev.php/')->shouldBeCalled(); |
| 263 | + |
| 264 | + $nameConverterProphecy = $this->prophesize(NameConverterInterface::class); |
| 265 | + $nameConverterProphecy->normalize('name')->willReturn('name')->shouldBeCalled(); |
| 266 | + $nameConverterProphecy->normalize('nameConverted')->willReturn('name_converted')->shouldBeCalled(); |
| 267 | + |
| 268 | + $operationPathResolver = new CustomOperationPathResolver(new UnderscoreOperationPathResolver()); |
| 269 | + |
| 270 | + $normalizer = new DocumentationNormalizer( |
| 271 | + $resourceMetadataFactoryProphecy->reveal(), |
| 272 | + $propertyNameCollectionFactoryProphecy->reveal(), |
| 273 | + $propertyMetadataFactoryProphecy->reveal(), |
| 274 | + $resourceClassResolverProphecy->reveal(), |
| 275 | + $operationMethodResolverProphecy->reveal(), |
| 276 | + $operationPathResolver, |
| 277 | + $urlGeneratorProphecy->reveal(), |
| 278 | + null, |
| 279 | + $nameConverterProphecy->reveal() |
| 280 | + ); |
| 281 | + |
| 282 | + $expected = [ |
| 283 | + 'swagger' => '2.0', |
| 284 | + 'basePath' => '/app_dev.php/', |
| 285 | + 'info' => [ |
| 286 | + 'title' => 'Dummy API', |
| 287 | + 'description' => 'This is a dummy API', |
| 288 | + 'version' => '1.2.3', |
| 289 | + ], |
| 290 | + 'paths' => new \ArrayObject([ |
| 291 | + '/dummies/{id}' => [ |
| 292 | + 'get' => new \ArrayObject([ |
| 293 | + 'tags' => ['Dummy'], |
| 294 | + 'operationId' => 'getDummyItem', |
| 295 | + 'produces' => ['application/ld+json'], |
| 296 | + 'summary' => 'Retrieves a Dummy resource.', |
| 297 | + 'parameters' => [ |
| 298 | + [ |
| 299 | + 'name' => 'id', |
| 300 | + 'in' => 'path', |
| 301 | + 'type' => 'integer', |
| 302 | + 'required' => true, |
| 303 | + ], |
| 304 | + ], |
| 305 | + 'responses' => [ |
| 306 | + 200 => [ |
| 307 | + 'description' => 'Dummy resource response', |
| 308 | + 'schema' => ['$ref' => '#/definitions/Dummy'], |
| 309 | + ], |
| 310 | + 404 => ['description' => 'Resource not found'], |
| 311 | + ], |
| 312 | + ]), |
| 313 | + ], |
| 314 | + ]), |
| 315 | + 'definitions' => new \ArrayObject([ |
| 316 | + 'Dummy' => new \ArrayObject([ |
| 317 | + 'type' => 'object', |
| 318 | + 'description' => 'This is a dummy.', |
| 319 | + 'properties' => [ |
| 320 | + 'name' => new \ArrayObject([ |
| 321 | + 'type' => 'string', |
| 322 | + 'description' => 'This is a name.', |
| 323 | + ]), |
| 324 | + 'name_converted' => new \ArrayObject([ |
| 325 | + 'type' => 'string', |
| 326 | + 'description' => 'This is a converted name.', |
| 327 | + ]), |
| 328 | + ], |
| 329 | + ]), |
| 330 | + ]), |
| 331 | + ]; |
| 332 | + |
| 333 | + $this->assertEquals($expected, $normalizer->normalize($documentation)); |
| 334 | + } |
| 335 | + |
238 | 336 | public function testNormalizeWithOnlyNormalizationGroups()
|
239 | 337 | {
|
240 | 338 | $title = 'Test API';
|
|
0 commit comments