Passed
Pull Request — master (#77)
by Marco
05:19 queued 03:13
created

testCannotIdentifyExceptionForDataObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
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 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 "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 "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
    /**
71
     * @expectedException Exception
72
     * @expectedExceptionMessage Method, "SuperNice" not found on "DBDatetime"
73
     */
74
    public function testMethodNotFoundFromDBField()
75
    {
76
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
77
        DataResolver::identify($pageOne, 'Created.SuperNice');
78
    }
79
80
    public function testDataObjectEmptyColumnIsToMap()
81
    {
82
        $objectOne = $this->objFromFixture(TestObject::class, 'objectOne');
83
        $this->assertEquals($objectOne->toMap(), DataResolver::identify($objectOne));
84
        $arrayOne = new ArrayData($objectOne->toMap());
85
        $this->assertEquals($objectOne->toMap(), $arrayOne->toMap());
86
        $this->assertEquals($arrayOne->toMap(), DataResolver::identify($arrayOne));
87
    }
88
89
    public function testDataListEmptyColumnIsToNestedArray()
90
    {
91
        $objectOne = $this->objFromFixture(TestObject::class, 'objectOne');
92
        $dataList = $objectOne->TestPages();
93
        $this->assertEquals($dataList->toNestedArray(), DataResolver::identify($dataList));
94
    }
95
96
    public function testDataObjectGetMethod()
97
    {
98
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
99
        $this->assertEquals($pageOne->getSalutation(), DataResolver::identify($pageOne, 'Salutation'));
100
    }
101
102
    public function testDataObjectAttributes()
103
    {
104
        $mockDate = '2019-07-04 22:01:00';
105
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
106
        $pageOne->Created = $mockDate;
107
        $pageOne->write();
108
        $this->assertEquals($pageOne->Title, DataResolver::identify($pageOne, 'Title'));
109
        $this->assertEquals($mockDate, $pageOne->Created);
110
        $this->assertEquals($mockDate, DataResolver::identify($pageOne, 'Created'));
111
        $this->assertEquals('Jul 4, 2019, 10:01 PM', DataResolver::identify($pageOne, 'Created.Nice'));
112
        $this->assertEquals('2019-07-04T22:01:00+00:00', DataResolver::identify($pageOne, 'Created.Rfc3339'));
113
        $this->assertEquals('1562277660', DataResolver::identify($pageOne, 'Created.Timestamp'));
114
        $this->assertEquals('1562277660', DataResolver::identify($pageOne, 'Created.getTimestamp'));
115
        $this->assertEquals('y-MM-dd HH:mm:ss', DataResolver::identify($pageOne, 'Created.ISOFormat'));
116
    }
117
118
    public function testDataArrayAttributes()
119
    {
120
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
121
        $arrayOne = new ArrayData($pageOne->toMap());
122
        $this->assertEquals($arrayOne->Title, DataResolver::identify($arrayOne, 'Title'));
123
    }
124
125
    public function testDataTraversal()
126
    {
127
        $mockDate = '2019-07-04 22:01:00';
128
        $objectOne = $this->objFromFixture(TestObject::class, 'objectOne');
129
        $pageOne = $this->objFromFixture(TestPage::class, 'pageOne');
130
        $relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne');
131
        $relationOne->Created = $mockDate;
132
        $relationOne->write();
133
        $this->assertEquals($objectOne->toMap(), DataResolver::identify($pageOne, 'TestObject'));
134
        $this->assertEquals($objectOne->Title, DataResolver::identify($pageOne, 'TestObject.Title'));
135
        $this->assertEquals(0, DataResolver::identify($pageOne, 'TestObject.ShowInSearch'));
136
        $this->assertEquals('No', DataResolver::identify($pageOne, 'TestObject.ShowInSearch.Nice'));
137
        $this->assertEquals($objectOne->TestRelation()->toNestedArray(), DataResolver::identify($pageOne, 'TestObject.TestRelation'));
138
        $this->assertEquals($objectOne->TestRelation()->First()->toMap(), DataResolver::identify($pageOne, 'TestObject.TestRelation.First'));
139
        $this->assertEquals($relationOne->Title, DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Title'));
140
        $this->assertEquals($mockDate, DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created'));
141
        $this->assertEquals($mockDate, $relationOne->Created);
142
        $this->assertEquals('Jul 4, 2019, 10:01 PM', DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created.Nice'));
143
        $this->assertEquals('2019-07-04T22:01:00+00:00', DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created.Rfc3339'));
144
        $relationTwo = $this->objFromFixture(TestRelationObject::class, 'relationTwo');
145
        $relationTwo->Created = '2019-07-05 23:05:00';
146
        $relationTwo->write();
147
        $this->assertEquals(
148
            ['Jul 4, 2019, 10:01 PM', 'Jul 5, 2019, 11:05 PM'],
149
            DataResolver::identify($pageOne, 'TestObject.TestRelation.Created.Nice')
150
        );
151
    }
152
153
    public function testGetMethodReturnsArray()
154
    {
155
        $relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne');
156
        $this->assertEquals($relationOne->FarmAnimals, DataResolver::identify($relationOne, 'FarmAnimals'));
157
    }
158
159
    public function testArrayList()
160
    {
161
        $list = new ArrayList(
162
            [
163
                new ArrayData(['Title' => 'one']),
164
                new ArrayData(['Title' => 'two']),
165
                new ArrayData(['Title' => 'three']),
166
            ]
167
        );
168
        $this->assertEquals($list->toNestedArray(), DataResolver::identify($list));
169
        $this->assertEquals($list->first()->Title, DataResolver::identify($list, 'First.Title'));
170
        $this->assertEquals($list->last()->Title, DataResolver::identify($list, 'Last.Title'));
171
    }
172
}