| Conditions | 1 |
| Paths | 1 |
| Total Lines | 90 |
| Code Lines | 60 |
| 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 |
||
| 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; |
||
| 95 | $two->text = $text2; |
||
| 96 | $three->text = $text3; |
||
| 97 | |||
| 98 | //write the to DB |
||
| 99 | $one->write(); |
||
| 100 | $two->write(); |
||
| 101 | $three->write(); |
||
| 102 | |||
| 103 | //create relations |
||
| 104 | $one->twos()->add($two); |
||
| 105 | $one->threes()->add($three, array('TestExtra'=>'three')); |
||
| 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()); |
||
| 112 | $this->assertCount(1, $one->threes()); |
||
| 113 | $this->assertCount(1, $three->ones()); |
||
| 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); |
||
| 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, |
||
| 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'), |
||
| 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, |
||
| 172 | "Match between extra field of copy and the original" |
||
| 173 | ); |
||
| 244 |