1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of AppBundle the package. |
4
|
|
|
* |
5
|
|
|
* (c) Ruslan Muriev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace RonteLtd\JsonApiBundle\Tests\Mapping; |
12
|
|
|
|
13
|
|
|
use RonteLtd\JsonApiBundle\Annotation\Attribute; |
14
|
|
|
use RonteLtd\JsonApiBundle\Annotation\ObjectNormalizer; |
15
|
|
|
use RonteLtd\JsonApiBundle\Annotation\Relationship; |
16
|
|
|
use RonteLtd\JsonApiBundle\Serializer\Mapping\AttributeMetadata; |
17
|
|
|
use RonteLtd\JsonApiBundle\Serializer\Mapping\ClassMetadata; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class TestClassMetadataFactory |
22
|
|
|
* |
23
|
|
|
* @package RonteLtd\JsonApiBundle\Tests\Mapping |
24
|
|
|
* @author Ruslan Muriev <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class TestClassMetadataFactory |
27
|
|
|
{ |
28
|
|
View Code Duplication |
public static function createAttributeClassMetadata() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$expected = new ClassMetadata('RonteLtd\JsonApiBundle\Tests\Fixtures\AttributeDummy'); |
31
|
|
|
|
32
|
|
|
$fooAttributeAnnotation = new Attribute(); |
33
|
|
|
$fooAttributeAnnotation->setName('a'); |
34
|
|
|
|
35
|
|
|
$foo = new AttributeMetadata('foo'); |
36
|
|
|
$foo->setAttribute($fooAttributeAnnotation); |
37
|
|
|
$expected->addAttributeMetadata($foo); |
38
|
|
|
|
39
|
|
|
$barAttributeAnnotation = new Attribute(); |
40
|
|
|
|
41
|
|
|
$bar = new AttributeMetadata('bar'); |
42
|
|
|
$bar->setAttribute($barAttributeAnnotation); |
43
|
|
|
$expected->addAttributeMetadata($bar); |
44
|
|
|
|
45
|
|
|
$fooBar = new AttributeMetadata('fooBar'); |
46
|
|
|
$expected->addAttributeMetadata($fooBar); |
47
|
|
|
|
48
|
|
|
// load reflection class so that the comparison passes |
49
|
|
|
$expected->getReflectionClass(); |
50
|
|
|
|
51
|
|
|
return $expected; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public static function createRelationshipClassMetadata() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$expected = new ClassMetadata('RonteLtd\JsonApiBundle\Tests\Fixtures\RelationshipDummy'); |
57
|
|
|
|
58
|
|
|
$fooRelationshipAnnotation = new Relationship(); |
59
|
|
|
$fooRelationshipAnnotation->setName('a'); |
60
|
|
|
|
61
|
|
|
$foo = new AttributeMetadata('foo'); |
62
|
|
|
$foo->setRelationship($fooRelationshipAnnotation); |
63
|
|
|
$expected->addAttributeMetadata($foo); |
64
|
|
|
|
65
|
|
|
$barRelationshipAnnotation = new Relationship(); |
66
|
|
|
|
67
|
|
|
$bar = new AttributeMetadata('bar'); |
68
|
|
|
$bar->setRelationship($barRelationshipAnnotation); |
69
|
|
|
$expected->addAttributeMetadata($bar); |
70
|
|
|
|
71
|
|
|
$fooBar = new AttributeMetadata('fooBar'); |
72
|
|
|
$expected->addAttributeMetadata($fooBar); |
73
|
|
|
|
74
|
|
|
// load reflection class so that the comparison passes |
75
|
|
|
$expected->getReflectionClass(); |
76
|
|
|
|
77
|
|
|
return $expected; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public static function createObjectNormalizerClassMetadata() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$expected = new ClassMetadata('RonteLtd\JsonApiBundle\Tests\Fixtures\ObjectNormalizerDummy'); |
83
|
|
|
|
84
|
|
|
$objectNormalizer = new ObjectNormalizer(); |
85
|
|
|
$objectNormalizer->setName("jsonApiEntityType"); |
86
|
|
|
$objectNormalizer->setMeta([ |
87
|
|
|
'copyright' => 'copyright', |
88
|
|
|
'authors' => ['a'] |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
$expected->addClassAnnotation($objectNormalizer); |
92
|
|
|
|
93
|
|
|
$nameAttributeAnnotation = new Attribute(); |
94
|
|
|
$nameAttributeAnnotation->setName("name"); |
95
|
|
|
|
96
|
|
|
$name = new AttributeMetadata('name'); |
97
|
|
|
$name->setAttribute($nameAttributeAnnotation); |
98
|
|
|
$expected->addAttributeMetadata($name); |
99
|
|
|
|
100
|
|
|
$enabledAnnotation = new Attribute(); |
101
|
|
|
$enabledAnnotation->setName('isEnabled'); |
102
|
|
|
|
103
|
|
|
$enabled = new AttributeMetadata('enabled'); |
104
|
|
|
$enabled->setAttribute($enabledAnnotation); |
105
|
|
|
$expected->addAttributeMetadata($enabled); |
106
|
|
|
|
107
|
|
|
$categoryRelationshipAnnotation = new Relationship(); |
108
|
|
|
|
109
|
|
|
$category = new AttributeMetadata('category'); |
110
|
|
|
$category->setRelationship($categoryRelationshipAnnotation); |
111
|
|
|
$expected->addAttributeMetadata($category); |
112
|
|
|
|
113
|
|
|
// load reflection class so that the comparison passes |
114
|
|
|
$expected->getReflectionClass(); |
115
|
|
|
|
116
|
|
|
return $expected; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
View Code Duplication |
public static function createMergedClassMetadata() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$expected = self::createAttributeClassMetadata(); |
122
|
|
|
|
123
|
|
|
$objectNormalizer = new ObjectNormalizer(); |
124
|
|
|
$objectNormalizer->setName("jsonApiEntityType"); |
125
|
|
|
$objectNormalizer->setMeta([ |
126
|
|
|
'copyright' => 'copyright', |
127
|
|
|
'authors' => ['a'] |
128
|
|
|
]); |
129
|
|
|
|
130
|
|
|
$expected->addClassAnnotation($objectNormalizer); |
131
|
|
|
|
132
|
|
|
$nameAttributeAnnotation = new Attribute(); |
133
|
|
|
$nameAttributeAnnotation->setName("name"); |
134
|
|
|
|
135
|
|
|
$name = new AttributeMetadata('name'); |
136
|
|
|
$name->setAttribute($nameAttributeAnnotation); |
137
|
|
|
$expected->addAttributeMetadata($name); |
138
|
|
|
|
139
|
|
|
$enabledAnnotation = new Attribute(); |
140
|
|
|
$enabledAnnotation->setName('isEnabled'); |
141
|
|
|
|
142
|
|
|
$enabled = new AttributeMetadata('enabled'); |
143
|
|
|
$enabled->setAttribute($enabledAnnotation); |
144
|
|
|
$expected->addAttributeMetadata($enabled); |
145
|
|
|
|
146
|
|
|
$categoryRelationshipAnnotation = new Relationship(); |
147
|
|
|
|
148
|
|
|
$category = new AttributeMetadata('category'); |
149
|
|
|
$category->setRelationship($categoryRelationshipAnnotation); |
150
|
|
|
$expected->addAttributeMetadata($category); |
151
|
|
|
|
152
|
|
|
// load reflection class so that the comparison passes |
153
|
|
|
$expected->getReflectionClass(); |
154
|
|
|
|
155
|
|
|
return $expected; |
156
|
|
|
} |
157
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.