Passed
Push — sheepy/rebased-intrtospection ( 8df98b...ec17aa )
by Simon
07:12
created

DataResolverTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 2
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Firesphere\SolrSearch\Tests;
4
5
use Exception;
6
use Firesphere\SolrSearch\Helpers\DataResolver;
7
use Firesphere\SolrSearch\Tasks\SolrConfigureTask;
8
use SilverStripe\Control\HTTPRequestBuilder;
9
use SilverStripe\Control\NullHTTPRequest;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Dev\Debug;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\ORM\ArrayList;
14
use SilverStripe\ORM\DatabaseAdmin;
15
use SilverStripe\ORM\DB;
16
use SilverStripe\View\ArrayData;
17
use stdClass;
18
19
class DataResolverTest extends SapphireTest
20
{
21
    protected static $fixture_file = '../fixtures/DataResolver.yml';
22
23
    public function assertEqualsDump()
24
    {
25
        Debug::dump(func_get_args());
26
    }
27
28
    /**
29
     * @expectedException Exception
30
     * @expectedExceptionMessage Class: stdClass, is not supported.
31
     */
32
    public function testUnsupportedObjectException()
33
    {
34
        $obj = new stdClass();
35
        $obj->Created = '2019-07-04 22:01:00';
36
        $obj->Title = 'Hello generic class';
37
        $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

37
        $this->assertEqualsDump($obj->Title, DataResolver::identify(/** @scrutinizer ignore-type */ $obj, 'Title'));
Loading history...
38
    }
39
40
    /**
41
     * @expectedException Exception
42
     * @expectedExceptionMessage Cannot identify, "UnknownColumn" from class "TestPage"
43
     */
44
    public function testCannotIdentifyExceptionForDataObject()
45
    {
46
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
47
        DataResolver::identify($pageOne, 'UnknownColumn');
48
    }
49
50
    /**
51
     * @expectedException Exception
52
     * @expectedExceptionMessage Cannot identify, "UnknownColumn" from class "ArrayData"
53
     */
54
    public function testCannotIdentifyExceptionForArrayData()
55
    {
56
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
57
        $arrayOne = new ArrayData($pageOne->toMap());
58
        DataResolver::identify($arrayOne, 'UnknownColumn');
59
    }
60
61
    /**
62
     * @expectedException Exception
63
     * @expectedExceptionMessage Method, "SuperNice" not found on "DBDatetime"
64
     */
65
    public function testMethodNotFoundFromDBField()
66
    {
67
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
68
        DataResolver::identify($pageOne, 'Created.SuperNice');
69
    }
70
71
    /**
72
     * @expectedException Exception
73
     * @expectedExceptionMessage Cannot identify, "Timestamp" from class "DBDatetime"
74
     */
75
    public function testCannotIdentifyExceptionForDBField()
76
    {
77
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
78
        DataResolver::identify($pageOne, 'Created.Nice.Timestamp');
79
    }
80
81
    public function testDataObjectEmptyColumnIsToMap()
82
    {
83
        $objectOne = $this->objFromFixture(TestObject::class, 'objectOne');
84
        $this->assertEquals($objectOne->toMap(), DataResolver::identify($objectOne));
85
        $arrayOne = new ArrayData($objectOne->toMap());
86
        $this->assertEquals($objectOne->toMap(), $arrayOne->toMap());
87
        $this->assertEquals($arrayOne->toMap(), DataResolver::identify($arrayOne));
88
    }
89
90
    public function testDataListEmptyColumnIsToNestedArray()
91
    {
92
        $objectOne = $this->objFromFixture(TestObject::class, 'objectOne');
93
        $dataList = $objectOne->TestPages();
94
        $this->assertEquals($dataList->toNestedArray(), DataResolver::identify($dataList));
95
    }
96
97
    public function testDataObjectGetMethod()
98
    {
99
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
100
        $this->assertEquals($pageOne->getSalutation(), DataResolver::identify($pageOne, 'Salutation'));
101
    }
102
103
    public function testDataObjectAttributes()
104
    {
105
        $mockDate = '2019-07-04 22:01:00';
106
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
107
        $pageOne->Created = $mockDate;
108
        $pageOne->write();
109
        $this->assertEquals($pageOne->Title, DataResolver::identify($pageOne, 'Title'));
110
        $this->assertEquals($mockDate, $pageOne->Created);
111
        $this->assertEquals($mockDate, DataResolver::identify($pageOne, 'Created'));
112
        $this->assertEquals('Jul 4, 2019, 10:01 PM', DataResolver::identify($pageOne, 'Created.Nice'));
113
        $this->assertEquals('2019-07-04T22:01:00+00:00', DataResolver::identify($pageOne, 'Created.Rfc3339'));
114
        $this->assertEquals('1562277660', DataResolver::identify($pageOne, 'Created.Timestamp'));
115
        $this->assertEquals('1562277660', DataResolver::identify($pageOne, 'Created.getTimestamp'));
116
        $this->assertEquals('y-MM-dd HH:mm:ss', DataResolver::identify($pageOne, 'Created.ISOFormat'));
117
    }
118
119
    public function testDataArrayAttributes()
120
    {
121
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
122
        $arrayOne = new ArrayData($pageOne->toMap());
123
        $this->assertEquals($arrayOne->Title, DataResolver::identify($arrayOne, 'Title'));
124
    }
125
126
    public function testDataTraversal()
127
    {
128
        $mockDate = '2019-07-04 22:01:00';
129
        $objectOne = $this->objFromFixture(TestObject::class, 'objectOne');
130
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
131
        $relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne');
132
        $relationOne->Created = $mockDate;
133
        $relationOne->write();
134
        $this->assertEquals($objectOne->toMap(), DataResolver::identify($pageOne, 'TestObject'));
135
        $this->assertEquals($objectOne->Title, DataResolver::identify($pageOne, 'TestObject.Title'));
136
        $this->assertEquals(0, DataResolver::identify($pageOne, 'TestObject.ShowInSearch'));
137
        $this->assertEquals('No', DataResolver::identify($pageOne, 'TestObject.ShowInSearch.Nice'));
138
        $this->assertEquals($objectOne->TestRelation()->toNestedArray(), DataResolver::identify($pageOne, 'TestObject.TestRelation'));
139
        $this->assertEquals($objectOne->TestRelation()->First()->toMap(), DataResolver::identify($pageOne, 'TestObject.TestRelation.First'));
140
        $this->assertEquals($relationOne->Title, DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Title'));
141
        $this->assertEquals($mockDate, DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created'));
142
        $this->assertEquals($mockDate, $relationOne->Created);
143
        $this->assertEquals('Jul 4, 2019, 10:01 PM', DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created.Nice'));
144
        $this->assertEquals('2019-07-04T22:01:00+00:00', DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created.Rfc3339'));
145
        $relationTwo = $this->objFromFixture(TestRelationObject::class, 'relationTwo');
146
        $relationTwo->Created = '2019-07-05 23:05:00';
147
        $relationTwo->write();
148
        $this->assertEquals(
149
            ['Jul 4, 2019, 10:01 PM', 'Jul 5, 2019, 11:05 PM'],
150
            DataResolver::identify($pageOne, 'TestObject.TestRelation.Created.Nice')
151
        );
152
    }
153
154
    public function testGetMethodReturnsArray()
155
    {
156
        $relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne');
157
        $this->assertEquals($relationOne->FarmAnimals, DataResolver::identify($relationOne, 'FarmAnimals'));
158
    }
159
160
    public function testArrayList()
161
    {
162
        $list = new ArrayList(
163
            [
164
                new ArrayData(['Title' => 'one']),
165
                new ArrayData(['Title' => 'two']),
166
                new ArrayData(['Title' => 'three']),
167
            ]
168
        );
169
        $this->assertEquals($list->toNestedArray(), DataResolver::identify($list));
170
        $this->assertEquals($list->first()->Title, DataResolver::identify($list, 'First.Title'));
171
        $this->assertEquals($list->last()->Title, DataResolver::identify($list, 'Last.Title'));
172
    }
173
174
    protected function setUp()
175
    {
176
        Injector::inst()->get(SolrConfigureTask::class)->run(new NullHTTPRequest());
177
        return parent::setUp();
178
    }
179
}