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() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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.