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 | final class ContainerTest extends TestCase |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @expectedException \Sensorario\Resources\Exceptions\EmptyConfigurationException |
||
| 21 | * @expectedExceptionMessage resources element is not defined |
||
| 22 | */ |
||
| 23 | public function testConfigurationCannotBeEmpty() |
||
| 24 | { |
||
| 25 | $container = new Container([]); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function testNeedsResourcesAsRootElement() |
||
| 29 | { |
||
| 30 | $container = new Container([ |
||
| 31 | 'resources' => [], |
||
| 32 | ]); |
||
| 33 | |||
| 34 | $this->assertSame( |
||
| 35 | 0, |
||
| 36 | $container->countResources() |
||
| 37 | ); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function testResourcesAreResourcesChild() |
||
| 41 | { |
||
| 42 | $container = new Container([ |
||
| 43 | 'resources' => [ |
||
| 44 | 'foo' => [], |
||
| 45 | 'bar' => [ |
||
| 46 | 'constraints' => [], |
||
| 47 | ], |
||
| 48 | ], |
||
| 49 | ]); |
||
| 50 | |||
| 51 | $this->assertSame( |
||
| 52 | 2, |
||
| 53 | $container->countResources() |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @expectedException \Sensorario\Resources\Exceptions\InvalidConstraintException |
||
| 59 | * @expectedExceptionMessageRegExp /Invalid constraint/ |
||
| 60 | */ |
||
| 61 | public function testResourceDefinesConstraints() |
||
| 62 | { |
||
| 63 | $container = new Container([ |
||
| 64 | 'resources' => [ |
||
| 65 | 'foo' => [ |
||
| 66 | 'constraints' => [ |
||
| 67 | 'invalid' => 'constraint', |
||
| 68 | ], |
||
| 69 | ], |
||
| 70 | ], |
||
| 71 | ]); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @expectedException \Sensorario\Resources\Exceptions\NotAllowedConstraintException |
||
| 76 | * @expectedExceptionMessageRegExp /Not allowed `foo` constraint/ |
||
| 77 | */ |
||
| 78 | public function testCannotCreateResourceWithoutAllowedConstraints() |
||
| 79 | { |
||
| 80 | $container = new Container([ |
||
| 81 | 'resources' => [ |
||
| 82 | 'foo' => [ |
||
| 83 | 'constraints' => [ |
||
| 84 | ], |
||
| 85 | ], |
||
| 86 | ], |
||
| 87 | ]); |
||
| 88 | |||
| 89 | $container->create( |
||
| 90 | 'foo', [ |
||
| 91 | 'foo' => 'bar', |
||
| 92 | ] |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function testCon() |
||
| 97 | { |
||
| 98 | $container = new Container([ |
||
| 99 | 'resources' => [ |
||
| 100 | 'foo' => [ |
||
| 101 | 'constraints' => [ |
||
| 102 | 'allowed' => [ |
||
| 103 | 'foo', |
||
| 104 | 'bar', |
||
| 105 | ] |
||
| 106 | ], |
||
| 107 | ], |
||
| 108 | ], |
||
| 109 | ]); |
||
| 110 | |||
| 111 | $this->assertEquals( [ |
||
| 112 | 'foo', |
||
| 113 | 'bar', |
||
| 114 | ], |
||
| 115 | $container->allowed('foo') |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function testRewriteRules() |
||
| 120 | { |
||
| 121 | $container = new Container([ |
||
| 122 | 'resources' => [ |
||
| 123 | 'foo' => [ |
||
| 124 | 'rewrite' => [ |
||
| 125 | 'foo' => [ |
||
| 126 | 'set' => [ |
||
| 127 | 'equals_to' => 'bar', |
||
| 128 | ], |
||
| 129 | 'when' => [ |
||
| 130 | 'greater_than' => 'bar', |
||
| 131 | ], |
||
| 132 | ], |
||
| 133 | ], |
||
| 134 | 'constraints' => [ |
||
| 135 | 'allowed' => [ |
||
| 136 | 'foo', |
||
| 137 | 'bar', |
||
| 138 | ] |
||
| 139 | ], |
||
| 140 | ], |
||
| 141 | ], |
||
| 142 | ]); |
||
| 143 | |||
| 144 | $this->assertEquals( [ |
||
| 145 | 'foo' => [ |
||
| 146 | 'set' => [ |
||
| 147 | 'equals_to' => 'bar', |
||
| 148 | ], |
||
| 149 | 'when' => [ |
||
| 150 | 'greater_than' => 'bar', |
||
| 151 | ], |
||
| 152 | ], |
||
| 153 | ], |
||
| 154 | $container->rewrites('foo') |
||
| 155 | ); |
||
| 156 | } |
||
| 157 | |||
| 158 | public function testCreationSucceedReturnsTrue() |
||
| 159 | { |
||
| 160 | $container = new Container([ |
||
| 161 | 'resources' => [ |
||
| 162 | 'foo' => [ |
||
| 163 | 'constraints' => [ |
||
| 164 | 'allowed' => [ |
||
| 165 | 'foo' |
||
| 166 | ] |
||
| 167 | ], |
||
| 168 | ], |
||
| 169 | ], |
||
| 170 | ]); |
||
| 171 | |||
| 172 | $creationSucceed = $container->create( |
||
| 173 | 'foo', [ |
||
| 174 | 'foo' => 'bar', |
||
| 175 | ] |
||
| 176 | ); |
||
| 177 | |||
| 178 | $this->assertSame( |
||
| 179 | true, |
||
| 180 | $creationSucceed |
||
| 181 | ); |
||
| 182 | } |
||
| 183 | } |
||
| 184 |