• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10315659289

09 Aug 2024 07:49AM UTC coverage: 7.841% (-0.006%) from 7.847%
10315659289

push

github

soyuka
style: cs fixes

70 of 529 new or added lines in 176 files covered. (13.23%)

160 existing lines in 58 files now uncovered.

12688 of 161818 relevant lines covered (7.84%)

26.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Metadata/Tests/Extractor/PropertyMetadataCompatibilityTest.php
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\Metadata\Tests\Extractor;
15

16
use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory;
17
use ApiPlatform\Metadata\ApiProperty;
18
use ApiPlatform\Metadata\Extractor\XmlPropertyExtractor;
19
use ApiPlatform\Metadata\Extractor\YamlPropertyExtractor;
20
use ApiPlatform\Metadata\Property\Factory\ExtractorPropertyMetadataFactory;
21
use ApiPlatform\Metadata\Tests\Extractor\Adapter\PropertyAdapterInterface;
22
use ApiPlatform\Metadata\Tests\Extractor\Adapter\XmlPropertyAdapter;
23
use ApiPlatform\Metadata\Tests\Extractor\Adapter\YamlPropertyAdapter;
24
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Comment;
25
use PHPUnit\Framework\AssertionFailedError;
26
use PHPUnit\Framework\TestCase;
27
use Symfony\Component\PropertyInfo\Type;
28

29
/**
30
 * Ensures XML and YAML mappings are fully compatible with ApiPlatform\Metadata\ApiProperty.
31
 *
32
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
33
 */
34
final class PropertyMetadataCompatibilityTest extends TestCase
35
{
36
    private const RESOURCE_CLASS = Comment::class;
37
    private const PROPERTY = 'comment';
38
    private const FIXTURES = [
39
        'description' => 'Comment message',
40
        'readable' => true,
41
        'writable' => true,
42
        'readableLink' => true,
43
        'writableLink' => true,
44
        'default' => 'Plop',
45
        'deprecationReason' => 'Foo',
46
        'schema' => ['https://schema.org/Thing'],
47
        'required' => true,
48
        'identifier' => false,
49
        'example' => 'Lorem ipsum dolor sit amet',
50
        'fetchable' => true,
51
        'fetchEager' => true,
52
        'jsonldContext' => [
53
            'bar' => [
54
                'foo' => [
55
                    'bar' => 'baz',
56
                ],
57
            ],
58
        ],
59
        'openapiContext' => [
60
            'foo' => 'bar',
61
        ],
62
        'jsonSchemaContext' => [
63
            'lorem' => 'ipsum',
64
        ],
65
        'push' => true,
66
        'security' => 'is_granted(\'IS_AUTHENTICATED_ANONYMOUSLY\')',
67
        'securityPostDenormalize' => 'is_granted(\'ROLE_CUSTOM_ADMIN\')',
68
        'types' => ['someirischema', 'anotheririschema'],
69
        'builtinTypes' => ['string'],
70
        'initializable' => true,
71
        'extraProperties' => [
72
            'custom_property' => 'Lorem ipsum dolor sit amet',
73
            SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED => true,
74
        ],
75
        'iris' => ['https://schema.org/totalPrice'],
76
        'genId' => true,
77
        'uriTemplate' => '/sub-resource-get-collection',
78
        'property' => 'test',
79
    ];
80

81
    #[\PHPUnit\Framework\Attributes\DataProvider('getExtractors')]
82
    public function testValidMetadata(string $extractorClass, PropertyAdapterInterface $adapter): void
83
    {
84
        $reflClass = new \ReflectionClass(ApiProperty::class);
×
85
        $parameters = $reflClass->getConstructor()->getParameters();
×
86

87
        try {
88
            $extractor = new $extractorClass($adapter(self::RESOURCE_CLASS, self::PROPERTY, $parameters, self::FIXTURES));
×
89
            $factory = new ExtractorPropertyMetadataFactory($extractor);
×
90
            $property = $factory->create(self::RESOURCE_CLASS, self::PROPERTY);
×
91
        } catch (\Exception $exception) {
×
92
            throw new AssertionFailedError('Failed asserting that the schema is valid according to '.ApiProperty::class, 0, $exception);
×
93
        }
94

95
        $this->assertEquals($this->buildApiProperty(), $property);
×
96
    }
97

98
    public static function getExtractors(): array
99
    {
100
        return [
×
101
            [XmlPropertyExtractor::class, new XmlPropertyAdapter()],
×
102
            [YamlPropertyExtractor::class, new YamlPropertyAdapter()],
×
103
        ];
×
104
    }
105

106
    private function buildApiProperty(): ApiProperty
107
    {
108
        $property = new ApiProperty();
×
109

110
        foreach (self::FIXTURES as $parameter => $value) {
×
111
            if (method_exists($this, 'with'.ucfirst($parameter))) {
×
112
                $value = $this->{'with'.ucfirst($parameter)}($value, self::FIXTURES);
×
113
            }
114

115
            if (method_exists($property, 'with'.ucfirst($parameter))) {
×
116
                $property = $property->{'with'.ucfirst($parameter)}($value, self::FIXTURES);
×
117
                continue;
×
118
            }
119

NEW
120
            throw new \RuntimeException(\sprintf('Unknown ApiProperty parameter "%s".', $parameter));
×
121
        }
122

123
        return $property;
×
124
    }
125

126
    private function withBuiltinTypes(array $values, array $fixtures): array
127
    {
128
        return array_map(fn (string $builtinType): Type => new Type($builtinType), $values);
×
129
    }
130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc