Completed
Push — 4.0 ( b59aea...80f83b )
by Loz
52s queued 21s
created

testDuplicateManyManyClasses()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 90
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 60
nc 1
nop 0
dl 0
loc 90
rs 8.8727
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\ORM\Tests;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\ORM\FieldType\DBDatetime;
8
9
class DataObjectDuplicationTest extends SapphireTest
10
{
11
12
    protected $usesDatabase = true;
13
14
    protected static $extra_dataobjects = array(
15
        DataObjectDuplicationTest\Class1::class,
16
        DataObjectDuplicationTest\Class2::class,
17
        DataObjectDuplicationTest\Class3::class,
18
        DataObjectDuplicationTest\Class4::class,
19
    );
20
21
    public function testDuplicate()
22
    {
23
        DBDatetime::set_mock_now('2016-01-01 01:01:01');
24
        $orig = new DataObjectDuplicationTest\Class1();
25
        $orig->text = 'foo';
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class1. Since you implemented __set, consider adding a @property annotation.
Loading history...
26
        $orig->write();
27
        DBDatetime::set_mock_now('2016-01-02 01:01:01');
28
        $duplicate = $orig->duplicate();
29
        $this->assertInstanceOf(
30
            DataObjectDuplicationTest\Class1::class,
31
            $duplicate,
32
            'Creates the correct type'
33
        );
34
        $this->assertNotEquals(
35
            $duplicate->ID,
36
            $orig->ID,
37
            'Creates a unique record'
38
        );
39
        $this->assertEquals(
40
            'foo',
41
            $duplicate->text,
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class1. Since you implemented __get, consider adding a @property annotation.
Loading history...
42
            'Copies fields'
43
        );
44
        $this->assertEquals(
45
            2,
46
            DataObjectDuplicationTest\Class1::get()->Count(),
47
            'Only creates a single duplicate'
48
        );
49
        $this->assertEquals(DBDatetime::now()->Nice(), $duplicate->dbObject('Created')->Nice());
0 ignored issues
show
Bug introduced by
The method Nice() does not exist on SilverStripe\ORM\FieldType\DBField. 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

49
        $this->assertEquals(DBDatetime::now()->Nice(), $duplicate->dbObject('Created')->/** @scrutinizer ignore-call */ Nice());
Loading history...
50
        $this->assertNotEquals($orig->dbObject('Created')->Nice(), $duplicate->dbObject('Created')->Nice());
51
    }
52
53
    public function testDuplicateHasOne()
54
    {
55
        $relationObj = new DataObjectDuplicationTest\Class1();
56
        $relationObj->text = 'class1';
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class1. Since you implemented __set, consider adding a @property annotation.
Loading history...
57
        $relationObj->write();
58
59
        $orig = new DataObjectDuplicationTest\Class2();
60
        $orig->text = 'class2';
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class2. Since you implemented __set, consider adding a @property annotation.
Loading history...
61
        $orig->oneID = $relationObj->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property oneID does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class2. Since you implemented __set, consider adding a @property annotation.
Loading history...
62
        $orig->write();
63
64
        $duplicate = $orig->duplicate();
65
        $this->assertEquals(
66
            $relationObj->ID,
67
            $duplicate->oneID,
0 ignored issues
show
Bug Best Practice introduced by
The property oneID does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class2. Since you implemented __get, consider adding a @property annotation.
Loading history...
68
            'Copies has_one relationship'
69
        );
70
        $this->assertEquals(
71
            2,
72
            DataObjectDuplicationTest\Class2::get()->Count(),
73
            'Only creates a single duplicate'
74
        );
75
        $this->assertEquals(
76
            1,
77
            DataObjectDuplicationTest\Class1::get()->Count(),
78
            'Does not create duplicate of has_one relationship'
79
        );
80
    }
81
82
83
    public function testDuplicateManyManyClasses()
84
    {
85
        //create new test classes below
86
        $one = new DataObjectDuplicationTest\Class1();
87
        $two = new DataObjectDuplicationTest\Class2();
88
        $three = new DataObjectDuplicationTest\Class3();
89
90
        //set some simple fields
91
        $text1 = "Test Text 1";
92
        $text2 = "Test Text 2";
93
        $text3 = "Test Text 3";
94
        $one->text = $text1;
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class1. Since you implemented __set, consider adding a @property annotation.
Loading history...
95
        $two->text = $text2;
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class2. Since you implemented __set, consider adding a @property annotation.
Loading history...
96
        $three->text = $text3;
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class3. Since you implemented __set, consider adding a @property annotation.
Loading history...
97
98
        //write the to DB
99
        $one->write();
100
        $two->write();
101
        $three->write();
102
103
        //create relations
104
        $one->twos()->add($two);
0 ignored issues
show
Bug introduced by
The method twos() does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class1. 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

104
        $one->/** @scrutinizer ignore-call */ 
105
              twos()->add($two);
Loading history...
105
        $one->threes()->add($three, array('TestExtra'=>'three'));
0 ignored issues
show
Bug introduced by
The method threes() does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class1. 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

105
        $one->/** @scrutinizer ignore-call */ 
106
              threes()->add($three, array('TestExtra'=>'three'));
Loading history...
106
107
        $one = DataObject::get_by_id(DataObjectDuplicationTest\Class1::class, $one->ID);
108
        $two = DataObject::get_by_id(DataObjectDuplicationTest\Class2::class, $two->ID);
109
        $three = DataObject::get_by_id(DataObjectDuplicationTest\Class3::class, $three->ID);
110
111
        $this->assertCount(1, $one->twos());
0 ignored issues
show
Bug introduced by
The method twos() 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

111
        $this->assertCount(1, $one->/** @scrutinizer ignore-call */ twos());
Loading history...
112
        $this->assertCount(1, $one->threes());
0 ignored issues
show
Bug introduced by
The method threes() 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

112
        $this->assertCount(1, $one->/** @scrutinizer ignore-call */ threes());
Loading history...
113
        $this->assertCount(1, $three->ones());
0 ignored issues
show
Bug introduced by
The method ones() 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

113
        $this->assertCount(1, $three->/** @scrutinizer ignore-call */ ones());
Loading history...
114
115
        //test duplication
116
        $oneCopy = $one->duplicate(true, true);
117
        $twoCopy = $two->duplicate(true, true);
118
        $threeCopy = $three->duplicate(true, true);
119
120
        $oneCopy = DataObject::get_by_id(DataObjectDuplicationTest\Class1::class, $oneCopy->ID);
121
        $twoCopy = DataObject::get_by_id(DataObjectDuplicationTest\Class2::class, $twoCopy->ID);
122
        $threeCopy = DataObject::get_by_id(DataObjectDuplicationTest\Class3::class, $threeCopy->ID);
123
124
        $this->assertNotNull($oneCopy, "Copy of 1 exists");
125
        $this->assertNotNull($twoCopy, "Copy of 2 exists");
126
        $this->assertNotNull($threeCopy, "Copy of 3 exists");
127
128
        $this->assertEquals($text1, $oneCopy->text);
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist on SilverStripe\ORM\DataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
129
        $this->assertEquals($text2, $twoCopy->text);
130
        $this->assertEquals($text3, $threeCopy->text);
131
132
        $this->assertCount(
133
            0,
134
            $oneCopy->twos(),
135
            "Many-to-one relation not copied (has_many)"
136
        );
137
        $this->assertCount(
138
            2,
139
            $oneCopy->threes(),
140
            "Object has the correct number of relations"
141
        );
142
        $this->assertCount(
143
            2,
144
            $threeCopy->ones(),
145
            "Object has the correct number of relations"
146
        );
147
148
        $this->assertEquals(
149
            $one->ID,
150
            $twoCopy->one()->ID,
0 ignored issues
show
Bug introduced by
The method one() 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

150
            $twoCopy->/** @scrutinizer ignore-call */ 
151
                      one()->ID,
Loading history...
151
            "Match between relation of copy and the original"
152
        );
153
        $this->assertCount(
154
            0,
155
            $oneCopy->twos(),
156
            "Many-to-one relation not copied (has_many)"
157
        );
158
        $this->assertContains(
159
            $three->ID,
160
            $oneCopy->threes()->column('ID'),
0 ignored issues
show
Bug introduced by
The method column() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as SilverStripe\ORM\SS_List or SilverStripe\View\ViewableData or SilverStripe\ORM\Connect\Query or SQLiteResult. ( Ignorable by Annotation )

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

160
            $oneCopy->threes()->/** @scrutinizer ignore-call */ column('ID'),
Loading history...
Bug introduced by
The method column() does not exist on Countable. It seems like you code against a sub-type of Countable such as SilverStripe\ORM\SS_List or SQLiteResult. ( Ignorable by Annotation )

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

160
            $oneCopy->threes()->/** @scrutinizer ignore-call */ column('ID'),
Loading history...
161
            "Match between relation of copy and the original"
162
        );
163
        $this->assertContains(
164
            $one->ID,
165
            $threeCopy->ones()->column('ID'),
166
            "Match between relation of copy and the original"
167
        );
168
169
        $this->assertEquals(
170
            'three',
171
            $oneCopy->threes()->byID($three->ID)->TestExtra,
0 ignored issues
show
Bug introduced by
The method byID() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as SilverStripe\View\ViewableData or SilverStripe\ORM\Filterable or SilverStripe\ORM\Relation or SilverStripe\ORM\ListDecorator or SilverStripe\ORM\DataList or SilverStripe\ORM\ArrayList or SilverStripe\ORM\Relation or SilverStripe\ORM\ListDecorator or SilverStripe\ORM\DataList or SilverStripe\ORM\ArrayList or SilverStripe\ORM\Relation or SilverStripe\ORM\ListDecorator or SilverStripe\ORM\DataList or SilverStripe\ORM\ArrayList. ( Ignorable by Annotation )

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

171
            $oneCopy->threes()->/** @scrutinizer ignore-call */ byID($three->ID)->TestExtra,
Loading history...
Bug introduced by
The method byID() does not exist on Countable. It seems like you code against a sub-type of Countable such as SilverStripe\ORM\Filterable or SilverStripe\ORM\Relation or SilverStripe\ORM\ListDecorator or SilverStripe\ORM\DataList or SilverStripe\ORM\ArrayList or SilverStripe\ORM\Relation or SilverStripe\ORM\ListDecorator or SilverStripe\ORM\DataList or SilverStripe\ORM\ArrayList or SilverStripe\ORM\Relation or SilverStripe\ORM\ListDecorator or SilverStripe\ORM\DataList or SilverStripe\ORM\ArrayList. ( Ignorable by Annotation )

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

171
            $oneCopy->threes()->/** @scrutinizer ignore-call */ byID($three->ID)->TestExtra,
Loading history...
172
            "Match between extra field of copy and the original"
173
        );
174
    }
175
176
    public function testDuplicateManyManyFiltered()
177
    {
178
        $parent = new DataObjectDuplicationTest\Class4();
179
        $parent->Title = 'Parent';
0 ignored issues
show
Bug Best Practice introduced by
The property Title does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class4. Since you implemented __set, consider adding a @property annotation.
Loading history...
180
        $parent->write();
181
182
        $child = new DataObjectDuplicationTest\Class4();
183
        $child->Title = 'Child';
184
        $child->write();
185
186
        $grandChild = new DataObjectDuplicationTest\Class4();
187
        $grandChild->Title = 'GrandChild';
188
        $grandChild->write();
189
190
        $parent->Children()->add($child);
191
        $child->Children()->add($grandChild);
192
193
        // Duplcating $child should only duplicate grandchild
194
        $childDuplicate = $child->duplicate(true, 'many_many');
195
        $this->assertEquals(0, $childDuplicate->Parents()->count());
196
        $this->assertListEquals(
197
            [['Title' => 'GrandChild']],
198
            $childDuplicate->Children()
199
        );
200
201
        // Duplicate belongs_many_many only
202
        $belongsDuplicate = $child->duplicate(true, 'belongs_many_many');
203
        $this->assertEquals(0, $belongsDuplicate->Children()->count());
204
        $this->assertListEquals(
205
            [['Title' => 'Parent']],
206
            $belongsDuplicate->Parents()
207
        );
208
209
        // Duplicate all
210
        $allDuplicate = $child->duplicate(true, true);
211
        $this->assertListEquals(
212
            [['Title' => 'Parent']],
213
            $allDuplicate->Parents()
214
        );
215
        $this->assertListEquals(
216
            [['Title' => 'GrandChild']],
217
            $allDuplicate->Children()
218
        );
219
    }
220
221
    /**
222
     * Test duplication of UnsavedRelations
223
     */
224
    public function testDuplicateUnsaved()
225
    {
226
        $one = new DataObjectDuplicationTest\Class1();
227
        $one->text = "Test Text 1";
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class1. Since you implemented __set, consider adding a @property annotation.
Loading history...
228
        $three = new DataObjectDuplicationTest\Class3();
229
        $three->text = "Test Text 3";
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class3. Since you implemented __set, consider adding a @property annotation.
Loading history...
230
        $one->threes()->add($three);
231
        $this->assertListEquals(
232
            [['text' => 'Test Text 3']],
233
            $one->threes()
234
        );
235
        // Test duplicate
236
        $dupe = $one->duplicate(false, true);
237
        $this->assertEquals('Test Text 1', $dupe->text);
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist on SilverStripe\ORM\Tests\D...tDuplicationTest\Class1. Since you implemented __get, consider adding a @property annotation.
Loading history...
238
        $this->assertListEquals(
239
            [['text' => 'Test Text 3']],
240
            $dupe->threes()
241
        );
242
    }
243
}
244