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 |
||
| 13 | class TextLineTest extends HandlerBaseTest |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var TextLine |
||
| 17 | */ |
||
| 18 | protected $textLine; |
||
| 19 | |||
| 20 | public function setUp() |
||
| 21 | { |
||
| 22 | $this->fieldHelper = $this->getMockBuilder(FieldHelper::class) |
||
| 23 | ->disableOriginalConstructor() |
||
| 24 | ->setMethods(array('isFieldEmpty')) |
||
| 25 | ->getMock(); |
||
| 26 | |||
| 27 | $this->translationHelper = $this->getMockBuilder(TranslationHelper::class) |
||
| 28 | ->disableOriginalConstructor() |
||
| 29 | ->setMethods(array('getTranslatedField')) |
||
| 30 | ->getMock(); |
||
| 31 | |||
| 32 | $this->content = $this->getMockBuilder(Content::class) |
||
| 33 | ->disableOriginalConstructor() |
||
| 34 | ->setMethods(array()) |
||
| 35 | ->getMock(); |
||
| 36 | |||
| 37 | $this->textLine = new TextLine($this->fieldHelper, $this->translationHelper); |
||
| 38 | $this->textLine->setContent($this->content); |
||
| 39 | |||
| 40 | $this->field = new Field(array('value' => new Value())); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testInstanceOfHandlerInterface() |
||
| 44 | { |
||
| 45 | $this->assertInstanceOf(HandlerInterface::class, $this->textLine); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
||
| 50 | * @expectedExceptionMessage Argument '$params[0]' is invalid: Field type handlers require at least a field identifier. |
||
| 51 | */ |
||
| 52 | public function testGettingTagsWithoutFieldIdentifier() |
||
| 53 | { |
||
| 54 | $this->textLine->getMetaTags('some_tag', array()); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
||
| 59 | * @expectedExceptionMessage Argument '$params[0]' is invalid: Field 'some_value' does not exist in content. |
||
| 60 | */ |
||
| 61 | public function testGettingTagsWithNonExistentField() |
||
| 62 | { |
||
| 63 | $this->translationHelper->expects($this->once()) |
||
| 64 | ->method('getTranslatedField') |
||
| 65 | ->willReturn(null); |
||
| 66 | |||
| 67 | $this->textLine->getMetaTags('some_tag', array('some_value')); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
||
| 72 | * @expectedExceptionMessage Argument '$params[0]' is invalid: Netgen\Bundle\OpenGraphBundle\Handler\FieldType\TextLine field type handler does not support field with identifier ''. |
||
| 73 | */ |
||
| 74 | public function testGettingTagsWithUnsupportedField() |
||
| 75 | { |
||
| 76 | $this->translationHelper->expects($this->once()) |
||
| 77 | ->method('getTranslatedField') |
||
| 78 | ->willReturn(new Field()); |
||
| 79 | |||
| 80 | $this->textLine->getMetaTags('some_tag', array('some_value')); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function testGettingTagsWithEmptyField() |
||
| 84 | { |
||
| 85 | $this->translationHelper->expects($this->once()) |
||
| 86 | ->method('getTranslatedField') |
||
| 87 | ->willReturn($this->field); |
||
| 88 | |||
| 89 | $this->fieldHelper->expects($this->once()) |
||
| 90 | ->method('isFieldEmpty') |
||
| 91 | ->willReturn(true); |
||
| 92 | |||
| 93 | $this->textLine->getMetaTags('some_tag', array('some_value')); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function testGettingTags() |
||
| 97 | { |
||
| 98 | $this->translationHelper->expects($this->once()) |
||
| 99 | ->method('getTranslatedField') |
||
| 100 | ->willReturn($this->field); |
||
| 101 | |||
| 102 | $this->textLine->getMetaTags('some_tag', array('some_value')); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function testGettingTagsWithMultipleArgumentsInArray() |
||
| 106 | { |
||
| 107 | $this->translationHelper->expects($this->once()) |
||
| 108 | ->method('getTranslatedField') |
||
| 109 | ->willReturn($this->field); |
||
| 110 | |||
| 111 | $this->textLine->getMetaTags('some_tag', array('some_value', 'some_value_2')); |
||
| 112 | } |
||
| 113 | } |
||
| 114 |