|
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')); |
|
|
|
|
|
|
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
|
|
|
/** |
|
71
|
|
|
* @expectedException Exception |
|
72
|
|
|
* @expectedExceptionMessage Method, "SuperNice" not found on "SilverStripe\ORM\FieldType\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
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testDataArrayAttributes() |
|
116
|
|
|
{ |
|
117
|
|
|
$pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); |
|
118
|
|
|
$arrayOne = new ArrayData($pageOne->toMap()); |
|
119
|
|
|
$this->assertEquals($arrayOne->Title, DataResolver::identify($arrayOne, 'Title')); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testDataTraversal() |
|
123
|
|
|
{ |
|
124
|
|
|
$mockDate = '2019-07-04 22:01:00'; |
|
125
|
|
|
$objectOne = $this->objFromFixture(TestObject::class, 'objectOne'); |
|
126
|
|
|
$pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); |
|
127
|
|
|
$relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne'); |
|
128
|
|
|
$relationOne->Created = $mockDate; |
|
129
|
|
|
$relationOne->write(); |
|
130
|
|
|
$this->assertEquals($objectOne->toMap(), DataResolver::identify($pageOne, 'TestObject')); |
|
131
|
|
|
$this->assertEquals($objectOne->Title, DataResolver::identify($pageOne, 'TestObject.Title')); |
|
132
|
|
|
$this->assertEquals(0, DataResolver::identify($pageOne, 'TestObject.ShowInSearch')); |
|
133
|
|
|
$this->assertEquals('No', DataResolver::identify($pageOne, 'TestObject.ShowInSearch.Nice')); |
|
134
|
|
|
$this->assertEquals($objectOne->TestRelation()->toNestedArray(), DataResolver::identify($pageOne, 'TestObject.TestRelation')); |
|
135
|
|
|
$this->assertEquals($objectOne->TestRelation()->First()->toMap(), DataResolver::identify($pageOne, 'TestObject.TestRelation.First')); |
|
136
|
|
|
$this->assertEquals($relationOne->Title, DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Title')); |
|
137
|
|
|
$this->assertEquals($mockDate, DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created')); |
|
138
|
|
|
$this->assertEquals($mockDate, $relationOne->Created); |
|
139
|
|
|
$this->assertEquals('Jul 4, 2019, 10:01 PM', DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created.Nice')); |
|
140
|
|
|
$this->assertEquals('2019-07-04T22:01:00+00:00', DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created.Rfc3339')); |
|
141
|
|
|
$relationTwo = $this->objFromFixture(TestRelationObject::class, 'relationTwo'); |
|
142
|
|
|
$relationTwo->Created = '2019-07-05 23:05:00'; |
|
143
|
|
|
$relationTwo->write(); |
|
144
|
|
|
$this->assertEquals( |
|
145
|
|
|
['Jul 4, 2019, 10:01 PM', 'Jul 5, 2019, 11:05 PM'], |
|
146
|
|
|
DataResolver::identify($pageOne, 'TestObject.TestRelation.Created.Nice') |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testGetMethodReturnsArray() |
|
151
|
|
|
{ |
|
152
|
|
|
$relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne'); |
|
153
|
|
|
$this->assertEquals($relationOne->FarmAnimals, DataResolver::identify($relationOne, 'FarmAnimals')); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function testArrayList() |
|
157
|
|
|
{ |
|
158
|
|
|
$list = new ArrayList( |
|
159
|
|
|
[ |
|
160
|
|
|
new ArrayData(['Title' => 'one']), |
|
161
|
|
|
new ArrayData(['Title' => 'two']), |
|
162
|
|
|
new ArrayData(['Title' => 'three']), |
|
163
|
|
|
] |
|
164
|
|
|
); |
|
165
|
|
|
$this->assertEquals($list->toNestedArray(), DataResolver::identify($list)); |
|
166
|
|
|
$this->assertEquals($list->first()->Title, DataResolver::identify($list, 'First.Title')); |
|
167
|
|
|
$this->assertEquals($list->last()->Title, DataResolver::identify($list, 'Last.Title')); |
|
168
|
|
|
} |
|
169
|
|
|
} |