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  | 
            ||
| 19 | class topicsolved_test extends \phpbb_database_test_case  | 
            ||
| 20 | { | 
            ||
| 21 | /** @var \tierra\topicsolved\topicsolved */  | 
            ||
| 22 | protected $topicsolved;  | 
            ||
| 23 | |||
| 24 | /** @var \phpbb\user|\PHPUnit_Framework_MockObject_MockObject */  | 
            ||
| 25 | protected $user;  | 
            ||
| 26 | |||
| 27 | /** @var \phpbb\auth\auth|\PHPUnit_Framework_MockObject_MockObject */  | 
            ||
| 28 | protected $auth;  | 
            ||
| 29 | |||
| 30 | /** @var \phpbb\event\dispatcher|\PHPUnit_Framework_MockObject_MockObject */  | 
            ||
| 31 | protected $dispatcher;  | 
            ||
| 32 | |||
| 33 | /**  | 
            ||
| 34 | * Configure the test environment.  | 
            ||
| 35 | *  | 
            ||
| 36 | * @return void  | 
            ||
| 37 | */  | 
            ||
| 38 | public function setUp()  | 
            ||
| 39 | 	{ | 
            ||
| 40 | global $phpbb_root_path, $phpEx;  | 
            ||
| 41 | |||
| 42 | parent::setUp();  | 
            ||
| 43 | |||
| 44 | 		$this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); | 
            ||
| 45 | 		$this->auth = $this->getMock('\phpbb\auth\auth'); | 
            ||
| 46 | 		$this->dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher') | 
            ||
| 47 | ->disableOriginalConstructor()->getMock();  | 
            ||
| 48 | |||
| 49 | $this->topicsolved = new topicsolved(  | 
            ||
| 50 | $this->new_dbal(), $this->user, $this->auth, $this->dispatcher, $phpbb_root_path, $phpEx  | 
            ||
| 51 | );  | 
            ||
| 52 | }  | 
            ||
| 53 | |||
| 54 | /**  | 
            ||
| 55 | * Load required fixtures.  | 
            ||
| 56 | *  | 
            ||
| 57 | * @return mixed  | 
            ||
| 58 | */  | 
            ||
| 59 | public function getDataSet()  | 
            ||
| 60 | 	{ | 
            ||
| 61 | return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/topicsolved.xml');  | 
            ||
| 62 | }  | 
            ||
| 63 | |||
| 64 | /**  | 
            ||
| 65 | * Ensure regular user can only lock own posts.  | 
            ||
| 66 | */  | 
            ||
| 67 | public function test_user_can_only_lock_own_post()  | 
            ||
| 68 | 	{ | 
            ||
| 69 | 		$this->auth->expects($this->exactly(2))->method('acl_get') | 
            ||
| 70 | 			->withConsecutive(array('m_lock', 1), array('f_user_lock', 1)) | 
            ||
| 71 | ->willReturn(false);  | 
            ||
| 72 | |||
| 73 | $this->assertFalse($this->topicsolved->user_can_lock_post(1));  | 
            ||
| 74 | }  | 
            ||
| 75 | |||
| 76 | /**  | 
            ||
| 77 | * Ensure regular user can lock their own posts.  | 
            ||
| 78 | */  | 
            ||
| 79 | public function test_user_can_lock_own_post()  | 
            ||
| 80 | 	{ | 
            ||
| 81 | 		$this->auth->expects($this->exactly(2))->method('acl_get') | 
            ||
| 82 | ->willReturnMap(array(  | 
            ||
| 83 | 				array('m_lock', 1, false), | 
            ||
| 84 | 				array('f_user_lock', 1, true)) | 
            ||
| 85 | );  | 
            ||
| 86 | |||
| 87 | $this->assertTrue($this->topicsolved->user_can_lock_post(1));  | 
            ||
| 88 | }  | 
            ||
| 89 | |||
| 90 | /**  | 
            ||
| 91 | * Ensure moderators can lock posts.  | 
            ||
| 92 | */  | 
            ||
| 93 | public function test_moderator_can_lock_post()  | 
            ||
| 94 | 	{ | 
            ||
| 95 | 		$this->auth->expects($this->once())->method('acl_get') | 
            ||
| 96 | 			->with('m_lock', 1)->willReturn(true); | 
            ||
| 97 | |||
| 98 | $this->assertTrue($this->topicsolved->user_can_lock_post(1));  | 
            ||
| 99 | }  | 
            ||
| 100 | |||
| 101 | /**  | 
            ||
| 102 | * Ensure tierra.topicsolved.mark_solved_after event works.  | 
            ||
| 103 | */  | 
            ||
| 104 | public function test_mark_solved_event()  | 
            ||
| 105 | 	{ | 
            ||
| 106 | $topic_data = array(  | 
            ||
| 107 | 'forum_lock_solved' => '1',  | 
            ||
| 108 | 'forum_id' => 2,  | 
            ||
| 109 | 'topic_id' => 1  | 
            ||
| 110 | );  | 
            ||
| 111 | |||
| 112 | 		$this->auth->expects($this->once())->method('acl_get') | 
            ||
| 113 | 			->with('m_lock', 2)->willReturn(true); | 
            ||
| 114 | 		$this->dispatcher->expects($this->once())->method('trigger_event') | 
            ||
| 115 | ->with(  | 
            ||
| 116 | 'tierra.topicsolved.mark_solved_after',  | 
            ||
| 117 | array(  | 
            ||
| 118 | 'topic_data' => $topic_data,  | 
            ||
| 119 | 'column_data' => array(  | 
            ||
| 120 | 'topic_solved' => 5,  | 
            ||
| 121 | 'topic_status' => ITEM_LOCKED  | 
            ||
| 122 | )  | 
            ||
| 123 | )  | 
            ||
| 124 | )->willReturn(array());  | 
            ||
| 125 | |||
| 126 | $this->topicsolved->mark_solved($topic_data, 5);  | 
            ||
| 127 | }  | 
            ||
| 128 | |||
| 129 | /**  | 
            ||
| 130 | * Ensure tierra.topicsolved.mark_unsolved_after event works.  | 
            ||
| 131 | */  | 
            ||
| 132 | public function test_mark_unsolved_event()  | 
            ||
| 133 | 	{ | 
            ||
| 134 | $topic_data = array(  | 
            ||
| 135 | 'forum_lock_solved' => '1',  | 
            ||
| 136 | 'forum_id' => 2,  | 
            ||
| 137 | 'topic_id' => 1  | 
            ||
| 138 | );  | 
            ||
| 139 | |||
| 140 | 		$this->auth->expects($this->once())->method('acl_get') | 
            ||
| 141 | 			->with('m_lock', 2)->willReturn(true); | 
            ||
| 142 | 		$this->dispatcher->expects($this->once())->method('trigger_event') | 
            ||
| 143 | ->with(  | 
            ||
| 144 | 'tierra.topicsolved.mark_unsolved_after',  | 
            ||
| 145 | array(  | 
            ||
| 146 | 'topic_data' => $topic_data,  | 
            ||
| 147 | 'column_data' => array(  | 
            ||
| 148 | 'topic_solved' => 0,  | 
            ||
| 149 | 'topic_status' => ITEM_UNLOCKED  | 
            ||
| 150 | )  | 
            ||
| 151 | )  | 
            ||
| 152 | )->willReturn(array());  | 
            ||
| 153 | |||
| 154 | $this->topicsolved->mark_unsolved($topic_data);  | 
            ||
| 155 | }  | 
            ||
| 156 | |||
| 157 | /**  | 
            ||
| 158 | * Data set for test_image  | 
            ||
| 159 | *  | 
            ||
| 160 | * @return array  | 
            ||
| 161 | */  | 
            ||
| 162 | public function image_test_data()  | 
            ||
| 163 | 	{ | 
            ||
| 164 | return array(  | 
            ||
| 165 | 			array('head', 'TOPIC_SOLVED', 'test.php', | 
            ||
| 166 | '<a href="test.php" title="TOPIC_SOLVED"><img src="" /></a>'),  | 
            ||
| 167 | 			array('list', 'T<>""S', '/anchor/test.php', | 
            ||
| 168 | '<a href="/anchor/test.php" title="T<>""S"><img src="" /></a>'),  | 
            ||
| 169 | 			array('post', 'TOPIC_SOLVED', '//example.com/test.php', | 
            ||
| 170 | '<a href="//example.com/test.php" title="TOPIC_SOLVED"><img src="" /></a>'),  | 
            ||
| 171 | 			array('head', '', 'http://example.com/', | 
            ||
| 172 | '<a href="http://example.com/"><img src="" /></a>'),  | 
            ||
| 173 | 			array('list', 'TOPIC_SOLVED', 'https://example.com', | 
            ||
| 174 | '<a href="https://example.com" title="TOPIC_SOLVED"><img src="" /></a>'),  | 
            ||
| 175 | 			array('post', '', 'test.php?f=1&s=two%20words', | 
            ||
| 176 | '<a href="test.php?f=1&s=two%20words"><img src="" /></a>'),  | 
            ||
| 177 | );  | 
            ||
| 178 | }  | 
            ||
| 179 | |||
| 180 | /**  | 
            ||
| 181 | * Ensure proper image markup is being generated.  | 
            ||
| 182 | *  | 
            ||
| 183 | * @param string $type One of "head", "list", or "post".  | 
            ||
| 184 | * @param string $alt Language code for title and alternative text.  | 
            ||
| 185 | * @param string $url Optional link to solved post.  | 
            ||
| 186 | * @param string $expected Result expected from image() call.  | 
            ||
| 187 | *  | 
            ||
| 188 | * @dataProvider image_test_data  | 
            ||
| 189 | */  | 
            ||
| 190 | public function test_image($type, $alt, $url, $expected)  | 
            ||
| 191 | 	{ | 
            ||
| 192 | 		$this->user->expects($this->any())->method('img') | 
            ||
| 193 | 			->willReturn('<img src="" />'); | 
            ||
| 194 | 		$this->user->expects($this->any())->method('lang') | 
            ||
| 195 | ->will($this->returnArgument(0));  | 
            ||
| 196 | |||
| 197 | $this->assertEquals($expected, $this->topicsolved->image($type, $alt, $url));  | 
            ||
| 198 | }  | 
            ||
| 199 | |||
| 200 | /**  | 
            ||
| 250 |