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 |
||
10 | class MultilingualBehaviorAbridgeTest extends DatabaseTestCase |
||
11 | { |
||
12 | public function testConfiguration() |
||
13 | { |
||
14 | $post = new Post(); |
||
15 | $post->detachBehavior('ml'); |
||
16 | |||
17 | try { |
||
18 | $post->attachBehavior('ml', [ |
||
19 | 'class' => MultilingualBehavior::className(), |
||
20 | 'languages' => [], |
||
21 | ]); |
||
22 | $this->fail("Expected exception not thrown"); |
||
23 | } catch (InvalidConfigException $e) { |
||
24 | $this->assertEquals(101, $e->getCode()); |
||
25 | } |
||
26 | |||
27 | try { |
||
28 | $post->detachBehavior('ml'); |
||
29 | $post->attachBehavior('ml', [ |
||
30 | 'class' => MultilingualBehavior::className(), |
||
31 | 'languages' => 'Some value', |
||
32 | ]); |
||
33 | $this->fail("Expected exception not thrown"); |
||
34 | } catch (InvalidConfigException $e) { |
||
35 | $this->assertEquals(101, $e->getCode()); |
||
36 | } |
||
37 | |||
38 | try { |
||
39 | $post->detachBehavior('ml'); |
||
40 | $post->attachBehavior('ml', [ |
||
41 | 'class' => MultilingualBehavior::className(), |
||
42 | 'languages' => [ |
||
43 | 'ru' => 'Russian', |
||
44 | 'en-US' => 'English', |
||
45 | ], |
||
46 | 'langForeignKey' => 'post_id', |
||
47 | 'tableName' => "{{%postLang}}", |
||
48 | ]); |
||
49 | $this->fail("Expected exception not thrown"); |
||
50 | } catch (InvalidConfigException $e) { |
||
51 | $this->assertEquals(103, $e->getCode()); |
||
52 | } |
||
53 | |||
54 | try { |
||
55 | $post->detachBehavior('ml'); |
||
56 | $post->attachBehavior('ml', [ |
||
57 | 'class' => MultilingualBehavior::className(), |
||
58 | 'languages' => [ |
||
59 | 'ru' => 'Russian', |
||
60 | 'en-US' => 'English', |
||
61 | ], |
||
62 | 'attributes' => [ |
||
63 | 'title', 'body', |
||
64 | ] |
||
65 | ]); |
||
66 | $this->fail("Expected exception not thrown"); |
||
67 | } catch (InvalidConfigException $e) { |
||
68 | $this->assertEquals(105, $e->getCode()); |
||
69 | } |
||
70 | |||
71 | $post->detachBehavior('ml'); |
||
72 | $post->attachBehavior('ml', [ |
||
73 | 'class' => MultilingualBehavior::className(), |
||
74 | 'languages' => [ |
||
75 | 'ru' => 'Russian', |
||
76 | 'en-US' => 'English', |
||
77 | ], |
||
78 | 'langForeignKey' => 'post_id', |
||
79 | 'attributes' => [ |
||
80 | 'title', 'body', |
||
81 | ] |
||
82 | ]); |
||
83 | |||
84 | $this->assertNotNull($post->defaultLanguage); |
||
85 | } |
||
86 | |||
87 | View Code Duplication | public function testFindPosts() |
|
88 | { |
||
89 | $data = []; |
||
90 | $models = Post::find()->multilingual()->all(); |
||
91 | foreach ($models as $model) { |
||
92 | $this->assertEquals($model->title, $model->title_ru); |
||
93 | $this->assertEquals($model->body, $model->body_ru); |
||
94 | $this->assertNotNull($model->title_en); |
||
95 | $this->assertNotNull($model->body_en); |
||
96 | $data[] = $model->toArray([], ['translations']); |
||
97 | } |
||
98 | |||
99 | $this->assertEquals(require(__DIR__ . '/data/test-find-posts.php'), $data); |
||
100 | } |
||
101 | |||
102 | View Code Duplication | public function testCreatePost() |
|
103 | { |
||
104 | $post = new Post([ |
||
105 | 'title' => 'New post title', |
||
106 | 'body' => 'New post body', |
||
107 | ]); |
||
108 | |||
109 | $this->assertTrue($post->save()); |
||
110 | |||
111 | $dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
||
112 | $expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-create-post.xml'); |
||
113 | |||
114 | $this->assertDataSetsEqual($expectedDataSet, $dataSet); |
||
115 | } |
||
116 | |||
117 | View Code Duplication | public function testCreatePostSetTranslations() |
|
139 | |||
140 | View Code Duplication | public function testUpdateNotPopulatedPost() |
|
141 | { |
||
142 | $post = Post::findOne(2); |
||
155 | |||
156 | View Code Duplication | public function testUpdatePopulatedPost() |
|
171 | |||
172 | public function testLocalized() |
||
188 | |||
189 | View Code Duplication | public function testDeletePost() |
|
197 | |||
198 | public function testLocalizedAndMultilingual() |
||
204 | |||
205 | public function testRequired() |
||
218 | } |
||
219 |