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 MultilingualBehaviorTest extends DatabaseTestCase |
||
| 10 | { |
||
| 11 | View Code Duplication | public function testFindPosts() |
|
| 12 | { |
||
| 13 | $data = []; |
||
| 14 | $models = Post::find()->multilingual()->all(); |
||
| 15 | foreach ($models as $model) { |
||
| 16 | $this->assertEquals($model->title, $model->title_ru); |
||
| 17 | $this->assertEquals($model->body, $model->body_ru); |
||
| 18 | $this->assertNotNull($model->title_en_us); |
||
| 19 | $this->assertNotNull($model->body_en_us); |
||
| 20 | $data[] = $model->toArray([], ['translations']); |
||
| 21 | } |
||
| 22 | |||
| 23 | $this->assertEquals(require(__DIR__ . '/data/test-find-posts-na.php'), $data); |
||
| 24 | } |
||
| 25 | |||
| 26 | View Code Duplication | public function testCreatePost() |
|
| 27 | { |
||
| 28 | $post = new Post([ |
||
| 29 | 'title' => 'New post title', |
||
| 30 | 'body' => 'New post body', |
||
| 31 | ]); |
||
| 32 | |||
| 33 | $this->assertTrue($post->save()); |
||
| 34 | |||
| 35 | $dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
||
| 36 | $expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-create-post-na.xml'); |
||
| 37 | |||
| 38 | $this->assertDataSetsEqual($expectedDataSet, $dataSet); |
||
| 39 | } |
||
| 40 | |||
| 41 | View Code Duplication | public function testCreatePostSetTranslations() |
|
| 63 | |||
| 64 | View Code Duplication | public function testUpdateNotPopulatedPost() |
|
| 65 | { |
||
| 66 | $post = Post::findOne(2); |
||
| 67 | $post->setAttributes([ |
||
| 68 | 'title' => 'Updated post title 2', |
||
| 69 | 'body' => 'Updated post body 2', |
||
| 70 | 'title_en_us' => 'Updated post title 2 en', |
||
| 71 | 'body_en_us' => 'Updated post title 2 en', |
||
| 72 | ]); |
||
| 73 | |||
| 74 | $this->assertTrue($post->save()); |
||
| 75 | $dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
||
| 76 | $expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-update-not-populated-post-na.xml'); |
||
| 77 | $this->assertDataSetsEqual($expectedDataSet, $dataSet); |
||
| 78 | } |
||
| 79 | |||
| 80 | View Code Duplication | public function testUpdatePopulatedPost() |
|
| 81 | { |
||
| 82 | $post = Post::find()->multilingual()->where(['id' => 2])->one(); |
||
| 83 | $post->setAttributes([ |
||
| 84 | 'title' => 'Updated post title 2', |
||
| 85 | 'body' => 'Updated post body 2', |
||
| 86 | 'title_en_us' => 'Updated post title 2 en', |
||
| 87 | 'body_en_us' => 'Updated post body 2 en', |
||
| 88 | ]); |
||
| 89 | |||
| 90 | $this->assertTrue($post->save()); |
||
| 91 | $dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
||
| 92 | $expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-update-populated-post-na.xml'); |
||
| 93 | $this->assertDataSetsEqual($expectedDataSet, $dataSet); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function testLocalized() |
||
| 97 | { |
||
| 98 | $post = Post::find()->localized(null, false)->where(['id' => 2])->one(); |
||
| 99 | $this->assertEquals(require(__DIR__ . '/data/test-localized-en.php'), [ |
||
| 100 | 'id' => $post->id, |
||
| 101 | 'title' => $post->title, |
||
| 102 | 'body' => $post->body, |
||
| 103 | ]); |
||
| 104 | |||
| 105 | $post = Post::find()->localized('ru', false)->where(['id' => 2])->one(); |
||
| 106 | $this->assertEquals(require(__DIR__ . '/data/test-localized-ru.php'), [ |
||
| 107 | 'id' => $post->id, |
||
| 108 | 'title' => $post->title, |
||
| 109 | 'body' => $post->body, |
||
| 110 | ]); |
||
| 111 | } |
||
| 112 | |||
| 113 | View Code Duplication | public function testDeletePost() |
|
| 114 | { |
||
| 115 | $post = Post::findOne(2); |
||
| 116 | $this->assertEquals(1, $post->delete()); |
||
| 117 | $dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
||
| 118 | $expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-delete-post-na.xml'); |
||
| 119 | $this->assertDataSetsEqual($expectedDataSet, $dataSet); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @inheritdoc |
||
| 124 | */ |
||
| 125 | public function getDataSet() |
||
| 129 | } |
||
| 130 |