Completed
Push — master ( 74531e...29a45b )
by Aimeos
02:46
created

Typo3Test::testGetSubManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2014
7
 */
8
9
10
namespace Aimeos\MShop\Customer\Manager\Lists;
11
12
13
/**
14
 * Test class for \Aimeos\MShop\Customer\Manager\Lists\Typo3Test.
15
 */
16
class Typo3Test extends \PHPUnit_Framework_TestCase
17
{
18
	private $object;
19
	private $context;
20
	private $editor = '';
21
22
23
	/**
24
	 * Sets up the fixture.
25
	 * This method is called before a test is executed.
26
	 *
27
	 * @access protected
28
	 */
29
	protected function setUp()
30
	{
31
		$this->context = \TestHelper::getContext();
32
		$this->editor = $this->context->getEditor();
33
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( \TestHelper::getContext(), 'Typo3' );
34
		$this->object = $manager->getSubManager( 'lists', 'Typo3' );
35
	}
36
37
38
	/**
39
	 * Tears down the fixture, for example, closes a network connection.
40
	 * This method is called after a test is executed.
41
	 *
42
	 * @access protected
43
	 */
44
	protected function tearDown()
45
	{
46
		unset( $this->object );
47
		\Aimeos\MShop\Factory::clear();
48
	}
49
50
51
	public function testAggregate()
52
	{
53
		$search = $this->object->createSearch( true );
54
		$result = $this->object->aggregate( $search, 'customer.lists.domain' );
55
56
		$this->assertEquals( 1, count( $result ) );
57
		$this->assertArrayHasKey( 'text', $result );
58
		$this->assertEquals( 4, $result['text'] );
59
	}
60
61
62
	public function testCreateItem()
63
	{
64
		$item = $this->object->createItem();
65
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $item );
66
	}
67
68
69
	public function testGetItem()
70
	{
71
		$search = $this->object->createSearch();
72
		$search->setSlice(0, 1);
73
		$results = $this->object->searchItems( $search );
74
75
		if( ( $item = reset( $results ) ) === false ) {
76
			throw new \Exception( 'No item found' );
77
		}
78
79
		$this->assertEquals( $item, $this->object->getItem( $item->getId() ) );
80
	}
81
82
83
	public function testSaveUpdateDeleteItem()
84
	{
85
		$search = $this->object->createSearch();
86
		$search->setSlice(0, 1);
87
		$items = $this->object->searchItems( $search );
88
89
		if( ( $item = reset( $items ) ) === false ) {
90
			throw new \Exception( 'No item found' );
91
		}
92
93
		$item->setId(null);
94
		$item->setDomain( 'unittest' );
95
		$this->object->saveItem( $item );
96
		$itemSaved = $this->object->getItem( $item->getId() );
97
98
		$itemExp = clone $itemSaved;
99
		$itemExp->setDomain( 'unittest2' );
100
		$this->object->saveItem( $itemExp );
101
		$itemUpd = $this->object->getItem( $itemExp->getId() );
102
103
		$this->object->deleteItem( $itemSaved->getId() );
104
105
106
		$this->assertTrue( $item->getId() !== null );
107
		$this->assertTrue( $itemSaved->getType() !== null );
108
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
109
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
110
		$this->assertEquals( $item->getParentId(), $itemSaved->getParentId() );
111
		$this->assertEquals( $item->getTypeId(), $itemSaved->getTypeId() );
112
		$this->assertEquals( $item->getRefId(), $itemSaved->getRefId() );
113
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
114
		$this->assertEquals( $item->getDateStart(), $itemSaved->getDateStart() );
115
		$this->assertEquals( $item->getDateEnd(), $itemSaved->getDateEnd() );
116
		$this->assertEquals( $item->getPosition(), $itemSaved->getPosition() );
117
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
118
		$this->assertStringStartsWith(date('Y-m-d', time()), $itemSaved->getTimeCreated());
119
		$this->assertStringStartsWith(date('Y-m-d', time()), $itemSaved->getTimeModified());
120
121
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
122
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
123
		$this->assertRegExp('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
124
125
		$this->assertTrue( $itemUpd->getType() !== null );
126
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
127
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
128
		$this->assertEquals( $itemExp->getParentId(), $itemUpd->getParentId() );
129
		$this->assertEquals( $itemExp->getTypeId(), $itemUpd->getTypeId() );
130
		$this->assertEquals( $itemExp->getRefId(), $itemUpd->getRefId() );
131
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
132
		$this->assertEquals( $itemExp->getDateStart(), $itemUpd->getDateStart() );
133
		$this->assertEquals( $itemExp->getDateEnd(), $itemUpd->getDateEnd() );
134
		$this->assertEquals( $itemExp->getPosition(), $itemUpd->getPosition() );
135
136
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
137
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
138
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
139
140
		$this->setExpectedException('\\Aimeos\\MShop\\Exception');
141
		$this->object->getItem( $itemSaved->getId() );
142
	}
143
144
145
	public function testSaveItemException()
146
	{
147
		$this->setExpectedException( '\Aimeos\MShop\Exception' );
148
		$this->object->saveItem( new \Aimeos\MShop\Common\Item\Type\Standard( 'common.lists.type.' ) );
149
	}
150
151
152
	public function testSaveDomainException()
153
	{
154
		$item = new \Aimeos\MShop\Common\Item\Lists\Standard( 'common.lists.' );
155
		$item->setDomain( 'customer/group' );
156
157
		$this->setExpectedException( '\Aimeos\MShop\Customer\Exception' );
158
		$this->object->saveItem( $item );
159
	}
160
161
162
	public function testMoveItemLastToFront()
163
	{
164
		$listItems = $this->getListItems();
165
		$this->assertGreaterThan( 1, count( $listItems ) );
166
167
		if( ( $first = reset( $listItems ) ) === false ) {
168
			throw new \Exception( 'No first customer list item' );
169
		}
170
171
		if( ( $last = end( $listItems ) ) === false ) {
172
			throw new \Exception( 'No last customer list item' );
173
		}
174
175
		$this->object->moveItem( $last->getId(), $first->getId() );
176
177
		$newFirst = $this->object->getItem( $last->getId() );
178
		$newSecond = $this->object->getItem( $first->getId() );
179
180
		$this->object->moveItem( $last->getId() );
181
182
		$this->assertEquals( 0, $newFirst->getPosition() );
183
		$this->assertEquals( 1, $newSecond->getPosition() );
184
	}
185
186
187
	public function testMoveItemFirstToLast()
188
	{
189
		$listItems = $this->getListItems();
190
		$this->assertGreaterThan( 1, count( $listItems ) );
191
192
		if( ( $first = reset( $listItems ) ) === false ) {
193
			throw new \Exception( 'No first customer list item' );
194
		}
195
196
		if( ( $second = next( $listItems ) ) === false ) {
197
			throw new \Exception( 'No second customer list item' );
198
		}
199
200
		if( ( $last = end( $listItems ) ) === false ) {
201
			throw new \Exception( 'No last customer list item' );
202
		}
203
204
		$this->object->moveItem( $first->getId() );
205
206
		$newBefore = $this->object->getItem( $last->getId() );
207
		$newLast = $this->object->getItem( $first->getId() );
208
209
		$this->object->moveItem( $first->getId(), $second->getId() );
210
211
		$this->assertEquals( $last->getPosition() - 1, $newBefore->getPosition() );
212
		$this->assertEquals( $last->getPosition(), $newLast->getPosition() );
213
	}
214
215
216
	public function testMoveItemFirstUp()
217
	{
218
		$listItems = $this->getListItems();
219
		$this->assertGreaterThan( 1, count( $listItems ) );
220
221
		if( ( $first = reset( $listItems ) ) === false ) {
222
			throw new \Exception( 'No first customer list item' );
223
		}
224
225
		if( ( $second = next( $listItems ) ) === false ) {
226
			throw new \Exception( 'No second customer list item' );
227
		}
228
229
		if( ( $last = end( $listItems ) ) === false ) {
230
			throw new \Exception( 'No last customer list item' );
231
		}
232
233
		$this->object->moveItem( $first->getId(), $last->getId() );
234
235
		$newLast = $this->object->getItem( $last->getId() );
236
		$newUp = $this->object->getItem( $first->getId() );
237
238
		$this->object->moveItem( $first->getId(), $second->getId() );
239
240
		$this->assertEquals( $last->getPosition() - 1, $newUp->getPosition() );
241
		$this->assertEquals( $last->getPosition(), $newLast->getPosition() );
242
	}
243
244
245
	public function testSearchItems()
246
	{
247
		$total = 0;
248
		$search = $this->object->createSearch();
249
250
		$expr = array();
251
		$expr[] = $search->compare( '!=', 'customer.lists.id', null );
252
		$expr[] = $search->compare( '!=', 'customer.lists.siteid', null );
253
		$expr[] = $search->compare( '>', 'customer.lists.parentid', 0 );
254
		$expr[] = $search->compare( '==', 'customer.lists.domain', 'text' );
255
		$expr[] = $search->compare( '>', 'customer.lists.typeid', 0 );
256
		$expr[] = $search->compare( '>', 'customer.lists.refid', 0 );
257
		$expr[] = $search->compare( '==', 'customer.lists.datestart', '2010-01-01 00:00:00' );
258
		$expr[] = $search->compare( '==', 'customer.lists.dateend', '2022-01-01 00:00:00' );
259
		$expr[] = $search->compare( '>', 'customer.lists.position', 0 );
260
		$expr[] = $search->compare( '==', 'customer.lists.status', 1 );
261
		$expr[] = $search->compare( '>=', 'customer.lists.mtime', '1970-01-01 00:00:00' );
262
		$expr[] = $search->compare( '>=', 'customer.lists.ctime', '1970-01-01 00:00:00' );
263
		$expr[] = $search->compare( '==', 'customer.lists.editor', $this->editor );
264
265
		$expr[] = $search->compare( '!=', 'customer.lists.type.id', 0 );
266
		$expr[] = $search->compare( '!=', 'customer.lists.type.siteid', null );
267
		$expr[] = $search->compare( '==', 'customer.lists.type.code', 'default' );
268
		$expr[] = $search->compare( '==', 'customer.lists.type.domain', 'text' );
269
		$expr[] = $search->compare( '==', 'customer.lists.type.label', 'Standard' );
270
		$expr[] = $search->compare( '==', 'customer.lists.type.status', 1 );
271
		$expr[] = $search->compare( '>=', 'customer.lists.type.mtime', '1970-01-01 00:00:00' );
272
		$expr[] = $search->compare( '>=', 'customer.lists.type.ctime', '1970-01-01 00:00:00' );
273
		$expr[] = $search->compare( '==', 'customer.lists.type.editor', $this->editor );
274
275
		$search->setConditions( $search->combine( '&&', $expr ) );
276
		$search->setSlice(0, 2);
277
		$results = $this->object->searchItems( $search, array(), $total );
278
		$this->assertEquals( 2, count( $results ) );
279
		$this->assertEquals( 3, $total );
280
281
		foreach($results as $itemId => $item) {
282
			$this->assertEquals( $itemId, $item->getId() );
283
		}
284
285
		//search without base criteria
286
		$search = $this->object->createSearch();
287
		$search->setConditions( $search->compare( '==', 'customer.lists.editor', $this->editor ) );
288
		$this->assertEquals( 4, count( $this->object->searchItems($search) ) );
289
290
		//search with base criteria
291
		$search = $this->object->createSearch(true);
292
		$conditions = array(
293
			$search->compare( '==', 'customer.lists.editor', $this->editor ),
294
			$search->getConditions()
295
		);
296
		$search->setConditions( $search->combine( '&&', $conditions ) );
297
		$this->assertEquals( 4, count( $this->object->searchItems($search) ) );
298
	}
299
300
301
	public function testGetSubManager()
302
	{
303
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type') );
304
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type', 'Typo3') );
305
306
		$this->setExpectedException('\\Aimeos\\MShop\\Exception');
307
		$this->object->getSubManager('unknown');
308
	}
309
310
311
	protected function getListItems()
312
	{
313
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $this->context, 'Typo3' );
314
315
		$search = $manager->createSearch();
316
		$search->setConditions( $search->compare( '==', 'customer.code', '[email protected]' ) );
317
		$search->setSlice( 0, 1 );
318
319
		$results = $manager->searchItems( $search );
320
321
		if( ( $item = reset( $results ) ) === false ) {
322
			throw new \Exception( 'No customer item found' );
323
		}
324
325
		$search = $this->object->createSearch();
326
		$expr = array(
327
			$search->compare( '==', 'customer.lists.parentid', $item->getId() ),
328
			$search->compare( '==', 'customer.lists.domain', 'text' ),
329
			$search->compare( '==', 'customer.lists.editor', $this->editor ),
330
			$search->compare( '==', 'customer.lists.type.code', 'default' ),
331
		);
332
		$search->setConditions( $search->combine( '&&', $expr ) );
333
		$search->setSortations( array( $search->sort( '+', 'customer.lists.position' ) ) );
334
335
		return $this->object->searchItems( $search );
336
	}
337
}
338