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 |
||
13 | class JSONTextBasicTest extends SapphireTest |
||
14 | { |
||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $fixtures = [ |
||
19 | 'array' => 'tests/fixtures/json/array.json', |
||
20 | 'object' => 'tests/fixtures/json/object.json', |
||
21 | 'invalid' => 'tests/fixtures/json/invalid.json' |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * JSONTextTest constructor. |
||
26 | * |
||
27 | * Modify fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows |
||
28 | */ |
||
29 | public function __construct() |
||
30 | { |
||
31 | foreach($this->fixtures as $name => $path) { |
||
32 | $this->fixtures[$name] = MODULE_DIR . '/' . $path; |
||
33 | } |
||
34 | } |
||
35 | |||
36 | public function testgetValueAsIterable() |
||
37 | { |
||
38 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
39 | $field->setValue($this->getFixture('invalid')); |
||
40 | $this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
||
41 | $this->assertEquals(['chinese' => 'great wall'], $field->getJSONStore()); |
||
42 | |||
43 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
44 | $field->setValue(''); |
||
45 | $this->assertEquals([], $field->getJSONStore()); |
||
46 | } |
||
47 | |||
48 | public function testFirst() |
||
49 | { |
||
50 | // Test: Source data is simple JSON array |
||
51 | // Return type: Array |
||
52 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
53 | $field->setReturnType('array'); |
||
54 | $field->setValue($this->getFixture('array')); |
||
55 | $this->assertInternalType('array', $field->first()); |
||
56 | $this->assertCount(1, $field->first()); |
||
57 | $this->assertEquals([0 => 'great wall'], $field->first()); |
||
58 | |||
59 | // Test: Source data is simple JSON array |
||
60 | // Return type: JSON |
||
61 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
62 | $field->setReturnType('json'); |
||
63 | $field->setValue($this->getFixture('array')); |
||
64 | $this->assertInternalType('string', $field->first()); |
||
65 | $this->assertEquals('["great wall"]', $field->first()); |
||
66 | |||
67 | // Test: Source data is simple JSON array |
||
68 | // Return type: SilverStripe |
||
69 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
70 | $field->setReturnType('silverstripe'); |
||
71 | $field->setValue($this->getFixture('array')); |
||
72 | $this->assertInternalType('array', $field->first()); |
||
73 | $this->assertInstanceOf('Varchar', $field->first()[0]); |
||
74 | $this->assertEquals('great wall', $field->first()[0]->getValue()); |
||
75 | |||
76 | // Test: Empty |
||
77 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
78 | $field->setReturnType('array'); |
||
79 | $field->setValue(''); |
||
80 | $this->assertInternalType('array', $field->first()); |
||
81 | $this->assertCount(0, $field->first()); |
||
82 | |||
83 | // Test: Invalid |
||
84 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
85 | $field->setReturnType('array'); |
||
86 | $field->setValue('{'); |
||
87 | $this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
||
88 | $field->first(); |
||
89 | } |
||
90 | |||
91 | public function testLast() |
||
92 | { |
||
93 | // Test: Source data is simple JSON array |
||
94 | // Return type: Array |
||
95 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
96 | $field->setReturnType('array'); |
||
97 | $field->setValue($this->getFixture('array')); |
||
98 | $this->assertInternalType('array', $field->last()); |
||
99 | $this->assertCount(1, $field->last()); |
||
100 | $this->assertEquals([6 => 33.3333], $field->last()); |
||
101 | |||
102 | // Test: Source data is simple JSON array |
||
103 | // Return type: JSON |
||
104 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
105 | $field->setReturnType('json'); |
||
106 | $field->setValue($this->getFixture('array')); |
||
107 | $this->assertInternalType('string', $field->last()); |
||
108 | $this->assertEquals('{"6":33.3333}', $field->last()); |
||
109 | |||
110 | // Test: Source data is simple JSON array |
||
111 | // Return type: SilverStripe |
||
112 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
113 | $field->setReturnType('silverstripe'); |
||
114 | $field->setValue($this->getFixture('array')); |
||
115 | $this->assertInternalType('array', $field->last()); |
||
116 | $this->assertInstanceOf('Float', $field->last()[6]); |
||
117 | $this->assertEquals(33.3333, $field->last()[6]->getValue()); |
||
118 | |||
119 | // Test: Empty |
||
120 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
121 | $field->setReturnType('array'); |
||
122 | $field->setValue(''); |
||
123 | $this->assertInternalType('array', $field->last()); |
||
124 | $this->assertCount(0, $field->last()); |
||
125 | |||
126 | // Test: Invalid |
||
127 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
128 | $field->setReturnType('array'); |
||
129 | $field->setValue('{'); |
||
130 | $this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
||
131 | $field->last(); |
||
132 | } |
||
133 | |||
134 | public function testNth() |
||
135 | { |
||
136 | // Test: Source data is simple JSON array |
||
137 | // Return type: Array |
||
138 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
139 | $field->setReturnType('array'); |
||
140 | $field->setValue($this->getFixture('array')); |
||
141 | $this->assertInternalType('array', $field->nth(1)); |
||
142 | $this->assertCount(1, $field->nth(1)); |
||
143 | $this->assertEquals([1 => true], $field->nth(1)); |
||
144 | |||
145 | // Test: Source data is simple JSON array |
||
146 | // Return type: JSON |
||
147 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
148 | $field->setReturnType('json'); |
||
149 | $field->setValue($this->getFixture('array')); |
||
150 | $this->assertInternalType('string', $field->nth(1)); |
||
151 | $this->assertEquals('{"1":true}', $field->nth(1)); |
||
152 | |||
153 | // Test: Source data is simple JSON array |
||
154 | // Return type: SilverStripe |
||
155 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
156 | $field->setReturnType('silverstripe'); |
||
157 | $field->setValue($this->getFixture('array')); |
||
158 | $this->assertInternalType('array', $field->nth(1)); |
||
159 | $this->assertInstanceOf('Boolean', $field->nth(1)[1]); |
||
160 | $this->assertEquals(true, $field->nth(1)[1]->getValue()); |
||
161 | |||
162 | // Test: Empty |
||
163 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
164 | $field->setReturnType('array'); |
||
165 | $field->setValue(''); |
||
166 | $this->assertInternalType('array', $field->nth(1)); |
||
167 | $this->assertCount(0, $field->nth(1)); |
||
168 | |||
169 | // Test: Invalid |
||
170 | $field = JSONText\Fields\JSONText::create('MyJSON'); |
||
171 | $field->setReturnType('array'); |
||
172 | $field->setValue('{'); |
||
173 | $this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
||
174 | $field->nth(1); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Get the contents of a fixture |
||
179 | * |
||
180 | * @param string $fixture |
||
181 | * @return string |
||
182 | */ |
||
183 | private function getFixture($fixture) |
||
184 | { |
||
185 | $files = $this->fixtures; |
||
186 | return file_get_contents($files[$fixture]); |
||
187 | } |
||
188 | |||
189 | } |
||
190 |