1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Locator\Tests; |
4
|
|
|
|
5
|
|
|
use \DOMDocument; |
6
|
|
|
use Dynamic\Locator\LocationCategory; |
7
|
|
|
use Dynamic\Locator\Locator; |
8
|
|
|
use Dynamic\Locator\LocatorController; |
9
|
|
|
use SilverStripe\Dev\FunctionalTest; |
10
|
|
|
use SilverStripe\Forms\FieldList; |
11
|
|
|
use SilverStripe\Forms\Form; |
12
|
|
|
use SilverStripe\View\ViewableData; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class LocatorControllerTest |
16
|
|
|
* @package Dynamic\Locator\Tests |
17
|
|
|
*/ |
18
|
|
|
class LocatorControllerTest extends FunctionalTest |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected static $fixture_file = '../fixtures.yml'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected static $use_draft_site = true; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
public function testIndex() |
35
|
|
|
{ |
36
|
|
|
$locator = $this->objFromFixture(Locator::class, 'locator1'); |
37
|
|
|
$controller = LocatorController::create($locator); |
38
|
|
|
$this->assertInstanceOf(ViewableData::class, $controller->index($controller->getRequest())); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function testXml() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
/** @var Locator $locator */ |
47
|
|
|
$locator = $this->objFromFixture(Locator::class, 'locator1'); |
48
|
|
|
$page = $this->get($locator->Link('xml')); |
49
|
|
|
|
50
|
|
|
$this->assertEquals(200, $page->getStatusCode()); |
51
|
|
|
$this->assertEquals('application/xml', $page->getHeader('content-type')); |
52
|
|
|
|
53
|
|
|
$dom = new DOMDocument(); |
54
|
|
|
// true if it loads, false if it doesn't |
55
|
|
|
$valid = $dom->loadXML($page->getBody()); |
56
|
|
|
$this->assertTrue($valid); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function testJson() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
/** @var Locator $locator */ |
65
|
|
|
$locator = $this->objFromFixture(Locator::class, 'locator1'); |
66
|
|
|
$page = $this->get($locator->Link('json')); |
67
|
|
|
|
68
|
|
|
$this->assertEquals(200, $page->getStatusCode()); |
69
|
|
|
$this->assertEquals('application/json', $page->getHeader('content-type')); |
70
|
|
|
|
71
|
|
|
$json = json_decode($page->getBody()); |
72
|
|
|
// if it is null its not valid |
73
|
|
|
$this->assertNotNull($json); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
*/ |
79
|
|
|
public function testLocationSearch() |
80
|
|
|
{ |
81
|
|
|
$locator = $this->objFromFixture(Locator::class, 'locator1'); |
82
|
|
|
$object = LocatorController::create($locator); |
83
|
|
|
$form = $object->LocationSearch(); |
84
|
|
|
$this->assertInstanceOf(Form::class, $form); |
85
|
|
|
|
86
|
|
|
$category = $this->objFromFixture(LocationCategory::class, 'service'); |
87
|
|
|
$category2 = $this->objFromFixture(LocationCategory::class, 'manufacturing'); |
88
|
|
|
$locator->Categories()->add($category); |
89
|
|
|
$locator->Categories()->add($category2); |
90
|
|
|
|
91
|
|
|
$form = $object->LocationSearch(); |
92
|
|
|
$fields = $form->Fields(); |
93
|
|
|
$this->assertInstanceOf(FieldList::class, $fields); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
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.