Passed
Push — sheepy/introspection ( 851cdd...69e16c )
by Marco
06:22
created

DataResolverTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Firesphere\SolrSearch\Tests;
4
5
use Exception;
6
use Firesphere\SolrSearch\Extensions\DataObjectExtension;
7
use Firesphere\SolrSearch\Helpers\DataResolver;
8
use Page;
9
use SilverStripe\Control\HTTPRequestBuilder;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\Debug;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\ORM\ArrayList;
15
use SilverStripe\ORM\DatabaseAdmin;
16
use SilverStripe\ORM\DB;
17
use SilverStripe\View\ArrayData;
18
use stdClass;
19
20
class DataResolverTest extends SapphireTest
21
{
22
    protected static $fixture_file = 'tests/fixtures/DataResolver.yml';
23
    protected static $extra_dataobjects = [
24
        TestObject::class,
25
        TestPage::class,
26
        TestRelationObject::class,
27
    ];
28
29
    public function assertEqualsDump()
30
    {
31
        Debug::dump(func_get_args());
32
    }
33
34
    public function setUp()
35
    {
36
        parent::setUp();
37
        Injector::inst()->get(Page::class)->requireDefaultRecords();
38
        foreach (self::$extra_dataobjects as $className) {
39
            Config::modify()->merge($className, 'extensions', [DataObjectExtension::class]);
40
        }
41
    }
42
43
    public static function ___setUpBeforeClass()
44
    {
45
        parent::setUpBeforeClass();
46
        // This is a hack on my local dev to make fixtures populate temporary database
47
        $conn = DB::get_conn();
48
        $dbName = self::tempDB()->build();
49
        $conn->selectDatabase($dbName);
50
        $dbAdmin = new DatabaseAdmin();
51
        $dbAdmin->setRequest(HTTPRequestBuilder::createFromEnvironment());
52
        $dbAdmin->doBuild(true, true, true);
53
    }
54
55
    /**
56
     * @expectedException Exception
57
     * @expectedExceptionMessage Class: stdClass, is not supported.
58
     */
59
    public function testUnsupportedObjectException()
60
    {
61
        $obj = new stdClass();
62
        $obj->Created = '2019-07-04 22:01:00';
63
        $obj->Title = 'Hello generic class';
64
        $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

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