Completed
Push — master ( 877393...5ce7ef )
by Florian
14:57 queued 13:27
created

SectionIOTest::setUp()   B

Complexity

Conditions 10
Paths 96

Size

Total Lines 43

Duplication

Lines 20
Ratio 46.51 %

Importance

Changes 0
Metric Value
dl 20
loc 43
rs 7.6666
c 0
b 0
f 0
cc 10
nc 96
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class SectionIOTest extends SapphireTest
4
{
5
    protected static $fixture_file = 'SectionIOTest.yml';
6
7
    public function setUpOnce()
8
    {
9
        parent::setUpOnce();
10
11
        // add config values
12
        Config::inst()->update('SectionIO', 'flush_on_dev_build', true);
13
        Config::inst()->update('SectionIO', 'api_url', 'https://example.com');
14
        Config::inst()->update('SectionIO', 'account_id', '123456');
15
        Config::inst()->update('SectionIO', 'application_id', '987654');
16
        Config::inst()->update('SectionIO', 'environment_name', 'Production');
17
        Config::inst()->update('SectionIO', 'proxy_name', 'myproxy');
18
        Config::inst()->update('SectionIO', 'username', 'someuser');
19
        Config::inst()->update('SectionIO', 'password', 'MySafePassword');
20
        Config::inst()->update('SectionIO', 'verify_ssl', false);
21
22
        // remove extensions otherwise the fixtures will break the tests (by calling the live flush)
23
        File::remove_extension('SectionIOFileExtension');
24
        SiteTree::remove_extension('SectionIOSiteTreeExtension');
25
    }
26
27
    public function setUp()
28
    {
29
        parent::setUp();
30
31
        if (!file_exists(ASSETS_PATH)) {
32
            mkdir(ASSETS_PATH);
33
        }
34
35
        // Create a test folders for each of the fixture references
36
        $folderIDs = $this->allFixtureIDs('Folder');
37
        foreach ($folderIDs as $folderID) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
            $folder = DataObject::get_by_id('Folder', $folderID);
39
            if (!file_exists(BASE_PATH."/$folder->Filename")) {
40
                mkdir(BASE_PATH."/$folder->Filename");
41
            }
42
        }
43
44
        // Copy test images for each of the fixture references
45
        $imageIDs = $this->allFixtureIDs('Image');
46 View Code Duplication
        foreach ($imageIDs as $imageID) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
            $image = DataObject::get_by_id('Image', $imageID);
48
            $filePath = BASE_PATH."/$image->Filename";
49
            $sourcePath = str_replace('assets/SectionTest/', 'section-io/tests/testfiles/', $filePath);
50
            if (!file_exists($filePath)) {
51
                if (!copy($sourcePath, $filePath)) {
52
                    user_error('Failed to copy test images', E_USER_ERROR);
53
                }
54
            }
55
        }
56
57
        // Copy test files for each of the fixture references
58
        $fileIDs = $this->allFixtureIDs('File');
59 View Code Duplication
        foreach ($fileIDs as $fileID) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            $file = DataObject::get_by_id('File', $fileID);
61
            $filePath = BASE_PATH."/$file->Filename";
62
            $sourcePath = str_replace('assets/SectionTest/', 'section-io/tests/testfiles/', $filePath);
63
            if (!file_exists($filePath)) {
64
                if (!copy($sourcePath, $filePath)) {
65
                    user_error('Failed to copy test files', E_USER_ERROR);
66
                }
67
            }
68
        }
69
    }
70
71
    public function tearDownOnce()
72
    {
73
        parent::tearDownOnce();
74
75
        // re-add extensions
76
        File::add_extension('SectionIOFileExtension');
77
        SiteTree::add_extension('SectionIOSiteTreeExtension');
78
    }
79
80
    public function tearDown()
81
    {
82
        // Remove the test images that we've created
83
        $imageIDs = $this->allFixtureIDs('Image');
84
        foreach ($imageIDs as $imageID) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
            $image = DataObject::get_by_id('Image', $imageID);
86
            if ($image && file_exists(BASE_PATH."/$image->Filename")) {
87
                unlink(BASE_PATH."/$image->Filename");
88
            }
89
        }
90
91
        // Remove the test files that we've created
92
        $fileIDs = $this->allFixtureIDs('File');
93
        foreach ($fileIDs as $fileID) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
            $file = DataObject::get_by_id('File', $fileID);
95
            if ($file && file_exists(BASE_PATH."/$file->Filename")) {
96
                unlink(BASE_PATH."/$file->Filename");
97
            }
98
        }
99
100
        // Remove the test folders that we've created
101
        $folderIDs = $this->allFixtureIDs('Folder');
102
        foreach ($folderIDs as $folderID) {
103
            $folder = DataObject::get_by_id('Folder', $folderID);
104
            if ($folder && file_exists(BASE_PATH.'/'.$folder->Filename.'_resampled')) {
105
                Filesystem::removeFolder(BASE_PATH.'/'.$folder->Filename.'_resampled');
106
            }
107
            if ($folder && file_exists(BASE_PATH."/$folder->Filename")) {
108
                Filesystem::removeFolder(BASE_PATH."/$folder->Filename");
109
            }
110
        }
111
112
        parent::tearDown();
113
    }
114
115
    public function testFlushAll()
116
    {
117
        $result = SectionIOTest_MySectionIO::flushAll();
118
119
        $this->assertCount(
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
120
            1,
121
            $result,
122
            'one url returned for one application id'
123
        );
124
125
        // url
126
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
127
            'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state',
128
            $result[0]['url'],
129
            'URL is concatenated correctly'
130
        );
131
132
        // ban expression
133
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
134
            'obj.http.x-url ~ /',
135
            $result[0]['banExpression'],
136
            'ban expression is correct'
137
        );
138
139
        // headers
140
        $this->assertContains(
141
            'Content-Type: application/json',
142
            $result[0]['headers'],
143
            'content type header is correct'
144
        );
145
        $this->assertContains(
146
            'Accept: application/json',
147
            $result[0]['headers'],
148
            'accept header is correct'
149
        );
150
151
        // service
152
        $this->assertInstanceOf(
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
153
            'RestfulService',
154
            $result[0]['service'],
155
            'service is of type RestfulService'
156
        );
157
    }
158
159
    public function testFlush()
160
    {
161
        $result = SectionIOTest_MySectionIO::flush();
162
163
        $this->assertCount(
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
164
            1,
165
            $result,
166
            'one url returned for one application id'
167
        );
168
169
        // url
170
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
171
            'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state',
172
            $result[0]['url'],
173
            'URL is concatenated correctly'
174
        );
175
176
        // ban expression
177
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
178
            'obj.http.x-url ~ /',
179
            $result[0]['banExpression'],
180
            'ban expression is correct'
181
        );
182
183
        // test deactivated flush on build
184
        Config::inst()->update('SectionIO', 'flush_on_dev_build', false);
185
        $result = SectionIOTest_MySectionIO::flush();
186
        $this->assertNull(
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
187
            $result,
188
            'null returned if flush on build deactivated'
189
        );
190
    }
191
192
    public function testMultipleApplicationIDs()
193
    {
194
        // add second application to config
195
        Config::inst()->update('SectionIO', 'application_id', '2546987,856954');
196
197
        $result = SectionIOTest_MySectionIO::flushAll();
198
199
        $this->assertCount(
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
200
            2,
201
            $result,
202
            'two urls returned for two application id'
203
        );
204
205
        // url
206
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
207
            'https://example.com/account/123456/application/2546987/environment/Production/proxy/myproxy/state',
208
            $result[0]['url'],
209
            'URL is concatenated correctly for app 1'
210
        );
211
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
212
            'https://example.com/account/123456/application/856954/environment/Production/proxy/myproxy/state',
213
            $result[1]['url'],
214
            'URL is concatenated correctly for app 2'
215
        );
216
217
        // add second application to config with spaces in csv
218
        Config::inst()->update('SectionIO', 'application_id', '741852, 369258');
219
220
        $result = SectionIOTest_MySectionIO::flushAll();
221
222
        $this->assertCount(
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
223
            2,
224
            $result,
225
            'two urls returned for two application id'
226
        );
227
228
        // url
229
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
230
            'https://example.com/account/123456/application/741852/environment/Production/proxy/myproxy/state',
231
            $result[0]['url'],
232
            'URL is concatenated correctly for app 1'
233
        );
234
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
235
            'https://example.com/account/123456/application/369258/environment/Production/proxy/myproxy/state',
236
            $result[1]['url'],
237
            'URL is concatenated correctly for app 2'
238
        );
239
    }
240
241
    public function testFlushImage()
242
    {
243
        $imageId = $this->idFromFixture('Image', 'testImage');
244
245
        $result = SectionIOTest_MySectionIO::flushImage($imageId);
246
247
        // ban expression
248
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
249
            'obj.http.x-url ~ "^/assets/SectionTest/test_image\.png$"'
250
                .' || obj.http.x-url ~ "^/assets/SectionTest/_resampled/(.*)\-test_image\.png$"',
251
            $result[0]['banExpression'],
252
            'ban expression is correct'
253
        );
254
    }
255
256
    public function testFlushFile()
257
    {
258
        $fileId = $this->idFromFixture('File', 'testFile');
259
260
        $result = SectionIOTest_MySectionIO::flushFile($fileId);
261
262
        // ban expression
263
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
264
            'obj.http.x-url ~ "^/assets/SectionTest/test_document\.pdf$"',
265
            $result[0]['banExpression'],
266
            'ban expression is correct'
267
        );
268
    }
269
270
    public function testFlushSiteTree()
271
    {
272
        $pageId = $this->idFromFixture('Page', 'ceo');
273
274
        // test single page flush
275
        Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'single');
276
        $result = SectionIOTest_MySectionIO::flushSiteTree($pageId);
277
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
278
            'obj.http.content-type ~ "text/html"'
279
            .' && obj.http.x-url ~ "^/about\-us/my\-staff/ceo/$"',
280
            $result[0]['banExpression'],
281
            'ban expression is correct'
282
        );
283
284
        // test parents flush
285
        Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'parents');
286
        $result = SectionIOTest_MySectionIO::flushSiteTree($pageId);
287
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
288
            'obj.http.content-type ~ "text/html"'
289
            .' && (obj.http.x-url ~ "^/about\-us/my\-staff/ceo/$" || obj.http.x-url ~ "^/about\-us/my\-staff/$" || obj.http.x-url ~ "^/about\-us/$")',
290
            $result[0]['banExpression'],
291
            'ban expression is correct'
292
        );
293
294
        // test all pages flush
295
        Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'all');
296
        $result = SectionIOTest_MySectionIO::flushSiteTree($pageId);
297
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
298
            'obj.http.content-type ~ "text/html"',
299
            $result[0]['banExpression'],
300
            'ban expression is correct'
301
        );
302
303
        // test whole site flush
304
        Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'everything');
305
        $result = SectionIOTest_MySectionIO::flushSiteTree($pageId);
306
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SectionIOTest>.

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.

Loading history...
307
            'obj.http.x-url ~ /',
308
            $result[0]['banExpression'],
309
            'ban expression is correct'
310
        );
311
    }
312
}
313
314
class SectionIOTest_MySectionIO extends SectionIO
315
{
316
    protected static function performFlush($banExpression)
317
    {
318
        $result = array();
319
        $urls = static::getUrls();
320
        // config loaded successfully
321
        if ($urls) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $urls of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
322
            foreach ($urls as $url) {
323
324
                // get restful service object
325
                $service = static::getService($url, $banExpression);
326
327
                // prepare headers
328
                $headers = static::getHeaders();
329
330
                // prepare curl options
331
                $options = static::getOptions();
332
333
                // store data for return
334
                $data = array();
335
                $data['url'] = $url;
336
                $data['banExpression'] = $banExpression;
337
                $data['headers'] = $headers;
338
                $data['options'] = $options;
339
                $data['service'] = $service;
340
                $result[] = $data;
341
            }
342
        } else {
343
            user_error('SectionIOTest_MySectionIO::performFlush :: no URLs loaded for ban.', E_USER_WARNING);
344
        }
345
346
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (array) is incompatible with the return type of the parent method SectionIO::performFlush of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
347
    }
348
}
349