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 |
||
| 17 | class RegistrableServicePassTest extends \PHPUnit_Framework_TestCase |
||
| 18 | { |
||
| 19 | public function testProcess() |
||
| 20 | { |
||
| 21 | $c = new ContainerBuilder; |
||
| 22 | (new InnmindNeo4jExtension)->load([], $c); |
||
| 23 | $c->setDefinition( |
||
| 24 | 'foo', |
||
| 25 | (new Definition) |
||
| 26 | ->addTag('innmind_neo4j.identity.generator', ['class' => 'stdClass']) |
||
| 27 | ); |
||
| 28 | $this->assertSame( |
||
| 29 | null, |
||
| 30 | ($p = new RegistrableServicePass( |
||
| 31 | 'innmind_neo4j.generators', |
||
| 32 | 'innmind_neo4j.identity.generator' |
||
| 33 | )) |
||
| 34 | ->process($c) |
||
| 35 | ); |
||
| 36 | $this->assertInstanceOf(CompilerPassInterface::class, $p); |
||
| 37 | |||
| 38 | $definition = $c->getDefinition('innmind_neo4j.generators'); |
||
| 39 | $calls = $definition->getMethodCalls(); |
||
| 40 | |||
| 41 | $this->assertSame(1, count($calls)); |
||
| 42 | $this->assertSame('register', $calls[0][0]); |
||
| 43 | $this->assertSame('stdClass', $calls[0][1][0]); |
||
| 44 | $this->assertInstanceOf(Reference::class, $calls[0][1][1]); |
||
| 45 | $this->assertSame('foo', (string) $calls[0][1][1]); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @expectedException Innmind\Neo4jBundle\Exception\RuntimeException |
||
| 50 | * @expectedExceptionMessage The class attribute must be defined |
||
| 51 | */ |
||
| 52 | public function testThrowWhenNoClassForGenerator() |
||
| 53 | { |
||
| 54 | $c = new ContainerBuilder; |
||
| 55 | (new InnmindNeo4jExtension)->load([], $c); |
||
| 56 | $c->setDefinition( |
||
| 57 | 'foo', |
||
| 58 | (new Definition) |
||
| 59 | ->addTag('innmind_neo4j.identity.generator') |
||
| 60 | ); |
||
| 61 | (new RegistrableServicePass( |
||
| 62 | 'innmind_neo4j.generators', |
||
| 63 | 'innmind_neo4j.identity.generator' |
||
| 64 | )) |
||
| 65 | ->process($c); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testProcessRepositoryFactoryConfigurator() |
||
| 69 | { |
||
| 70 | $c = new ContainerBuilder; |
||
| 71 | (new InnmindNeo4jExtension)->load([], $c); |
||
| 72 | $c->setDefinition( |
||
| 73 | 'foo', |
||
| 74 | (new Definition) |
||
| 75 | ->addTag('innmind_neo4j.repository', ['class' => 'stdClass']) |
||
| 76 | ); |
||
| 77 | $this->assertSame( |
||
| 78 | null, |
||
| 79 | ($p = new RegistrableServicePass( |
||
| 80 | 'innmind_neo4j.repository_factory.configurator', |
||
| 81 | 'innmind_neo4j.repository' |
||
| 82 | )) |
||
| 83 | ->process($c) |
||
| 84 | ); |
||
| 85 | $this->assertInstanceOf(CompilerPassInterface::class, $p); |
||
| 86 | |||
| 87 | $definition = $c->getDefinition('innmind_neo4j.repository_factory.configurator'); |
||
| 88 | $calls = $definition->getMethodCalls(); |
||
| 89 | |||
| 90 | $this->assertSame(1, count($calls)); |
||
| 91 | $this->assertSame('register', $calls[0][0]); |
||
| 92 | $this->assertSame('stdClass', $calls[0][1][0]); |
||
| 93 | $this->assertInstanceOf(Reference::class, $calls[0][1][1]); |
||
| 94 | $this->assertSame('foo', (string) $calls[0][1][1]); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @expectedException Innmind\Neo4jBundle\Exception\RuntimeException |
||
| 99 | * @expectedExceptionMessage The class attribute must be defined |
||
| 100 | */ |
||
| 101 | public function testThrowWhenNoClassForRepository() |
||
| 102 | { |
||
| 103 | $c = new ContainerBuilder; |
||
| 104 | (new InnmindNeo4jExtension)->load([], $c); |
||
| 105 | $c->setDefinition( |
||
| 106 | 'foo', |
||
| 107 | (new Definition) |
||
| 108 | ->addTag('innmind_neo4j.repository') |
||
| 109 | ); |
||
| 110 | (new RegistrableServicePass( |
||
| 111 | 'innmind_neo4j.repository_factory.configurator', |
||
| 112 | 'innmind_neo4j.repository' |
||
| 113 | )) |
||
| 114 | ->process($c); |
||
| 115 | } |
||
| 116 | } |
||
| 117 |