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
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected static $fixture_file = '../fixtures/DataResolver.yml'; |
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected static $extra_dataobjects = [ |
30
|
|
|
TestObject::class, |
31
|
|
|
TestPage::class, |
32
|
|
|
TestRelationObject::class, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
public static function ___setUpBeforeClass() |
36
|
|
|
{ |
37
|
|
|
parent::setUpBeforeClass(); |
38
|
|
|
// This is a hack on my local dev to make fixtures populate temporary database |
39
|
|
|
$conn = DB::get_conn(); |
40
|
|
|
$dbName = self::tempDB()->build(); |
41
|
|
|
$conn->selectDatabase($dbName); |
42
|
|
|
$dbAdmin = new DatabaseAdmin(); |
43
|
|
|
$dbAdmin->setRequest(HTTPRequestBuilder::createFromEnvironment()); |
44
|
|
|
$dbAdmin->doBuild(true, true, true); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setUp() |
48
|
|
|
{ |
49
|
|
|
parent::setUp(); |
50
|
|
|
Injector::inst()->get(Page::class)->requireDefaultRecords(); |
51
|
|
|
foreach (self::$extra_dataobjects as $className) { |
52
|
|
|
Config::modify()->merge($className, 'extensions', [DataObjectExtension::class]); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @expectedException Exception |
58
|
|
|
* @expectedExceptionMessage Class: stdClass is not supported. |
59
|
|
|
*/ |
60
|
|
|
public function testUnsupportedObjectException() |
61
|
|
|
{ |
62
|
|
|
$obj = new stdClass(); |
63
|
|
|
$obj->Created = '2019-07-04 22:01:00'; |
64
|
|
|
$obj->Title = 'Hello generic class'; |
65
|
|
|
$this->assertEqualsDump($obj->Title, DataResolver::identify($obj, 'Title')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function assertEqualsDump() |
69
|
|
|
{ |
70
|
|
|
Debug::dump(func_get_args()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @expectedException Exception |
75
|
|
|
* @expectedExceptionMessage Cannot identify, "UnknownColumn" from class "TestPage" |
76
|
|
|
*/ |
77
|
|
|
public function testCannotIdentifyExceptionForDataObject() |
78
|
|
|
{ |
79
|
|
|
$pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); |
80
|
|
|
DataResolver::identify($pageOne, 'UnknownColumn'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @expectedException Exception |
85
|
|
|
* @expectedExceptionMessage Cannot identify, "UnknownColumn" from class "ArrayData" |
86
|
|
|
*/ |
87
|
|
|
public function testCannotIdentifyExceptionForArrayData() |
88
|
|
|
{ |
89
|
|
|
$pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); |
90
|
|
|
$arrayOne = new ArrayData($pageOne->toMap()); |
91
|
|
|
DataResolver::identify($arrayOne, 'UnknownColumn'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @expectedException \LogicException |
96
|
|
|
* @expectedExceptionMessage Method, "SuperNice" not found on "DBDatetime" |
97
|
|
|
*/ |
98
|
|
|
public function testMethodNotFoundFromDBField() |
99
|
|
|
{ |
100
|
|
|
$pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); |
101
|
|
|
DataResolver::identify($pageOne, 'Created.SuperNice'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @expectedException Exception |
106
|
|
|
* @expectedExceptionMessage Cannot identify, "Timestamp" from class "DBDatetime" |
107
|
|
|
*/ |
108
|
|
|
public function testCannotIdentifyExceptionForDBField() |
109
|
|
|
{ |
110
|
|
|
$pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); |
111
|
|
|
DataResolver::identify($pageOne, 'Created.Nice.Timestamp'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testDataObjectEmptyColumnIsToMap() |
115
|
|
|
{ |
116
|
|
|
$objectOne = $this->objFromFixture(TestObject::class, 'objectOne'); |
117
|
|
|
$this->assertEquals($objectOne->toMap(), DataResolver::identify($objectOne)); |
118
|
|
|
$arrayOne = new ArrayData($objectOne->toMap()); |
119
|
|
|
$this->assertEquals($objectOne->toMap(), $arrayOne->toMap()); |
120
|
|
|
$this->assertEquals($arrayOne->toMap(), DataResolver::identify($arrayOne)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testDataListEmptyColumnIsToNestedArray() |
124
|
|
|
{ |
125
|
|
|
$objectOne = $this->objFromFixture(TestObject::class, 'objectOne'); |
126
|
|
|
$dataList = $objectOne->TestPages(); |
127
|
|
|
$this->assertEquals($dataList->toNestedArray(), DataResolver::identify($dataList)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testDataObjectGetMethod() |
131
|
|
|
{ |
132
|
|
|
$pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); |
133
|
|
|
$this->assertEquals($pageOne->getSalutation(), DataResolver::identify($pageOne, 'Salutation')); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testDataObjectAttributes() |
137
|
|
|
{ |
138
|
|
|
$mockDate = '2019-07-04 22:01:00'; |
139
|
|
|
$pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); |
140
|
|
|
$pageOne->Created = $mockDate; |
141
|
|
|
$pageOne->write(); |
142
|
|
|
$this->assertEquals($pageOne->Title, DataResolver::identify($pageOne, 'Title')); |
143
|
|
|
$this->assertEquals($mockDate, $pageOne->Created); |
144
|
|
|
$this->assertEquals($mockDate, DataResolver::identify($pageOne, 'Created')); |
145
|
|
|
$this->assertEquals('Jul 4, 2019, 10:01 PM', DataResolver::identify($pageOne, 'Created.Nice')); |
146
|
|
|
$this->assertEquals('2019-07-04T22:01:00+00:00', DataResolver::identify($pageOne, 'Created.Rfc3339')); |
147
|
|
|
$this->assertEquals('1562277660', DataResolver::identify($pageOne, 'Created.Timestamp')); |
148
|
|
|
$this->assertEquals('1562277660', DataResolver::identify($pageOne, 'Created.getTimestamp')); |
149
|
|
|
$this->assertEquals('y-MM-dd HH:mm:ss', DataResolver::identify($pageOne, 'Created.ISOFormat')); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testDataArrayAttributes() |
153
|
|
|
{ |
154
|
|
|
$pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); |
155
|
|
|
$arrayOne = new ArrayData($pageOne->toMap()); |
156
|
|
|
$this->assertEquals($arrayOne->Title, DataResolver::identify($arrayOne, 'Title')); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testDataTraversal() |
160
|
|
|
{ |
161
|
|
|
$mockDate = '2019-07-04 22:01:00'; |
162
|
|
|
$objectOne = $this->objFromFixture(TestObject::class, 'objectOne'); |
163
|
|
|
$pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); |
164
|
|
|
$relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne'); |
165
|
|
|
$relationOne->Created = $mockDate; |
166
|
|
|
$relationOne->write(); |
167
|
|
|
$this->assertEquals($objectOne->toMap(), DataResolver::identify($pageOne, 'TestObject')); |
168
|
|
|
$this->assertEquals($objectOne->Title, DataResolver::identify($pageOne, 'TestObject.Title')); |
169
|
|
|
$this->assertEquals(0, DataResolver::identify($pageOne, 'TestObject.ShowInSearch')); |
170
|
|
|
$this->assertEquals('No', DataResolver::identify($pageOne, 'TestObject.ShowInSearch.Nice')); |
171
|
|
|
$this->assertEquals( |
172
|
|
|
$objectOne->TestRelation()->toNestedArray(), |
173
|
|
|
DataResolver::identify($pageOne, 'TestObject.TestRelation') |
174
|
|
|
); |
175
|
|
|
$this->assertEquals( |
176
|
|
|
$objectOne->TestRelation()->First()->toMap(), |
177
|
|
|
DataResolver::identify($pageOne, 'TestObject.TestRelation.First') |
178
|
|
|
); |
179
|
|
|
$this->assertEquals( |
180
|
|
|
$relationOne->Title, |
181
|
|
|
DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Title') |
182
|
|
|
); |
183
|
|
|
$this->assertEquals($mockDate, DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created')); |
184
|
|
|
$this->assertEquals($mockDate, $relationOne->Created); |
185
|
|
|
$this->assertEquals( |
186
|
|
|
'Jul 4, 2019, 10:01 PM', |
187
|
|
|
DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created.Nice') |
188
|
|
|
); |
189
|
|
|
$this->assertEquals( |
190
|
|
|
'2019-07-04T22:01:00+00:00', |
191
|
|
|
DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created.Rfc3339') |
192
|
|
|
); |
193
|
|
|
$relationTwo = $this->objFromFixture(TestRelationObject::class, 'relationTwo'); |
194
|
|
|
$relationTwo->Created = '2019-07-05 23:05:00'; |
195
|
|
|
$relationTwo->write(); |
196
|
|
|
$this->assertEquals( |
197
|
|
|
['Jul 4, 2019, 10:01 PM', 'Jul 5, 2019, 11:05 PM'], |
198
|
|
|
DataResolver::identify($pageOne, 'TestObject.TestRelation.Created.Nice') |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testGetMethodReturnsArray() |
203
|
|
|
{ |
204
|
|
|
/** @var TestRelationObject $relationOne */ |
205
|
|
|
$relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne'); |
206
|
|
|
$this->assertEquals($relationOne->getFarmAnimals(), DataResolver::identify($relationOne, 'FarmAnimals')); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testMethodReturnsString() |
210
|
|
|
{ |
211
|
|
|
/** @var TestRelationObject $relationOne */ |
212
|
|
|
$relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne'); |
213
|
|
|
$this->assertEquals('cow', DataResolver::identify($relationOne, 'Cow')); |
214
|
|
|
$this->assertEquals('sheep', DataResolver::identify($relationOne, 'Sheep')); |
215
|
|
|
$this->assertEquals(['cow', 'sheep'], DataResolver::identify($relationOne, 'getFarmAnimals')); |
216
|
|
|
$this->assertEquals('cow', DataResolver::identify($relationOne, 'getCow')); |
217
|
|
|
$this->assertEquals('sheep', DataResolver::identify($relationOne, 'getSheep')); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testArrayList() |
221
|
|
|
{ |
222
|
|
|
$list = new ArrayList( |
223
|
|
|
[ |
224
|
|
|
new ArrayData(['Title' => 'one']), |
225
|
|
|
new ArrayData(['Title' => 'two']), |
226
|
|
|
new ArrayData(['Title' => 'three']), |
227
|
|
|
] |
228
|
|
|
); |
229
|
|
|
$this->assertEquals($list->toNestedArray(), DataResolver::identify($list)); |
230
|
|
|
$this->assertEquals($list->first()->Title, DataResolver::identify($list, 'First.Title')); |
231
|
|
|
$this->assertEquals($list->last()->Title, DataResolver::identify($list, 'Last.Title')); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|