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 |
||
| 10 | class ApplicationTest extends \PHPUnit_Framework_TestCase { |
||
| 11 | |||
| 12 | public function testGetInstanceApp() { |
||
| 13 | Url::instance()->setUrl('index'); |
||
| 14 | $app = new Application(); |
||
| 15 | $this->assertTrue($app instanceof Application); |
||
| 16 | $this->assertTrue(Application::app() instanceof Application); |
||
| 17 | } |
||
| 18 | |||
| 19 | public function testGetName() { |
||
| 20 | $config = ['name' => 'My custom Application Name']; |
||
| 21 | $app = new Application($config); |
||
| 22 | |||
| 23 | $this->assertEquals('My custom Application Name', $app->getName()); |
||
| 24 | $this->assertEquals($app->getName(), $app->getConfig('name')); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function testGetTitle() { |
||
| 28 | Url::instance()->setUrl(''); |
||
| 29 | $app = new Application(); |
||
| 30 | $app->controller->setTitle('Index'); |
||
| 31 | $this->assertEquals('Index', $app->view->getTitle()); |
||
| 32 | |||
| 33 | Url::instance()->setUrl('my-custom-page'); |
||
| 34 | $app2 = new Application(); |
||
| 35 | $this->assertEquals('My Custom Page', $app2->view->getTitle()); |
||
| 36 | |||
| 37 | Url::instance()->setUrl('my-custom-page/my-action'); |
||
| 38 | $app3 = new Application(); |
||
| 39 | $app3->controller->load(); |
||
| 40 | $this->assertNotEquals('My Custom Page', $app3->view->getTitle()); |
||
| 41 | |||
| 42 | $app->controller->setTitle('My Custom Test Title'); |
||
| 43 | |||
| 44 | $app->view->addData('title','My Custom Test Title'); |
||
| 45 | $this->assertEquals('My Custom Test Title' , $app->view->getTitle()); |
||
| 46 | |||
| 47 | $app->pageNotFound(); |
||
| 48 | $this->assertNotEquals('My Custom Test Title' , $app->view->getTitle()); |
||
| 49 | |||
| 50 | |||
| 51 | } |
||
| 52 | |||
| 53 | public function testGetPage() { |
||
| 54 | Url::instance()->setUrl('my-page/my-action/first-param/2nd-param/3rd-param'); |
||
| 55 | $app = new Application(); |
||
| 56 | |||
| 57 | $this->assertEquals('my-page', $app->getPage()); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function testGetParamDefault() { |
||
| 61 | Url::instance()->setUrl('my-page'); |
||
| 62 | $app = new Application(); |
||
| 63 | |||
| 64 | $this->assertEquals('index', $app->getParam(1)); |
||
| 65 | $this->assertNotEquals('', $app->getParam(1)); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testGetAction() { |
||
| 69 | Url::instance()->setUrl('my-page/my-action/first-param/2nd-param/3rd-param'); |
||
| 70 | $app = new Application(); |
||
| 71 | |||
| 72 | $this->assertEquals('myAction', $app->controller->getAction()); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function testGetParam() { |
||
| 76 | Url::instance()->setUrl('my-page/my-action/second-param/3rd-param'); |
||
| 77 | $app = new Application(); |
||
| 78 | |||
| 79 | $this->assertEquals('my-page', $app->getParam(0)); |
||
| 80 | $this->assertEquals('my-action', $app->getParam(1)); |
||
| 81 | $this->assertEquals('second-param', $app->getParam(2)); |
||
| 82 | $this->assertEquals('3rd-param', $app->getParam(3)); |
||
| 83 | $this->assertEquals('', $app->getParam(4)); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function testIsErrorPage() { |
||
| 87 | Url::instance()->setUrl('index'); |
||
| 88 | $app = new Application(); |
||
| 89 | $this->assertFalse($app->isErrorPage()); |
||
| 90 | |||
| 91 | $app->pageNotFound(); |
||
| 92 | $this->assertTrue($app->isErrorPage()); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function testIsHomePage() { |
||
| 96 | Url::instance()->setUrl(''); |
||
| 97 | $app = new Application(); |
||
| 98 | $this->assertTrue($app->isHomePage()); |
||
| 99 | |||
| 100 | Url::instance()->setUrl('index'); |
||
| 101 | $app2 = new Application(); |
||
| 102 | $this->assertTrue($app2->isHomePage()); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function testIsNotHomePage() { |
||
| 106 | Url::instance()->setUrl('index'); |
||
| 107 | $app = new Application(); |
||
| 108 | $app->pageNotFound(); |
||
| 109 | $this->assertFalse($app->isHomePage()); |
||
| 110 | |||
| 111 | Url::instance()->setUrl('other-page'); |
||
| 112 | $app2 = new Application(); |
||
| 113 | $this->assertFalse($app2->isHomePage()); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function testIsLocalHost() { |
||
| 117 | $this->assertEquals(true, Server::isLocalHost()); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function testGetConfig() { |
||
| 121 | $config = ['year' => 2040]; |
||
| 122 | $app = new Application($config); |
||
| 123 | $this->assertEquals(2040, $app->getConfig('year', 3000)); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function testGetConfigDefault() { |
||
| 127 | $configEmpty = []; |
||
| 128 | $app2 = new Application($configEmpty); |
||
| 129 | $this->assertEquals(3000, $app2->getConfig('year', 3000)); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function testRun() { |
||
| 133 | Url::instance()->setUrl('this-page-doesnt-exist'); |
||
| 134 | $app = new Application(); |
||
| 135 | $this->assertNotEquals('404', $app->getPage()); |
||
| 136 | |||
| 137 | Url::instance()->setUrl('this-page-doesnt-exist'); |
||
| 138 | $app2 = new Application(); |
||
| 139 | |||
| 140 | ob_start(); |
||
| 141 | $app2->run(); |
||
| 142 | ob_end_clean(); |
||
| 143 | $this->assertEquals('404', $app2->getPage()); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function testGetUrl() { |
||
| 147 | $currentUrl = Url::instance()->format('my-page/my-action/param1/param2'); |
||
| 148 | Url::instance()->setUrl($currentUrl); |
||
| 149 | $app = new Application(); |
||
| 150 | $this->assertEquals($currentUrl, $app->getUrl()); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function testValidatePage404() { |
||
| 154 | Url::instance()->setUrl('this-url-doent-exist'); |
||
| 155 | $app = new Application(); |
||
| 156 | $this->assertEquals('This Url Doent Exist', $app->view->getTitle()); |
||
| 157 | |||
| 158 | Url::instance()->setUrl('404'); |
||
| 159 | $app2 = new Application(); |
||
| 160 | $this->assertNotEquals('404', $app2->view->getTitle()); |
||
| 161 | } |
||
| 162 | |||
| 163 | } |
||
| 164 |