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

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 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
        'hydra' => false,
80
    ];
81

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

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

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

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

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

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

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

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

UNCOV
124
        return $property;
×
125
    }
126

127
    private function withBuiltinTypes(array $values, array $fixtures): array
128
    {
UNCOV
129
        return array_map(fn (string $builtinType): Type => new Type($builtinType), $values);
×
130
    }
131
}
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