Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Jul 19, 2024
1 parent ed16132 commit 1f7fd42
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 44 deletions.
9 changes: 5 additions & 4 deletions src/GraphQl/Subscription/SubscriptionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\GraphQl\Subscription;

use ApiPlatform\GraphQl\Resolver\Stage\SerializeStage;
use ApiPlatform\GraphQl\Resolver\Stage\SerializeStageInterface;
use ApiPlatform\GraphQl\Resolver\Util\IdentifierTrait;
use ApiPlatform\Metadata\GraphQl\Operation;
Expand All @@ -37,7 +38,7 @@ final class SubscriptionManager implements OperationAwareSubscriptionManagerInte
use ResourceClassInfoTrait;
use SortTrait;

public function __construct(private readonly CacheItemPoolInterface $subscriptionsCache, private readonly SubscriptionIdentifierGeneratorInterface $subscriptionIdentifierGenerator, private readonly SerializeStageInterface|ProcessorInterface|null $serializeStage = null, private readonly IriConverterInterface $iriConverter, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ?ProcessorInterface $normalizeProcessor = null)
public function __construct(private readonly CacheItemPoolInterface $subscriptionsCache, private readonly SubscriptionIdentifierGeneratorInterface $subscriptionIdentifierGenerator, private readonly SerializeStageInterface|ProcessorInterface|null $serializeStage = null, private readonly ?IriConverterInterface $iriConverter = null, private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null)
{
if (!$serializeStage instanceof ProcessorInterface) {
trigger_deprecation('api-platform/core', '4.0', sprintf('Using an instanceof "%s" is deprecated, use "%s" instead.', SerializeStageInterface::class, ProcessorInterface::class));
Expand Down Expand Up @@ -88,9 +89,9 @@ public function getPushPayloads(object $object): array
$resolverContext = ['fields' => $subscriptionFields, 'is_collection' => false, 'is_mutation' => false, 'is_subscription' => true];
/** @var Operation */
$operation = (new Subscription())->withName('update_subscription')->withShortName($shortName);
if ($this->normalizeProcessor) {
$data = $this->normalizeProcessor->process($object, $operation, [], $resolverContext);
} elseif ($this->serializeStage) {
if ($this->serializeStage instanceof ProcessorInterface) {
$data = $this->serializeStage->process($object, $operation, [], $resolverContext);
} elseif ($this->serializeStage instanceof SerializeStage) {
$data = ($this->serializeStage)($object, $resourceClass, $operation, $resolverContext);
} else {
throw new \LogicException();
Expand Down
27 changes: 21 additions & 6 deletions src/GraphQl/Tests/Subscription/SubscriptionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace ApiPlatform\GraphQl\Tests\Subscription;

use ApiPlatform\GraphQl\Resolver\Stage\SerializeStageInterface;
use ApiPlatform\GraphQl\Subscription\SubscriptionIdentifierGeneratorInterface;
use ApiPlatform\GraphQl\Subscription\SubscriptionManager;
use ApiPlatform\GraphQl\Tests\Fixtures\ApiResource\Dummy;
Expand All @@ -24,6 +23,7 @@
use ApiPlatform\Metadata\Operations;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\State\ProcessorInterface;
use GraphQL\Type\Definition\ResolveInfo;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
Expand All @@ -40,7 +40,7 @@ class SubscriptionManagerTest extends TestCase

private ObjectProphecy $subscriptionsCacheProphecy;
private ObjectProphecy $subscriptionIdentifierGeneratorProphecy;
private ObjectProphecy $serializeStageProphecy;
private ObjectProphecy $normalizeProcessor;
private ObjectProphecy $iriConverterProphecy;
private SubscriptionManager $subscriptionManager;
private ObjectProphecy $resourceMetadataCollectionFactory;
Expand All @@ -52,10 +52,10 @@ protected function setUp(): void
{
$this->subscriptionsCacheProphecy = $this->prophesize(CacheItemPoolInterface::class);
$this->subscriptionIdentifierGeneratorProphecy = $this->prophesize(SubscriptionIdentifierGeneratorInterface::class);
$this->serializeStageProphecy = $this->prophesize(SerializeStageInterface::class);
$this->normalizeProcessor = $this->prophesize(ProcessorInterface::class);
$this->iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$this->resourceMetadataCollectionFactory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$this->subscriptionManager = new SubscriptionManager($this->subscriptionsCacheProphecy->reveal(), $this->subscriptionIdentifierGeneratorProphecy->reveal(), $this->serializeStageProphecy->reveal(), $this->iriConverterProphecy->reveal(), $this->resourceMetadataCollectionFactory->reveal());
$this->subscriptionManager = new SubscriptionManager($this->subscriptionsCacheProphecy->reveal(), $this->subscriptionIdentifierGeneratorProphecy->reveal(), $this->normalizeProcessor->reveal(), $this->iriConverterProphecy->reveal(), $this->resourceMetadataCollectionFactory->reveal());
}

public function testRetrieveSubscriptionIdNoIdentifier(): void
Expand Down Expand Up @@ -206,8 +206,23 @@ public function testGetPushPayloadsHit(): void
]);
$this->subscriptionsCacheProphecy->getItem('_dummies_2')->willReturn($cacheItemProphecy->reveal());

$this->serializeStageProphecy->__invoke($object, Dummy::class, (new Subscription())->withName('update_subscription')->withShortName('Dummy'), ['fields' => ['fieldsFoo'], 'is_collection' => false, 'is_mutation' => false, 'is_subscription' => true])->willReturn(['newResultFoo', 'clientSubscriptionId' => 'client-subscription-id']);
$this->serializeStageProphecy->__invoke($object, Dummy::class, (new Subscription())->withName('update_subscription')->withShortName('Dummy'), ['fields' => ['fieldsBar'], 'is_collection' => false, 'is_mutation' => false, 'is_subscription' => true])->willReturn(['resultBar', 'clientSubscriptionId' => 'client-subscription-id']);
$this->normalizeProcessor->process(
$object,
(new Subscription())->withName('update_subscription')->withShortName('Dummy'),
[],
['fields' => ['fieldsFoo'], 'is_collection' => false, 'is_mutation' => false, 'is_subscription' => true]
)->willReturn(
['newResultFoo', 'clientSubscriptionId' => 'client-subscription-id']
);

$this->normalizeProcessor->process(
$object,
(new Subscription())->withName('update_subscription')->withShortName('Dummy'),
[],
['fields' => ['fieldsBar'], 'is_collection' => false, 'is_mutation' => false, 'is_subscription' => true]
)->willReturn(
['resultBar', 'clientSubscriptionId' => 'client-subscription-id']
);

$this->assertEquals([['subscriptionIdFoo', ['newResultFoo']]], $this->subscriptionManager->getPushPayloads($object));
}
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@
"scripts": {
"test": "./vendor/bin/phpunit"
}
}
}
2 changes: 1 addition & 1 deletion src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
public function __construct(?TypeFactoryInterface $typeFactory, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ?NameConverterInterface $nameConverter = null, ?ResourceClassResolverInterface $resourceClassResolver = null, private readonly ?array $distinctFormats = null, private ?DefinitionNameFactoryInterface $definitionNameFactory = null)
{
if ($typeFactory) {
trigger_deprecation('api-platform/core', '4.0', sprintf('Injecting the "%s" inside "%s" is deprecated and "%s" will be removed in 4.x.', TypeFactoryInterface::class, self::class, TypeFactoryInterface::class));
trigger_deprecation('api-platform/core', '3.4', sprintf('Injecting the "%s" inside "%s" is deprecated and "%s" will be removed in 4.x.', TypeFactoryInterface::class, self::class, TypeFactoryInterface::class));
$this->typeFactory = $typeFactory;
}
if (!$definitionNameFactory) {
Expand Down
6 changes: 3 additions & 3 deletions src/OpenApi/Factory/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function __construct(private readonly ResourceNameCollectionFactoryInterf
$this->paginationOptions = $paginationOptions ?: new PaginationOptions();

if ($jsonSchemaTypeFactory) {
trigger_deprecation('api-platform/core', '4.0', sprintf('Injecting the "%s" inside "%s" is deprecated and "%s" will be removed in 4.x.', TypeFactoryInterface::class, self::class, TypeFactoryInterface::class));
trigger_deprecation('api-platform/core', '3.4', sprintf('Injecting the "%s" inside "%s" is deprecated and "%s" will be removed in 4.x.', TypeFactoryInterface::class, self::class, TypeFactoryInterface::class));
$this->jsonSchemaTypeFactory = $jsonSchemaTypeFactory;
}
}
Expand Down Expand Up @@ -649,7 +649,7 @@ private function getFiltersParameters(CollectionOperationInterface|HttpOperation
trigger_deprecation('api-platform/core', '4.0', sprintf('Using the "swagger" field of the %s::getDescription() (%s) is deprecated.', $filter::class, $operation->getShortName()));
}

if (isset($data['openapi']) && $data['openapi'] instanceof Parameter) {
if (!isset($data['openapi']) || $data['openapi'] instanceof Parameter) {
$schema = $data['schema'] ?? [];

if (isset($data['type']) && \in_array($data['type'] ?? null, Type::$builtinTypes, true) && !isset($schema['type'])) {
Expand Down Expand Up @@ -684,7 +684,7 @@ private function getFiltersParameters(CollectionOperationInterface|HttpOperation
if ($this->jsonSchemaTypeFactory) {
$schema = $data['schema'] ?? (\in_array($data['type'], Type::$builtinTypes, true) ? $this->jsonSchemaTypeFactory->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false), 'openapi') : ['type' => 'string']);
} else {
$schema = $data['schema'] ?? (\in_array($data['type'], Type::$builtinTypes, true) ? $this->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false), 'openapi') : ['type' => 'string']);
$schema = $data['schema'] ?? (\in_array($data['type'], Type::$builtinTypes, true) ? $this->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false)) : ['type' => 'string']);
}

$parameters[] = new Parameter(
Expand Down
11 changes: 6 additions & 5 deletions src/OpenApi/Tests/Factory/OpenApiFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public function testInvoke(): void
'type' => 'string',
'required' => true,
'strategy' => 'exact',
'openapi' => ['example' => 'bar', 'deprecated' => true, 'allowEmptyValue' => true, 'allowReserved' => true, 'explode' => true],
'openapi' => new Parameter(in: 'query', name: 'name', example: 'bar', deprecated: true, allowEmptyValue: true, allowReserved: true, explode: true),
]]),
'f2' => new DummyFilter(['ha' => [
'property' => 'foo',
Expand Down Expand Up @@ -458,7 +458,7 @@ public function testInvoke(): void
$propertyNameCollectionFactory,
$propertyMetadataFactory,
$schemaFactory,
$typeFactory,
null,
$filterLocatorProphecy->reveal(),
[],
new Options('Test API', 'This is a test API.', '1.2.3', true, 'oauth2', 'authorizationCode', '/oauth/v2/token', '/oauth/v2/auth', '/oauth/v2/refresh', ['scope param'], [
Expand Down Expand Up @@ -787,14 +787,15 @@ public function testInvoke(): void
new Parameter('name', 'query', '', true, true, true, [
'type' => 'string',
], 'form', true, true, 'bar'),
new Parameter('ha', 'query', '', false, false, true, [
new Parameter('ha', 'query', '', false, false, false, [
'type' => 'integer',
]),
new Parameter('toto', 'query', '', true, false, true, [
new Parameter('toto', 'query', '', true, false, false, [
'type' => 'array',
'items' => ['type' => 'string'],
], 'deepObject', true),
new Parameter('order[name]', 'query', '', false, false, true, [

new Parameter('order[name]', 'query', '', false, false, false, [
'type' => 'string',
'enum' => ['asc', 'desc'],
]),
Expand Down
2 changes: 1 addition & 1 deletion src/OpenApi/Tests/Serializer/OpenApiNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function testNormalize(): void
$propertyNameCollectionFactory,
$propertyMetadataFactory,
$schemaFactory,
$typeFactory,
null,
$filterLocatorProphecy->reveal(),
[],
new Options('Test API', 'This is a test API.', '1.2.3', true, 'oauth2', 'authorizationCode', '/oauth/v2/token', '/oauth/v2/auth', '/oauth/v2/refresh', ['scope param'], [
Expand Down
20 changes: 0 additions & 20 deletions src/Symfony/Tests/EventListener/ErrorListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,4 @@ public function testDuplicateExceptionWithErrorResource(): void
$errorListener = new ErrorListener('action', null, true, [], $resourceMetadataCollectionFactory, ['jsonld' => ['application/ld+json']], [], $identifiersExtractor, $resourceClassResolver);
$errorListener->onKernelException($exceptionEvent);
}

public function testDisableErrorResourceHandling(): void
{
$exception = Error::createFromException(new \Exception(), 400);
$resourceMetadataCollectionFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataCollectionFactory->expects($this->never())->method('create');
$resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
$resourceClassResolver->expects($this->never())->method('isResourceClass');
$kernel = $this->createStub(KernelInterface::class);
$kernel->method('handle')->willReturnCallback(function ($request) {
$this->assertEquals($request->attributes->get('_api_operation'), null);

return new Response();
});

$exceptionEvent = new ExceptionEvent($kernel, Request::create('/'), HttpKernelInterface::SUB_REQUEST, $exception);
$identifiersExtractor = $this->createStub(IdentifiersExtractorInterface::class);
$errorListener = new ErrorListener('action', null, true, [], $resourceMetadataCollectionFactory, ['jsonld' => ['application/ld+json']], [], $identifiersExtractor, $resourceClassResolver, null, false);
$errorListener->onKernelException($exceptionEvent);
}
}
1 change: 0 additions & 1 deletion src/deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
ApiPlatform\GraphQl\Resolver\Stage\WriteStage::class => true,
ApiPlatform\GraphQl\Resolver\Stage\WriteStageInterface::class => true,
ApiPlatform\HttpCache\EventListener\AddHeadersListener::class => true,
ApiPlatform\Symfony\EventListener\AddHeadersListener::class => true,
ApiPlatform\HttpCache\EventListener\AddTagsListener::class => true,
ApiPlatform\Hydra\EventListener\AddLinkHeaderListener::class => true,
ApiPlatform\Hydra\Serializer\ErrorNormalizer::class => true,
Expand Down
3 changes: 2 additions & 1 deletion tests/.ignored-deprecations
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
%Since api-platform/core 3.4: The "exclusiveMaximum" schema value should be a number not a boolean.%
%Since api-platform/core 3.4: The "exclusiveMinimum" schema value should be a number not a boolean.%
%Since api-platform/core 3.4: The "allowEmptyValue" option should be declared using an "openapi" parameter.%

%Since api-platform/core 3.4: Injecting the "ApiPlatform\\JsonSchema\\TypeFactoryInterface" inside "ApiPlatform\\JsonSchema\\SchemaFactory" is deprecated and "ApiPlatform\\JsonSchema\\TypeFactoryInterface" will be removed in 4.x.%
%Since api-platform/core 3.4: Injecting the "ApiPlatform\\JsonSchema\\TypeFactoryInterface" inside "ApiPlatform\\OpenApi\\Factory\\OpenApiFactory" is deprecated and "ApiPlatform\\JsonSchema\\TypeFactoryInterface" will be removed in 4.x.%
2 changes: 2 additions & 0 deletions tests/.ignored-deprecations-legacy-events
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@

%The "Symfony\\Bundle\\MakerBundle\\Maker\\MakeAuthenticator" class is deprecated, use any of the Security\\Make\* commands instead%
%Since symfony/validator 7.1: Not passing a value for the "requireTld" option to the Url constraint is deprecated. Its default value will change to "true".%
%Since api-platform/core 3.4: Injecting the "ApiPlatform\\JsonSchema\\TypeFactoryInterface" inside "ApiPlatform\\JsonSchema\\SchemaFactory" is deprecated and "ApiPlatform\\JsonSchema\\TypeFactoryInterface" will be removed in 4.x.%
%Since api-platform/core 3.4: Injecting the "ApiPlatform\\JsonSchema\\TypeFactoryInterface" inside "ApiPlatform\\OpenApi\\Factory\\OpenApiFactory" is deprecated and "ApiPlatform\\JsonSchema\\TypeFactoryInterface" will be removed in 4.x.%
2 changes: 1 addition & 1 deletion tests/Symfony/Bundle/Command/OpenApiCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function testBackedEnumExamplesAreNotLost(): void
};

$assertExample($json['components']['schemas']['Issue6317']['properties'], 'id');
$assertExample($json['components']['schemas']['Issue6317.jsonld']['properties'], 'id');
$assertExample($json['components']['schemas']['Issue6317.jsonld.output']['properties'], 'id');
$assertExample($json['components']['schemas']['Issue6317.jsonapi']['properties']['data']['properties']['attributes']['properties'], '_id');
$assertExample($json['components']['schemas']['Issue6317.jsonhal']['properties'], 'id');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm
'keep_legacy_inflector' => true,
'event_listeners_backward_compatibility_layer' => null,
'use_symfony_listeners' => false,
'use_deprecated_json_schema_type_factory' => null,
'handle_symfony_errors' => false,
'enable_link_security' => false,
], $config);
Expand Down

0 comments on commit 1f7fd42

Please sign in to comment.