Passed
Pull Request — master (#223)
by Nic
03:50
created

LocatorControllerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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