1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajir\RabbitMqSqlBundle\Tests\Factory; |
4
|
|
|
|
5
|
|
|
use Ajir\RabbitMqSqlBundle\DataMapper\DataMapper; |
6
|
|
|
use Ajir\RabbitMqSqlBundle\Factory\RelationFactory; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Florian Ajir <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class RelationFactoryTest extends \PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
protected $classImplement = 'Ajir\RabbitMqSqlBundle\Model\RelationInterface'; |
14
|
|
|
protected $oneToOneRelationClass = 'Ajir\RabbitMqSqlBundle\Model\OneToOneRelation'; |
15
|
|
|
protected $manyToOneRelationClass = 'Ajir\RabbitMqSqlBundle\Model\ManyToOneRelation'; |
16
|
|
|
protected $oneToManyRelationClass = 'Ajir\RabbitMqSqlBundle\Model\OneToManyRelation'; |
17
|
|
|
protected $manyToManyRelationClass = 'Ajir\RabbitMqSqlBundle\Model\ManyToManyRelation'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
public function testFactoryInterface() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$factory = new RelationFactory( |
25
|
|
|
$this->oneToOneRelationClass, |
26
|
|
|
$this->manyToOneRelationClass, |
27
|
|
|
$this->oneToManyRelationClass, |
28
|
|
|
$this->manyToManyRelationClass |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$this->assertInstanceOf('Ajir\RabbitMqSqlBundle\Factory\RelationFactoryInterface', $factory); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function testCreateOneToOne() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$factory = new RelationFactory( |
40
|
|
|
$this->oneToOneRelationClass, |
41
|
|
|
$this->manyToOneRelationClass, |
42
|
|
|
$this->oneToManyRelationClass, |
43
|
|
|
$this->manyToManyRelationClass |
44
|
|
|
); |
45
|
|
|
$this->assertInstanceOf( |
46
|
|
|
$this->classImplement, |
47
|
|
|
$factory->create( |
48
|
|
|
DataMapper::RELATION_ONE_TO_ONE, |
49
|
|
|
array( |
50
|
|
|
'_relation' => array( |
51
|
|
|
'table' => 'test', |
52
|
|
|
'targetEntity' => 'test', |
53
|
|
|
'joinColumn' => array( |
54
|
|
|
'name' => 'test', |
55
|
|
|
'referencedColumnName' => 'id' |
56
|
|
|
), |
57
|
|
|
), |
58
|
|
|
'_data' => array( |
59
|
|
|
'test' => array() |
60
|
|
|
) |
61
|
|
|
) |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function testCreateManyToOne() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$factory = new RelationFactory( |
72
|
|
|
$this->oneToOneRelationClass, |
73
|
|
|
$this->manyToOneRelationClass, |
74
|
|
|
$this->oneToManyRelationClass, |
75
|
|
|
$this->manyToManyRelationClass |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$this->assertInstanceOf( |
79
|
|
|
$this->classImplement, |
80
|
|
|
$factory->create( |
81
|
|
|
DataMapper::RELATION_MANY_TO_ONE, |
82
|
|
|
array( |
83
|
|
|
'_relation' => array( |
84
|
|
|
'table' => 'test', |
85
|
|
|
'targetEntity' => 'test', |
86
|
|
|
'joinColumn' => array( |
87
|
|
|
'name' => 'test', |
88
|
|
|
'referencedColumnName' => 'id' |
89
|
|
|
), |
90
|
|
|
), |
91
|
|
|
'_data' => array( |
92
|
|
|
'test' => array() |
93
|
|
|
) |
94
|
|
|
) |
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* |
101
|
|
|
*/ |
102
|
|
|
public function testCreateOneToMany() |
103
|
|
|
{ |
104
|
|
|
$factory = new RelationFactory( |
105
|
|
|
$this->oneToOneRelationClass, |
106
|
|
|
$this->manyToOneRelationClass, |
107
|
|
|
$this->oneToManyRelationClass, |
108
|
|
|
$this->manyToManyRelationClass |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->assertInstanceOf( |
112
|
|
|
$this->classImplement, |
113
|
|
|
$factory->create( |
114
|
|
|
DataMapper::RELATION_ONE_TO_MANY, |
115
|
|
|
array( |
116
|
|
|
'_relation' => array( |
117
|
|
|
'removeReferenced' => 'true', |
118
|
|
|
'table' => 'selection_lang', |
119
|
|
|
'targetEntity' => 'selection_lang', |
120
|
|
|
'joinColumn' => array( |
121
|
|
|
'referencedColumnName' => 'id', |
122
|
|
|
'name' => 'selection_id', |
123
|
|
|
), |
124
|
|
|
'references' => array( |
125
|
|
|
'lang_id' => array( |
126
|
|
|
'table' => 'lang', |
127
|
|
|
'referencedColumnName' => 'id', |
128
|
|
|
'where' => array( |
129
|
|
|
'iso_code' => 'fr' |
130
|
|
|
) |
131
|
|
|
), |
132
|
|
|
), |
133
|
|
|
), |
134
|
|
|
'_data' => array( |
135
|
|
|
'selection_lang' => array() |
136
|
|
|
) |
137
|
|
|
) |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* |
144
|
|
|
*/ |
145
|
|
|
public function testCreateManyToMany() |
146
|
|
|
{ |
147
|
|
|
$factory = new RelationFactory( |
148
|
|
|
$this->oneToOneRelationClass, |
149
|
|
|
$this->manyToOneRelationClass, |
150
|
|
|
$this->oneToManyRelationClass, |
151
|
|
|
$this->manyToManyRelationClass |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$this->assertInstanceOf( |
155
|
|
|
$this->classImplement, |
156
|
|
|
$factory->create( |
157
|
|
|
DataMapper::RELATION_MANY_TO_MANY, |
158
|
|
|
array( |
159
|
|
|
'_relation' => array( |
160
|
|
|
'targetEntity' => 'Category', |
161
|
|
|
'table' => 'category', |
162
|
|
|
'joinTable' => array( |
163
|
|
|
'name' => 'product_category', |
164
|
|
|
'joinColumn' => array( |
165
|
|
|
'name' => 'product_id', |
166
|
|
|
'referencedColumnName' => 'id' |
167
|
|
|
), |
168
|
|
|
'inverseJoinColumn' => array( |
169
|
|
|
'name' => 'category_id', |
170
|
|
|
'referencedColumnName' => 'id' |
171
|
|
|
) |
172
|
|
|
), |
173
|
|
|
), |
174
|
|
|
'_data' => array() |
175
|
|
|
) |
176
|
|
|
) |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* |
182
|
|
|
*/ |
183
|
|
|
public function testConstructAssumingException() |
184
|
|
|
{ |
185
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
186
|
|
|
new RelationFactory('stdClass', 'stdClass', 'stdClass', 'stdClass'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
public function testCreateInvalidRelationParameter() |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
195
|
|
|
$factory = new RelationFactory( |
196
|
|
|
$this->oneToOneRelationClass, |
197
|
|
|
$this->manyToOneRelationClass, |
198
|
|
|
$this->oneToManyRelationClass, |
199
|
|
|
$this->manyToManyRelationClass |
200
|
|
|
); |
201
|
|
|
$factory->create('invalid', array()); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
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.