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 |
||
| 9 | class MysqlIntroductionRepositoryTest extends PHPUnit_Framework_TestCase |
||
| 10 | { |
||
| 11 | |||
| 12 | protected static $connection; |
||
| 13 | |||
| 14 | public static function setUpBeforeClass() |
||
| 15 | { |
||
| 16 | $extendedPdo = new ExtendedPdo('sqlite::memory:'); |
||
| 17 | $extendedPdo->exec("ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`"); |
||
| 18 | |||
| 19 | $extendedPdo->exec(" |
||
| 20 | CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`introduction` ( |
||
| 21 | `id` integer PRIMARY KEY AUTOINCREMENT, |
||
| 22 | `type` varchar(10) NOT NULL, |
||
| 23 | `value` varchar(25) NOT NULL, |
||
| 24 | `title` varchar(100) NOT NULL, |
||
| 25 | `content` text NOT NULL, |
||
| 26 | `image` image |
||
| 27 | )" |
||
| 28 | ); |
||
| 29 | |||
| 30 | self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
||
| 31 | return $extendedPdo; |
||
| 32 | }); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function testIsInstanceOfIntroductionRepository() |
||
| 36 | { |
||
| 37 | $repository = new MysqlIntroductionRepository(self::$connection); |
||
| 38 | |||
| 39 | $this->assertInstanceOf( |
||
| 40 | 'Jacobemerick\Web\Domain\Blog\Introduction\MysqlIntroductionRepository', |
||
| 41 | $repository |
||
| 42 | ); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function testImplementsIntroductionInterface() |
||
| 46 | { |
||
| 47 | $repository = new MysqlIntroductionRepository(self::$connection); |
||
| 48 | |||
| 49 | $this->assertInstanceOf( |
||
| 50 | 'Jacobemerick\Web\Domain\Blog\Introduction\IntroductionRepositoryInterface', |
||
| 51 | $repository |
||
| 52 | ); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function testConstructSetsConnections() |
||
| 56 | { |
||
| 57 | $respository = new MysqlIntroductionRepository(self::$connection); |
||
| 58 | |||
| 59 | $this->assertAttributeSame( |
||
| 60 | self::$connection, |
||
| 61 | 'connections', |
||
| 62 | $respository |
||
| 63 | ); |
||
| 64 | } |
||
| 65 | |||
| 66 | public function testFindByType() |
||
| 67 | { |
||
| 68 | $testData = [ |
||
| 69 | 'id' => rand(1, 100), |
||
| 70 | 'type' => 'test', |
||
| 71 | 'title' => 'test title', |
||
| 72 | 'content' => 'test content', |
||
| 73 | 'image' => rand(1, 100), |
||
| 74 | ]; |
||
| 75 | |||
| 76 | $this->insertData($testData); |
||
| 77 | |||
| 78 | $repository = new MysqlIntroductionRepository(self::$connection); |
||
| 79 | $data = $repository->findByType('test'); |
||
| 80 | |||
| 81 | $this->assertNotFalse($data); |
||
| 82 | $this->assertInternalType('array', $data); |
||
| 83 | $this->assertArrayHasKey('title', $data); |
||
| 84 | $this->assertEquals($testData['title'], $data['title']); |
||
| 85 | $this->assertArrayHasKey('content', $data); |
||
| 86 | $this->assertEquals($testData['content'], $data['content']); |
||
| 87 | $this->assertArrayHasKey('image', $data); |
||
| 88 | $this->assertEquals($testData['image'], $data['image']); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function testFindByTypeFilterValue() |
||
| 92 | { |
||
| 93 | $testData = [ |
||
| 94 | [ |
||
| 95 | 'id' => rand(1, 100), |
||
| 96 | 'type' => 'test', |
||
| 97 | 'title' => 'title a', |
||
| 98 | 'value' => 'value a', |
||
| 99 | ], |
||
| 100 | [ |
||
| 101 | 'id' => rand(101, 200), |
||
| 102 | 'type' => 'test', |
||
| 103 | 'title' => 'title b', |
||
| 104 | 'value' => 'value b', |
||
| 105 | ], |
||
| 106 | ]; |
||
| 107 | |||
| 108 | array_walk($testData, [$this, 'insertData']); |
||
| 109 | |||
| 110 | $repository = new MysqlIntroductionRepository(self::$connection); |
||
| 111 | $data = $repository->findByType('test', reset($testData)['value']); |
||
| 112 | |||
| 113 | $this->assertNotFalse($data); |
||
| 114 | $this->assertInternalType('array', $data); |
||
| 115 | $this->assertEquals(reset($testData)['title'], $data['title']); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function testFindByTypeFailure() |
||
| 119 | { |
||
| 120 | $repository = new MysqlIntroductionRepository(self::$connection); |
||
| 121 | $data = $repository->findByType('empty type'); |
||
| 122 | |||
| 123 | $this->assertFalse($data); |
||
| 124 | } |
||
| 125 | |||
| 126 | protected function insertData(array $data) |
||
| 127 | { |
||
| 128 | $defaultData = [ |
||
| 129 | 'id' => null, |
||
| 130 | 'type' => '', |
||
| 131 | 'value' => '', |
||
| 132 | 'title' => '', |
||
| 133 | 'content' => '', |
||
| 134 | 'image' => null, |
||
| 135 | ]; |
||
| 136 | |||
| 137 | $data = array_merge($defaultData, $data); |
||
| 138 | |||
| 139 | return self::$connection->getDefault()->perform(" |
||
| 140 | INSERT INTO `jpemeric_blog`.`introduction` |
||
| 141 | (id, type, value, title, content, image) |
||
| 142 | VALUES |
||
| 143 | (:id, :type, :value, :title, :content, :image)", |
||
| 144 | $data |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | |||
| 148 | protected function tearDown() |
||
| 149 | { |
||
| 150 | self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`introduction`"); |
||
| 151 | } |
||
| 152 | |||
| 153 | public static function tearDownAfterClass() |
||
| 154 | { |
||
| 155 | self::$connection->getDefault()->disconnect(); |
||
| 156 | unlink('jpemeric_blog.db'); |
||
| 157 | } |
||
| 158 | } |
||
| 159 |