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 |
||
| 8 | class FilterTest extends \PHPUnit_Framework_TestCase |
||
| 9 | { |
||
| 10 | const PHPUNIT_UTIL_XML_PROXY_CLASS = 'Paraunit\Proxy\PHPUnitUtilXMLProxy'; |
||
| 11 | const FILE_ITERATOR_FACADE_CLASS = '\File_Iterator_Facade'; |
||
| 12 | |||
| 13 | /** @var string */ |
||
| 14 | private $absoluteConfigBaseDir; |
||
| 15 | |||
| 16 | public function __construct($name = null, array $data = array(), $dataName = '') |
||
| 17 | { |
||
| 18 | parent::__construct($name, $data, $dataName); |
||
| 19 | $this->absoluteConfigBaseDir = realpath(__DIR__ . '/../../Stub/StubbedXMLConfigs/') . DIRECTORY_SEPARATOR; |
||
| 20 | } |
||
| 21 | |||
| 22 | public function testFilterTestFiles_gets_only_requested_testsuite() |
||
| 23 | { |
||
| 24 | $configFile = $this->absoluteConfigBaseDir . 'stubbed_for_filter_test.xml'; |
||
| 25 | $configFilePhpUnit = new PHPUnitConfigFile($configFile); |
||
| 26 | |||
| 27 | $testSuiteName = 'test_only_requested_testsuite'; |
||
| 28 | |||
| 29 | $utilXml = $this->prophesize(static::PHPUNIT_UTIL_XML_PROXY_CLASS); |
||
| 30 | $utilXml->loadFile($configFile, false, true, true) |
||
| 31 | ->willReturn($this->getStubbedXMLConf($configFile)) |
||
| 32 | ->shouldBeCalled(); |
||
| 33 | |||
| 34 | $file1 = $this->absoluteConfigBaseDir . './only/selected/test/suite/OnlyTestSuiteTest.php'; |
||
| 35 | $file2 = $this->absoluteConfigBaseDir . './other/test/suite/OtherTest.php'; |
||
| 36 | |||
| 37 | $fileIterator = $this->prophesize(static::FILE_ITERATOR_FACADE_CLASS); |
||
| 38 | $fileIterator->getFilesAsArray($this->absoluteConfigBaseDir . './only/selected/test/suite/', 'Test.php', '', array()) |
||
| 39 | ->willReturn(array($file1)) |
||
| 40 | ->shouldBeCalledTimes(1); |
||
| 41 | $fileIterator->getFilesAsArray($this->absoluteConfigBaseDir . './other/test/suite/', 'Test.php', '', array()) |
||
| 42 | ->willReturn(array($file2)) |
||
| 43 | ->shouldNotBeCalled(); |
||
| 44 | |||
| 45 | $filter = new Filter($utilXml->reveal(), $fileIterator->reveal()); |
||
| 46 | |||
| 47 | $result = $filter->filterTestFiles($configFilePhpUnit, $testSuiteName); |
||
| 48 | |||
| 49 | $this->assertCount(1, $result); |
||
| 50 | $this->assertEquals(array($file1), $result); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function testFilterTestFiles_supports_suffix_attribute() |
||
| 54 | { |
||
| 55 | $configFile = $this->absoluteConfigBaseDir . 'stubbed_for_suffix_test.xml'; |
||
| 56 | $configFilePhpUnit = new PHPUnitConfigFile($configFile); |
||
| 57 | |||
| 58 | $utilXml = $this->prophesize(static::PHPUNIT_UTIL_XML_PROXY_CLASS); |
||
| 59 | $utilXml->loadFile($configFile, false, true, true) |
||
| 60 | ->willReturn($this->getStubbedXMLConf($configFile)) |
||
| 61 | ->shouldBeCalled(); |
||
| 62 | |||
| 63 | $file1 = $this->absoluteConfigBaseDir . './only/selected/test/suite/OnlyTestSuiteTest.php'; |
||
| 64 | $file2 = $this->absoluteConfigBaseDir . './other/test/suite/OtherTest.php'; |
||
| 65 | |||
| 66 | $fileIterator = $this->prophesize(static::FILE_ITERATOR_FACADE_CLASS); |
||
| 67 | $fileIterator->getFilesAsArray($this->absoluteConfigBaseDir . './only/selected/test/suite/', 'TestSuffix.php', '', array()) |
||
| 68 | ->willReturn(array($file1)) |
||
| 69 | ->shouldBeCalledTimes(1); |
||
| 70 | $fileIterator->getFilesAsArray($this->absoluteConfigBaseDir . './other/test/suite/', 'Test.php', '', array()) |
||
| 71 | ->willReturn(array($file2)) |
||
| 72 | ->shouldBeCalledTimes(1); |
||
| 73 | |||
| 74 | $filter = new Filter($utilXml->reveal(), $fileIterator->reveal()); |
||
| 75 | |||
| 76 | $result = $filter->filterTestFiles($configFilePhpUnit); |
||
| 77 | $this->assertEquals(array($file1, $file2), $result); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function testFilterTestFiles_supports_prefix_attribute() |
||
| 81 | { |
||
| 82 | $configFile = $this->absoluteConfigBaseDir . 'stubbed_for_prefix_test.xml'; |
||
| 83 | $configFilePhpUnit = new PHPUnitConfigFile($configFile); |
||
| 84 | |||
| 85 | $utilXml = $this->prophesize(static::PHPUNIT_UTIL_XML_PROXY_CLASS); |
||
| 86 | $utilXml->loadFile($configFile, false, true, true) |
||
| 87 | ->willReturn($this->getStubbedXMLConf($configFile)) |
||
| 88 | ->shouldBeCalled(); |
||
| 89 | |||
| 90 | $file1 = $this->absoluteConfigBaseDir . './only/selected/test/suite/TestPrefixOneTest.php'; |
||
| 91 | $file2 = $this->absoluteConfigBaseDir . './other/test/suite/OtherTest.php'; |
||
| 92 | |||
| 93 | $fileIterator = $this->prophesize(static::FILE_ITERATOR_FACADE_CLASS); |
||
| 94 | $fileIterator->getFilesAsArray($this->absoluteConfigBaseDir . './only/selected/test/suite/', 'Test.php', 'TestPrefix', array()) |
||
| 95 | ->willReturn(array($file1)) |
||
| 96 | ->shouldBeCalledTimes(1); |
||
| 97 | $fileIterator->getFilesAsArray($this->absoluteConfigBaseDir . './other/test/suite/', 'Test.php', '', array()) |
||
| 98 | ->willReturn(array($file2)) |
||
| 99 | ->shouldBeCalledTimes(1); |
||
| 100 | |||
| 101 | $filter = new Filter($utilXml->reveal(), $fileIterator->reveal()); |
||
| 102 | |||
| 103 | $result = $filter->filterTestFiles($configFilePhpUnit); |
||
| 104 | $this->assertEquals(array($file1, $file2), $result); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function testFilterTestFiles_supports_exclude_nodes() |
||
| 108 | { |
||
| 109 | $configFile = $this->absoluteConfigBaseDir . 'stubbed_for_node_exclude.xml'; |
||
| 110 | $configFilePhpUnit = new PHPUnitConfigFile($configFile); |
||
| 111 | |||
| 112 | $utilXml = $this->prophesize(static::PHPUNIT_UTIL_XML_PROXY_CLASS); |
||
| 113 | $utilXml->loadFile($configFile, false, true, true) |
||
| 114 | ->willReturn($this->getStubbedXMLConf($configFile)) |
||
| 115 | ->shouldBeCalled(); |
||
| 116 | |||
| 117 | $excludeArray1 = array( |
||
| 118 | '/path/to/exclude1', |
||
| 119 | '/path/to/exclude2', |
||
| 120 | ); |
||
| 121 | |||
| 122 | $excludeArray2 = array( |
||
| 123 | '/path/to/exclude3', |
||
| 124 | '/path/to/exclude4', |
||
| 125 | ); |
||
| 126 | |||
| 127 | $file1 = $this->absoluteConfigBaseDir . './only/selected/test/suite/TestPrefixOneTest.php'; |
||
| 128 | $file2 = $this->absoluteConfigBaseDir . './other/test/suite/OtherTest.php'; |
||
| 129 | |||
| 130 | $fileIterator = $this->prophesize(static::FILE_ITERATOR_FACADE_CLASS); |
||
| 131 | $fileIterator->getFilesAsArray($this->absoluteConfigBaseDir . './only/selected/test/suite/', 'Test.php', 'TestPrefix', $excludeArray1) |
||
| 132 | ->willReturn(array($file1)) |
||
| 133 | ->shouldBeCalledTimes(1); |
||
| 134 | $fileIterator->getFilesAsArray($this->absoluteConfigBaseDir . './other/test/suite/', 'Test.php', '', $excludeArray2) |
||
| 135 | ->willReturn(array($file2)) |
||
| 136 | ->shouldBeCalledTimes(1); |
||
| 137 | |||
| 138 | $filter = new Filter($utilXml->reveal(), $fileIterator->reveal()); |
||
| 139 | |||
| 140 | $result = $filter->filterTestFiles($configFilePhpUnit); |
||
| 141 | $this->assertEquals(array($file1, $file2), $result); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function testFilterTestFiles_avoids_duplicate_runs() |
||
| 145 | { |
||
| 146 | $configFile = $this->absoluteConfigBaseDir . 'stubbed_for_filter_test.xml'; |
||
| 147 | $configFilePhpUnit = new PHPUnitConfigFile($configFile); |
||
| 148 | |||
| 149 | $utilXml = $this->prophesize(static::PHPUNIT_UTIL_XML_PROXY_CLASS); |
||
| 150 | $utilXml->loadFile($configFile, false, true, true) |
||
| 151 | ->willReturn($this->getStubbedXMLConf($configFile)) |
||
| 152 | ->shouldBeCalled(); |
||
| 153 | |||
| 154 | $file = $this->absoluteConfigBaseDir . './only/selected/test/suite/SameFile.php'; |
||
| 155 | |||
| 156 | $fileIterator = $this->prophesize(static::FILE_ITERATOR_FACADE_CLASS); |
||
| 157 | $fileIterator->getFilesAsArray($this->absoluteConfigBaseDir . './only/selected/test/suite/', 'Test.php', '', array()) |
||
| 158 | ->willReturn(array($file)) |
||
| 159 | ->shouldBeCalledTimes(1); |
||
| 160 | $fileIterator->getFilesAsArray($this->absoluteConfigBaseDir . './other/test/suite/', 'Test.php', '', array()) |
||
| 161 | ->willReturn(array($file)) |
||
| 162 | ->shouldBeCalledTimes(1); |
||
| 163 | |||
| 164 | $filter = new Filter($utilXml->reveal(), $fileIterator->reveal()); |
||
| 165 | |||
| 166 | $result = $filter->filterTestFiles($configFilePhpUnit); |
||
| 167 | $this->assertCount(1, $result); |
||
| 168 | $this->assertEquals(array($file), $result); |
||
| 169 | } |
||
| 170 | |||
| 171 | public function testFilterTestFiles_supports_file_nodes() |
||
| 172 | { |
||
| 173 | $configFile = $this->absoluteConfigBaseDir . 'stubbed_for_node_file.xml'; |
||
| 174 | $configFilePhpUnit = new PHPUnitConfigFile($configFile); |
||
| 175 | |||
| 176 | $utilXml = $this->prophesize(static::PHPUNIT_UTIL_XML_PROXY_CLASS); |
||
| 177 | $utilXml->loadFile($configFile, false, true, true) |
||
| 178 | ->willReturn($this->getStubbedXMLConf($configFile)) |
||
| 179 | ->shouldBeCalled(); |
||
| 180 | |||
| 181 | $file1 = $this->absoluteConfigBaseDir . './only/selected/test/suite/TestPrefixOneTest.php'; |
||
| 182 | $file2 = $this->absoluteConfigBaseDir . './other/test/suite/OtherTest.php'; |
||
| 183 | |||
| 184 | $fileIterator = $this->prophesize(static::FILE_ITERATOR_FACADE_CLASS); |
||
| 185 | $fileIterator->getFilesAsArray($this->absoluteConfigBaseDir . './only/selected/test/suite/', 'Test.php', '', array()) |
||
| 186 | ->willReturn(array($file1)) |
||
| 187 | ->shouldBeCalledTimes(1); |
||
| 188 | $fileIterator->getFilesAsArray($this->absoluteConfigBaseDir . './other/test/suite/', 'Test.php', '', array()) |
||
| 189 | ->willReturn(array($file2)) |
||
| 190 | ->shouldBeCalledTimes(1); |
||
| 191 | |||
| 192 | $filter = new Filter($utilXml->reveal(), $fileIterator->reveal()); |
||
| 193 | |||
| 194 | $result = $filter->filterTestFiles($configFilePhpUnit); |
||
| 195 | $this->assertEquals( |
||
| 196 | array( |
||
| 197 | $file1, |
||
| 198 | $this->absoluteConfigBaseDir . './this/file.php', |
||
| 199 | $this->absoluteConfigBaseDir . './this/file2.php', |
||
| 200 | $file2, |
||
| 201 | ), |
||
| 202 | $result |
||
| 203 | ); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $fileName |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | * |
||
| 211 | * @throws \Exception |
||
| 212 | */ |
||
| 213 | private function getStubbedXMLConf($fileName) |
||
| 214 | { |
||
| 215 | $filePath = realpath($fileName); |
||
| 216 | |||
| 217 | if (!file_exists($filePath)) { |
||
| 218 | throw new \Exception('Stub XML config file missing: ' . $fileName); |
||
| 219 | } |
||
| 220 | |||
| 221 | return \PHPUnit_Util_XML::loadFile($filePath, false, true, true); |
||
| 222 | } |
||
| 223 | } |
||
| 224 |