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 User_Model_test extends TestCase { |
||
| 4 | private $User_Model; |
||
| 5 | |||
| 6 | public function setUp() { |
||
| 7 | $this->resetInstance(); |
||
| 8 | |||
| 9 | $this->User_Model = new User_Model(); |
||
| 10 | } |
||
| 11 | |||
| 12 | public function test_init_model_while_logged_in() { |
||
| 13 | |||
| 14 | } |
||
| 15 | |||
| 16 | public function test_logged_in_true() { |
||
| 17 | $User_Model = $this->User_Model; |
||
| 18 | |||
| 19 | $ion_auth = $this->getMockBuilder('ion_auth') |
||
| 20 | ->disableOriginalConstructor() |
||
| 21 | ->getMock(); |
||
| 22 | $ion_auth->method('logged_in')->willReturn(TRUE); |
||
| 23 | |||
| 24 | $User_Model->ion_auth = $ion_auth; |
||
| 25 | |||
| 26 | $result = $User_Model->logged_in(); |
||
| 27 | $this->assertTrue($result); |
||
| 28 | } |
||
| 29 | public function test_logged_in_false() { |
||
| 30 | $User_Model = $this->User_Model; |
||
| 31 | |||
| 32 | $ion_auth = $this->getMockBuilder('ion_auth') |
||
| 33 | ->disableOriginalConstructor() |
||
| 34 | ->getMock(); |
||
| 35 | $ion_auth->method('logged_in')->willReturn(FALSE); |
||
| 36 | |||
| 37 | $User_Model->ion_auth = $ion_auth; |
||
| 38 | |||
| 39 | $result = $User_Model->logged_in(); |
||
| 40 | $this->assertFalse($result); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function test_login_redirect() { |
||
| 44 | $this->User_Model->login_redirect(); |
||
| 45 | |||
| 46 | $result = $this->CI->session->flashdata('referred_from'); |
||
| 47 | |||
| 48 | //FIXME: We should probably be checking against a custom set current_url() |
||
| 49 | $this->assertEquals("https://test.trackr.moe/", $result); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function test_username_exists_true() { |
||
| 53 | $User_Model = $this->User_Model; |
||
| 54 | |||
| 55 | // Create mock object for CI_DB_result |
||
| 56 | $db_result = $this->getMock_CI_DB_result(['num_rows' => 1]); |
||
| 57 | |||
| 58 | // Create mock object for CI_DB |
||
| 59 | $db = $this->getMock_CI_DB($db_result); |
||
| 60 | |||
| 61 | $User_Model->db = $db; |
||
| 62 | |||
| 63 | $result = $User_Model->username_exists('EXAMPLE_USER'); |
||
| 64 | $this->assertTrue($result); |
||
| 65 | } |
||
| 66 | public function test_username_exists_false() { |
||
| 67 | $User_Model = $this->User_Model; |
||
| 68 | |||
| 69 | // Create mock object for CI_DB_result |
||
| 70 | $db_result = $this->getMock_CI_DB_result(['num_rows' => 0]); |
||
| 71 | |||
| 72 | // Create mock object for CI_DB |
||
| 73 | $db = $this->getMock_CI_DB($db_result); |
||
| 74 | |||
| 75 | $User_Model->db = $db; |
||
| 76 | |||
| 77 | $result = $User_Model->username_exists('TEST_USER'); |
||
| 78 | $this->assertFalse($result); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function test_find_email_from_identity_using_username_true() { |
||
| 82 | $User_Model = $this->User_Model; |
||
| 83 | |||
| 84 | // Create mock object for CI_DB_result |
||
| 85 | $db_result = $this->getMock_CI_DB_result(['num_rows' => 1, 'row' => '[email protected]']); |
||
| 86 | |||
| 87 | // Create mock object for CI_DB |
||
| 88 | $db = $this->getMock_CI_DB($db_result); |
||
| 89 | |||
| 90 | $User_Model->db = $db; |
||
| 91 | |||
| 92 | $result = $User_Model->find_email_from_identity('TEST_USER'); |
||
| 93 | $this->assertEquals("[email protected]", $result); |
||
| 94 | } |
||
| 95 | public function test_find_email_from_identity_using_username_false() { |
||
| 96 | $User_Model = $this->User_Model; |
||
| 97 | |||
| 98 | // Create mock object for CI_DB_result |
||
| 99 | $db_result = $this->getMock_CI_DB_result(['num_rows' => 0]); |
||
| 100 | |||
| 101 | // Create mock object for CI_DB |
||
| 102 | $db = $this->getMock_CI_DB($db_result); |
||
| 103 | |||
| 104 | $User_Model->db = $db; |
||
| 105 | |||
| 106 | $result = $User_Model->find_email_from_identity('TEST_USER'); |
||
| 107 | $this->assertFalse($result); |
||
| 108 | } |
||
| 109 | public function test_find_email_from_identity_using_email() { |
||
| 110 | $User_Model = $this->User_Model; |
||
| 111 | |||
| 112 | $result = $User_Model->find_email_from_identity('[email protected]'); |
||
| 113 | $this->assertEquals('[email protected]', $result); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function test_get_user_by_username_exists() { |
||
| 117 | $User_Model = $this->User_Model; |
||
| 118 | |||
| 119 | // Create mock object for CI_DB_result |
||
| 120 | $db_result = $this->getMock_CI_DB_result(['num_rows' => 1, 'row' => ['id' => '1', 'username' => 'TEST_USER']]); |
||
| 121 | |||
| 122 | // Create mock object for CI_DB |
||
| 123 | $db = $this->getMock_CI_DB($db_result); |
||
| 124 | |||
| 125 | $User_Model->db = $db; |
||
| 126 | |||
| 127 | $result = $User_Model->get_user_by_username('TEST_USER'); |
||
| 128 | $this->assertEquals(['id' => '1', 'username' => 'TEST_USER'], $result); |
||
| 129 | } |
||
| 130 | public function test_get_user_by_username_no_exists() { |
||
| 131 | $User_Model = $this->User_Model; |
||
| 132 | |||
| 133 | // Create mock object for CI_DB_result |
||
| 134 | $db_result = $this->getMock_CI_DB_result(['num_rows' => 0]); |
||
| 135 | |||
| 136 | // Create mock object for CI_DB |
||
| 137 | $db = $this->getMock_CI_DB($db_result); |
||
| 138 | |||
| 139 | $User_Model->db = $db; |
||
| 140 | |||
| 141 | $result = $User_Model->get_user_by_username('TEST_USER'); |
||
| 142 | $this->assertNull($result); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function test_get_gravatar_url() { |
||
| 146 | $User_Model = $this->User_Model; |
||
| 147 | |||
| 148 | //FIXME: This uses global_data which causes errors |
||
| 149 | $result = $User_Model->get_gravatar_url(); |
||
| 150 | |||
| 151 | //NOTE: Gravatar lib changes between using http & https depending on what is being used, tests only use http though. |
||
| 152 | $this->assertRegExp('/^http:\/\/www\.gravatar\.com\/avatar\/[a-z0-9]+\.png\?s=[0-9]+\&d=[a-z]/i', $result); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function test_getPrivacyLevel() { |
||
| 156 | $this->markTestNotImplemented('Plan on remaking how privacy works.'); |
||
| 157 | } |
||
| 158 | } |
||
| 159 |