-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathConfigurationAttributeGeneratorTest.php
125 lines (101 loc) · 4.98 KB
/
ConfigurationAttributeGeneratorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\SchemaGenerator\Tests\AttributeGenerator;
use ApiPlatform\SchemaGenerator\AttributeGenerator\ConfigurationAttributeGenerator;
use ApiPlatform\SchemaGenerator\Model\Attribute;
use ApiPlatform\SchemaGenerator\Model\Use_;
use ApiPlatform\SchemaGenerator\PhpTypeConverter;
use ApiPlatform\SchemaGenerator\Schema\Model\Class_ as SchemaClass;
use ApiPlatform\SchemaGenerator\Schema\Model\Property;
use ApiPlatform\SchemaGenerator\SchemaGeneratorConfiguration;
use EasyRdf\Graph as RdfGraph;
use EasyRdf\Resource as RdfResource;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\String\Inflector\EnglishInflector;
class ConfigurationAttributeGeneratorTest extends TestCase
{
#[DataProvider('provideGenerateClassAttributesCases')]
public function testGenerateClassAttributes(SchemaClass $class, array $config, array $attributes): void
{
$this->assertEquals($attributes, $this->generator($config)->generateClassAttributes($class));
}
public static function provideGenerateClassAttributesCases(): \Generator
{
$class = new SchemaClass('Foo', new RdfResource('https://schema.org/Foo', new RdfGraph()));
yield 'no configuration' => [$class, [], []];
$class = new SchemaClass('Foo', new RdfResource('https://schema.org/Foo', new RdfGraph()));
yield 'type configuration' => [
$class,
['types' => ['Foo' => ['attributes' => [['ApiResource' => ['routePrefix' => '/prefix']]]]]],
[new Attribute('ApiResource', ['routePrefix' => '/prefix', 'mergeable' => false])],
];
$class = new SchemaClass('Foo', new RdfResource('https://schema.org/Foo', new RdfGraph(SchemaGeneratorConfiguration::SCHEMA_ORG_URI)));
$expectedAttribute = new Attribute('ApiResource', ['routePrefix' => '/prefix']);
$expectedAttribute->append = false;
$expectedAttribute->mergeable = false;
yield 'vocab configuration' => [
$class,
['vocabularies' => [SchemaGeneratorConfiguration::SCHEMA_ORG_URI => ['attributes' => [['ApiResource' => ['routePrefix' => '/prefix']]]]]],
[$expectedAttribute],
];
$class = new SchemaClass('Foo', new RdfResource('https://schema.org/Foo', new RdfGraph(SchemaGeneratorConfiguration::SCHEMA_ORG_URI)));
yield 'vocab and type configuration' => [
$class,
[
'vocabularies' => [SchemaGeneratorConfiguration::SCHEMA_ORG_URI => ['attributes' => [['ApiResource' => ['routePrefix' => '/prefix']]]]],
'types' => ['Foo' => ['attributes' => [['ApiResource' => ['security' => "is_granted('ROLE_USER')"]]]]],
],
[new Attribute('ApiResource', ['security' => "is_granted('ROLE_USER')", 'mergeable' => false])],
];
}
#[DataProvider('provideGeneratePropertyAttributesCases')]
public function testGeneratePropertyAttributes(Property $property, array $config, array $attributes): void
{
$this->assertEquals($attributes, $this->generator($config)->generatePropertyAttributes($property, 'Res'));
}
public static function provideGeneratePropertyAttributesCases(): \Generator
{
$property = new Property('prop');
yield 'no configuration' => [$property, [], []];
$property = new Property('prop');
yield 'type configuration' => [
$property,
['types' => ['Res' => ['properties' => ['prop' => ['attributes' => [['ApiResource' => ['security' => "is_granted('ROLE_USER')"]]]]]]]],
[new Attribute('ApiResource', ['security' => "is_granted('ROLE_USER')", 'mergeable' => false])],
];
}
#[DataProvider('provideGenerateUsesCases')]
public function testGenerateUses(SchemaClass $class, array $config, array $uses): void
{
$this->assertEquals($uses, $this->generator($config)->generateUses($class));
}
public static function provideGenerateUsesCases(): \Generator
{
$class = new SchemaClass('Foo', new RdfResource('https://schema.org/Foo', new RdfGraph()));
yield 'no configuration' => [$class, ['uses' => []], []];
$class = new SchemaClass('Foo', new RdfResource('https://schema.org/Foo', new RdfGraph()));
yield 'type configuration' => [
$class,
['uses' => ['Symfony\Component\Validator\Constraints' => ['alias' => 'Assert']]],
[new Use_('Symfony\Component\Validator\Constraints', 'Assert')],
];
}
private function generator(array $config = []): ConfigurationAttributeGenerator
{
return new ConfigurationAttributeGenerator(
new PhpTypeConverter(),
new EnglishInflector(),
$config,
[],
);
}
}