Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 11 | class CIPHPUnitTestDouble |
||
|
|
|||
| 12 | { |
||
| 13 | protected $testCase; |
||
| 14 | |||
| 15 | public function __construct(PHPUnit_Framework_TestCase $testCase) |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Get Mock Object |
||
| 22 | * |
||
| 23 | * $email = $this->getMockBuilder('CI_Email') |
||
| 24 | * ->disableOriginalConstructor() |
||
| 25 | * ->setMethods(['send']) |
||
| 26 | * ->getMock(); |
||
| 27 | * $email->method('send')->willReturn(TRUE); |
||
| 28 | * |
||
| 29 | * will be |
||
| 30 | * |
||
| 31 | * $email = $this->getDouble('CI_Email', ['send' => TRUE]); |
||
| 32 | * |
||
| 33 | * @param string $classname |
||
| 34 | * @param array $params [method_name => return_value] |
||
| 35 | * @param mixed $constructor_args enable constructor and args or not |
||
| 36 | * |
||
| 37 | * @return object PHPUnit mock object |
||
| 38 | */ |
||
| 39 | public function getDouble($classname, $params, $constructor_args = []) |
||
| 66 | |||
| 67 | protected function _verify($mock, $method, $params = null, $expects, $with) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Verifies that method was called exactly $times times |
||
| 111 | * |
||
| 112 | * $loader->expects($this->exactly(2)) |
||
| 113 | * ->method('view') |
||
| 114 | * ->withConsecutive( |
||
| 115 | * ['shop_confirm', $this->anything(), TRUE], |
||
| 116 | * ['shop_tmpl_checkout', $this->anything()] |
||
| 117 | * ); |
||
| 118 | * |
||
| 119 | * will be |
||
| 120 | * |
||
| 121 | * $this->verifyInvokedMultipleTimes( |
||
| 122 | * $loader, |
||
| 123 | * 'view', |
||
| 124 | * 2, |
||
| 125 | * [ |
||
| 126 | * ['shop_confirm', $this->anything(), TRUE], |
||
| 127 | * ['shop_tmpl_checkout', $this->anything()] |
||
| 128 | * ] |
||
| 129 | * ); |
||
| 130 | * |
||
| 131 | * @param object $mock PHPUnit mock object |
||
| 132 | * @param string $method |
||
| 133 | * @param int $times |
||
| 134 | * @param array $params arguments |
||
| 135 | */ |
||
| 136 | public function verifyInvokedMultipleTimes($mock, $method, $times, $params = null) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Verifies a method was invoked at least once |
||
| 145 | * |
||
| 146 | * @param object $mock PHPUnit mock object |
||
| 147 | * @param string $method |
||
| 148 | * @param array $params arguments |
||
| 149 | */ |
||
| 150 | public function verifyInvoked($mock, $method, $params = null) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Verifies that method was invoked only once |
||
| 159 | * |
||
| 160 | * @param object $mock PHPUnit mock object |
||
| 161 | * @param string $method |
||
| 162 | * @param array $params arguments |
||
| 163 | */ |
||
| 164 | public function verifyInvokedOnce($mock, $method, $params = null) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Verifies that method was not called |
||
| 173 | * |
||
| 174 | * @param object $mock PHPUnit mock object |
||
| 175 | * @param string $method |
||
| 176 | * @param array $params arguments |
||
| 177 | */ |
||
| 178 | public function verifyNeverInvoked($mock, $method, $params = null) |
||
| 184 | } |
||
| 185 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.