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 |
||
| 6 | class IdiormResultSetTest extends PHPUnit_Framework_TestCase { |
||
| 7 | |||
| 8 | public function setUp() { |
||
| 9 | // Enable logging |
||
| 10 | ORM::configure('logging', true); |
||
| 11 | |||
| 12 | // Set up the dummy database connection |
||
| 13 | $db = new MockPDO('sqlite::memory:'); |
||
| 14 | ORM::set_db($db); |
||
| 15 | } |
||
| 16 | |||
| 17 | public function tearDown() { |
||
| 18 | ORM::reset_config(); |
||
| 19 | ORM::reset_db(); |
||
| 20 | } |
||
| 21 | |||
| 22 | public function testGet() { |
||
| 23 | $IdiormResultSet = new IdiormResultSet(); |
||
| 24 | $this->assertInternalType('array', $IdiormResultSet->get_results()); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function testConstructor() { |
||
| 28 | $result_set = array('item' => new stdClass); |
||
| 29 | $IdiormResultSet = new IdiormResultSet($result_set); |
||
| 30 | $this->assertSame($IdiormResultSet->get_results(), $result_set); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function testSetResultsAndGetResults() { |
||
| 34 | $result_set = array('item' => new stdClass); |
||
| 35 | $IdiormResultSet = new IdiormResultSet(); |
||
| 36 | $IdiormResultSet->set_results($result_set); |
||
| 37 | $this->assertSame($IdiormResultSet->get_results(), $result_set); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function testAsArray() { |
||
| 41 | $result_set = array('item' => new stdClass); |
||
| 42 | $IdiormResultSet = new IdiormResultSet(); |
||
| 43 | $IdiormResultSet->set_results($result_set); |
||
| 44 | $this->assertSame($IdiormResultSet->as_array(), $result_set); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function testCount() { |
||
| 48 | $result_set = array('item' => new stdClass); |
||
| 49 | $IdiormResultSet = new IdiormResultSet($result_set); |
||
| 50 | $this->assertSame($IdiormResultSet->count(), 1); |
||
| 51 | $this->assertSame(count($IdiormResultSet), 1); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function testGetIterator() { |
||
| 55 | $result_set = array('item' => new stdClass); |
||
| 56 | $IdiormResultSet = new IdiormResultSet($result_set); |
||
| 57 | $this->assertInstanceOf('ArrayIterator', $IdiormResultSet->getIterator()); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function testForeach() { |
||
| 61 | $result_set = array('item' => new stdClass); |
||
| 62 | $IdiormResultSet = new IdiormResultSet($result_set); |
||
| 63 | $return_array = array(); |
||
| 64 | foreach($IdiormResultSet as $key => $record) { |
||
| 65 | $return_array[$key] = $record; |
||
| 66 | } |
||
| 67 | $this->assertSame($result_set, $return_array); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function testCallingMethods() { |
||
| 71 | $result_set = array('item' => ORM::for_table('test'), 'item2' => ORM::for_table('test')); |
||
| 72 | $IdiormResultSet = new IdiormResultSet($result_set); |
||
| 73 | $IdiormResultSet->set('field', 'value')->set('field2', 'value'); |
||
| 74 | |||
| 75 | foreach($IdiormResultSet as $record) { |
||
| 76 | $this->assertTrue(isset($record->field)); |
||
| 77 | $this->assertSame($record->field, 'value'); |
||
| 78 | |||
| 79 | $this->assertTrue(isset($record->field2)); |
||
| 80 | $this->assertSame($record->field2, 'value'); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | } |