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 |
||
| 21 | class ImageTest extends HandlerBaseTest |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var Image |
||
| 25 | */ |
||
| 26 | protected $image; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
| 30 | */ |
||
| 31 | protected $variationHandler; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
| 35 | */ |
||
| 36 | protected $requestStack; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
| 40 | */ |
||
| 41 | protected $logger; |
||
| 42 | |||
| 43 | public function setUp() |
||
| 44 | { |
||
| 45 | $this->fieldHelper = $this->getMockBuilder(FieldHelper::class) |
||
| 46 | ->disableOriginalConstructor() |
||
| 47 | ->setMethods(array('isFieldEmpty')) |
||
| 48 | ->getMock(); |
||
| 49 | |||
| 50 | $this->translationHelper = $this->getMockBuilder(TranslationHelper::class) |
||
| 51 | ->disableOriginalConstructor() |
||
| 52 | ->setMethods(array('getTranslatedField')) |
||
| 53 | ->getMock(); |
||
| 54 | |||
| 55 | $this->content = new Content(array('versionInfo' => new VersionInfo())); |
||
| 56 | |||
| 57 | $this->variationHandler = $this->getMockBuilder(AliasGenerator::class) |
||
| 58 | ->disableOriginalConstructor() |
||
| 59 | ->setMethods(array('getVariation')) |
||
| 60 | ->getMock(); |
||
| 61 | |||
| 62 | $this->requestStack = $this->getMockBuilder(RequestStack::class) |
||
| 63 | ->disableOriginalConstructor() |
||
| 64 | ->setMethods(array('getCurrentRequest')) |
||
| 65 | ->getMock(); |
||
| 66 | |||
| 67 | $this->logger = $this->getMockBuilder(NullLogger::class) |
||
| 68 | ->disableOriginalConstructor() |
||
| 69 | ->setMethods(array()) |
||
| 70 | ->getMock(); |
||
| 71 | |||
| 72 | $this->image = new Image($this->fieldHelper, $this->translationHelper, $this->variationHandler, $this->requestStack, $this->logger); |
||
| 73 | $this->image->setContent($this->content); |
||
| 74 | |||
| 75 | $this->field = new Field(array('value' => new Value())); |
||
| 76 | } |
||
| 77 | |||
| 78 | public function testInstanceOfHandlerInterface() |
||
| 79 | { |
||
| 80 | $this->assertInstanceOf(HandlerInterface::class, $this->image); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
||
| 85 | * @expectedExceptionMessage Argument '$params[0]' is invalid: Field type handlers require at least a field identifier. |
||
| 86 | */ |
||
| 87 | public function testGettingTagsWithoutFieldIdentifier() |
||
| 88 | { |
||
| 89 | $this->image->getMetaTags('some_tag', array()); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
||
| 94 | * @expectedExceptionMessage Argument '$params[0]' is invalid: Field 'some_value' does not exist in content. |
||
| 95 | */ |
||
| 96 | public function testGettingTagsWithNonExistentField() |
||
| 97 | { |
||
| 98 | $this->translationHelper->expects($this->once()) |
||
| 99 | ->method('getTranslatedField') |
||
| 100 | ->willReturn(null); |
||
| 101 | |||
| 102 | $this->image->getMetaTags('some_tag', array('some_value')); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
||
| 107 | * @expectedExceptionMessage Argument '$params[0]' is invalid: Netgen\Bundle\OpenGraphBundle\Handler\FieldType\Image field type handler does not support field with identifier ''. |
||
| 108 | */ |
||
| 109 | public function testGettingTagsWithUnsupportedField() |
||
| 110 | { |
||
| 111 | $this->translationHelper->expects($this->once()) |
||
| 112 | ->method('getTranslatedField') |
||
| 113 | ->willReturn(new Field()); |
||
| 114 | |||
| 115 | $this->image->getMetaTags('some_tag', array('some_value')); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function testGettingTagsWithEmptyField() |
||
| 119 | { |
||
| 120 | $this->translationHelper->expects($this->once()) |
||
| 121 | ->method('getTranslatedField') |
||
| 122 | ->willReturn($this->field); |
||
| 123 | |||
| 124 | $this->fieldHelper->expects($this->once()) |
||
| 125 | ->method('isFieldEmpty') |
||
| 126 | ->willReturn(true); |
||
| 127 | |||
| 128 | $this->image->getMetaTags('some_tag', array('some_value')); |
||
| 129 | } |
||
| 130 | |||
| 131 | public function testGettingTagsWithExceptionThrownByVariationHandler() |
||
| 132 | { |
||
| 133 | $this->translationHelper->expects($this->once()) |
||
| 134 | ->method('getTranslatedField') |
||
| 135 | ->willReturn($this->field); |
||
| 136 | |||
| 137 | $request = $this->getMockBuilder(Request::class) |
||
| 138 | ->disableOriginalConstructor() |
||
| 139 | ->setMethods(array()) |
||
| 140 | ->getMock(); |
||
| 141 | |||
| 142 | $this->requestStack->expects($this->once()) |
||
| 143 | ->method('getCurrentRequest') |
||
| 144 | ->willReturn($request); |
||
| 145 | |||
| 146 | $this->image->getMetaTags('some_tag', array('some_value', 'some_value_2', 'some_value_3')); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function testGettingTags() |
||
| 150 | { |
||
| 151 | $this->translationHelper->expects($this->once()) |
||
| 152 | ->method('getTranslatedField') |
||
| 153 | ->willReturn($this->field); |
||
| 154 | |||
| 155 | $this->fieldHelper->expects($this->once()) |
||
| 156 | ->method('isFieldEmpty') |
||
| 157 | ->willReturn(false); |
||
| 158 | |||
| 159 | $variation = new Variation(array('uri' => '/some/uri')); |
||
| 160 | |||
| 161 | $this->variationHandler->expects($this->once()) |
||
| 162 | ->method('getVariation') |
||
| 163 | ->willReturn($variation); |
||
| 164 | |||
| 165 | $request = $this->getMockBuilder(Request::class) |
||
| 166 | ->disableOriginalConstructor() |
||
| 167 | ->setMethods(array()) |
||
| 168 | ->getMock(); |
||
| 169 | |||
| 170 | $this->requestStack->expects($this->exactly(2)) |
||
| 171 | ->method('getCurrentRequest') |
||
| 172 | ->willReturn($request); |
||
| 173 | |||
| 174 | $this->image->getMetaTags('some_tag', array('some_value', 'some_value_2', 'some_value_3')); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function testGettingTagsWithVariationServiceThrowsInvalidVariationException() |
||
| 178 | { |
||
| 179 | $this->translationHelper->expects($this->once()) |
||
| 180 | ->method('getTranslatedField') |
||
| 181 | ->willReturn($this->field); |
||
| 182 | |||
| 183 | $this->fieldHelper->expects($this->once()) |
||
| 184 | ->method('isFieldEmpty') |
||
| 185 | ->willReturn(false); |
||
| 186 | |||
| 187 | $this->variationHandler->expects($this->once()) |
||
| 188 | ->method('getVariation') |
||
| 189 | ->willThrowException(new InvalidVariationException('name', 'type')); |
||
| 190 | |||
| 191 | $request = $this->getMockBuilder(Request::class) |
||
| 192 | ->disableOriginalConstructor() |
||
| 193 | ->setMethods(array()) |
||
| 194 | ->getMock(); |
||
| 195 | |||
| 196 | $this->requestStack->expects($this->once()) |
||
| 197 | ->method('getCurrentRequest') |
||
| 198 | ->willReturn($request); |
||
| 199 | |||
| 200 | $this->logger->expects($this->once()) |
||
| 201 | ->method('error'); |
||
| 202 | |||
| 203 | $this->image->getMetaTags('some_tag', array('some_value', 'some_value_2', 'some_value_3')); |
||
| 204 | } |
||
| 205 | |||
| 206 | public function testGettingTagsWithVariationServiceThrowsSourceImageNotFoundException() |
||
| 207 | { |
||
| 208 | $this->translationHelper->expects($this->once()) |
||
| 209 | ->method('getTranslatedField') |
||
| 210 | ->willReturn($this->field); |
||
| 211 | |||
| 212 | $this->fieldHelper->expects($this->once()) |
||
| 213 | ->method('isFieldEmpty') |
||
| 214 | ->willReturn(false); |
||
| 215 | |||
| 216 | $this->variationHandler->expects($this->once()) |
||
| 217 | ->method('getVariation') |
||
| 218 | ->willThrowException(new SourceImageNotFoundException('message')); |
||
| 219 | |||
| 220 | $request = $this->getMockBuilder(Request::class) |
||
| 221 | ->disableOriginalConstructor() |
||
| 222 | ->setMethods(array()) |
||
| 223 | ->getMock(); |
||
| 224 | |||
| 225 | $this->requestStack->expects($this->once()) |
||
| 226 | ->method('getCurrentRequest') |
||
| 227 | ->willReturn($request); |
||
| 228 | |||
| 229 | $this->logger->expects($this->once()) |
||
| 230 | ->method('error'); |
||
| 231 | |||
| 232 | $this->image->getMetaTags('some_tag', array('some_value', 'some_value_2', 'some_value_3')); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function testGettingTagsWithMultipleArgumentsInArray() |
||
| 236 | { |
||
| 237 | $this->translationHelper->expects($this->once()) |
||
| 238 | ->method('getTranslatedField') |
||
| 239 | ->willReturn($this->field); |
||
| 240 | |||
| 241 | $this->image->getMetaTags('some_tag', array('some_value', 'some_value_2')); |
||
| 242 | } |
||
| 243 | } |
||
| 244 |