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 |
||
7 | class SessionTest extends \PHPUnit_Framework_TestCase { |
||
8 | |||
9 | public function setUp() { |
||
10 | Session::clearAlerts(); |
||
11 | } |
||
12 | |||
13 | public function testCountAlerts() { |
||
14 | ob_start(); |
||
15 | new AlertError('My Alert 01'); |
||
16 | new AlertError('My Alert 02'); |
||
17 | $this->assertEquals(2, count(Session::getAlerts())); |
||
18 | |||
19 | Session::clearAlerts(); |
||
20 | $this->assertEquals(0, count(Session::getAlerts())); |
||
21 | ob_end_clean(); |
||
22 | } |
||
23 | |||
24 | public function testHasAlert() { |
||
25 | new AlertDefault('Test'); |
||
26 | $this->assertEquals(true, Session::hasAlert()); |
||
27 | } |
||
28 | |||
29 | public function testHasNoAlert() { |
||
30 | new AlertDefault('Test'); |
||
31 | Session::clearAlerts(); |
||
32 | $this->assertEquals(false, Session::hasAlert()); |
||
33 | } |
||
34 | |||
35 | public function testGetAlerts() { |
||
36 | $alert = new AlertError('I am the firt error'); |
||
37 | $alerts = Session::getAlerts(); |
||
38 | $this->assertEquals($alert->message, $alerts[0]->message); |
||
39 | } |
||
40 | |||
41 | public function testShowOneAlert() { |
||
42 | ob_start(); |
||
43 | $msg = new AlertError('My Error msg'); |
||
44 | Session::showAlerts(); |
||
45 | $content = ob_get_contents(); |
||
46 | ob_end_clean(); |
||
47 | ob_start(); |
||
48 | $msg->load(); |
||
49 | $msgContent = ob_get_contents(); |
||
50 | ob_end_clean(); |
||
51 | $this->assertEquals($content, $msgContent); |
||
52 | } |
||
53 | |||
54 | public function testShowNothing() { |
||
55 | ob_start(); |
||
56 | Session::showAlerts(); |
||
57 | $content = ob_get_contents(); |
||
58 | ob_end_clean(); |
||
59 | $this->assertEmpty($content); |
||
63 |