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 |
||
| 12 | class IndexControllerTest extends TestCase |
||
| 13 | { |
||
| 14 | public function createApplication() |
||
| 15 | { |
||
| 16 | return new Application(__DIR__.'/../../default'); |
||
| 17 | } |
||
| 18 | |||
| 19 | public function testSayHello() |
||
| 20 | { |
||
| 21 | $response = $this->app->handleRequest($this->request('GET', '/')); |
||
| 22 | $this->equalsJson($response, ['foo' => 'bar']); |
||
| 23 | $response = $this->app->handleRequest($this->request('GET', '/?foo=var')); |
||
| 24 | $this->equalsJson($response, ['foo' => 'var']); |
||
| 25 | $response = $this->app->handleRequest($this->request('GET', '/?foo=bar')); |
||
| 26 | $this->equalsJson($response, ['foo' => 'bar']); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function testDb() |
||
| 30 | { |
||
| 31 | $response = $this->app->handleRequest($this->request('GET', '/db')); |
||
| 32 | |||
| 33 | $this->equalsStatus($response, 200); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function testHandleDynamicRequest() |
||
| 37 | { |
||
| 38 | $response = $this->app->handleRequest($this->request('GET', '/foo/bar')); |
||
| 39 | $this->equalsJson($response, ['foo' => 'bar']); |
||
| 40 | $response = $this->app->handleRequest($this->request('GET', '/foo/foobar')); |
||
| 41 | $this->equalsJson($response, ['foo' => 'foobar']); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function testHandleMiddlewareRequest() |
||
| 45 | { |
||
| 46 | $response = $this->app->handleRequest($this->request('POST', '/foo/bar')); |
||
| 47 | $this->equalsJson($response, ['foo' => 'middleware']); |
||
| 48 | $response = $this->app->handleRequest($this->request('POST', '/foo/not')); |
||
| 49 | $this->equalsJson($response, ['foo' => 'bar']); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function testModel() |
||
| 53 | { |
||
| 54 | $response = $this->app->handleRequest($this->request('GET', '/model')); |
||
| 55 | $this->assertEquals(200, $response->getStatusCode()); |
||
| 56 | $this->isSuccessful($response); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testAuth() |
||
| 60 | { |
||
| 61 | $response = $this->app->handleRequest($this->request('GET', '/auth')); |
||
| 62 | |||
| 63 | $this->assertEquals(401, $response->getStatusCode()); |
||
| 64 | |||
| 65 | $this->equalsJson($response, [ |
||
| 66 | 'msg' => 'not allow access', |
||
| 67 | 'code' => 401, |
||
| 68 | ]); |
||
| 69 | |||
| 70 | $response = $this->app->handleRequest($this->request('GET', 'http://foo:[email protected]/auth', [ |
||
| 71 | 'PHP_AUTH_USER' => 'foo', |
||
| 72 | 'PHP_AUTH_PW' => 'bar', |
||
| 73 | ])); |
||
| 74 | |||
| 75 | $this->assertEquals(200, $response->getStatusCode()); |
||
| 76 | |||
| 77 | $this->equalsJson($response, [ |
||
| 78 | 'foo' => 'bar', |
||
| 79 | ]); |
||
| 80 | } |
||
| 81 | } |
||
| 82 |