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 |
||
| 5 | class ConfigTest53 extends PHPUnit_Framework_TestCase { |
||
| 6 | |||
| 7 | public function setUp() { |
||
| 8 | // Enable logging |
||
| 9 | ORM::configure('logging', true); |
||
| 10 | |||
| 11 | // Set up the dummy database connection |
||
| 12 | $db = new MockPDO('sqlite::memory:'); |
||
| 13 | ORM::set_db($db); |
||
| 14 | |||
| 15 | ORM::configure('id_column', 'primary_key'); |
||
| 16 | } |
||
| 17 | |||
| 18 | public function tearDown() { |
||
| 19 | ORM::configure('logging', false); |
||
| 20 | ORM::set_db(null); |
||
| 21 | |||
| 22 | ORM::configure('id_column', 'id'); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function testLoggerCallback() { |
||
| 26 | ORM::configure('logger', function($log_string) { |
||
| 27 | return $log_string; |
||
| 28 | }); |
||
| 29 | $function = ORM::get_config('logger'); |
||
| 30 | $this->assertTrue(is_callable($function)); |
||
| 31 | |||
| 32 | $log_string = "UPDATE `widget` SET `added` = NOW() WHERE `id` = '1'"; |
||
| 33 | $this->assertEquals($log_string, $function($log_string)); |
||
| 34 | |||
| 35 | ORM::configure('logger', null); |
||
| 36 | } |
||
| 37 | |||
| 38 | } |