1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SilverStripe\ORM\DataObject; |
4
|
|
|
/** |
5
|
|
|
* @package framework |
6
|
|
|
* @subpackage tests |
7
|
|
|
*/ |
8
|
|
|
class FixtureBlueprintTest extends SapphireTest { |
9
|
|
|
|
10
|
|
|
protected $usesDatabase = true; |
11
|
|
|
|
12
|
|
|
protected $extraDataObjects = array( |
13
|
|
|
'FixtureFactoryTest_DataObject', |
14
|
|
|
'FixtureFactoryTest_DataObjectRelation', |
15
|
|
|
'FixtureBlueprintTest_SiteTree', |
16
|
|
|
'FixtureBlueprintTest_Page' |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
public function testCreateWithRelationshipExtraFields() { |
20
|
|
|
$blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); |
21
|
|
|
|
22
|
|
|
$relation1 = new FixtureFactoryTest_DataObjectRelation(); |
23
|
|
|
$relation1->write(); |
24
|
|
|
$relation2 = new FixtureFactoryTest_DataObjectRelation(); |
25
|
|
|
$relation2->write(); |
26
|
|
|
|
27
|
|
|
// in YAML these look like |
28
|
|
|
// RelationName: |
29
|
|
|
// - =>Relational.obj: |
30
|
|
|
// ExtraFieldName: test |
31
|
|
|
// - =>.. |
32
|
|
|
$obj = $blueprint->createObject( |
33
|
|
|
'one', |
34
|
|
|
array( |
35
|
|
|
'ManyManyRelation' => |
36
|
|
|
array( |
37
|
|
|
array( |
38
|
|
|
"=>FixtureFactoryTest_DataObjectRelation.relation1" => array(), |
39
|
|
|
"Label" => 'This is a label for relation 1' |
40
|
|
|
), |
41
|
|
|
array( |
42
|
|
|
"=>FixtureFactoryTest_DataObjectRelation.relation2" => array(), |
43
|
|
|
"Label" => 'This is a label for relation 2' |
44
|
|
|
) |
45
|
|
|
) |
46
|
|
|
), |
47
|
|
|
array( |
48
|
|
|
'FixtureFactoryTest_DataObjectRelation' => array( |
49
|
|
|
'relation1' => $relation1->ID, |
50
|
|
|
'relation2' => $relation2->ID |
51
|
|
|
) |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$this->assertEquals(2, $obj->ManyManyRelation()->Count()); |
|
|
|
|
56
|
|
|
$this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation1->ID)); |
|
|
|
|
57
|
|
|
$this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation2->ID)); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->assertEquals( |
60
|
|
|
array('Label' => 'This is a label for relation 1'), |
61
|
|
|
$obj->ManyManyRelation()->getExtraData('ManyManyRelation', $relation1->ID) |
|
|
|
|
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$this->assertEquals( |
65
|
|
|
array('Label' => 'This is a label for relation 2'), |
66
|
|
|
$obj->ManyManyRelation()->getExtraData('ManyManyRelation', $relation2->ID) |
|
|
|
|
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
public function testCreateWithoutData() { |
72
|
|
|
$blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); |
73
|
|
|
$obj = $blueprint->createObject('one'); |
74
|
|
|
$this->assertNotNull($obj); |
75
|
|
|
$this->assertGreaterThan(0, $obj->ID); |
76
|
|
|
$this->assertEquals('', $obj->Name); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testCreateWithData() { |
80
|
|
|
$blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); |
81
|
|
|
$obj = $blueprint->createObject('one', array('Name' => 'My Name')); |
82
|
|
|
$this->assertNotNull($obj); |
83
|
|
|
$this->assertGreaterThan(0, $obj->ID); |
84
|
|
|
$this->assertEquals('My Name', $obj->Name); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
public function testCreateWithRelationship() { |
89
|
|
|
$blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); |
90
|
|
|
|
91
|
|
|
$relation1 = new FixtureFactoryTest_DataObjectRelation(); |
92
|
|
|
$relation1->write(); |
93
|
|
|
$relation2 = new FixtureFactoryTest_DataObjectRelation(); |
94
|
|
|
$relation2->write(); |
95
|
|
|
|
96
|
|
|
$obj = $blueprint->createObject( |
97
|
|
|
'one', |
98
|
|
|
array( |
99
|
|
|
'ManyManyRelation' => |
100
|
|
|
'=>FixtureFactoryTest_DataObjectRelation.relation1,' . |
101
|
|
|
'=>FixtureFactoryTest_DataObjectRelation.relation2' |
102
|
|
|
), |
103
|
|
|
array( |
104
|
|
|
'FixtureFactoryTest_DataObjectRelation' => array( |
105
|
|
|
'relation1' => $relation1->ID, |
106
|
|
|
'relation2' => $relation2->ID |
107
|
|
|
) |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->assertEquals(2, $obj->ManyManyRelation()->Count()); |
|
|
|
|
112
|
|
|
$this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation1->ID)); |
|
|
|
|
113
|
|
|
$this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation2->ID)); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$obj2 = $blueprint->createObject( |
116
|
|
|
'two', |
117
|
|
|
array( |
118
|
|
|
// Note; using array format here, not comma separated |
119
|
|
|
'HasManyRelation' => array( |
120
|
|
|
'=>FixtureFactoryTest_DataObjectRelation.relation1', |
121
|
|
|
'=>FixtureFactoryTest_DataObjectRelation.relation2' |
122
|
|
|
) |
123
|
|
|
), |
124
|
|
|
array( |
125
|
|
|
'FixtureFactoryTest_DataObjectRelation' => array( |
126
|
|
|
'relation1' => $relation1->ID, |
127
|
|
|
'relation2' => $relation2->ID |
128
|
|
|
) |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
$this->assertEquals(2, $obj2->HasManyRelation()->Count()); |
132
|
|
|
$this->assertNotNull($obj2->HasManyRelation()->find('ID', $relation1->ID)); |
133
|
|
|
$this->assertNotNull($obj2->HasManyRelation()->find('ID', $relation2->ID)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @expectedException InvalidArgumentException |
138
|
|
|
* @expectedExceptionMessage No fixture definitions found |
139
|
|
|
*/ |
140
|
|
|
public function testCreateWithInvalidRelationName() { |
141
|
|
|
$blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); |
142
|
|
|
|
143
|
|
|
$obj = $blueprint->createObject( |
144
|
|
|
'one', |
145
|
|
|
array( |
146
|
|
|
'ManyManyRelation' => '=>UnknownClass.relation1' |
147
|
|
|
), |
148
|
|
|
array( |
149
|
|
|
'FixtureFactoryTest_DataObjectRelation' => array( |
150
|
|
|
'relation1' => 99 |
151
|
|
|
) |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @expectedException InvalidArgumentException |
158
|
|
|
* @expectedExceptionMessage No fixture definitions found |
159
|
|
|
*/ |
160
|
|
|
public function testCreateWithInvalidRelationIdentifier() { |
161
|
|
|
$blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); |
162
|
|
|
|
163
|
|
|
$obj = $blueprint->createObject( |
164
|
|
|
'one', |
165
|
|
|
array( |
166
|
|
|
'ManyManyRelation' => '=>FixtureFactoryTest_DataObjectRelation.unknown_identifier' |
167
|
|
|
), |
168
|
|
|
array( |
169
|
|
|
'FixtureFactoryTest_DataObjectRelation' => array( |
170
|
|
|
'relation1' => 99 |
171
|
|
|
) |
172
|
|
|
) |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @expectedException InvalidArgumentException |
178
|
|
|
* @expectedExceptionMessage Invalid format |
179
|
|
|
*/ |
180
|
|
|
public function testCreateWithInvalidRelationFormat() { |
181
|
|
|
$factory = new FixtureFactory(); |
182
|
|
|
$blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); |
183
|
|
|
|
184
|
|
|
$relation1 = new FixtureFactoryTest_DataObjectRelation(); |
185
|
|
|
$relation1->write(); |
186
|
|
|
|
187
|
|
|
$obj = $blueprint->createObject( |
188
|
|
|
'one', |
189
|
|
|
array( |
190
|
|
|
'ManyManyRelation' => 'FixtureFactoryTest_DataObjectRelation.relation1' |
191
|
|
|
), |
192
|
|
|
array( |
193
|
|
|
'FixtureFactoryTest_DataObjectRelation' => array( |
194
|
|
|
'relation1' => $relation1->ID |
195
|
|
|
) |
196
|
|
|
) |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testCreateWithId() { |
201
|
|
|
$blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); |
202
|
|
|
$obj = $blueprint->createObject('ninetynine', array('ID' => 99)); |
203
|
|
|
$this->assertNotNull($obj); |
204
|
|
|
$this->assertEquals(99, $obj->ID); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testCreateWithLastEdited() { |
208
|
|
|
$extpectedDate = '2010-12-14 16:18:20'; |
209
|
|
|
$blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); |
210
|
|
|
$obj = $blueprint->createObject('lastedited', array('LastEdited' => $extpectedDate)); |
211
|
|
|
$this->assertNotNull($obj); |
212
|
|
|
$this->assertEquals($extpectedDate, $obj->LastEdited); |
213
|
|
|
|
214
|
|
|
$obj = FixtureFactoryTest_DataObject::get()->byID($obj->ID); |
215
|
|
|
$this->assertEquals($extpectedDate, $obj->LastEdited); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testCreateWithClassAncestry() { |
219
|
|
|
$data = array( |
220
|
|
|
'Title' => 'My Title', |
221
|
|
|
'Created' => '2010-12-14 16:18:20', |
222
|
|
|
'LastEdited' => '2010-12-14 16:18:20', |
223
|
|
|
'PublishDate' => '2015-12-09 06:03:00' |
224
|
|
|
); |
225
|
|
|
$blueprint = new FixtureBlueprint('FixtureBlueprintTest_Article'); |
226
|
|
|
$obj = $blueprint->createObject('home', $data); |
227
|
|
|
$this->assertNotNull($obj); |
228
|
|
|
$this->assertEquals($data['Title'], $obj->Title); |
229
|
|
|
$this->assertEquals($data['Created'], $obj->Created); |
230
|
|
|
$this->assertEquals($data['LastEdited'], $obj->LastEdited); |
231
|
|
|
$this->assertEquals($data['PublishDate'], $obj->PublishDate); |
232
|
|
|
|
233
|
|
|
$obj = FixtureBlueprintTest_Article::get()->byID($obj->ID); |
234
|
|
|
$this->assertNotNull($obj); |
235
|
|
|
$this->assertEquals($data['Title'], $obj->Title); |
236
|
|
|
$this->assertEquals($data['Created'], $obj->Created); |
237
|
|
|
$this->assertEquals($data['LastEdited'], $obj->LastEdited); |
238
|
|
|
$this->assertEquals($data['PublishDate'], $obj->PublishDate); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function testCallbackOnBeforeCreate() { |
242
|
|
|
$blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); |
243
|
|
|
$this->_called = 0; |
|
|
|
|
244
|
|
|
$self = $this; |
245
|
|
|
$cb = function($identifier, $data, $fixtures) use($self) { |
|
|
|
|
246
|
|
|
$self->_called = $self->_called + 1; |
247
|
|
|
}; |
248
|
|
|
$blueprint->addCallback('beforeCreate', $cb); |
249
|
|
|
$this->assertEquals(0, $this->_called); |
250
|
|
|
$obj1 = $blueprint->createObject('one'); |
251
|
|
|
$this->assertEquals(1, $this->_called); |
252
|
|
|
$obj2 = $blueprint->createObject('two'); |
253
|
|
|
$this->assertEquals(2, $this->_called); |
254
|
|
|
|
255
|
|
|
$this->_called = 0; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testCallbackOnAfterCreate() { |
259
|
|
|
$blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject'); |
260
|
|
|
$this->_called = 0; |
261
|
|
|
$self = $this; |
262
|
|
|
$cb = function($obj, $identifier, $data, $fixtures) use($self) { |
|
|
|
|
263
|
|
|
$self->_called = $self->_called + 1; |
264
|
|
|
}; |
265
|
|
|
$blueprint->addCallback('afterCreate', $cb); |
266
|
|
|
$this->assertEquals(0, $this->_called); |
267
|
|
|
$obj1 = $blueprint->createObject('one'); |
268
|
|
|
$this->assertEquals(1, $this->_called); |
269
|
|
|
$obj2 = $blueprint->createObject('two'); |
270
|
|
|
$this->assertEquals(2, $this->_called); |
271
|
|
|
|
272
|
|
|
$this->_called = 0; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function testDefineWithDefaultCustomSetters() { |
276
|
|
|
$blueprint = new FixtureBlueprint( |
277
|
|
|
'FixtureFactoryTest_DataObject', |
278
|
|
|
null, |
279
|
|
|
array( |
280
|
|
|
'Name' => function($obj, $data, $fixtures) { |
|
|
|
|
281
|
|
|
return 'Default Name'; |
282
|
|
|
} |
283
|
|
|
)); |
284
|
|
|
|
285
|
|
|
$obj1 = $blueprint->createObject('one'); |
286
|
|
|
$this->assertEquals('Default Name', $obj1->Name); |
287
|
|
|
|
288
|
|
|
$obj2 = $blueprint->createObject('one', array('Name' => 'Override Name')); |
289
|
|
|
$this->assertEquals('Override Name', $obj2->Name); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @package framework |
296
|
|
|
* @subpackage tests |
297
|
|
|
*/ |
298
|
|
|
class FixtureBlueprintTest_SiteTree extends DataObject implements TestOnly { |
299
|
|
|
|
300
|
|
|
private static $db = array( |
301
|
|
|
"Title" => "Varchar" |
302
|
|
|
); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @package framework |
307
|
|
|
* @subpackage tests |
308
|
|
|
*/ |
309
|
|
|
class FixtureBlueprintTest_Page extends FixtureBlueprintTest_SiteTree { |
310
|
|
|
|
311
|
|
|
private static $db = array( |
312
|
|
|
'PublishDate' => 'Datetime' |
313
|
|
|
); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @package framework |
318
|
|
|
* @subpackage tests |
319
|
|
|
*/ |
320
|
|
|
class FixtureBlueprintTest_Article extends FixtureBlueprintTest_Page { |
321
|
|
|
} |
322
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.