Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Aug 10, 2021
1 parent aed0e8f commit 51ba59c
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 357 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -7,7 +7,7 @@ on:
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERAGE: '0'
SYMFONY_DEPRECATIONS_HELPER: 'disabled=1'
SYMFONY_DEPRECATIONS_HELPER: disabled=1

jobs:
php-cs-fixer:
Expand Down Expand Up @@ -148,7 +148,7 @@ jobs:
run: |
mkdir -p build/logs/phpunit
if [ "$COVERAGE" = '1' ]; then
vendor/bin/simple-phpunit --coverage-clover build/logs/phpunit/clover.xml --log-junit build/logs/phpunit/junit.xml
vendor/bin/simple-phpunit --log-junit build/logs/phpunit/junit.xml --coverage-clover build/logs/phpunit/clover.xml
else
vendor/bin/simple-phpunit --log-junit build/logs/phpunit/junit.xml
fi
Expand Down
Expand Up @@ -26,188 +26,190 @@
use Psr\Container\ContainerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* Creates Nelmio ApiDoc annotations for the api platform.
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Teoh Han Hui <teohhanhui@gmail.com>
*
* @deprecated since version 2.2, to be removed in 3.0. NelmioApiDocBundle 3 has native support for API Platform.
*/
final class ApiPlatformProvider implements AnnotationsProviderInterface
{
use FilterLocatorTrait;

private $resourceNameCollectionFactory;
private $documentationNormalizer;
private $resourceMetadataFactory;
private $operationMethodResolver;

if (interface_exists(AnnotationsProviderInterface::class)) {
/**
* @param ContainerInterface|FilterCollection $filterLocator The new filter locator or the deprecated filter collection
* Creates Nelmio ApiDoc annotations for the api platform.
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Teoh Han Hui <teohhanhui@gmail.com>
*
* @deprecated since version 2.2, to be removed in 3.0. NelmioApiDocBundle 3 has native support for API Platform.
*/
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, NormalizerInterface $documentationNormalizer, ResourceMetadataFactoryInterface $resourceMetadataFactory, $filterLocator, OperationMethodResolverInterface $operationMethodResolver)
final class ApiPlatformProvider implements AnnotationsProviderInterface
{
@trigger_error('The '.__CLASS__.' class is deprecated since version 2.2 and will be removed in 3.0. NelmioApiDocBundle 3 has native support for API Platform.', \E_USER_DEPRECATED);

$this->setFilterLocator($filterLocator);

$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
$this->documentationNormalizer = $documentationNormalizer;
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->operationMethodResolver = $operationMethodResolver;
}

/**
* {@inheritdoc}
*/
public function getAnnotations(): array
{
$resourceNameCollection = $this->resourceNameCollectionFactory->create();

$hydraDoc = $this->documentationNormalizer->normalize(new Documentation($resourceNameCollection));
if (!\is_array($hydraDoc)) {
throw new \UnexpectedValueException('Expected data to be an array');
use FilterLocatorTrait;

private $resourceNameCollectionFactory;
private $documentationNormalizer;
private $resourceMetadataFactory;
private $operationMethodResolver;

/**
* @param ContainerInterface|FilterCollection $filterLocator The new filter locator or the deprecated filter collection
*/
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, NormalizerInterface $documentationNormalizer, ResourceMetadataFactoryInterface $resourceMetadataFactory, $filterLocator, OperationMethodResolverInterface $operationMethodResolver)
{
@trigger_error('The '.__CLASS__.' class is deprecated since version 2.2 and will be removed in 3.0. NelmioApiDocBundle 3 has native support for API Platform.', \E_USER_DEPRECATED);

$this->setFilterLocator($filterLocator);

$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
$this->documentationNormalizer = $documentationNormalizer;
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->operationMethodResolver = $operationMethodResolver;
}

if (empty($hydraDoc)) {
return [];
}
/**
* {@inheritdoc}
*/
public function getAnnotations(): array
{
$resourceNameCollection = $this->resourceNameCollectionFactory->create();

$entrypointHydraDoc = $this->getResourceHydraDoc($hydraDoc, '#Entrypoint');
if (null === $entrypointHydraDoc) {
return [];
}
$hydraDoc = $this->documentationNormalizer->normalize(new Documentation($resourceNameCollection));
if (!\is_array($hydraDoc)) {
throw new \UnexpectedValueException('Expected data to be an array');
}

$annotations = [];
foreach ($resourceNameCollection as $resourceClass) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
if (empty($hydraDoc)) {
return [];
}

$prefixedShortName = ($iri = $resourceMetadata->getIri()) ? $iri : '#'.$resourceMetadata->getShortName();
$resourceHydraDoc = $this->getResourceHydraDoc($hydraDoc, $prefixedShortName);
if (null === $resourceHydraDoc) {
continue;
$entrypointHydraDoc = $this->getResourceHydraDoc($hydraDoc, '#Entrypoint');
if (null === $entrypointHydraDoc) {
return [];
}

if (null !== $collectionOperations = $resourceMetadata->getCollectionOperations()) {
foreach ($collectionOperations as $operationName => $operation) {
$annotations[] = $this->getApiDoc(true, $resourceClass, $resourceMetadata, $operationName, $resourceHydraDoc, $entrypointHydraDoc);
$annotations = [];
foreach ($resourceNameCollection as $resourceClass) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);

$prefixedShortName = ($iri = $resourceMetadata->getIri()) ? $iri : '#'.$resourceMetadata->getShortName();
$resourceHydraDoc = $this->getResourceHydraDoc($hydraDoc, $prefixedShortName);
if (null === $resourceHydraDoc) {
continue;
}
}

if (null !== $itemOperations = $resourceMetadata->getItemOperations()) {
foreach ($itemOperations as $operationName => $operation) {
$annotations[] = $this->getApiDoc(false, $resourceClass, $resourceMetadata, $operationName, $resourceHydraDoc);
if (null !== $collectionOperations = $resourceMetadata->getCollectionOperations()) {
foreach ($collectionOperations as $operationName => $operation) {
$annotations[] = $this->getApiDoc(true, $resourceClass, $resourceMetadata, $operationName, $resourceHydraDoc, $entrypointHydraDoc);
}
}
}
}

return $annotations;
}
if (null !== $itemOperations = $resourceMetadata->getItemOperations()) {
foreach ($itemOperations as $operationName => $operation) {
$annotations[] = $this->getApiDoc(false, $resourceClass, $resourceMetadata, $operationName, $resourceHydraDoc);
}
}
}

/**
* Builds ApiDoc annotation from ApiPlatform data.
*/
private function getApiDoc(bool $collection, string $resourceClass, ResourceMetadata $resourceMetadata, string $operationName, array $resourceHydraDoc, array $entrypointHydraDoc = []): ApiDoc
{
if ($collection) {
$method = $this->operationMethodResolver->getCollectionOperationMethod($resourceClass, $operationName);
$route = $this->operationMethodResolver->getCollectionOperationRoute($resourceClass, $operationName);
$operationHydraDoc = $this->getCollectionOperationHydraDoc($resourceMetadata->getShortName(), $method, $entrypointHydraDoc);
} else {
$method = $this->operationMethodResolver->getItemOperationMethod($resourceClass, $operationName);
$route = $this->operationMethodResolver->getItemOperationRoute($resourceClass, $operationName);
$operationHydraDoc = $this->getOperationHydraDoc($method, $resourceHydraDoc);
return $annotations;
}

$data = [
'resource' => $route->getPath(),
'description' => $operationHydraDoc['hydra:title'] ?? '',
'resourceDescription' => $resourceHydraDoc['hydra:title'] ?? '',
'section' => $resourceHydraDoc['hydra:title'] ?? '',
];
/**
* Builds ApiDoc annotation from ApiPlatform data.
*/
private function getApiDoc(bool $collection, string $resourceClass, ResourceMetadata $resourceMetadata, string $operationName, array $resourceHydraDoc, array $entrypointHydraDoc = []): ApiDoc
{
if ($collection) {
$method = $this->operationMethodResolver->getCollectionOperationMethod($resourceClass, $operationName);
$route = $this->operationMethodResolver->getCollectionOperationRoute($resourceClass, $operationName);
$operationHydraDoc = $this->getCollectionOperationHydraDoc($resourceMetadata->getShortName(), $method, $entrypointHydraDoc);
} else {
$method = $this->operationMethodResolver->getItemOperationMethod($resourceClass, $operationName);
$route = $this->operationMethodResolver->getItemOperationRoute($resourceClass, $operationName);
$operationHydraDoc = $this->getOperationHydraDoc($method, $resourceHydraDoc);
}

if (isset($operationHydraDoc['expects']) && 'owl:Nothing' !== $operationHydraDoc['expects']) {
$data['input'] = sprintf('%s:%s:%s', ApiPlatformParser::IN_PREFIX, $resourceClass, $operationName);
}
$data = [
'resource' => $route->getPath(),
'description' => $operationHydraDoc['hydra:title'] ?? '',
'resourceDescription' => $resourceHydraDoc['hydra:title'] ?? '',
'section' => $resourceHydraDoc['hydra:title'] ?? '',
];

if (isset($operationHydraDoc['returns']) && 'owl:Nothing' !== $operationHydraDoc['returns']) {
$data['output'] = sprintf('%s:%s:%s', ApiPlatformParser::OUT_PREFIX, $resourceClass, $operationName);
}
if (isset($operationHydraDoc['expects']) && 'owl:Nothing' !== $operationHydraDoc['expects']) {
$data['input'] = sprintf('%s:%s:%s', ApiPlatformParser::IN_PREFIX, $resourceClass, $operationName);
}

if ($collection && 'GET' === $method) {
$resourceFilters = $resourceMetadata->getCollectionOperationAttribute($operationName, 'filters', [], true);
if (isset($operationHydraDoc['returns']) && 'owl:Nothing' !== $operationHydraDoc['returns']) {
$data['output'] = sprintf('%s:%s:%s', ApiPlatformParser::OUT_PREFIX, $resourceClass, $operationName);
}

$data['filters'] = [];
foreach ($resourceFilters as $filterId) {
if ($filter = $this->getFilter($filterId)) {
foreach ($filter->getDescription($resourceClass) as $name => $definition) {
$data['filters'][] = ['name' => $name] + $definition;
if ($collection && 'GET' === $method) {
$resourceFilters = $resourceMetadata->getCollectionOperationAttribute($operationName, 'filters', [], true);

$data['filters'] = [];
foreach ($resourceFilters as $filterId) {
if ($filter = $this->getFilter($filterId)) {
foreach ($filter->getDescription($resourceClass) as $name => $definition) {
$data['filters'][] = ['name' => $name] + $definition;
}
}
}
}
}

$apiDoc = new ApiDoc($data);
$apiDoc->setRoute($route);
$apiDoc = new ApiDoc($data);
$apiDoc->setRoute($route);

return $apiDoc;
}

/**
* Gets Hydra documentation for the given resource.
*/
private function getResourceHydraDoc(array $hydraApiDoc, string $prefixedShortName): ?array
{
if (!isset($hydraApiDoc['hydra:supportedClass']) || !\is_array($hydraApiDoc['hydra:supportedClass'])) {
return null;
return $apiDoc;
}

foreach ($hydraApiDoc['hydra:supportedClass'] as $supportedClass) {
if (isset($supportedClass['@id']) && $supportedClass['@id'] === $prefixedShortName) {
return $supportedClass;
/**
* Gets Hydra documentation for the given resource.
*/
private function getResourceHydraDoc(array $hydraApiDoc, string $prefixedShortName): ?array
{
if (!isset($hydraApiDoc['hydra:supportedClass']) || !\is_array($hydraApiDoc['hydra:supportedClass'])) {
return null;
}
}

return null;
}
foreach ($hydraApiDoc['hydra:supportedClass'] as $supportedClass) {
if (isset($supportedClass['@id']) && $supportedClass['@id'] === $prefixedShortName) {
return $supportedClass;
}
}

/**
* Gets the Hydra documentation of a given operation.
*/
private function getOperationHydraDoc(string $method, array $hydraDoc): array
{
if (!isset($hydraDoc['hydra:supportedOperation']) || !\is_array($hydraDoc['hydra:supportedOperation'])) {
return [];
return null;
}

foreach ($hydraDoc['hydra:supportedOperation'] as $supportedOperation) {
if ($supportedOperation['hydra:method'] === $method) {
return $supportedOperation;
/**
* Gets the Hydra documentation of a given operation.
*/
private function getOperationHydraDoc(string $method, array $hydraDoc): array
{
if (!isset($hydraDoc['hydra:supportedOperation']) || !\is_array($hydraDoc['hydra:supportedOperation'])) {
return [];
}
}

return [];
}
foreach ($hydraDoc['hydra:supportedOperation'] as $supportedOperation) {
if ($supportedOperation['hydra:method'] === $method) {
return $supportedOperation;
}
}

/**
* Gets the Hydra documentation for the collection operation.
*/
private function getCollectionOperationHydraDoc(string $shortName, string $method, array $hydraEntrypointDoc): array
{
if (!isset($hydraEntrypointDoc['hydra:supportedProperty']) || !\is_array($hydraEntrypointDoc['hydra:supportedProperty'])) {
return [];
}

$propertyName = '#Entrypoint/'.lcfirst($shortName);
/**
* Gets the Hydra documentation for the collection operation.
*/
private function getCollectionOperationHydraDoc(string $shortName, string $method, array $hydraEntrypointDoc): array
{
if (!isset($hydraEntrypointDoc['hydra:supportedProperty']) || !\is_array($hydraEntrypointDoc['hydra:supportedProperty'])) {
return [];
}

$propertyName = '#Entrypoint/'.lcfirst($shortName);

foreach ($hydraEntrypointDoc['hydra:supportedProperty'] as $supportedProperty) {
if (isset($supportedProperty['hydra:property']['@id'])
&& $supportedProperty['hydra:property']['@id'] === $propertyName) {
return $this->getOperationHydraDoc($method, $supportedProperty['hydra:property']);
foreach ($hydraEntrypointDoc['hydra:supportedProperty'] as $supportedProperty) {
if (isset($supportedProperty['hydra:property']['@id'])
&& $supportedProperty['hydra:property']['@id'] === $propertyName) {
return $this->getOperationHydraDoc($method, $supportedProperty['hydra:property']);
}
}
}

return [];
return [];
}
}
}

0 comments on commit 51ba59c

Please sign in to comment.