Passed
Push — master ( f7458c...78a5fa )
by Aimeos
02:33
created

FosUserTest::testClear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
6
 */
7
8
9
namespace Aimeos\MShop\Customer\Manager\Lists;
10
11
12
class FosUserTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $context;
16
	private $editor = '';
17
18
19
	protected function setUp() : void
20
	{
21
		$this->context = \TestHelper::getContext();
22
		$this->editor = $this->context->getEditor();
23
		$manager = \Aimeos\MShop\Customer\Manager\Factory::create( $this->context, 'FosUser' );
24
		$this->object = $manager->getSubManager( 'lists', 'FosUser' );
25
	}
26
27
28
	protected function tearDown() : void
29
	{
30
		unset( $this->object, $this->context );
31
	}
32
33
34
	public function testClear()
35
	{
36
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( array( -1 ) ) );
37
	}
38
39
40
	public function testAggregate()
41
	{
42
		$search = $this->object->createSearch( true );
43
		$expr = array(
44
			$search->getConditions(),
45
			$search->compare( '==', 'customer.lists.editor', 'ai-fosuser:lib/custom' ),
46
		);
47
		$search->setConditions( $search->combine( '&&', $expr ) );
48
49
		$result = $this->object->aggregate( $search, 'customer.lists.domain' );
50
51
		$this->assertEquals( 2, count( $result ) );
52
		$this->assertArrayHasKey( 'text', $result );
53
		$this->assertEquals( 4, $result['text'] );
54
	}
55
56
57
	public function testCreateItem()
58
	{
59
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $this->object->createItem() );
60
	}
61
62
63
	public function testGetItem()
64
	{
65
		$search = $this->object->createSearch();
66
		$search->setSlice( 0, 1 );
67
		$results = $this->object->searchItems( $search );
68
69
		if( ( $item = reset( $results ) ) === false ) {
70
			throw new \RuntimeException( 'No item found' );
71
		}
72
73
		$this->assertEquals( $item, $this->object->getItem( $item->getId() ) );
74
	}
75
76
77
	public function testSaveUpdateDeleteItem()
78
	{
79
		$search = $this->object->createSearch();
80
		$search->setSlice( 0, 1 );
81
		$items = $this->object->searchItems( $search );
82
83
		if( ( $item = reset( $items ) ) === false ) {
84
			throw new \RuntimeException( 'No item found' );
85
		}
86
87
		$item->setId( null );
88
		$item->setDomain( 'unittest' );
89
		$resultSaved = $this->object->saveItem( $item );
90
		$itemSaved = $this->object->getItem( $item->getId() );
91
92
		$itemExp = clone $itemSaved;
93
		$itemExp->setDomain( 'unittest2' );
94
		$resultUpd = $this->object->saveItem( $itemExp );
95
		$itemUpd = $this->object->getItem( $itemExp->getId() );
96
97
		$this->object->deleteItem( $itemSaved->getId() );
98
99
100
		$this->assertTrue( $item->getId() !== null );
101
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
102
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
103
		$this->assertEquals( $item->getParentId(), $itemSaved->getParentId() );
104
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
105
		$this->assertEquals( $item->getRefId(), $itemSaved->getRefId() );
106
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
107
		$this->assertEquals( $item->getDateStart(), $itemSaved->getDateStart() );
108
		$this->assertEquals( $item->getDateEnd(), $itemSaved->getDateEnd() );
109
		$this->assertEquals( $item->getPosition(), $itemSaved->getPosition() );
110
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
111
		$this->assertStringStartsWith( date( 'Y-m-d', time() ), $itemSaved->getTimeCreated() );
112
		$this->assertStringStartsWith( date( 'Y-m-d', time() ), $itemSaved->getTimeModified() );
113
114
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
115
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
116
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
117
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->getType(), $itemUpd->getType() );
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::class, $resultSaved );
133
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
134
135
		$this->expectException( '\\Aimeos\\MShop\\Exception' );
136
		$this->object->getItem( $itemSaved->getId() );
137
	}
138
139
140
	public function testSearchItems()
141
	{
142
		$total = 0;
143
		$search = $this->object->createSearch();
144
145
		$expr = [];
146
		$expr[] = $search->compare( '!=', 'customer.lists.id', null );
147
		$expr[] = $search->compare( '!=', 'customer.lists.siteid', null );
148
		$expr[] = $search->compare( '!=', 'customer.lists.parentid', null );
149
		$expr[] = $search->compare( '!=', 'customer.lists.key', null );
150
		$expr[] = $search->compare( '==', 'customer.lists.domain', 'text' );
151
		$expr[] = $search->compare( '==', 'customer.lists.type', 'default' );
152
		$expr[] = $search->compare( '>', 'customer.lists.refid', 0 );
153
		$expr[] = $search->compare( '==', 'customer.lists.datestart', '2010-01-01 00:00:00' );
154
		$expr[] = $search->compare( '==', 'customer.lists.dateend', '2098-01-01 00:00:00' );
155
		$expr[] = $search->compare( '!=', 'customer.lists.config', null );
156
		$expr[] = $search->compare( '>', 'customer.lists.position', 0 );
157
		$expr[] = $search->compare( '==', 'customer.lists.status', 1 );
158
		$expr[] = $search->compare( '>=', 'customer.lists.mtime', '1970-01-01 00:00:00' );
159
		$expr[] = $search->compare( '>=', 'customer.lists.ctime', '1970-01-01 00:00:00' );
160
		$expr[] = $search->compare( '==', 'customer.lists.editor', $this->editor );
161
162
		$search->setConditions( $search->combine( '&&', $expr ) );
163
		$search->setSlice( 0, 2 );
164
		$results = $this->object->searchItems( $search, [], $total );
165
		$this->assertEquals( 2, count( $results ) );
166
		$this->assertEquals( 3, $total );
167
168
		foreach( $results as $itemId => $item ) {
169
			$this->assertEquals( $itemId, $item->getId() );
170
		}
171
	}
172
173
174
	public function testSearchItemsAll()
175
	{
176
		//search without base criteria
177
		$search = $this->object->createSearch();
178
		$search->setConditions( $search->compare( '==', 'customer.lists.editor', $this->editor ) );
179
		$result = $this->object->searchItems( $search );
180
		$this->assertEquals( 5, count( $result ) );
181
	}
182
183
184
	public function testSearchItemsBase()
185
	{
186
		//search with base criteria
187
		$search = $this->object->createSearch( true );
188
		$conditions = array(
189
			$search->compare( '==', 'customer.lists.editor', $this->editor ),
190
			$search->getConditions()
191
		);
192
		$search->setConditions( $search->combine( '&&', $conditions ) );
193
		$this->assertEquals( 5, count( $this->object->searchItems( $search ) ) );
194
	}
195
196
197
	public function testGetSubManager()
198
	{
199
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type' ) );
200
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type', 'Standard' ) );
201
202
		$this->expectException( '\\Aimeos\\MShop\\Exception' );
203
		$this->object->getSubManager( 'unknown' );
204
	}
205
}
206