|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Dev\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Dev\FixtureBlueprint; |
|
6
|
|
|
use SilverStripe\Dev\FixtureFactory; |
|
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
8
|
|
|
use SilverStripe\Dev\Tests\FixtureFactoryTest\DataObjectRelation; |
|
9
|
|
|
use SilverStripe\Dev\Tests\FixtureFactoryTest\TestDataObject; |
|
10
|
|
|
|
|
11
|
|
|
class FixtureFactoryTest extends SapphireTest |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
protected $usesDatabase = true; |
|
15
|
|
|
|
|
16
|
|
|
protected static $extra_dataobjects = array( |
|
17
|
|
|
TestDataObject::class, |
|
18
|
|
|
DataObjectRelation::class |
|
19
|
|
|
); |
|
20
|
|
|
|
|
21
|
|
|
public function testCreateRaw() |
|
22
|
|
|
{ |
|
23
|
|
|
$factory = new FixtureFactory(); |
|
24
|
|
|
$id = $factory->createRaw( |
|
25
|
|
|
TestDataObject::singleton()->baseTable(), |
|
26
|
|
|
'one', |
|
27
|
|
|
array('Name' => 'My Name') |
|
28
|
|
|
); |
|
29
|
|
|
$this->assertNotNull($id); |
|
30
|
|
|
$this->assertGreaterThan(0, $id); |
|
31
|
|
|
$obj = TestDataObject::get()->find('ID', $id); |
|
32
|
|
|
$this->assertNotNull($obj); |
|
33
|
|
|
$this->assertEquals('My Name', $obj->Name); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testSetId() |
|
37
|
|
|
{ |
|
38
|
|
|
$factory = new FixtureFactory(); |
|
39
|
|
|
$obj = new TestDataObject(); |
|
40
|
|
|
$obj->write(); |
|
41
|
|
|
$factory->setId(TestDataObject::class, 'one', $obj->ID); |
|
42
|
|
|
$this->assertEquals( |
|
43
|
|
|
$obj->ID, |
|
44
|
|
|
$factory->getId(TestDataObject::class, 'one') |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testGetId() |
|
49
|
|
|
{ |
|
50
|
|
|
$factory = new FixtureFactory(); |
|
51
|
|
|
$obj = $factory->createObject(TestDataObject::class, 'one'); |
|
52
|
|
|
$this->assertEquals( |
|
53
|
|
|
$obj->ID, |
|
54
|
|
|
$factory->getId(TestDataObject::class, 'one') |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testGetIds() |
|
59
|
|
|
{ |
|
60
|
|
|
$factory = new FixtureFactory(); |
|
61
|
|
|
$obj = $factory->createObject(TestDataObject::class, 'one'); |
|
62
|
|
|
$this->assertEquals( |
|
63
|
|
|
array('one' => $obj->ID), |
|
64
|
|
|
$factory->getIds(TestDataObject::class) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testDefine() |
|
69
|
|
|
{ |
|
70
|
|
|
$factory = new FixtureFactory(); |
|
71
|
|
|
$this->assertFalse($factory->getBlueprint(TestDataObject::class)); |
|
72
|
|
|
$factory->define(TestDataObject::class); |
|
73
|
|
|
$this->assertInstanceOf( |
|
74
|
|
|
FixtureBlueprint::class, |
|
75
|
|
|
$factory->getBlueprint(TestDataObject::class) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testDefineWithCustomBlueprint() |
|
80
|
|
|
{ |
|
81
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class); |
|
82
|
|
|
$factory = new FixtureFactory(); |
|
83
|
|
|
$this->assertFalse($factory->getBlueprint(TestDataObject::class)); |
|
84
|
|
|
$factory->define(TestDataObject::class, $blueprint); |
|
85
|
|
|
$this->assertInstanceOf( |
|
86
|
|
|
FixtureBlueprint::class, |
|
87
|
|
|
$factory->getBlueprint(TestDataObject::class) |
|
88
|
|
|
); |
|
89
|
|
|
$this->assertEquals( |
|
90
|
|
|
$blueprint, |
|
91
|
|
|
$factory->getBlueprint(TestDataObject::class) |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testDefineWithDefaults() |
|
96
|
|
|
{ |
|
97
|
|
|
$factory = new FixtureFactory(); |
|
98
|
|
|
$factory->define(TestDataObject::class, array('Name' => 'Default')); |
|
99
|
|
|
$obj = $factory->createObject(TestDataObject::class, 'one'); |
|
100
|
|
|
$this->assertEquals('Default', $obj->Name); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testDefineMultipleBlueprintsForClass() |
|
104
|
|
|
{ |
|
105
|
|
|
$factory = new FixtureFactory(); |
|
106
|
|
|
$factory->define( |
|
107
|
|
|
TestDataObject::class, |
|
108
|
|
|
new FixtureBlueprint(TestDataObject::class) |
|
109
|
|
|
); |
|
110
|
|
|
$factory->define( |
|
111
|
|
|
'FixtureFactoryTest_DataObjectWithDefaults', |
|
112
|
|
|
new FixtureBlueprint( |
|
113
|
|
|
'FixtureFactoryTest_DataObjectWithDefaults', |
|
114
|
|
|
TestDataObject::class, |
|
115
|
|
|
array('Name' => 'Default') |
|
116
|
|
|
) |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
$obj = $factory->createObject(TestDataObject::class, 'one'); |
|
120
|
|
|
$this->assertNull($obj->Name); |
|
121
|
|
|
|
|
122
|
|
|
$objWithDefaults = $factory->createObject('FixtureFactoryTest_DataObjectWithDefaults', 'two'); |
|
123
|
|
|
$this->assertEquals('Default', $objWithDefaults->Name); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertEquals( |
|
126
|
|
|
$obj->ID, |
|
127
|
|
|
$factory->getId(TestDataObject::class, 'one') |
|
128
|
|
|
); |
|
129
|
|
|
$this->assertEquals( |
|
130
|
|
|
$objWithDefaults->ID, |
|
131
|
|
|
$factory->getId(TestDataObject::class, 'two'), |
|
132
|
|
|
'Can access fixtures under class name, not blueprint name' |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testClear() |
|
137
|
|
|
{ |
|
138
|
|
|
$factory = new FixtureFactory(); |
|
139
|
|
|
$obj1Id = $factory->createRaw( |
|
140
|
|
|
TestDataObject::singleton()->baseTable(), |
|
141
|
|
|
'one', |
|
142
|
|
|
array('Name' => 'My Name') |
|
143
|
|
|
); |
|
144
|
|
|
$obj2 = $factory->createObject(TestDataObject::class, 'two'); |
|
145
|
|
|
|
|
146
|
|
|
$factory->clear(); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertFalse($factory->getId(TestDataObject::class, 'one')); |
|
149
|
|
|
$this->assertNull(TestDataObject::get()->byID($obj1Id)); |
|
150
|
|
|
$this->assertFalse($factory->getId(TestDataObject::class, 'two')); |
|
151
|
|
|
$this->assertNull(TestDataObject::get()->byID($obj2->ID)); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function testClearWithClass() |
|
155
|
|
|
{ |
|
156
|
|
|
$factory = new FixtureFactory(); |
|
157
|
|
|
$obj1 = $factory->createObject(TestDataObject::class, 'object-one'); |
|
158
|
|
|
$relation1 = $factory->createObject(DataObjectRelation::class, 'relation-one'); |
|
159
|
|
|
|
|
160
|
|
|
$factory->clear(TestDataObject::class); |
|
161
|
|
|
|
|
162
|
|
|
$this->assertFalse( |
|
163
|
|
|
$factory->getId(TestDataObject::class, 'one') |
|
164
|
|
|
); |
|
165
|
|
|
$this->assertNull(TestDataObject::get()->byID($obj1->ID)); |
|
166
|
|
|
$this->assertEquals( |
|
167
|
|
|
$relation1->ID, |
|
168
|
|
|
$factory->getId(DataObjectRelation::class, 'relation-one') |
|
169
|
|
|
); |
|
170
|
|
|
$this->assertInstanceOf( |
|
171
|
|
|
DataObjectRelation::class, |
|
172
|
|
|
DataObjectRelation::get()->byID($relation1->ID) |
|
173
|
|
|
); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function testGetByClassOrTable() |
|
177
|
|
|
{ |
|
178
|
|
|
$factory = new FixtureFactory(); |
|
179
|
|
|
$obj1 = $factory->createObject(TestDataObject::class, 'object-one', [ 'Name' => 'test one' ]); |
|
180
|
|
|
$this->assertInstanceOf(TestDataObject::class, $factory->get(TestDataObject::class, 'object-one')); |
|
181
|
|
|
$this->assertEquals('test one', $factory->get(TestDataObject::class, 'object-one')->Name); |
|
182
|
|
|
|
|
183
|
|
|
$obj2 = $factory->createRaw('FixtureFactoryTest_TestDataObject', 'object-two', [ 'Name' => 'test two' ]); |
|
184
|
|
|
$this->assertInstanceOf( |
|
185
|
|
|
TestDataObject::class, |
|
186
|
|
|
$factory->get('FixtureFactoryTest_TestDataObject', 'object-two') |
|
187
|
|
|
); |
|
188
|
|
|
$this->assertEquals('test two', $factory->get('FixtureFactoryTest_TestDataObject', 'object-two')->Name); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|