1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Aimeos\MShop\Customer\Manager\Lists; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
8
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2017 |
9
|
|
|
*/ |
10
|
|
|
class LaravelTest extends \PHPUnit\Framework\TestCase |
11
|
|
|
{ |
12
|
|
|
private $object; |
13
|
|
|
private $context; |
14
|
|
|
private $editor = 'ai-laravel:unittest'; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
protected function setUp() |
18
|
|
|
{ |
19
|
|
|
$this->context = \TestHelper::getContext(); |
20
|
|
|
$this->editor = $this->context->getEditor(); |
21
|
|
|
$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $this->context, 'Laravel' ); |
22
|
|
|
$this->object = $manager->getSubManager( 'lists', 'Laravel' ); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
protected function tearDown() |
27
|
|
|
{ |
28
|
|
|
unset( $this->object, $this->context ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
public function testCleanup() |
33
|
|
|
{ |
34
|
|
|
$this->object->cleanup( array( -1 ) ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
public function testAggregate() |
39
|
|
|
{ |
40
|
|
|
$search = $this->object->createSearch( true ); |
41
|
|
|
$expr = array( |
42
|
|
|
$search->getConditions(), |
43
|
|
|
$search->compare( '==', 'customer.lists.editor', 'ai-laravel:unittest' ), |
44
|
|
|
); |
45
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
46
|
|
|
|
47
|
|
|
$result = $this->object->aggregate( $search, 'customer.lists.domain' ); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$this->assertEquals( 1, count( $result ) ); |
50
|
|
|
$this->assertArrayHasKey( 'text', $result ); |
51
|
|
|
$this->assertEquals( 4, $result['text'] ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
public function testCreateItem() |
56
|
|
|
{ |
57
|
|
|
$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $this->object->createItem() ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
View Code Duplication |
public function testGetItem() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$search = $this->object->createSearch(); |
64
|
|
|
$search->setSlice(0, 1); |
65
|
|
|
$results = $this->object->searchItems( $search ); |
66
|
|
|
|
67
|
|
|
if( ( $item = reset( $results ) ) === false ) { |
68
|
|
|
throw new \RuntimeException( 'No item found' ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->assertEquals( $item, $this->object->getItem( $item->getId() ) ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
public function testSaveUpdateDeleteItem() |
76
|
|
|
{ |
77
|
|
|
$search = $this->object->createSearch(); |
78
|
|
|
$search->setSlice(0, 1); |
79
|
|
|
$items = $this->object->searchItems( $search ); |
80
|
|
|
|
81
|
|
|
if( ( $item = reset( $items ) ) === false ) { |
82
|
|
|
throw new \RuntimeException( 'No item found' ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$item->setId( null ); |
86
|
|
|
$item->setDomain( 'unittest' ); |
87
|
|
|
$resultSaved = $this->object->saveItem( $item ); |
|
|
|
|
88
|
|
|
$itemSaved = $this->object->getItem( $item->getId() ); |
89
|
|
|
|
90
|
|
|
$itemExp = clone $itemSaved; |
91
|
|
|
$itemExp->setDomain( 'unittest2' ); |
92
|
|
|
$resultUpd = $this->object->saveItem( $itemExp ); |
|
|
|
|
93
|
|
|
$itemUpd = $this->object->getItem( $itemExp->getId() ); |
94
|
|
|
|
95
|
|
|
$this->object->deleteItem( $itemSaved->getId() ); |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
$this->assertTrue( $item->getId() !== null ); |
99
|
|
|
$this->assertTrue( $itemSaved->getType() !== null ); |
100
|
|
|
$this->assertEquals( $item->getId(), $itemSaved->getId() ); |
101
|
|
|
$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() ); |
102
|
|
|
$this->assertEquals( $item->getParentId(), $itemSaved->getParentId() ); |
103
|
|
|
$this->assertEquals( $item->getTypeId(), $itemSaved->getTypeId() ); |
104
|
|
|
$this->assertEquals( $item->getRefId(), $itemSaved->getRefId() ); |
105
|
|
|
$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() ); |
106
|
|
|
$this->assertEquals( $item->getDateStart(), $itemSaved->getDateStart() ); |
107
|
|
|
$this->assertEquals( $item->getDateEnd(), $itemSaved->getDateEnd() ); |
108
|
|
|
$this->assertEquals( $item->getPosition(), $itemSaved->getPosition() ); |
109
|
|
|
$this->assertEquals( $this->editor, $itemSaved->getEditor() ); |
110
|
|
|
$this->assertStringStartsWith(date('Y-m-d', time()), $itemSaved->getTimeCreated()); |
111
|
|
|
$this->assertStringStartsWith(date('Y-m-d', time()), $itemSaved->getTimeModified()); |
112
|
|
|
|
113
|
|
|
$this->assertEquals( $this->editor, $itemSaved->getEditor() ); |
114
|
|
|
$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() ); |
115
|
|
|
$this->assertRegExp('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() ); |
116
|
|
|
|
117
|
|
|
$this->assertTrue( $itemUpd->getType() !== null ); |
118
|
|
|
$this->assertEquals( $itemExp->getId(), $itemUpd->getId() ); |
119
|
|
|
$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() ); |
120
|
|
|
$this->assertEquals( $itemExp->getParentId(), $itemUpd->getParentId() ); |
121
|
|
|
$this->assertEquals( $itemExp->getTypeId(), $itemUpd->getTypeId() ); |
122
|
|
|
$this->assertEquals( $itemExp->getRefId(), $itemUpd->getRefId() ); |
123
|
|
|
$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() ); |
124
|
|
|
$this->assertEquals( $itemExp->getDateStart(), $itemUpd->getDateStart() ); |
125
|
|
|
$this->assertEquals( $itemExp->getDateEnd(), $itemUpd->getDateEnd() ); |
126
|
|
|
$this->assertEquals( $itemExp->getPosition(), $itemUpd->getPosition() ); |
127
|
|
|
|
128
|
|
|
$this->assertEquals( $this->editor, $itemUpd->getEditor() ); |
129
|
|
|
$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() ); |
130
|
|
|
$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() ); |
131
|
|
|
|
132
|
|
|
$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultSaved ); |
133
|
|
|
$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultUpd ); |
134
|
|
|
|
135
|
|
|
$this->setExpectedException('\\Aimeos\\MShop\\Exception'); |
136
|
|
|
$this->object->getItem( $itemSaved->getId() ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
public function testMoveItemLastToFront() |
141
|
|
|
{ |
142
|
|
|
$listItems = $this->getListItems(); |
143
|
|
|
$this->assertGreaterThan( 1, count( $listItems ) ); |
144
|
|
|
|
145
|
|
|
if( ( $first = reset( $listItems ) ) === false ) { |
146
|
|
|
throw new \RuntimeException( 'No first customer list item' ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if( ( $last = end( $listItems ) ) === false ) { |
150
|
|
|
throw new \RuntimeException( 'No last customer list item' ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->object->moveItem( $last->getId(), $first->getId() ); |
|
|
|
|
154
|
|
|
|
155
|
|
|
$newFirst = $this->object->getItem( $last->getId() ); |
156
|
|
|
$newSecond = $this->object->getItem( $first->getId() ); |
157
|
|
|
|
158
|
|
|
$this->object->moveItem( $last->getId() ); |
|
|
|
|
159
|
|
|
|
160
|
|
|
$this->assertEquals( 1, $newFirst->getPosition() ); |
161
|
|
|
$this->assertEquals( 2, $newSecond->getPosition() ); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
View Code Duplication |
public function testMoveItemFirstToLast() |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$listItems = $this->getListItems(); |
168
|
|
|
$this->assertGreaterThan( 1, count( $listItems ) ); |
169
|
|
|
|
170
|
|
|
if( ( $first = reset( $listItems ) ) === false ) { |
171
|
|
|
throw new \RuntimeException( 'No first customer list item' ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if( ( $second = next( $listItems ) ) === false ) { |
175
|
|
|
throw new \RuntimeException( 'No second customer list item' ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if( ( $last = end( $listItems ) ) === false ) { |
179
|
|
|
throw new \RuntimeException( 'No last customer list item' ); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$this->object->moveItem( $first->getId() ); |
|
|
|
|
183
|
|
|
|
184
|
|
|
$newBefore = $this->object->getItem( $last->getId() ); |
185
|
|
|
$newLast = $this->object->getItem( $first->getId() ); |
186
|
|
|
|
187
|
|
|
$this->object->moveItem( $first->getId(), $second->getId() ); |
|
|
|
|
188
|
|
|
|
189
|
|
|
$this->assertEquals( $last->getPosition() - 1, $newBefore->getPosition() ); |
190
|
|
|
$this->assertEquals( $last->getPosition(), $newLast->getPosition() ); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
View Code Duplication |
public function testMoveItemFirstUp() |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
$listItems = $this->getListItems(); |
197
|
|
|
$this->assertGreaterThan( 1, count( $listItems ) ); |
198
|
|
|
|
199
|
|
|
if( ( $first = reset( $listItems ) ) === false ) { |
200
|
|
|
throw new \RuntimeException( 'No first customer list item' ); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if( ( $second = next( $listItems ) ) === false ) { |
204
|
|
|
throw new \RuntimeException( 'No second customer list item' ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if( ( $last = end( $listItems ) ) === false ) { |
208
|
|
|
throw new \RuntimeException( 'No last customer list item' ); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$this->object->moveItem( $first->getId(), $last->getId() ); |
|
|
|
|
212
|
|
|
|
213
|
|
|
$newLast = $this->object->getItem( $last->getId() ); |
214
|
|
|
$newUp = $this->object->getItem( $first->getId() ); |
215
|
|
|
|
216
|
|
|
$this->object->moveItem( $first->getId(), $second->getId() ); |
|
|
|
|
217
|
|
|
|
218
|
|
|
$this->assertEquals( $last->getPosition() - 1, $newUp->getPosition() ); |
219
|
|
|
$this->assertEquals( $last->getPosition(), $newLast->getPosition() ); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
public function testSearchItems() |
224
|
|
|
{ |
225
|
|
|
$total = 0; |
226
|
|
|
$search = $this->object->createSearch(); |
227
|
|
|
|
228
|
|
|
$expr = []; |
229
|
|
|
$expr[] = $search->compare( '!=', 'customer.lists.id', null ); |
230
|
|
|
$expr[] = $search->compare( '!=', 'customer.lists.siteid', null ); |
231
|
|
|
$expr[] = $search->compare( '>', 'customer.lists.parentid', 0 ); |
232
|
|
|
$expr[] = $search->compare( '==', 'customer.lists.domain', 'text' ); |
233
|
|
|
$expr[] = $search->compare( '>', 'customer.lists.typeid', 0 ); |
234
|
|
|
$expr[] = $search->compare( '>', 'customer.lists.refid', 0 ); |
235
|
|
|
$expr[] = $search->compare( '==', 'customer.lists.datestart', '2010-01-01 00:00:00' ); |
236
|
|
|
$expr[] = $search->compare( '==', 'customer.lists.dateend', '2100-01-01 00:00:00' ); |
237
|
|
|
$expr[] = $search->compare( '!=', 'customer.lists.config', null ); |
238
|
|
|
$expr[] = $search->compare( '>', 'customer.lists.position', 1 ); |
239
|
|
|
$expr[] = $search->compare( '==', 'customer.lists.status', 1 ); |
240
|
|
|
$expr[] = $search->compare( '>=', 'customer.lists.mtime', '1970-01-01 00:00:00' ); |
241
|
|
|
$expr[] = $search->compare( '>=', 'customer.lists.ctime', '1970-01-01 00:00:00' ); |
242
|
|
|
$expr[] = $search->compare( '==', 'customer.lists.editor', $this->editor ); |
243
|
|
|
|
244
|
|
|
$expr[] = $search->compare( '!=', 'customer.lists.type.id', 0 ); |
245
|
|
|
$expr[] = $search->compare( '!=', 'customer.lists.type.siteid', null ); |
246
|
|
|
$expr[] = $search->compare( '==', 'customer.lists.type.code', 'default' ); |
247
|
|
|
$expr[] = $search->compare( '==', 'customer.lists.type.domain', 'text' ); |
248
|
|
|
$expr[] = $search->compare( '==', 'customer.lists.type.label', 'Standard' ); |
249
|
|
|
$expr[] = $search->compare( '==', 'customer.lists.type.status', 1 ); |
250
|
|
|
$expr[] = $search->compare( '>=', 'customer.lists.type.mtime', '1970-01-01 00:00:00' ); |
251
|
|
|
$expr[] = $search->compare( '>=', 'customer.lists.type.ctime', '1970-01-01 00:00:00' ); |
252
|
|
|
$expr[] = $search->compare( '==', 'customer.lists.type.editor', $this->editor ); |
253
|
|
|
|
254
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
255
|
|
|
$search->setSlice(0, 1); |
256
|
|
|
$results = $this->object->searchItems( $search, [], $total ); |
257
|
|
|
$this->assertEquals( 1, count( $results ) ); |
258
|
|
|
$this->assertEquals( 2, $total ); |
259
|
|
|
|
260
|
|
|
foreach($results as $itemId => $item) { |
261
|
|
|
$this->assertEquals( $itemId, $item->getId() ); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
|
266
|
|
|
public function testSearchItemsNoCriteria() |
267
|
|
|
{ |
268
|
|
|
$search = $this->object->createSearch(); |
269
|
|
|
$search->setConditions( $search->compare( '==', 'customer.lists.editor', $this->editor ) ); |
270
|
|
|
$this->assertEquals( 4, count( $this->object->searchItems($search) ) ); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
|
274
|
|
View Code Duplication |
public function testSearchItemsBaseCriteria() |
|
|
|
|
275
|
|
|
{ |
276
|
|
|
$search = $this->object->createSearch(true); |
277
|
|
|
$conditions = array( |
278
|
|
|
$search->compare( '==', 'customer.lists.editor', $this->editor ), |
279
|
|
|
$search->getConditions() |
280
|
|
|
); |
281
|
|
|
$search->setConditions( $search->combine( '&&', $conditions ) ); |
282
|
|
|
$this->assertEquals( 4, count( $this->object->searchItems($search) ) ); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
|
286
|
|
View Code Duplication |
public function testGetSubManager() |
|
|
|
|
287
|
|
|
{ |
288
|
|
|
$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type') ); |
289
|
|
|
$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type', 'Standard') ); |
290
|
|
|
|
291
|
|
|
$this->setExpectedException( '\\Aimeos\\MShop\\Exception' ); |
292
|
|
|
$this->object->getSubManager( 'unknown' ); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
|
296
|
|
|
protected function getListItems() |
297
|
|
|
{ |
298
|
|
|
$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $this->context, 'Laravel' ); |
299
|
|
|
|
300
|
|
|
$search = $manager->createSearch(); |
301
|
|
|
$search->setConditions( $search->compare( '==', 'customer.code', 'UTC003' ) ); |
302
|
|
|
$search->setSlice( 0, 1 ); |
303
|
|
|
|
304
|
|
|
$results = $manager->searchItems( $search ); |
305
|
|
|
|
306
|
|
|
if( ( $item = reset( $results ) ) === false ) { |
307
|
|
|
throw new \RuntimeException( 'No customer item found' ); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$search = $this->object->createSearch(); |
311
|
|
|
$expr = array( |
312
|
|
|
$search->compare( '==', 'customer.lists.parentid', $item->getId() ), |
313
|
|
|
$search->compare( '==', 'customer.lists.domain', 'text' ), |
314
|
|
|
$search->compare( '==', 'customer.lists.editor', $this->editor ), |
315
|
|
|
$search->compare( '==', 'customer.lists.type.code', 'default' ), |
316
|
|
|
); |
317
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
318
|
|
|
$search->setSortations( array( $search->sort( '+', 'customer.lists.position' ) ) ); |
319
|
|
|
|
320
|
|
|
return $this->object->searchItems( $search ); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: