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 |
||
3 | namespace Innoweb\SectionIO\Tests; |
||
4 | |||
5 | use Innoweb\SectionIO\SectionIO; |
||
6 | use Innoweb\SectionIO\Extensions\SectionIOFileExtension; |
||
7 | use Innoweb\SectionIO\Extensions\SectionIOSiteTreeExtension; |
||
8 | use SilverStripe\Assets\File; |
||
9 | use SilverStripe\Assets\Image; |
||
10 | use SilverStripe\Assets\Dev\TestAssetStore; |
||
11 | use SilverStripe\CMS\Model\SiteTree; |
||
12 | use SilverStripe\Core\Config\Config; |
||
13 | use SilverStripe\Dev\SapphireTest; |
||
14 | use SilverStripe\ORM\DataObject; |
||
15 | |||
16 | class SectionIOTest extends SapphireTest |
||
17 | { |
||
18 | protected static $fixture_file = 'SectionIOTest.yml'; |
||
19 | |||
20 | public function setUpOnce() |
||
21 | { |
||
22 | parent::setUpOnce(); |
||
|
|||
23 | |||
24 | // add config values |
||
25 | Config::inst()->update(SectionIO::class, 'flush_on_dev_build', true); |
||
26 | Config::inst()->update(SectionIO::class, 'api_url', 'https://example.com'); |
||
27 | Config::inst()->update(SectionIO::class, 'account_id', '123456'); |
||
28 | Config::inst()->update(SectionIO::class, 'application_id', '987654'); |
||
29 | Config::inst()->update(SectionIO::class, 'environment_name', 'Production'); |
||
30 | Config::inst()->update(SectionIO::class, 'proxy_name', 'myproxy'); |
||
31 | Config::inst()->update(SectionIO::class, 'username', 'someuser'); |
||
32 | Config::inst()->update(SectionIO::class, 'password', 'MySafePassword'); |
||
33 | Config::inst()->update(SectionIO::class, 'verify_ssl', false); |
||
34 | |||
35 | // remove extensions otherwise the fixtures will break the tests (by calling the live flush) |
||
36 | File::remove_extension(SectionIOFileExtension::class); |
||
37 | SiteTree::remove_extension(SectionIOSiteTreeExtension::class); |
||
38 | } |
||
39 | |||
40 | public function setUp() |
||
41 | { |
||
42 | parent::setUp(); |
||
43 | |||
44 | // Set backend root to /ImageTest |
||
45 | TestAssetStore::activate('FileTest'); |
||
46 | |||
47 | // Create a test files for each of the fixture references |
||
48 | $fileIDs = array_merge( |
||
49 | $this->allFixtureIDs(File::class), |
||
50 | $this->allFixtureIDs(Image::class) |
||
51 | ); |
||
52 | foreach ($fileIDs as $fileID) { |
||
53 | /** @var File $file */ |
||
54 | $file = DataObject::get_by_id(File::class, $fileID); |
||
55 | $file->setFromString(str_repeat('x', 1000000), $file->getFilename()); |
||
56 | } |
||
57 | |||
58 | } |
||
59 | |||
60 | public function tearDownOnce() |
||
61 | { |
||
62 | parent::tearDownOnce(); |
||
63 | |||
64 | // re-add extensions |
||
65 | File::add_extension('SectionIOFileExtension'); |
||
66 | SiteTree::add_extension('SectionIOSiteTreeExtension'); |
||
67 | } |
||
68 | |||
69 | public function tearDown() |
||
70 | { |
||
71 | TestAssetStore::reset(); |
||
72 | |||
73 | parent::tearDown(); |
||
74 | } |
||
75 | |||
76 | public function testFlushAll() |
||
77 | { |
||
78 | $result = SectionIOTest_MySectionIO::flushAll(); |
||
79 | |||
80 | $this->assertCount( |
||
81 | 1, |
||
82 | $result, |
||
83 | 'one url returned for one application id' |
||
84 | ); |
||
85 | |||
86 | // url |
||
87 | $this->assertEquals( |
||
88 | 'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state', |
||
89 | $result[0]['url'], |
||
90 | 'URL is concatenated correctly' |
||
91 | ); |
||
92 | |||
93 | // ban expression |
||
94 | $this->assertEquals( |
||
95 | 'obj.http.x-url ~ /', |
||
96 | $result[0]['banExpression'], |
||
97 | 'ban expression is correct' |
||
98 | ); |
||
99 | |||
100 | } |
||
101 | |||
102 | public function testFlush() |
||
103 | { |
||
104 | $result = SectionIOTest_MySectionIO::flush(); |
||
105 | |||
106 | $this->assertCount( |
||
107 | 1, |
||
108 | $result, |
||
109 | 'one url returned for one application id' |
||
110 | ); |
||
111 | |||
112 | // url |
||
113 | $this->assertEquals( |
||
114 | 'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state', |
||
115 | $result[0]['url'], |
||
116 | 'URL is concatenated correctly' |
||
117 | ); |
||
118 | |||
119 | // ban expression |
||
120 | $this->assertEquals( |
||
121 | 'obj.http.x-url ~ /', |
||
122 | $result[0]['banExpression'], |
||
123 | 'ban expression is correct' |
||
124 | ); |
||
125 | |||
126 | // test deactivated flush on build |
||
127 | Config::inst()->update(SectionIO::class, 'flush_on_dev_build', false); |
||
128 | $result = SectionIOTest_MySectionIO::flush(); |
||
129 | $this->assertNull( |
||
130 | $result, |
||
131 | 'null returned if flush on build deactivated' |
||
132 | ); |
||
133 | } |
||
134 | |||
135 | public function testMultipleApplicationIDs() |
||
136 | { |
||
137 | // add second application to config |
||
138 | Config::inst()->update(SectionIO::class, 'application_id', '2546987,856954'); |
||
139 | |||
140 | $result = SectionIOTest_MySectionIO::flushAll(); |
||
141 | |||
142 | $this->assertCount( |
||
143 | 2, |
||
144 | $result, |
||
145 | 'two urls returned for two application id' |
||
146 | ); |
||
147 | |||
148 | // url |
||
149 | $this->assertEquals( |
||
150 | 'https://example.com/account/123456/application/2546987/environment/Production/proxy/myproxy/state', |
||
151 | $result[0]['url'], |
||
152 | 'URL is concatenated correctly for app 1' |
||
153 | ); |
||
154 | $this->assertEquals( |
||
155 | 'https://example.com/account/123456/application/856954/environment/Production/proxy/myproxy/state', |
||
156 | $result[1]['url'], |
||
157 | 'URL is concatenated correctly for app 2' |
||
158 | ); |
||
159 | |||
160 | // add second application to config with spaces in csv |
||
161 | Config::inst()->update(SectionIO::class, 'application_id', '741852, 369258'); |
||
162 | |||
163 | $result = SectionIOTest_MySectionIO::flushAll(); |
||
164 | |||
165 | $this->assertCount( |
||
166 | 2, |
||
167 | $result, |
||
168 | 'two urls returned for two application id' |
||
169 | ); |
||
170 | |||
171 | // url |
||
172 | $this->assertEquals( |
||
173 | 'https://example.com/account/123456/application/741852/environment/Production/proxy/myproxy/state', |
||
174 | $result[0]['url'], |
||
175 | 'URL is concatenated correctly for app 1' |
||
176 | ); |
||
177 | $this->assertEquals( |
||
178 | 'https://example.com/account/123456/application/369258/environment/Production/proxy/myproxy/state', |
||
179 | $result[1]['url'], |
||
180 | 'URL is concatenated correctly for app 2' |
||
181 | ); |
||
182 | } |
||
183 | |||
184 | public function testFlushImage() |
||
196 | ); |
||
197 | } |
||
198 | |||
199 | public function testFlushFile() |
||
200 | { |
||
201 | $fileId = $this->idFromFixture('File', 'testFile'); |
||
202 | |||
203 | $result = SectionIOTest_MySectionIO::flushFile($fileId); |
||
204 | |||
205 | // ban expression |
||
206 | $this->assertEquals( |
||
207 | 'obj.http.x-url ~ "^/assets/SectionTest/test_document\.pdf$"', |
||
208 | $result[0]['banExpression'], |
||
209 | 'ban expression is correct' |
||
210 | ); |
||
211 | } |
||
212 | |||
213 | public function testFlushSiteTree() |
||
253 | ); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | class SectionIOTest_MySectionIO extends SectionIO |
||
258 | { |
||
259 | protected static function performFlush($banExpression) |
||
274 | } |
||
275 | } |
||
276 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.