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 |
||
| 34 | class PimpleTest extends \PHPUnit_Framework_TestCase |
||
| 35 | { |
||
| 36 | public function testWithString() |
||
| 37 | { |
||
| 38 | $pimple = new Container(); |
||
| 39 | $pimple['param'] = 'value'; |
||
| 40 | |||
| 41 | $this->assertEquals('value', $pimple['param']); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function testWithClosure() |
||
| 45 | { |
||
| 46 | $pimple = new Container(); |
||
| 47 | $pimple['service'] = function () { |
||
| 48 | return new Fixtures\Service(); |
||
| 49 | }; |
||
| 50 | |||
| 51 | $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $pimple['service']); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function testServicesShouldBeDifferent() |
||
| 55 | { |
||
| 56 | $pimple = new Container(); |
||
| 57 | $pimple['service'] = $pimple->factory(function () { |
||
| 58 | return new Fixtures\Service(); |
||
| 59 | }); |
||
| 60 | |||
| 61 | $serviceOne = $pimple['service']; |
||
| 62 | $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceOne); |
||
| 63 | |||
| 64 | $serviceTwo = $pimple['service']; |
||
| 65 | $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceTwo); |
||
| 66 | |||
| 67 | $this->assertNotSame($serviceOne, $serviceTwo); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function testShouldPassContainerAsParameter() |
||
| 71 | { |
||
| 72 | $pimple = new Container(); |
||
| 73 | $pimple['service'] = function () { |
||
| 74 | return new Fixtures\Service(); |
||
| 75 | }; |
||
| 76 | $pimple['container'] = function ($container) { |
||
| 77 | return $container; |
||
| 78 | }; |
||
| 79 | |||
| 80 | $this->assertNotSame($pimple, $pimple['service']); |
||
| 81 | $this->assertSame($pimple, $pimple['container']); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function testIsset() |
||
| 85 | { |
||
| 86 | $pimple = new Container(); |
||
| 87 | $pimple['param'] = 'value'; |
||
| 88 | $pimple['service'] = function () { |
||
| 89 | return new Fixtures\Service(); |
||
| 90 | }; |
||
| 91 | |||
| 92 | $pimple['null'] = null; |
||
| 93 | |||
| 94 | $this->assertTrue(isset($pimple['param'])); |
||
| 95 | $this->assertTrue(isset($pimple['service'])); |
||
| 96 | $this->assertTrue(isset($pimple['null'])); |
||
| 97 | $this->assertFalse(isset($pimple['non_existent'])); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function testConstructorInjection() |
||
| 101 | { |
||
| 102 | $params = array('param' => 'value'); |
||
| 103 | $pimple = new Container($params); |
||
| 104 | |||
| 105 | $this->assertSame($params['param'], $pimple['param']); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @expectedException \InvalidArgumentException |
||
| 110 | * @expectedExceptionMessage Identifier "foo" is not defined. |
||
| 111 | */ |
||
| 112 | public function testOffsetGetValidatesKeyIsPresent() |
||
| 113 | { |
||
| 114 | $pimple = new Container(); |
||
| 115 | echo $pimple['foo']; |
||
| 116 | } |
||
| 117 | |||
| 118 | public function testOffsetGetHonorsNullValues() |
||
| 119 | { |
||
| 120 | $pimple = new Container(); |
||
| 121 | $pimple['foo'] = null; |
||
| 122 | $this->assertNull($pimple['foo']); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function testUnset() |
||
| 126 | { |
||
| 127 | $pimple = new Container(); |
||
| 128 | $pimple['param'] = 'value'; |
||
| 129 | $pimple['service'] = function () { |
||
| 130 | return new Fixtures\Service(); |
||
| 131 | }; |
||
| 132 | |||
| 133 | unset($pimple['param'], $pimple['service']); |
||
| 134 | $this->assertFalse(isset($pimple['param'])); |
||
| 135 | $this->assertFalse(isset($pimple['service'])); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @dataProvider serviceDefinitionProvider |
||
| 140 | */ |
||
| 141 | public function testShare($service) |
||
| 142 | { |
||
| 143 | $pimple = new Container(); |
||
| 144 | $pimple['shared_service'] = $service; |
||
| 145 | |||
| 146 | $serviceOne = $pimple['shared_service']; |
||
| 147 | $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceOne); |
||
| 148 | |||
| 149 | $serviceTwo = $pimple['shared_service']; |
||
| 150 | $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceTwo); |
||
| 151 | |||
| 152 | $this->assertSame($serviceOne, $serviceTwo); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @dataProvider serviceDefinitionProvider |
||
| 157 | */ |
||
| 158 | public function testProtect($service) |
||
| 159 | { |
||
| 160 | $pimple = new Container(); |
||
| 161 | $pimple['protected'] = $pimple->protect($service); |
||
| 162 | |||
| 163 | $this->assertSame($service, $pimple['protected']); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function testGlobalFunctionNameAsParameterValue() |
||
| 167 | { |
||
| 168 | $pimple = new Container(); |
||
| 169 | $pimple['global_function'] = 'strlen'; |
||
| 170 | $this->assertSame('strlen', $pimple['global_function']); |
||
| 171 | } |
||
| 172 | |||
| 173 | public function testRaw() |
||
| 174 | { |
||
| 175 | $pimple = new Container(); |
||
| 176 | $pimple['service'] = $definition = $pimple->factory(function () { return 'foo'; }); |
||
| 177 | $this->assertSame($definition, $pimple->raw('service')); |
||
| 178 | } |
||
| 179 | |||
| 180 | public function testRawHonorsNullValues() |
||
| 181 | { |
||
| 182 | $pimple = new Container(); |
||
| 183 | $pimple['foo'] = null; |
||
| 184 | $this->assertNull($pimple->raw('foo')); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function testFluentRegister() |
||
| 188 | { |
||
| 189 | $pimple = new Container(); |
||
| 190 | $this->assertSame($pimple, $pimple->register($this->createMock('Pimple\ServiceProviderInterface'))); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @expectedException \InvalidArgumentException |
||
| 195 | * @expectedExceptionMessage Identifier "foo" is not defined. |
||
| 196 | */ |
||
| 197 | public function testRawValidatesKeyIsPresent() |
||
| 198 | { |
||
| 199 | $pimple = new Container(); |
||
| 200 | $pimple->raw('foo'); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @dataProvider serviceDefinitionProvider |
||
| 205 | */ |
||
| 206 | public function testExtend($service) |
||
| 207 | { |
||
| 208 | $pimple = new Container(); |
||
| 209 | $pimple['shared_service'] = function () { |
||
| 210 | return new Fixtures\Service(); |
||
| 211 | }; |
||
| 212 | $pimple['factory_service'] = $pimple->factory(function () { |
||
| 213 | return new Fixtures\Service(); |
||
| 214 | }); |
||
| 215 | |||
| 216 | $pimple->extend('shared_service', $service); |
||
| 217 | $serviceOne = $pimple['shared_service']; |
||
| 218 | $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceOne); |
||
| 219 | $serviceTwo = $pimple['shared_service']; |
||
| 220 | $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceTwo); |
||
| 221 | $this->assertSame($serviceOne, $serviceTwo); |
||
| 222 | $this->assertSame($serviceOne->value, $serviceTwo->value); |
||
| 223 | |||
| 224 | $pimple->extend('factory_service', $service); |
||
| 225 | $serviceOne = $pimple['factory_service']; |
||
| 226 | $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceOne); |
||
| 227 | $serviceTwo = $pimple['factory_service']; |
||
| 228 | $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceTwo); |
||
| 229 | $this->assertNotSame($serviceOne, $serviceTwo); |
||
| 230 | $this->assertNotSame($serviceOne->value, $serviceTwo->value); |
||
| 231 | } |
||
| 232 | |||
| 233 | public function testExtendDoesNotLeakWithFactories() |
||
| 234 | { |
||
| 235 | if (extension_loaded('pimple')) { |
||
| 236 | $this->markTestSkipped('Pimple extension does not support this test'); |
||
| 237 | } |
||
| 238 | $pimple = new Container(); |
||
| 239 | |||
| 240 | $pimple['foo'] = $pimple->factory(function () { return; }); |
||
| 241 | $pimple['foo'] = $pimple->extend('foo', function ($foo, $pimple) { return; }); |
||
| 242 | unset($pimple['foo']); |
||
| 243 | |||
| 244 | $p = new \ReflectionProperty($pimple, 'values'); |
||
| 245 | $p->setAccessible(true); |
||
| 246 | $this->assertEmpty($p->getValue($pimple)); |
||
| 247 | |||
| 248 | $p = new \ReflectionProperty($pimple, 'factories'); |
||
| 249 | $p->setAccessible(true); |
||
| 250 | $this->assertCount(0, $p->getValue($pimple)); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @expectedException \InvalidArgumentException |
||
| 255 | * @expectedExceptionMessage Identifier "foo" is not defined. |
||
| 256 | */ |
||
| 257 | public function testExtendValidatesKeyIsPresent() |
||
| 258 | { |
||
| 259 | $pimple = new Container(); |
||
| 260 | $pimple->extend('foo', function () {}); |
||
| 261 | } |
||
| 262 | |||
| 263 | public function testKeys() |
||
| 264 | { |
||
| 265 | $pimple = new Container(); |
||
| 266 | $pimple['foo'] = 123; |
||
| 267 | $pimple['bar'] = 123; |
||
| 268 | |||
| 269 | $this->assertEquals(array('foo', 'bar'), $pimple->keys()); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** @test */ |
||
| 273 | public function settingAnInvokableObjectShouldTreatItAsFactory() |
||
| 274 | { |
||
| 275 | $pimple = new Container(); |
||
| 276 | $pimple['invokable'] = new Fixtures\Invokable(); |
||
| 277 | |||
| 278 | $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $pimple['invokable']); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** @test */ |
||
| 282 | public function settingNonInvokableObjectShouldTreatItAsParameter() |
||
| 283 | { |
||
| 284 | $pimple = new Container(); |
||
| 285 | $pimple['non_invokable'] = new Fixtures\NonInvokable(); |
||
| 286 | |||
| 287 | $this->assertInstanceOf('Pimple\Tests\Fixtures\NonInvokable', $pimple['non_invokable']); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @dataProvider badServiceDefinitionProvider |
||
| 292 | * @expectedException \InvalidArgumentException |
||
| 293 | * @expectedExceptionMessage Service definition is not a Closure or invokable object. |
||
| 294 | */ |
||
| 295 | public function testFactoryFailsForInvalidServiceDefinitions($service) |
||
| 296 | { |
||
| 297 | $pimple = new Container(); |
||
| 298 | $pimple->factory($service); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @dataProvider badServiceDefinitionProvider |
||
| 303 | * @expectedException \InvalidArgumentException |
||
| 304 | * @expectedExceptionMessage Callable is not a Closure or invokable object. |
||
| 305 | */ |
||
| 306 | public function testProtectFailsForInvalidServiceDefinitions($service) |
||
| 307 | { |
||
| 308 | $pimple = new Container(); |
||
| 309 | $pimple->protect($service); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @dataProvider badServiceDefinitionProvider |
||
| 314 | * @expectedException \InvalidArgumentException |
||
| 315 | * @expectedExceptionMessage Identifier "foo" does not contain an object definition. |
||
| 316 | */ |
||
| 317 | public function testExtendFailsForKeysNotContainingServiceDefinitions($service) |
||
| 318 | { |
||
| 319 | $pimple = new Container(); |
||
| 320 | $pimple['foo'] = $service; |
||
| 321 | $pimple->extend('foo', function () {}); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @dataProvider badServiceDefinitionProvider |
||
| 326 | * @expectedException \InvalidArgumentException |
||
| 327 | * @expectedExceptionMessage Extension service definition is not a Closure or invokable object. |
||
| 328 | */ |
||
| 329 | public function testExtendFailsForInvalidServiceDefinitions($service) |
||
| 330 | { |
||
| 331 | $pimple = new Container(); |
||
| 332 | $pimple['foo'] = function () {}; |
||
| 333 | $pimple->extend('foo', $service); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Provider for invalid service definitions. |
||
| 338 | */ |
||
| 339 | public function badServiceDefinitionProvider() |
||
| 340 | { |
||
| 341 | return array( |
||
| 342 | array(123), |
||
| 343 | array(new Fixtures\NonInvokable()), |
||
| 344 | ); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Provider for service definitions. |
||
| 349 | */ |
||
| 350 | public function serviceDefinitionProvider() |
||
| 351 | { |
||
| 352 | return array( |
||
| 353 | array(function ($value) { |
||
| 354 | $service = new Fixtures\Service(); |
||
| 355 | $service->value = $value; |
||
| 356 | |||
| 357 | return $service; |
||
| 358 | }), |
||
| 359 | array(new Fixtures\Invokable()), |
||
| 360 | ); |
||
| 361 | } |
||
| 362 | |||
| 363 | public function testDefiningNewServiceAfterFreeze() |
||
| 364 | { |
||
| 365 | $pimple = new Container(); |
||
| 366 | $pimple['foo'] = function () { |
||
| 367 | return 'foo'; |
||
| 368 | }; |
||
| 369 | $foo = $pimple['foo']; |
||
| 370 | |||
| 371 | $pimple['bar'] = function () { |
||
| 372 | return 'bar'; |
||
| 373 | }; |
||
| 374 | $this->assertSame('bar', $pimple['bar']); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @expectedException \RuntimeException |
||
| 379 | * @expectedExceptionMessage Cannot override frozen service "foo". |
||
| 380 | */ |
||
| 381 | public function testOverridingServiceAfterFreeze() |
||
| 382 | { |
||
| 383 | $pimple = new Container(); |
||
| 384 | $pimple['foo'] = function () { |
||
| 385 | return 'foo'; |
||
| 386 | }; |
||
| 387 | $foo = $pimple['foo']; |
||
| 388 | |||
| 389 | $pimple['foo'] = function () { |
||
| 390 | return 'bar'; |
||
| 391 | }; |
||
| 392 | } |
||
| 393 | |||
| 394 | public function testRemovingServiceAfterFreeze() |
||
| 395 | { |
||
| 396 | $pimple = new Container(); |
||
| 397 | $pimple['foo'] = function () { |
||
| 398 | return 'foo'; |
||
| 399 | }; |
||
| 400 | $foo = $pimple['foo']; |
||
| 401 | |||
| 402 | unset($pimple['foo']); |
||
| 403 | $pimple['foo'] = function () { |
||
| 404 | return 'bar'; |
||
| 405 | }; |
||
| 406 | $this->assertSame('bar', $pimple['foo']); |
||
| 407 | } |
||
| 408 | |||
| 409 | public function testExtendingService() |
||
| 410 | { |
||
| 411 | $pimple = new Container(); |
||
| 412 | $pimple['foo'] = function () { |
||
| 413 | return 'foo'; |
||
| 414 | }; |
||
| 415 | $pimple['foo'] = $pimple->extend('foo', function ($foo, $app) { |
||
| 416 | return "$foo.bar"; |
||
| 417 | }); |
||
| 418 | $pimple['foo'] = $pimple->extend('foo', function ($foo, $app) { |
||
| 419 | return "$foo.baz"; |
||
| 420 | }); |
||
| 421 | $this->assertSame('foo.bar.baz', $pimple['foo']); |
||
| 422 | } |
||
| 423 | |||
| 424 | public function testExtendingServiceAfterOtherServiceFreeze() |
||
| 425 | { |
||
| 426 | $pimple = new Container(); |
||
| 427 | $pimple['foo'] = function () { |
||
| 428 | return 'foo'; |
||
| 429 | }; |
||
| 430 | $pimple['bar'] = function () { |
||
| 431 | return 'bar'; |
||
| 432 | }; |
||
| 433 | $foo = $pimple['foo']; |
||
| 434 | |||
| 435 | $pimple['bar'] = $pimple->extend('bar', function ($bar, $app) { |
||
| 436 | return "$bar.baz"; |
||
| 437 | }); |
||
| 438 | $this->assertSame('bar.baz', $pimple['bar']); |
||
| 439 | } |
||
| 440 | } |
||
| 441 |