Completed
Push — 4.0 ( 5203dd...7a0d43 )
by Daniel
36s
created

FixtureBlueprintTest::testCreateWithoutData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Dev\Tests;
4
5
use InvalidArgumentException;
6
use SilverStripe\Dev\FixtureBlueprint;
7
use SilverStripe\Dev\FixtureFactory;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\Dev\Tests\FixtureBlueprintTest\Article;
10
use SilverStripe\Dev\Tests\FixtureBlueprintTest\TestPage;
11
use SilverStripe\Dev\Tests\FixtureBlueprintTest\TestSiteTree;
12
use SilverStripe\Dev\Tests\FixtureFactoryTest\DataObjectRelation;
13
use SilverStripe\Dev\Tests\FixtureFactoryTest\TestDataObject;
14
15
class FixtureBlueprintTest extends SapphireTest
16
{
17
18
    protected $usesDatabase = true;
19
20
    protected static $extra_dataobjects = array(
21
        TestDataObject::class,
22
        DataObjectRelation::class,
23
        TestSiteTree::class,
24
        TestPage::class,
25
    );
26
27
    public function testCreateWithRelationshipExtraFields()
28
    {
29
        $blueprint = new FixtureBlueprint(TestDataObject::class);
30
31
        $relation1 = new DataObjectRelation();
32
        $relation1->write();
33
        $relation2 = new DataObjectRelation();
34
        $relation2->write();
35
36
        // in YAML these look like
37
        // RelationName:
38
        //   - =>Relational.obj:
39
        //     ExtraFieldName: test
40
        //   - =>..
41
        $obj = $blueprint->createObject(
42
            'one',
43
            array(
44
                'ManyManyRelation' =>
45
                    array(
46
                        array(
47
                            "=>" . DataObjectRelation::class . ".relation1" => array(),
48
                            "Label" => 'This is a label for relation 1'
49
                        ),
50
                        array(
51
                            "=>" . DataObjectRelation::class . ".relation2" => array(),
52
                            "Label" => 'This is a label for relation 2'
53
                        )
54
                    )
55
            ),
56
            array(
57
                DataObjectRelation::class => array(
58
                    'relation1' => $relation1->ID,
59
                    'relation2' => $relation2->ID
60
                )
61
            )
62
        );
63
64
        $this->assertEquals(2, $obj->ManyManyRelation()->Count());
0 ignored issues
show
Bug introduced by
The method ManyManyRelation() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $this->assertEquals(2, $obj->/** @scrutinizer ignore-call */ ManyManyRelation()->Count());
Loading history...
65
        $this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation1->ID));
66
        $this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation2->ID));
67
68
        $this->assertEquals(
69
            array('Label' => 'This is a label for relation 1'),
70
            $obj->ManyManyRelation()->getExtraData('ManyManyRelation', $relation1->ID)
71
        );
72
73
        $this->assertEquals(
74
            array('Label' => 'This is a label for relation 2'),
75
            $obj->ManyManyRelation()->getExtraData('ManyManyRelation', $relation2->ID)
76
        );
77
    }
78
79
80
    public function testCreateWithoutData()
81
    {
82
        $blueprint = new FixtureBlueprint(TestDataObject::class);
83
        $obj = $blueprint->createObject('one');
84
        $this->assertNotNull($obj);
85
        $this->assertGreaterThan(0, $obj->ID);
86
        $this->assertEquals('', $obj->Name);
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverStripe\ORM\DataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
87
    }
88
89
    public function testCreateWithData()
90
    {
91
        $blueprint = new FixtureBlueprint(TestDataObject::class);
92
        $obj = $blueprint->createObject('one', array('Name' => 'My Name'));
93
        $this->assertNotNull($obj);
94
        $this->assertGreaterThan(0, $obj->ID);
95
        $this->assertEquals('My Name', $obj->Name);
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverStripe\ORM\DataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
96
    }
97
98
99
    public function testCreateWithRelationship()
100
    {
101
        $blueprint = new FixtureBlueprint(TestDataObject::class);
102
103
        $relation1 = new DataObjectRelation();
104
        $relation1->write();
105
        $relation2 = new DataObjectRelation();
106
        $relation2->write();
107
108
        $obj = $blueprint->createObject(
109
            'one',
110
            array(
111
                'ManyManyRelation' =>
112
                    '=>' . DataObjectRelation::class . '.relation1,' . '=>' . DataObjectRelation::class . '.relation2'
113
            ),
114
            array(
115
                DataObjectRelation::class => array(
116
                    'relation1' => $relation1->ID,
117
                    'relation2' => $relation2->ID
118
                )
119
            )
120
        );
121
122
        $this->assertEquals(2, $obj->ManyManyRelation()->Count());
123
        $this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation1->ID));
124
        $this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation2->ID));
125
126
        $obj2 = $blueprint->createObject(
127
            'two',
128
            array(
129
                // Note; using array format here, not comma separated
130
                'HasManyRelation' => array(
131
                    '=>' . DataObjectRelation::class . '.relation1',
132
                    '=>' . DataObjectRelation::class . '.relation2'
133
                )
134
            ),
135
            array(
136
                DataObjectRelation::class => array(
137
                    'relation1' => $relation1->ID,
138
                    'relation2' => $relation2->ID
139
                )
140
            )
141
        );
142
        $this->assertEquals(2, $obj2->HasManyRelation()->Count());
0 ignored issues
show
Bug introduced by
The method HasManyRelation() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
        $this->assertEquals(2, $obj2->/** @scrutinizer ignore-call */ HasManyRelation()->Count());
Loading history...
143
        $this->assertNotNull($obj2->HasManyRelation()->find('ID', $relation1->ID));
144
        $this->assertNotNull($obj2->HasManyRelation()->find('ID', $relation2->ID));
145
    }
146
147
    /**
148
     * @expectedException InvalidArgumentException
149
     * @expectedExceptionMessage No fixture definitions found
150
     */
151
    public function testCreateWithInvalidRelationName()
152
    {
153
        $blueprint = new FixtureBlueprint(TestDataObject::class);
154
155
        $obj = $blueprint->createObject(
0 ignored issues
show
Unused Code introduced by
The assignment to $obj is dead and can be removed.
Loading history...
156
            'one',
157
            array(
158
                'ManyManyRelation' => '=>UnknownClass.relation1'
159
            ),
160
            array(
161
                DataObjectRelation::class => array(
162
                    'relation1' => 99
163
                )
164
            )
165
        );
166
    }
167
168
    /**
169
     * @expectedException InvalidArgumentException
170
     * @expectedExceptionMessage No fixture definitions found
171
     */
172
    public function testCreateWithInvalidRelationIdentifier()
173
    {
174
        $blueprint = new FixtureBlueprint(TestDataObject::class);
175
176
        $obj = $blueprint->createObject(
0 ignored issues
show
Unused Code introduced by
The assignment to $obj is dead and can be removed.
Loading history...
177
            'one',
178
            array(
179
                'ManyManyRelation' => '=>' . DataObjectRelation::class . '.unknown_identifier'
180
            ),
181
            array(
182
                DataObjectRelation::class => array(
183
                    'relation1' => 99
184
                )
185
            )
186
        );
187
    }
188
189
    /**
190
     * @expectedException InvalidArgumentException
191
     * @expectedExceptionMessage Invalid format
192
     */
193
    public function testCreateWithInvalidRelationFormat()
194
    {
195
        $factory = new FixtureFactory();
0 ignored issues
show
Unused Code introduced by
The assignment to $factory is dead and can be removed.
Loading history...
196
        $blueprint = new FixtureBlueprint(TestDataObject::class);
197
198
        $relation1 = new DataObjectRelation();
199
        $relation1->write();
200
201
        $obj = $blueprint->createObject(
0 ignored issues
show
Unused Code introduced by
The assignment to $obj is dead and can be removed.
Loading history...
202
            'one',
203
            array(
204
                'ManyManyRelation' => DataObjectRelation::class . '.relation1'
205
            ),
206
            array(
207
                DataObjectRelation::class => array(
208
                    'relation1' => $relation1->ID
209
                )
210
            )
211
        );
212
    }
213
214
    public function testCreateWithId()
215
    {
216
        $blueprint = new FixtureBlueprint(TestDataObject::class);
217
        $obj = $blueprint->createObject('ninetynine', array('ID' => 99));
218
        $this->assertNotNull($obj);
219
        $this->assertEquals(99, $obj->ID);
220
    }
221
222
    public function testCreateWithLastEdited()
223
    {
224
        $extpectedDate = '2010-12-14 16:18:20';
225
        $blueprint = new FixtureBlueprint(TestDataObject::class);
226
        $obj = $blueprint->createObject('lastedited', array('LastEdited' => $extpectedDate));
227
        $this->assertNotNull($obj);
228
        $this->assertEquals($extpectedDate, $obj->LastEdited);
229
230
        $obj = TestDataObject::get()->byID($obj->ID);
231
        $this->assertEquals($extpectedDate, $obj->LastEdited);
232
    }
233
234
    public function testCreateWithClassAncestry()
235
    {
236
        $data = array(
237
            'Title' => 'My Title',
238
            'Created' => '2010-12-14 16:18:20',
239
            'LastEdited' => '2010-12-14 16:18:20',
240
            'PublishDate' => '2015-12-09 06:03:00'
241
        );
242
        $blueprint = new FixtureBlueprint(Article::class);
243
        $obj = $blueprint->createObject('home', $data);
244
        $this->assertNotNull($obj);
245
        $this->assertEquals($data['Title'], $obj->Title);
0 ignored issues
show
Bug Best Practice introduced by
The property Title does not exist on SilverStripe\ORM\DataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
246
        $this->assertEquals($data['Created'], $obj->Created);
247
        $this->assertEquals($data['LastEdited'], $obj->LastEdited);
248
        $this->assertEquals($data['PublishDate'], $obj->PublishDate);
0 ignored issues
show
Bug Best Practice introduced by
The property PublishDate does not exist on SilverStripe\ORM\DataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
249
250
        $obj = Article::get()->byID($obj->ID);
251
        $this->assertNotNull($obj);
252
        $this->assertEquals($data['Title'], $obj->Title);
253
        $this->assertEquals($data['Created'], $obj->Created);
254
        $this->assertEquals($data['LastEdited'], $obj->LastEdited);
255
        $this->assertEquals($data['PublishDate'], $obj->PublishDate);
256
    }
257
258
    public function testCallbackOnBeforeCreate()
259
    {
260
        $blueprint = new FixtureBlueprint(TestDataObject::class);
261
        $this->_called = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property _called does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
262
        $self = $this;
263
        $cb = function ($identifier, $data, $fixtures) use ($self) {
0 ignored issues
show
Unused Code introduced by
The parameter $fixtures is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

263
        $cb = function ($identifier, $data, /** @scrutinizer ignore-unused */ $fixtures) use ($self) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $identifier is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

263
        $cb = function (/** @scrutinizer ignore-unused */ $identifier, $data, $fixtures) use ($self) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

263
        $cb = function ($identifier, /** @scrutinizer ignore-unused */ $data, $fixtures) use ($self) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
264
            $self->_called = $self->_called + 1;
0 ignored issues
show
Bug Best Practice introduced by
The property _called does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
265
        };
266
        $blueprint->addCallback('beforeCreate', $cb);
267
        $this->assertEquals(0, $this->_called);
268
        $obj1 = $blueprint->createObject('one');
0 ignored issues
show
Unused Code introduced by
The assignment to $obj1 is dead and can be removed.
Loading history...
269
        $this->assertEquals(1, $this->_called);
270
        $obj2 = $blueprint->createObject('two');
0 ignored issues
show
Unused Code introduced by
The assignment to $obj2 is dead and can be removed.
Loading history...
271
        $this->assertEquals(2, $this->_called);
272
273
        $this->_called = 0;
274
    }
275
276
    public function testCallbackOnAfterCreate()
277
    {
278
        $blueprint = new FixtureBlueprint(TestDataObject::class);
279
        $this->_called = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property _called does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
280
        $self = $this;
281
        $cb = function ($obj, $identifier, $data, $fixtures) use ($self) {
0 ignored issues
show
Unused Code introduced by
The parameter $obj is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

281
        $cb = function (/** @scrutinizer ignore-unused */ $obj, $identifier, $data, $fixtures) use ($self) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

281
        $cb = function ($obj, $identifier, /** @scrutinizer ignore-unused */ $data, $fixtures) use ($self) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fixtures is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

281
        $cb = function ($obj, $identifier, $data, /** @scrutinizer ignore-unused */ $fixtures) use ($self) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $identifier is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

281
        $cb = function ($obj, /** @scrutinizer ignore-unused */ $identifier, $data, $fixtures) use ($self) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
282
            $self->_called = $self->_called + 1;
0 ignored issues
show
Bug Best Practice introduced by
The property _called does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
283
        };
284
        $blueprint->addCallback('afterCreate', $cb);
285
        $this->assertEquals(0, $this->_called);
286
        $obj1 = $blueprint->createObject('one');
0 ignored issues
show
Unused Code introduced by
The assignment to $obj1 is dead and can be removed.
Loading history...
287
        $this->assertEquals(1, $this->_called);
288
        $obj2 = $blueprint->createObject('two');
0 ignored issues
show
Unused Code introduced by
The assignment to $obj2 is dead and can be removed.
Loading history...
289
        $this->assertEquals(2, $this->_called);
290
291
        $this->_called = 0;
292
    }
293
294
    public function testDefineWithDefaultCustomSetters()
295
    {
296
        $blueprint = new FixtureBlueprint(
297
            TestDataObject::class,
298
            null,
299
            array(
300
            'Name' => function ($obj, $data, $fixtures) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

300
            'Name' => function ($obj, /** @scrutinizer ignore-unused */ $data, $fixtures) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $obj is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

300
            'Name' => function (/** @scrutinizer ignore-unused */ $obj, $data, $fixtures) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fixtures is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

300
            'Name' => function ($obj, $data, /** @scrutinizer ignore-unused */ $fixtures) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
301
                return 'Default Name';
302
            }
303
            )
304
        );
305
306
        $obj1 = $blueprint->createObject('one');
307
        $this->assertEquals('Default Name', $obj1->Name);
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverStripe\ORM\DataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
308
309
        $obj2 = $blueprint->createObject('one', array('Name' => 'Override Name'));
310
        $this->assertEquals('Override Name', $obj2->Name);
311
    }
312
}
313