Conditions | 1 |
Paths | 1 |
Total Lines | 90 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
21 | public function testDuplicate() |
||
22 | { |
||
23 | /** @var DataObjectDuplicationTest\Antelope $orig */ |
||
24 | $orig = $this->objFromFixture(DataObjectDuplicationTest\Antelope::class, 'one'); |
||
25 | /** @var DataObjectDuplicationTest\Antelope $duplicate */ |
||
26 | $duplicate = $orig->duplicate(); |
||
27 | $this->assertInstanceOf( |
||
28 | DataObjectDuplicationTest\Antelope::class, |
||
29 | $duplicate, |
||
30 | 'Creates the correct type' |
||
31 | ); |
||
32 | $this->assertNotEquals( |
||
33 | $duplicate->ID, |
||
34 | $orig->ID, |
||
35 | 'Creates a unique record' |
||
36 | ); |
||
37 | |||
38 | // Check 'bobcats' relation duplicated |
||
39 | $twoOne = $this->objFromFixture(DataObjectDuplicationTest\Bobcat::class, 'one'); |
||
40 | $twoTwo = $this->objFromFixture(DataObjectDuplicationTest\Bobcat::class, 'two'); |
||
41 | $this->assertListEquals( |
||
42 | [ |
||
43 | ['Title' => 'Bobcat two'], |
||
44 | ['Title' => 'Bobcat three'], |
||
45 | ], |
||
46 | $duplicate->bobcats() |
||
47 | ); |
||
48 | $this->assertEmpty( |
||
49 | array_intersect( |
||
50 | $orig->bobcats()->getIDList(), |
||
51 | $duplicate->bobcats()->getIDList() |
||
52 | ) |
||
53 | ); |
||
54 | /** @var DataObjectDuplicationTest\Bobcat $twoTwoDuplicate */ |
||
55 | $twoTwoDuplicate = $duplicate->bobcats()->filter('Title', 'Bobcat two')->first(); |
||
56 | $this->assertNotEmpty($twoTwoDuplicate); |
||
57 | $this->assertNotEquals($twoTwo->ID, $twoTwoDuplicate->ID); |
||
58 | |||
59 | // Check 'bobcats.self' relation duplicated |
||
60 | /** @var DataObjectDuplicationTest\Bobcat $twoOneDuplicate */ |
||
61 | $twoOneDuplicate = $twoTwoDuplicate->self(); |
||
62 | $this->assertNotEmpty($twoOneDuplicate); |
||
63 | $this->assertNotEquals($twoOne->ID, $twoOneDuplicate->ID); |
||
64 | |||
65 | // Ensure 'bobcats.seven' instance is not duplicated |
||
66 | $sevenOne = $this->objFromFixture(DataObjectDuplicationTest\Goat::class, 'one'); |
||
67 | $sevenTwo = $this->objFromFixture(DataObjectDuplicationTest\Goat::class, 'two'); |
||
68 | $this->assertEquals($sevenOne->ID, $twoOneDuplicate->goat()->ID); |
||
69 | $this->assertEquals($sevenTwo->ID, $twoTwoDuplicate->goat()->ID); |
||
70 | |||
71 | // Ensure that 'caribou' many_many list exists on both, but only the mapping table is duplicated |
||
72 | // many_many_extraFields are also duplicated |
||
73 | $caribouList = [ |
||
74 | [ |
||
75 | 'Title' => 'Caribou one', |
||
76 | 'Sort' => 4, |
||
77 | ], |
||
78 | [ |
||
79 | 'Title' => 'Caribou two', |
||
80 | 'Sort' => 5, |
||
81 | ], |
||
82 | ]; |
||
83 | // Original and duplicate lists have the same content |
||
84 | $this->assertListEquals( |
||
85 | $caribouList, |
||
86 | $orig->caribou() |
||
87 | ); |
||
88 | $this->assertListEquals( |
||
89 | $caribouList, |
||
90 | $duplicate->caribou() |
||
91 | ); |
||
92 | // Ids of each record are the same (only mapping content is duplicated) |
||
93 | $this->assertEquals( |
||
94 | $orig->caribou()->getIDList(), |
||
95 | $duplicate->caribou()->getIDList() |
||
96 | ); |
||
97 | |||
98 | // Ensure 'five' belongs_to is duplicated |
||
99 | $fiveOne = $this->objFromFixture(DataObjectDuplicationTest\Elephant::class, 'one'); |
||
100 | $fiveOneDuplicate = $duplicate->elephant(); |
||
101 | $this->assertNotEmpty($fiveOneDuplicate); |
||
102 | $this->assertEquals('Elephant one', $fiveOneDuplicate->Title); |
||
103 | $this->assertNotEquals($fiveOne->ID, $fiveOneDuplicate->ID); |
||
104 | |||
105 | // Ensure 'five.Child' is duplicated |
||
106 | $sixOne = $this->objFromFixture(DataObjectDuplicationTest\Frog::class, 'one'); |
||
107 | $sixOneDuplicate = $fiveOneDuplicate->Child(); |
||
108 | $this->assertNotEmpty($sixOneDuplicate); |
||
109 | $this->assertEquals('Frog one', $sixOneDuplicate->Title); |
||
110 | $this->assertNotEquals($sixOne->ID, $sixOneDuplicate->ID); |
||
111 | } |
||
113 |