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 NetgenOpenGraphRuntimeTest extends TestCase |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var \Netgen\Bundle\OpenGraphBundle\Templating\Twig\Extension\NetgenOpenGraphRuntime |
||
| 17 | */ |
||
| 18 | protected $runtime; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
| 22 | */ |
||
| 23 | protected $collector; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
| 27 | */ |
||
| 28 | protected $renderer; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
| 32 | */ |
||
| 33 | protected $logger; |
||
| 34 | |||
| 35 | public function setUp() |
||
| 36 | { |
||
| 37 | $this->collector = $this->getMockBuilder(Collector::class) |
||
| 38 | ->disableOriginalConstructor() |
||
| 39 | ->setMethods(array('collect')) |
||
| 40 | ->getMock(); |
||
| 41 | |||
| 42 | $this->renderer = $this->getMockBuilder(Renderer::class) |
||
| 43 | ->disableOriginalConstructor() |
||
| 44 | ->setMethods(array('render')) |
||
| 45 | ->getMock(); |
||
| 46 | |||
| 47 | $this->logger = $this->getMockBuilder(NullLogger::class) |
||
| 48 | ->disableOriginalConstructor() |
||
| 49 | ->setMethods(array('error')) |
||
| 50 | ->getMock(); |
||
| 51 | |||
| 52 | $this->runtime = new NetgenOpenGraphRuntime($this->collector, $this->renderer, $this->logger); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function testInstanceOfTwigExtensionInterface() |
||
| 56 | { |
||
| 57 | $this->assertInstanceOf(NetgenOpenGraphRuntime::class, $this->runtime); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @doesNotPerformAssertions |
||
| 62 | */ |
||
| 63 | public function testSetThrowExceptions() |
||
| 64 | { |
||
| 65 | $this->runtime->setThrowExceptions(true); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testGetOpenGraphTags() |
||
| 69 | { |
||
| 70 | $items = array( |
||
| 71 | new Item('tag1', 'some_value'), |
||
| 72 | ); |
||
| 73 | |||
| 74 | $this->collector->expects($this->once()) |
||
| 75 | ->method('collect') |
||
| 76 | ->willReturn($items); |
||
| 77 | |||
| 78 | $result = $this->runtime->getOpenGraphTags(new Content()); |
||
| 79 | |||
| 80 | $this->assertEquals($items, $result); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @expectedException \Exception |
||
| 85 | */ |
||
| 86 | public function testGetOpenGraphTagsWithThrowedException() |
||
| 87 | { |
||
| 88 | $this->collector->expects($this->once()) |
||
| 89 | ->method('collect') |
||
| 90 | ->willThrowException(new \Exception()); |
||
| 91 | |||
| 92 | $this->runtime->setThrowExceptions(true); |
||
| 93 | $this->runtime->getOpenGraphTags(new Content()); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function testGetOpenGraphTagsWithLoggedException() |
||
| 97 | { |
||
| 98 | $this->collector->expects($this->once()) |
||
| 99 | ->method('collect') |
||
| 100 | ->willThrowException(new \Exception()); |
||
| 101 | |||
| 102 | $this->logger->expects($this->once()) |
||
| 103 | ->method('error'); |
||
| 104 | |||
| 105 | $this->runtime->setThrowExceptions(false); |
||
| 106 | $this->runtime->getOpenGraphTags(new Content()); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testRenderOpenGraphTags() |
||
| 110 | { |
||
| 111 | $items = array( |
||
| 112 | new Item('tag1', 'some_value'), |
||
| 113 | ); |
||
| 114 | |||
| 115 | $resultString = '<meta property="tag1" content="some_value" />'; |
||
| 116 | |||
| 117 | $this->collector->expects($this->once()) |
||
| 118 | ->method('collect') |
||
| 119 | ->willReturn($items); |
||
| 120 | |||
| 121 | $this->renderer->expects($this->once()) |
||
| 122 | ->method('render') |
||
| 123 | ->willReturn($resultString); |
||
| 124 | |||
| 125 | $result = $this->runtime->renderOpenGraphTags(new Content()); |
||
| 126 | |||
| 127 | $this->assertEquals($resultString, $result); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @expectedException \Exception |
||
| 132 | */ |
||
| 133 | public function testRenderOpenGraphTagsWithThrowedException() |
||
| 134 | { |
||
| 135 | $this->collector->expects($this->once()) |
||
| 136 | ->method('collect') |
||
| 137 | ->willReturn(array()); |
||
| 138 | |||
| 139 | $this->renderer->expects($this->once()) |
||
| 140 | ->method('render') |
||
| 141 | ->willThrowException(new \Exception()); |
||
| 142 | |||
| 143 | $this->runtime->setThrowExceptions(true); |
||
| 144 | $this->runtime->renderOpenGraphTags(new Content()); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function testRenderOpenGraphTagsWithLoggedException() |
||
| 148 | { |
||
| 149 | $this->collector->expects($this->once()) |
||
| 150 | ->method('collect') |
||
| 151 | ->willReturn(array()); |
||
| 152 | |||
| 153 | $this->renderer->expects($this->once()) |
||
| 154 | ->method('render') |
||
| 155 | ->willThrowException(new \Exception()); |
||
| 156 | |||
| 157 | $this->logger->expects($this->once()) |
||
| 158 | ->method('error'); |
||
| 159 | |||
| 160 | $this->runtime->setThrowExceptions(false); |
||
| 161 | $this->runtime->renderOpenGraphTags(new Content()); |
||
| 162 | } |
||
| 163 | } |
||
| 164 |