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 |
||
| 14 | class Kernel extends atoum |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var $class : Instance de la class Kernel |
||
| 18 | */ |
||
| 19 | protected $class; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var $mock : Instance du mock pour la class Kernel |
||
| 23 | */ |
||
| 24 | protected $mock; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Instanciation de la class avant chaque méthode de test |
||
| 28 | */ |
||
| 29 | public function beforeTestMethod($testMethod) |
||
| 30 | { |
||
| 31 | //$this->class = new \BFW\Kernel(); |
||
| 32 | $this->mock = new MockKernel(); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Test de la méthode attach($observer) |
||
| 37 | */ |
||
| 38 | public function testAttach() |
||
| 39 | { |
||
| 40 | $observer = new MockKernelSplObserver; |
||
| 41 | |||
| 42 | $this->mock->attach($observer); |
||
| 43 | $this->object($this->mock->observers[0])->isIdenticalTo($observer); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Test de la méthode attachOther($observer) |
||
| 48 | */ |
||
| 49 | public function testAttachOther() |
||
| 50 | { |
||
| 51 | $observer = new MockKernelObserver; |
||
| 52 | |||
| 53 | $this->mock->attachOther($observer); |
||
| 54 | $this->object($this->mock->observers[0])->isIdenticalTo($observer); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Test de la méthode detach($observer) |
||
| 59 | */ |
||
| 60 | public function testDetach() |
||
| 61 | { |
||
| 62 | $observerA = new MockKernelSplObserver; |
||
| 63 | $observerB = new MockKernelSplObserver; |
||
| 64 | $observerC = new MockKernelSplObserver; |
||
| 65 | |||
| 66 | $this->mock->attach($observerA); |
||
| 67 | $this->object($this->mock->observers[0])->isIdenticalTo($observerA); |
||
| 68 | $this->mock->attach($observerB); |
||
| 69 | $this->object($this->mock->observers[1])->isIdenticalTo($observerB); |
||
| 70 | $this->mock->attach($observerC); |
||
| 71 | $this->object($this->mock->observers[2])->isIdenticalTo($observerC); |
||
| 72 | |||
| 73 | $this->mock->detach($observerB); |
||
| 74 | |||
| 75 | $this->object($this->mock->observers[0])->isIdenticalTo($observerA); |
||
| 76 | $this->array($this->mock->observers)->notHasKey(1); |
||
| 77 | $this->object($this->mock->observers[2])->isIdenticalTo($observerC); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Test de la méthode detachOther($observer) |
||
| 82 | */ |
||
| 83 | public function testDetachOther() |
||
| 84 | { |
||
| 85 | $observerA = new MockKernelObserver; |
||
| 86 | $observerB = new MockKernelObserver; |
||
| 87 | $observerC = new MockKernelObserver; |
||
| 88 | |||
| 89 | $this->mock->attachOther($observerA); |
||
| 90 | $this->object($this->mock->observers[0])->isIdenticalTo($observerA); |
||
| 91 | $this->mock->attachOther($observerB); |
||
| 92 | $this->object($this->mock->observers[1])->isIdenticalTo($observerB); |
||
| 93 | $this->mock->attachOther($observerC); |
||
| 94 | $this->object($this->mock->observers[2])->isIdenticalTo($observerC); |
||
| 95 | |||
| 96 | $this->mock->detachOther($observerB); |
||
| 97 | |||
| 98 | $this->object($this->mock->observers[0])->isIdenticalTo($observerA); |
||
| 99 | $this->array($this->mock->observers)->notHasKey(1); |
||
| 100 | $this->object($this->mock->observers[2])->isIdenticalTo($observerC); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Test de la méthode notifyObserver($action) |
||
| 105 | */ |
||
| 106 | public function testNotifyObserver() |
||
| 107 | { |
||
| 108 | $observer = new MockKernelObserver; |
||
| 109 | $this->mock->attachOther($observer); |
||
| 110 | |||
| 111 | $this->mock->notifyObserver('test'); |
||
| 112 | $this->variable($this->mock->notify_action)->isNull(); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Test de la méthode notifyAction($action) |
||
| 117 | */ |
||
| 118 | public function testNotifyAction() |
||
| 119 | { |
||
| 120 | $this->object($this->mock->notifyAction('test'))->isIdenticalTo($this->mock); |
||
| 121 | $this->string($this->mock->notify_action)->isEqualTo('test'); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Test de la méthode notify() |
||
| 126 | */ |
||
| 127 | public function testNotify() |
||
| 128 | { |
||
| 129 | //Test avec une action à envoyer : appel updateWithAction |
||
| 130 | $this->mock->notifyAction('test'); |
||
| 131 | $this->mock->notify(); |
||
| 132 | |||
| 133 | //Test sans action : appel update() |
||
| 134 | $observer = new MockKernelSplObserver; |
||
| 135 | $this->mock->attach($observer); |
||
| 136 | $this->mock->notify(); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Test de la méthode setDebug($debug) |
||
| 141 | */ |
||
| 142 | public function testSetDebug() |
||
| 143 | { |
||
| 144 | $this->mock->setDebug(true); |
||
| 145 | |||
| 146 | $this->boolean($this->mock->debug)->isTrue(); |
||
| 147 | $this->integer(error_reporting())->isEqualTo(E_ALL); |
||
| 148 | $this->string(ini_get('display_errors'))->isEqualTo('On'); |
||
| 149 | $this->string(ini_get('html_errors'))->isEqualTo('1'); |
||
| 150 | |||
| 151 | |||
| 152 | $this->mock->setDebug(false); |
||
| 153 | |||
| 154 | $this->boolean($this->mock->debug)->isFalse(); |
||
| 155 | $this->integer(error_reporting())->isEqualTo(0); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Test de la méthode getDebug() |
||
| 160 | */ |
||
| 161 | public function testGetDebug() |
||
| 162 | { |
||
| 163 | $this->boolean($this->mock->getDebug())->isFalse(); |
||
| 164 | |||
| 165 | $this->mock->setDebug(false); |
||
| 166 | $this->boolean($this->mock->getDebug())->isFalse(); |
||
| 167 | |||
| 168 | $this->mock->setDebug(true); |
||
| 169 | $this->boolean($this->mock->debug)->isTrue(); |
||
| 170 | } |
||
| 171 | |||
| 172 | } |
||
| 173 | |||
| 228 |