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 |
||
| 3 | class DeCaptchaAbstractTest extends PHPUnit_Framework_TestCase |
||
| 4 | { |
||
| 5 | public function testGetBaseUrl() |
||
| 6 | { |
||
| 7 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 8 | // $foo->expects($this->any()) |
||
| 9 | // ->method("baz") |
||
| 10 | // ->will($this->returnValue("You called baz!")); |
||
| 11 | $getBaseUrlCaller = function () { |
||
| 12 | return $this->getBaseUrl(); |
||
| 13 | }; |
||
| 14 | $abstract->domain = 'domain'; |
||
| 15 | $bound = $getBaseUrlCaller->bindTo($abstract, $abstract); |
||
| 16 | $this->assertEquals('http://domain/', $bound()); |
||
| 17 | } |
||
| 18 | |||
| 19 | public function testSetApiKey() |
||
| 20 | { |
||
| 21 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 22 | $abstract->setApiKey('123456val'); |
||
| 23 | $apiKeyValCaller = function () { |
||
| 24 | return $this->apiKey; |
||
| 25 | }; |
||
| 26 | $bound = $apiKeyValCaller->bindTo($abstract, $abstract); |
||
| 27 | $this->assertEquals('123456val', $bound()); |
||
| 28 | |||
| 29 | $abstract->setApiKey(function () { |
||
| 30 | return '123456' . 'fun'; |
||
| 31 | }); |
||
| 32 | $apiKeyFunCaller = function () { |
||
| 33 | return $this->apiKey; |
||
| 34 | }; |
||
| 35 | $bound = $apiKeyFunCaller->bindTo($abstract, $abstract); |
||
| 36 | $this->assertEquals('123456fun', $bound()); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function testGetActionUrl() |
||
| 40 | { |
||
| 41 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 42 | $getBaseUrlGetCodeCaller = function () { |
||
| 43 | $this->captchaId = 123; |
||
| 44 | return $this->getActionUrl('get_code'); |
||
| 45 | }; |
||
| 46 | $getBaseUrlGetBalanceCaller = function () { |
||
| 47 | $this->captchaId = 234; |
||
| 48 | return $this->getActionUrl('get_balance'); |
||
| 49 | }; |
||
| 50 | $abstract->domain = 'domain'; |
||
| 51 | $abstract->setApiKey('123456'); |
||
| 52 | $bound = $getBaseUrlGetCodeCaller->bindTo($abstract, $abstract); |
||
| 53 | $this->assertEquals('http://domain/res.php?key=123456&action=get_code&id=123', $bound()); |
||
| 54 | $bound = $getBaseUrlGetBalanceCaller->bindTo($abstract, $abstract); |
||
| 55 | $this->assertEquals('http://domain/res.php?key=123456&action=get_balance&id=234', $bound()); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function testGetFilePath() |
||
| 59 | { |
||
| 60 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 61 | $getFilePathCaller = function ($val) { |
||
| 62 | return $this->getFilePath($val); |
||
| 63 | }; |
||
| 64 | $bound = $getFilePathCaller->bindTo($abstract, $abstract); |
||
| 65 | $this->assertEquals(__DIR__ . '/data/Captcha.jpg', $bound(__DIR__ . '/data/Captcha.jpg')); |
||
| 66 | $filePathUpload = $bound('https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha.jpg'); |
||
| 67 | $file1 = file_get_contents(__DIR__ . '/data/Captcha.jpg'); |
||
| 68 | $file2 = file_get_contents($filePathUpload); |
||
| 69 | $this->assertEquals($file1, $file2); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 74 | * @expectedExceptionCode 16 |
||
| 75 | */ |
||
| 76 | public function testGetFilePathErrorFileNotFound() |
||
| 77 | { |
||
| 78 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 79 | $abstract->errorLang = \jumper423\decaptcha\core\DeCaptchaErrors::LANG_RU; |
||
| 80 | $getFilePathCaller = function ($val) { |
||
| 81 | return $this->getFilePath($val); |
||
| 82 | }; |
||
| 83 | $bound = $getFilePathCaller->bindTo($abstract, $abstract); |
||
| 84 | $bound(__DIR__ . '/data/Captcha1.jpg'); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 89 | * @expectedExceptionMessage Файл не загрузился: https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123 |
||
| 90 | * @expectedExceptionCode 15 |
||
| 91 | */ |
||
| 92 | public function testGetFilePathErrorFileIsNotLoaded() |
||
| 93 | { |
||
| 94 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 95 | $abstract->errorLang = \jumper423\decaptcha\core\DeCaptchaErrors::LANG_RU; |
||
| 96 | $getFilePathCaller = function ($val) { |
||
| 97 | return $this->getFilePath($val); |
||
| 98 | }; |
||
| 99 | $bound = $getFilePathCaller->bindTo($abstract, $abstract); |
||
| 100 | $bound('https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123'); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function testGetResponse() |
||
| 104 | { |
||
| 105 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 106 | $abstract->domain = 'echo.jsontest.com/aaa/bbb'; |
||
| 107 | $getResponseCaller = function ($val) { |
||
| 108 | return $this->getResponse($val); |
||
| 109 | }; |
||
| 110 | $bound = $getResponseCaller->bindTo($abstract, $abstract); |
||
| 111 | $res = $bound(''); |
||
| 112 | $this->assertEquals('{"res.php":"","aaa":"bbb"}', str_replace("\n", '', str_replace(" ", '', $res))); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function testExecutionDelayed() |
||
| 116 | { |
||
| 117 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 118 | $executionDelayedCaller = function ($second, $call = null) { |
||
| 119 | return $this->executionDelayed($second, $call); |
||
| 120 | }; |
||
| 121 | $bound = $executionDelayedCaller->bindTo($abstract, $abstract); |
||
| 122 | $start = microtime(true); |
||
| 123 | $bound(0); |
||
| 124 | $bound(0.1); |
||
| 125 | $timePassed = microtime(true) - $start; |
||
| 126 | $this->assertTrue(abs($timePassed - 0.1) < 0.025); |
||
| 127 | |||
| 128 | $start = microtime(true); |
||
| 129 | $bound(0.15, function () { |
||
| 130 | sleep(0.2); |
||
| 131 | }); |
||
| 132 | $bound(0.1); |
||
| 133 | $timePassed = microtime(true) - $start; |
||
| 134 | $this->assertTrue(abs($timePassed - 0.25) < 0.025); |
||
| 135 | |||
| 136 | $start = microtime(true); |
||
| 137 | $bound(0.15, function () { |
||
| 138 | sleep(0.2); |
||
| 139 | }); |
||
| 140 | $bound(0.3); |
||
| 141 | $timePassed = microtime(true) - $start; |
||
| 142 | $this->assertTrue(abs($timePassed - 0.45) < 0.025); |
||
| 143 | |||
| 144 | $this->assertEquals(2, $bound(0, function () { |
||
| 145 | return 2; |
||
| 146 | })); |
||
| 147 | $this->assertEquals(null, $bound(0)); |
||
| 148 | } |
||
| 149 | |||
| 150 | public function testGetInUrl() |
||
| 151 | { |
||
| 152 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 153 | $getInUrlCaller = function () { |
||
| 154 | return $this->getInUrl(); |
||
| 155 | }; |
||
| 156 | $abstract->domain = 'domain'; |
||
| 157 | $abstract->setApiKey('123456'); |
||
| 158 | $bound = $getInUrlCaller->bindTo($abstract, $abstract); |
||
| 159 | $this->assertEquals('http://domain/in.php', $bound()); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 164 | * @expectedExceptionCode 4 |
||
| 165 | */ |
||
| 166 | public function testIsError() |
||
| 167 | { |
||
| 168 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 169 | $abstract->errorLang = \jumper423\decaptcha\core\DeCaptchaErrors::LANG_RU; |
||
| 170 | $isErrorCaller = function ($val) { |
||
| 171 | return $this->isError($val); |
||
| 172 | }; |
||
| 173 | $bound = $isErrorCaller->bindTo($abstract, $abstract); |
||
| 174 | $bound('ERROR_IP_NOT_ALLOWED'); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function testIsErrorNot() |
||
| 178 | { |
||
| 179 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 180 | $abstract->errorLang = \jumper423\decaptcha\core\DeCaptchaErrors::LANG_RU; |
||
| 181 | $isErrorCaller = function ($val) { |
||
| 182 | return $this->isError($val); |
||
| 183 | }; |
||
| 184 | $bound = $isErrorCaller->bindTo($abstract, $abstract); |
||
| 185 | $this->assertNull($bound('BALANCE:56')); |
||
| 186 | } |
||
| 187 | } |