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 AlertTest extends \PHPUnit_Framework_TestCase { |
||
8 | |||
9 | public function testGetMessage() { |
||
10 | $instance = new AlertError('My Alert'); |
||
11 | $this->assertEquals('My Alert', $instance->message); |
||
12 | $this->assertEquals('My Alert', $instance); |
||
13 | } |
||
14 | |||
15 | public function testGetType() { |
||
16 | $instance = new AlertError('My Error msg'); |
||
17 | $this->assertEquals('danger', $instance->type); |
||
18 | |||
19 | $instance2 = new AlertSuccess('My Success msg'); |
||
20 | $this->assertEquals('success', $instance2->type); |
||
21 | } |
||
22 | |||
23 | public function testLoad() { |
||
24 | ob_start(); |
||
25 | $alert = new AlertError('My Error msg'); |
||
26 | $alert->load(); |
||
27 | $content = ob_get_contents(); |
||
28 | ob_end_clean(); |
||
29 | $this->assertNotEmpty($content); |
||
30 | } |
||
31 | |||
32 | public function testCreateAlertWithError() { |
||
33 | ob_start(); |
||
34 | Session::showAlerts(); |
||
35 | $alert = Alert::create('This is a error', 'Congratulations'); |
||
36 | $this->assertTrue($alert instanceof AlertError); |
||
37 | $this->assertEquals('This is a error', $alert->message); |
||
38 | ob_end_clean(); |
||
39 | } |
||
40 | |||
41 | public function testCreateNoError() { |
||
42 | ob_start(); |
||
43 | Session::showAlerts(); |
||
44 | $alert = Alert::create(null, 'Congratulations'); |
||
45 | $this->assertTrue($alert instanceof AlertSuccess); |
||
46 | $this->assertEquals('Congratulations', $alert->message); |
||
47 | ob_end_clean(); |
||
48 | } |
||
49 | |||
50 | } |
||
51 |