|
| 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\Symfony\Tests\Bundle\DependencyInjection; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\Exception\ExceptionInterface; |
| 17 | +use ApiPlatform\Metadata\Exception\InvalidArgumentException; |
| 18 | +use ApiPlatform\Metadata\UrlGeneratorInterface; |
| 19 | +use ApiPlatform\Symfony\Bundle\DependencyInjection\ApiPlatformExtension; |
| 20 | +use ApiPlatform\Tests\Fixtures\TestBundle\TestBundle; |
| 21 | +use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; |
| 22 | +use Doctrine\ORM\OptimisticLockException; |
| 23 | +use PHPUnit\Framework\Attributes\Group; |
| 24 | +use PHPUnit\Framework\Attributes\IgnoreDeprecations; |
| 25 | +use PHPUnit\Framework\TestCase; |
| 26 | +use Symfony\Bundle\SecurityBundle\SecurityBundle; |
| 27 | +use Symfony\Bundle\TwigBundle\TwigBundle; |
| 28 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 29 | +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
| 30 | +use Symfony\Component\HttpFoundation\Response; |
| 31 | + |
| 32 | +final class JsonApiUseIriAsIdDeprecationTest extends TestCase |
| 33 | +{ |
| 34 | + private ContainerBuilder $container; |
| 35 | + |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + $containerParameterBag = new ParameterBag([ |
| 39 | + 'kernel.bundles' => [ |
| 40 | + 'DoctrineBundle' => DoctrineBundle::class, |
| 41 | + 'SecurityBundle' => SecurityBundle::class, |
| 42 | + 'TwigBundle' => TwigBundle::class, |
| 43 | + ], |
| 44 | + 'kernel.bundles_metadata' => [ |
| 45 | + 'TestBundle' => [ |
| 46 | + 'parent' => null, |
| 47 | + 'path' => realpath(__DIR__.'/../../../Fixtures/TestBundle'), |
| 48 | + 'namespace' => TestBundle::class, |
| 49 | + ], |
| 50 | + ], |
| 51 | + 'kernel.project_dir' => __DIR__.'/../../../Fixtures/app', |
| 52 | + 'kernel.debug' => false, |
| 53 | + 'kernel.environment' => 'test', |
| 54 | + ]); |
| 55 | + |
| 56 | + $this->container = new ContainerBuilder($containerParameterBag); |
| 57 | + } |
| 58 | + |
| 59 | + #[Group('legacy')] |
| 60 | + #[IgnoreDeprecations] |
| 61 | + public function testNotSettingUseIriAsIdIsDeprecatedAndResolvesToTrue(): void |
| 62 | + { |
| 63 | + $this->expectUserDeprecationMessage('Since api-platform/core 4.4: Not setting "api_platform.jsonapi.use_iri_as_id" explicitly is deprecated. Its default value will change from "true" to "false" in API Platform 5.0. Set it to "true" to keep the current behavior or to "false" to use entity identifiers as the "id" field, and silence this deprecation.'); |
| 64 | + |
| 65 | + (new ApiPlatformExtension())->load($this->buildConfig(), $this->container); |
| 66 | + |
| 67 | + $this->assertTrue($this->container->getDefinition('api_platform.jsonapi.normalizer.item')->getArgument(13)); |
| 68 | + $this->assertTrue($this->container->getDefinition('api_platform.jsonapi.denormalizer.item')->getArgument(12)); |
| 69 | + } |
| 70 | + |
| 71 | + public function testSettingUseIriAsIdToFalseDoesNotDeprecateAndResolvesToFalse(): void |
| 72 | + { |
| 73 | + (new ApiPlatformExtension())->load($this->buildConfig(['use_iri_as_id' => false]), $this->container); |
| 74 | + |
| 75 | + $this->assertFalse($this->container->getDefinition('api_platform.jsonapi.normalizer.item')->getArgument(13)); |
| 76 | + $this->assertFalse($this->container->getDefinition('api_platform.jsonapi.denormalizer.item')->getArgument(12)); |
| 77 | + } |
| 78 | + |
| 79 | + public function testSettingUseIriAsIdToTrueDoesNotDeprecateAndResolvesToTrue(): void |
| 80 | + { |
| 81 | + (new ApiPlatformExtension())->load($this->buildConfig(['use_iri_as_id' => true]), $this->container); |
| 82 | + |
| 83 | + $this->assertTrue($this->container->getDefinition('api_platform.jsonapi.normalizer.item')->getArgument(13)); |
| 84 | + $this->assertTrue($this->container->getDefinition('api_platform.jsonapi.denormalizer.item')->getArgument(12)); |
| 85 | + } |
| 86 | + |
| 87 | + private function buildConfig(?array $jsonapi = null): array |
| 88 | + { |
| 89 | + $config = ['api_platform' => [ |
| 90 | + 'title' => 'title', |
| 91 | + 'description' => 'description', |
| 92 | + 'version' => 'version', |
| 93 | + 'enable_json_streamer' => false, |
| 94 | + 'serializer' => ['hydra_prefix' => true], |
| 95 | + 'formats' => [ |
| 96 | + 'json' => ['mime_types' => ['json']], |
| 97 | + 'jsonld' => ['mime_types' => ['application/ld+json']], |
| 98 | + 'jsonapi' => ['mime_types' => ['application/vnd.api+json']], |
| 99 | + ], |
| 100 | + 'doctrine_mongodb_odm' => [ |
| 101 | + 'enabled' => true, |
| 102 | + ], |
| 103 | + 'defaults' => [ |
| 104 | + 'extra_properties' => [], |
| 105 | + 'url_generation_strategy' => UrlGeneratorInterface::ABS_URL, |
| 106 | + ], |
| 107 | + 'error_formats' => [ |
| 108 | + 'jsonproblem' => ['application/problem+json'], |
| 109 | + 'jsonld' => ['application/ld+json'], |
| 110 | + ], |
| 111 | + 'patch_formats' => [], |
| 112 | + 'exception_to_status' => [ |
| 113 | + ExceptionInterface::class => Response::HTTP_BAD_REQUEST, |
| 114 | + InvalidArgumentException::class => Response::HTTP_BAD_REQUEST, |
| 115 | + OptimisticLockException::class => Response::HTTP_CONFLICT, |
| 116 | + ], |
| 117 | + 'show_webby' => true, |
| 118 | + 'eager_loading' => [ |
| 119 | + 'enabled' => true, |
| 120 | + 'max_joins' => 30, |
| 121 | + 'force_eager' => true, |
| 122 | + 'fetch_partial' => false, |
| 123 | + ], |
| 124 | + 'asset_package' => null, |
| 125 | + 'enable_entrypoint' => true, |
| 126 | + 'enable_docs' => true, |
| 127 | + 'enable_swagger' => true, |
| 128 | + 'enable_swagger_ui' => true, |
| 129 | + 'use_symfony_listeners' => false, |
| 130 | + ]]; |
| 131 | + |
| 132 | + if (null !== $jsonapi) { |
| 133 | + $config['api_platform']['jsonapi'] = $jsonapi; |
| 134 | + } |
| 135 | + |
| 136 | + return $config; |
| 137 | + } |
| 138 | +} |
0 commit comments