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

api-platform / core / 6148660584

11 Sep 2023 03:40PM UTC coverage: 37.077% (-0.1%) from 37.185%
6148660584

push

github

soyuka
chore(symfony): security after validate when validator installed

10090 of 27214 relevant lines covered (37.08%)

19.39 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\Metadata\ApiProperty;
17
use ApiPlatform\Metadata\Extractor\XmlPropertyExtractor;
18
use ApiPlatform\Metadata\Extractor\YamlPropertyExtractor;
19
use ApiPlatform\Metadata\Property\Factory\ExtractorPropertyMetadataFactory;
20
use ApiPlatform\Metadata\Tests\Extractor\Adapter\PropertyAdapterInterface;
21
use ApiPlatform\Metadata\Tests\Extractor\Adapter\XmlPropertyAdapter;
22
use ApiPlatform\Metadata\Tests\Extractor\Adapter\YamlPropertyAdapter;
23
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Comment;
24
use PHPUnit\Framework\AssertionFailedError;
25
use PHPUnit\Framework\TestCase;
26
use Symfony\Component\PropertyInfo\Type;
27

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

78
    /**
79
     * @dataProvider getExtractors
80
     */
81
    public function testValidMetadata(string $extractorClass, PropertyAdapterInterface $adapter): void
82
    {
83
        $reflClass = new \ReflectionClass(ApiProperty::class);
×
84
        $parameters = $reflClass->getConstructor()->getParameters();
×
85

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

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

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

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

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

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

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

122
        return $property;
×
123
    }
124

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