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 |
||
| 11 | class FileTest extends \PHPUnit_Framework_TestCase |
||
| 12 | { |
||
| 13 | public static $full_log_location; |
||
| 14 | public static $log_file_name; |
||
| 15 | public static $log_file_path; |
||
| 16 | |||
| 17 | public static function setUpBeforeClass() |
||
| 18 | { |
||
| 19 | self::$log_file_name = "File_test.log"; |
||
| 20 | self::$log_file_path = DILMUN_LOGS_DIR; |
||
| 21 | self::$full_log_location = DILMUN_LOGS_DIR . "File_test.log"; |
||
| 22 | |||
| 23 | if(file_exists(self::$full_log_location)) |
||
| 24 | { |
||
| 25 | unlink(self::$full_log_location); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | |||
| 29 | protected function assertPreconditions() |
||
| 30 | { |
||
| 31 | $this->assertFileNotExists(self::$full_log_location); |
||
| 32 | } |
||
| 33 | |||
| 34 | protected function setUp() |
||
| 35 | { |
||
| 36 | if(file_exists(self::$full_log_location)) |
||
| 37 | { |
||
| 38 | unlink(self::$full_log_location); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | public static function tearDownAfterClass() |
||
| 43 | { |
||
| 44 | if(file_exists(self::$full_log_location)) |
||
| 45 | { |
||
| 46 | unlink(self::$full_log_location); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | public function testGettingSetFileName() |
||
| 51 | { |
||
| 52 | $some_file = new File(); |
||
| 53 | |||
| 54 | $some_file->setFileName(self::$log_file_name); |
||
| 55 | |||
| 56 | $result_file_name = $some_file->getFileName(); |
||
| 57 | |||
| 58 | $this->assertEquals(self::$log_file_name, $result_file_name); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function testGettingSetFilePath() |
||
| 62 | { |
||
| 63 | $some_file = new File(); |
||
| 64 | |||
| 65 | $some_file->setFilePath(self::$log_file_path); |
||
| 66 | |||
| 67 | $trimmed_file_path = rtrim(self::$log_file_path, '\/'); |
||
| 68 | |||
| 69 | $result_file_path = $some_file->getFilePath(); |
||
| 70 | |||
| 71 | $this->assertEquals($trimmed_file_path, $result_file_path); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function testClearingFileOfContents() |
||
| 75 | { |
||
| 76 | $log_file = new File(self::$full_log_location); |
||
| 77 | |||
| 78 | $log_file->initialize(); |
||
| 79 | |||
| 80 | $string_to_write = "This should be written to the log file!"; |
||
| 81 | |||
| 82 | $log_file->write($string_to_write); |
||
| 83 | |||
| 84 | //Must call clearstatcache to report on file size accurately! |
||
| 85 | clearstatcache(); |
||
| 86 | |||
| 87 | $written_file_size = filesize(self::$full_log_location); |
||
| 88 | |||
| 89 | $this->assertGreaterThan(0, $written_file_size); |
||
| 90 | |||
| 91 | $file_cleared = $log_file->clearFile(); |
||
| 92 | |||
| 93 | //Must call clearstatcache to report on file size accurately! |
||
| 94 | clearstatcache(); |
||
| 95 | |||
| 96 | $cleared_file_size = filesize(self::$full_log_location); |
||
| 97 | |||
| 98 | $this->assertTrue($file_cleared); |
||
| 99 | $this->assertEquals(0, $cleared_file_size); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function testClearFileReturnsFalseWhenFileNotInitialized() |
||
| 103 | { |
||
| 104 | $some_file = new File(); |
||
| 105 | |||
| 106 | $file_cleared = $some_file->clearFile(); |
||
| 107 | |||
| 108 | $this->assertFalse($file_cleared); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function testFileInitializationCreatesFile() |
||
| 112 | { |
||
| 113 | $log_file = new File(self::$full_log_location); |
||
| 114 | |||
| 115 | $log_file->initialize(); |
||
| 116 | |||
| 117 | $this->assertFileExists(self::$full_log_location); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function testFileInitializationCreatesFileFromSeparateNameAndPath() |
||
| 121 | { |
||
| 122 | $log_file = new File(); |
||
| 123 | |||
| 124 | $log_file->setFileName(self::$log_file_name); |
||
| 125 | $log_file->setFilePath(self::$log_file_path); |
||
| 126 | |||
| 127 | $log_file->initialize(); |
||
| 128 | |||
| 129 | $this->assertFileExists(self::$full_log_location); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @dataProvider badNameProvider |
||
| 134 | */ |
||
| 135 | public function testFileInitiailizationReturnsFalseWhenAttemptingToInitializeWithBadData($bad_name) |
||
| 136 | { |
||
| 137 | $bad_file = new File($bad_name); |
||
| 138 | |||
| 139 | $bad_file->suppressInitFailureMessage(); |
||
| 140 | |||
| 141 | $file_initialized = $bad_file->initialize(); |
||
| 142 | |||
| 143 | $this->assertFalse($file_initialized); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @dataProvider badNameProvider |
||
| 148 | */ |
||
| 149 | public function testWritingReturnsFalseWhenAttemptingToWriteToInvalidPointer($bad_name) |
||
| 150 | { |
||
| 151 | $bad_file = new File($bad_name); |
||
| 152 | |||
| 153 | $bad_file->suppressInitFailureMessage(); |
||
| 154 | |||
| 155 | $bad_file->initialize(); |
||
| 156 | |||
| 157 | $write_succeeded = $bad_file->write("This should never be written!"); |
||
| 158 | |||
| 159 | $this->assertFalse($write_succeeded); |
||
| 160 | } |
||
| 161 | |||
| 162 | public function testWritingReturnsFalseWhenAttemptingToWriteToUninitializedFile() |
||
| 163 | { |
||
| 164 | $bad_file = new File(); |
||
| 165 | |||
| 166 | $write_succeeded = $bad_file->write("This should never be written!"); |
||
| 167 | |||
| 168 | $this->assertFalse($write_succeeded); |
||
| 169 | } |
||
| 170 | |||
| 171 | public function testSuppressingInitFailureMessageSetsDisplayInitFailureToFalse() |
||
| 172 | { |
||
| 173 | $stub_file = new File(); |
||
| 174 | |||
| 175 | $stub_file->suppressInitFailureMessage(); |
||
| 176 | |||
| 177 | $this->assertEquals(false, \PHPUnit_Framework_Assert::readAttribute($stub_file, "display_init_failure")); |
||
| 178 | } |
||
| 179 | |||
| 180 | public function testDisplayingInitFailureMessageSetsDisplayInitFailureToTrue() |
||
| 181 | { |
||
| 182 | $stub_file = new File(); |
||
| 183 | |||
| 184 | $stub_file->suppressInitFailureMessage(); |
||
| 185 | |||
| 186 | $this->assertEquals(false, \PHPUnit_Framework_Assert::readAttribute($stub_file, "display_init_failure")); |
||
| 187 | |||
| 188 | $stub_file->displayInitFailureMessage(); |
||
| 189 | |||
| 190 | $this->assertEquals(true, \PHPUnit_Framework_Assert::readAttribute($stub_file, "display_init_failure")); |
||
| 191 | } |
||
| 192 | |||
| 193 | public function testSuppressingInitFailureMessagePreventsFailureMessage() |
||
| 194 | { |
||
| 195 | $expected_output = ""; |
||
| 196 | |||
| 197 | $stub_file = new File(); |
||
| 198 | |||
| 199 | $stub_file->suppressInitFailureMessage(); |
||
| 200 | |||
| 201 | $this->assertEquals(false, \PHPUnit_Framework_Assert::readAttribute($stub_file, "display_init_failure")); |
||
| 202 | |||
| 203 | ob_start(); |
||
| 204 | |||
| 205 | $stub_file->initialize(); |
||
| 206 | |||
| 207 | $output = ob_get_contents(); |
||
| 208 | |||
| 209 | ob_end_clean(); |
||
| 210 | |||
| 211 | $this->assertEquals($expected_output, $output); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function testDisplayingInitFailureMessageDisplaysFailureMessage() |
||
| 215 | { |
||
| 216 | $expected_output = "Cannot open specified log file for writing: : SplFileObject::__construct(): Filename cannot be empty\n"; |
||
| 217 | |||
| 218 | $this->expectOutputString($expected_output); |
||
| 219 | |||
| 220 | $stub_file = new File(); |
||
| 221 | |||
| 222 | $stub_file->suppressInitFailureMessage(); |
||
| 223 | |||
| 224 | $this->assertEquals(false, \PHPUnit_Framework_Assert::readAttribute($stub_file, "display_init_failure")); |
||
| 225 | |||
| 226 | $stub_file->displayInitFailureMessage(); |
||
| 227 | |||
| 228 | $this->assertEquals(true, \PHPUnit_Framework_Assert::readAttribute($stub_file, "display_init_failure")); |
||
| 229 | |||
| 230 | $stub_file->initialize(); |
||
| 231 | } |
||
| 232 | |||
| 233 | public function testSendInitFailureEmailAddressSetsPrivateEmailAddress() |
||
| 234 | { |
||
| 235 | $email_address = "[email protected]"; |
||
| 236 | |||
| 237 | $stub_file = new File(); |
||
| 238 | |||
| 239 | $stub_file->sendInitFailureEmailAddress($email_address); |
||
| 240 | |||
| 241 | $this->assertEquals($email_address, \PHPUnit_Framework_Assert::readAttribute($stub_file, "init_failure_email_address")); |
||
| 242 | } |
||
| 243 | |||
| 244 | public function testSendInitFailureEmailAddressSetAttemptsToSendErrorEmail() |
||
| 245 | { |
||
| 246 | $email_address = "[email protected]"; |
||
| 247 | |||
| 248 | $stub_file = new File(); |
||
| 249 | |||
| 250 | $stub_file->sendInitFailureEmailAddress($email_address); |
||
| 251 | |||
| 252 | $this->assertEquals($email_address, \PHPUnit_Framework_Assert::readAttribute($stub_file, "init_failure_email_address")); |
||
| 253 | |||
| 254 | $stub_file->suppressInitFailureMessage(); |
||
| 255 | |||
| 256 | $stub_file->initialize(); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Data provider of invalid file names. |
||
| 261 | * |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function badNameProvider() |
||
| 265 | { |
||
| 266 | return array( |
||
| 267 | array(""), |
||
| 268 | array('t|-|1$is/IN\/@L1d!'), |
||
| 269 | array(self::$log_file_path) |
||
| 270 | ); |
||
| 271 | } |
||
| 272 | } |