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 |
||
| 9 | class RouteTest extends \PHPUnit_Framework_TestCase { |
||
| 10 | |||
| 11 | public function testThereIsNoRoutes() { |
||
| 12 | |||
| 13 | Url::instance()->setUrl('my-page/my-action/my-param/'); |
||
| 14 | $config = []; |
||
| 15 | $app = new Application($config); |
||
| 16 | $app->controller->load(); |
||
| 17 | |||
| 18 | $this->assertEquals('404', $app->getPage()); |
||
| 19 | $this->assertFalse(Route::instance()->hasCustomUrl()); |
||
| 20 | $this->assertEquals([null, null], Route::instance()->getCustomUrl()); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function testThereIsOneRoute() { |
||
| 24 | Url::instance()->setUrl('my-page/my-action/my-param/'); |
||
| 25 | $config = [ |
||
| 26 | 'route' => [ |
||
| 27 | 'my-page/(.*)' => 'index' |
||
| 28 | ] |
||
| 29 | ]; |
||
| 30 | |||
| 31 | $app = new Application($config); |
||
| 32 | $app->controller->load(); |
||
| 33 | |||
| 34 | $this->assertEquals('index', $app->getPage()); |
||
| 35 | $this->assertTrue(Route::instance()->hasCustomUrl()); |
||
| 36 | $this->assertEquals(['index', null], Route::instance()->getCustomUrl()); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function testRouteToPageNotFound() { |
||
| 40 | Url::instance()->setUrl('my-page/my-action/my-param/'); |
||
| 41 | $config = [ |
||
| 42 | 'route' => [ |
||
| 43 | 'other-page' => 'index' |
||
| 44 | ] |
||
| 45 | ]; |
||
| 46 | |||
| 47 | $app = new Application($config); |
||
| 48 | $app->controller->load(); |
||
| 49 | |||
| 50 | $this->assertEquals('404', $app->getPage()); |
||
| 51 | $this->assertFalse(Route::instance()->hasCustomUrl()); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function testRouteToActionNotFound() { |
||
| 55 | Url::instance()->setUrl('my-page/my-action/my-param/'); |
||
| 56 | $config = [ |
||
| 57 | 'route' => [ |
||
| 58 | 'my-page/(.*)' => 'index/this-action-doeent-exit' |
||
| 59 | ] |
||
| 60 | ]; |
||
| 61 | |||
| 62 | $app = new Application($config); |
||
| 63 | $app->controller->load(); |
||
| 64 | |||
| 65 | $this->assertEquals('404', $app->getPage()); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testRouteWithCustomParam() { |
||
| 69 | Url::instance()->setUrl('my-product/list/id/100'); |
||
| 70 | $config = [ |
||
| 71 | 'route' => [ |
||
| 72 | 'my-product/(.*)/id/(.*)' => 'index/index/$1/teste/$2' |
||
| 73 | ] |
||
| 74 | ]; |
||
| 75 | |||
| 76 | $app = new Application($config); |
||
| 77 | $app->controller->load(); |
||
| 78 | |||
| 79 | $this->assertEquals('index', $app->getPage()); |
||
| 80 | $this->assertEquals('list', $app->getParam(2)); |
||
| 81 | $this->assertEquals('teste', $app->getParam(3)); |
||
| 82 | $this->assertEquals('100', $app->getParam(4)); |
||
| 83 | $this->assertEquals(['index', 'index', 'list', 'teste', '100'], array_filter(Route::instance()->getCustomUrl())); |
||
| 84 | } |
||
| 85 | |||
| 86 | } |
||
| 87 |