Passed
Pull Request — master (#174)
by Matthew
05:38
created

LocatorControllerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 76
Duplicated Lines 35.53 %

Importance

Changes 0
Metric Value
dl 27
loc 76
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testJson() 12 12 1
A testLocationSearch() 0 15 1
A testXml() 13 13 1
A testIndex() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
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
        /** @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()
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...
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