Passed
Pull Request — master (#173)
by Matthew
13:34
created

LocatorControllerTest::testGetLocations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 31
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 52
rs 9.4929

How to fix   Long Method   

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
namespace Dynamic\Locator\Tests;
4
5
use Dynamic\Locator\Location;
6
use Dynamic\Locator\LocationCategory;
7
use Dynamic\Locator\Locator;
8
use Dynamic\Locator\LocatorController;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Dev\FunctionalTest;
12
use SilverStripe\ORM\ArrayList;
13
use SilverStripe\ORM\DataObject;
14
15
/**
16
 * Class LocatorControllerTest
17
 * @package Dynamic\Locator\Tests
18
 */
19
class LocatorControllerTest extends FunctionalTest
20
{
21
22
    protected static $fixture_file = '../fixtures.yml';
23
24
    protected static $use_draft_site = true;
25
26
    /**
27
     * Tests the json route
28
     */
29 View Code Duplication
    public function testJson()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
30
    {
31
        $locator = $this->objFromFixture(Locator::class, 'locator1');
32
33
        $page = $this->get($locator->Link('json'));
34
35
        $this->assertEquals(200, $page->getStatusCode());
36
        $this->assertEquals('application/json', $page->getHeader('Content-Type'));
37
38
        $this->assertNotEquals(null, json_decode($page->getBody()));
39
    }
40
41
    /**
42
     * Tests the settings route
43
     */
44 View Code Duplication
    public function testSettings()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
45
    {
46
        $locator = $this->objFromFixture(Locator::class, 'locator1');
47
48
        $page = $this->get($locator->Link('settings'));
49
50
        $this->assertEquals(200, $page->getStatusCode());
51
        $this->assertEquals('application/json', $page->getHeader('Content-Type'));
52
53
        $this->assertNotEquals(null, json_decode($page->getBody()));
54
    }
55
56
    /**
57
     * Tests specific parts of getLocations()
58
     */
59
    public function testGetLocations()
60
    {
61
        $category = $this->objFromFixture(LocationCategory::class, 'service');
62
63
        $loc1 = $this->objFromFixture(Location::class, 'dynamic');
64
        // generic Sheboygan WI coordinates
65
        $loc1->Lat = 43.7444081;
66
        $loc1->Lng = -87.8005188;
67
        $loc1->write();
68
69
        $loc2 = $this->objFromFixture(Location::class, 'silverstripe');
70
        // generic New Zealand coordinates
71
        $loc2->Lat = -39.3846249;
72
        $loc2->Lng = 157.3172296;
73
        $loc2->write();
74
75
        $locator = $this->objFromFixture(Locator::class, 'locator1');
76
77
        $controller = Injector::inst()->create(LocatorController::class);
78
79
        $request = new HTTPRequest(
80
            'GET',
81
            $locator->Link('json'),
82
            array(
83
                'address' => '1526+S+12th+St+Sheboygan+WI',
84
                'radius' => 25,
85
                'category' => $category->ID,
86
            )
87
        );
88
89
        $locations = $controller->getLocations($request);
90
91
        $this->assertInstanceOf(ArrayList::class, $locations);
92
        // because address isn't actually used to determine distance here,
93
        // everything defaults to -1
94
        $this->assertEquals(2, $locations->Count());
95
96
        // radius of -1, everything in the category should be in the list
97
        $request = new HTTPRequest(
98
            'GET',
99
            $locator->Link('json'),
100
            array(
101
                'address' => '1526+S+12th+St+Sheboygan+WI',
102
                'radius' => -1,
103
                'category' => $category->ID,
104
            )
105
        );
106
107
        $locations = $controller->getLocations($request);
108
109
        $this->assertInstanceOf(ArrayList::class, $locations);
110
        $this->assertEquals(2, $locations->Count());
111
    }
112
}
113