Passed
Push — sheepy/introspection ( 9a33c8...7d1300 )
by Marco
05:54
created

DataResolverTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 73
c 2
b 0
f 0
dl 0
loc 142
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testCannotIdentifyExceptionForArrayData() 0 5 1
A assertEqualsDump() 0 3 1
A testDataListEmptyColumnIsToNestedArray() 0 5 1
A testDataTraversal() 0 25 1
A testGetMethodReturnsArray() 0 4 1
A testDataObjectGetMethod() 0 4 1
A testUnsupportedObjectException() 0 6 1
A testDataObjectEmptyColumnIsToMap() 0 7 1
A setUpBeforeClass() 0 10 1
A testCannotIdentifyExceptionForDataObject() 0 4 1
A testDataArrayAttributes() 0 5 1
A testDataObjectAttributes() 0 11 1
A testArrayList() 0 12 1
1
<?php
2
3
namespace Firesphere\SolrSearch\Tests;
4
5
use Exception;
6
use Firesphere\SolrSearch\Helpers\DataResolver;
7
use SilverStripe\Control\HTTPRequestBuilder;
8
use SilverStripe\Dev\Debug;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\ORM\ArrayList;
11
use SilverStripe\ORM\DatabaseAdmin;
12
use SilverStripe\ORM\DB;
13
use SilverStripe\View\ArrayData;
14
use stdClass;
15
16
class DataResolverTest extends SapphireTest
17
{
18
    protected static $fixture_file = 'tests/fixtures/DataResolver.yml';
19
20
    public function assertEqualsDump()
21
    {
22
        Debug::dump(func_get_args());
23
    }
24
25
    public static function setUpBeforeClass()
26
    {
27
        parent::setUpBeforeClass();
28
        // This is a hack on my local dev to make fixtures populate temporary database
29
        $conn = DB::get_conn();
30
        $dbName = self::tempDB()->build();
31
        $conn->selectDatabase($dbName);
32
        $dbAdmin = new DatabaseAdmin();
33
        $dbAdmin->setRequest(HTTPRequestBuilder::createFromEnvironment());
34
        $dbAdmin->doBuild(true, true, true);
35
    }
36
37
    /**
38
     * @expectedException Exception
39
     * @expectedExceptionMessage Class: stdClass, is not supported.
40
     */
41
    public function testUnsupportedObjectException()
42
    {
43
        $obj = new stdClass();
44
        $obj->Created = '2019-07-04 22:01:00';
45
        $obj->Title = 'Hello generic class';
46
        $this->assertEqualsDump($obj->Title, DataResolver::identify($obj, 'Title'));
0 ignored issues
show
Bug introduced by
$obj of type stdClass is incompatible with the type SilverStripe\ORM\DataObj...erStripe\View\ArrayData expected by parameter $obj of Firesphere\SolrSearch\He...ataResolver::identify(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        $this->assertEqualsDump($obj->Title, DataResolver::identify(/** @scrutinizer ignore-type */ $obj, 'Title'));
Loading history...
47
    }
48
49
    /**
50
     * @expectedException Exception
51
         * @expectedExceptionMessage Cannot identify, "UnknownColumn" from class "Firesphere\SolrSearch\Tests\TestPage"
52
     */
53
    public function testCannotIdentifyExceptionForDataObject()
54
    {
55
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
56
        DataResolver::identify($pageOne, 'UnknownColumn');
57
    }
58
59
    /**
60
     * @expectedException Exception
61
     * @expectedExceptionMessage Cannot identify, "UnknownColumn" from class "SilverStripe\View\ArrayData"
62
     */
63
    public function testCannotIdentifyExceptionForArrayData()
64
    {
65
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
66
        $arrayOne = new ArrayData($pageOne->toMap());
67
        DataResolver::identify($arrayOne, 'UnknownColumn');
68
    }
69
70
    public function testDataObjectEmptyColumnIsToMap()
71
    {
72
        $objectOne = $this->objFromFixture(TestObject::class, 'objectOne');
73
        $this->assertEquals($objectOne->toMap(), DataResolver::identify($objectOne));
74
        $arrayOne = new ArrayData($objectOne->toMap());
75
        $this->assertEquals($objectOne->toMap(), $arrayOne->toMap());
76
        $this->assertEquals($arrayOne->toMap(), DataResolver::identify($arrayOne));
77
    }
78
79
    public function testDataListEmptyColumnIsToNestedArray()
80
    {
81
        $objectOne = $this->objFromFixture(TestObject::class, 'objectOne');
82
        $dataList = $objectOne->TestPages();
83
        $this->assertEquals($dataList->toNestedArray(), DataResolver::identify($dataList));
84
    }
85
86
    public function testDataObjectGetMethod()
87
    {
88
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
89
        $this->assertEquals($pageOne->getSalutation(), DataResolver::identify($pageOne, 'Salutation'));
90
    }
91
92
    public function testDataObjectAttributes()
93
    {
94
        $mockDate = '2019-07-04 22:01:00';
95
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
96
        $pageOne->Created = $mockDate;
97
        $pageOne->write();
98
        $this->assertEquals($pageOne->Title, DataResolver::identify($pageOne, 'Title'));
99
        $this->assertEquals($mockDate, $pageOne->Created);
100
        $this->assertEquals($mockDate, DataResolver::identify($pageOne, 'Created'));
101
        $this->assertEquals('Jul 4, 2019, 10:01 PM', DataResolver::identify($pageOne, 'Created.Nice'));
102
        $this->assertEquals('2019-07-04T22:01:00+00:00', DataResolver::identify($pageOne, 'Created.Rfc3339'));
103
    }
104
105
    public function testDataArrayAttributes()
106
    {
107
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
108
        $arrayOne = new ArrayData($pageOne->toMap());
109
        $this->assertEquals($arrayOne->Title, DataResolver::identify($arrayOne, 'Title'));
110
    }
111
112
    public function testDataTraversal()
113
    {
114
        $mockDate = '2019-07-04 22:01:00';
115
        $objectOne = $this->objFromFixture(TestObject::class, 'objectOne');
116
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
117
        $relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne');
118
        $relationOne->Created = $mockDate;
119
        $relationOne->write();
120
        $this->assertEquals($objectOne->toMap(), DataResolver::identify($pageOne, 'TestObject'));
121
        $this->assertEquals($objectOne->Title, DataResolver::identify($pageOne, 'TestObject.Title'));
122
        $this->assertEquals(0, DataResolver::identify($pageOne, 'TestObject.ShowInSearch'));
123
        $this->assertEquals('No', DataResolver::identify($pageOne, 'TestObject.ShowInSearch.Nice'));
124
        $this->assertEquals($objectOne->TestRelation()->toNestedArray(), DataResolver::identify($pageOne, 'TestObject.TestRelation'));
125
        $this->assertEquals($objectOne->TestRelation()->First()->toMap(), DataResolver::identify($pageOne, 'TestObject.TestRelation.First'));
126
        $this->assertEquals($relationOne->Title, DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Title'));
127
        $this->assertEquals($mockDate, DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created'));
128
        $this->assertEquals($mockDate, $relationOne->Created);
129
        $this->assertEquals('Jul 4, 2019, 10:01 PM', DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created.Nice'));
130
        $this->assertEquals('2019-07-04T22:01:00+00:00', DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created.Rfc3339'));
131
        $relationTwo = $this->objFromFixture(TestRelationObject::class, 'relationTwo');
132
        $relationTwo->Created = '2019-07-05 23:05:00';
133
        $relationTwo->write();
134
        $this->assertEquals(
135
            ['Jul 4, 2019, 10:01 PM', 'Jul 5, 2019, 11:05 PM'],
136
            DataResolver::identify($pageOne, 'TestObject.TestRelation.Created.Nice')
137
        );
138
    }
139
140
    public function testGetMethodReturnsArray()
141
    {
142
        $relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne');
143
        $this->assertEquals($relationOne->FarmAnimals, DataResolver::identify($relationOne, 'FarmAnimals'));
144
    }
145
146
    public function testArrayList()
147
    {
148
        $list = new ArrayList(
149
            [
150
                new ArrayData(['Title' => 'one']),
151
                new ArrayData(['Title' => 'two']),
152
                new ArrayData(['Title' => 'three']),
153
            ]
154
        );
155
        $this->assertEquals($list->toNestedArray(), DataResolver::identify($list));
156
        $this->assertEquals($list->first()->Title, DataResolver::identify($list, 'First.Title'));
157
        $this->assertEquals($list->last()->Title, DataResolver::identify($list, 'Last.Title'));
158
    }
159
}