Passed
Push — master ( eade66...a4d1b5 )
by Aimeos
03:55
created

FosUserTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 14
eloc 108
c 6
b 2
f 0
dl 0
loc 189
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testClear() 0 3 1
A testSearchItemsAll() 0 7 1
A testSearchItemsBase() 0 10 1
A tearDown() 0 3 1
A testCreateItem() 0 3 1
A testGetItem() 0 9 2
A testSaveUpdateDeleteItem() 0 59 2
A setUp() 0 6 1
A testGetSubManager() 0 7 1
A testSearchItems() 0 30 2
A testAggregate() 0 14 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2020
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' )->toArray();
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()->setSlice( 0, 1 );
66
67
		if( ( $item = $this->object->searchItems( $search )->first() ) === null ) {
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
80
		if( ( $item = $this->object->searchItems( $search )->first() ) === null ) {
81
			throw new \RuntimeException( 'No item found' );
82
		}
83
84
		$item->setId( null );
85
		$item->setDomain( 'unittest' );
86
		$resultSaved = $this->object->saveItem( $item );
87
		$itemSaved = $this->object->getItem( $item->getId() );
88
89
		$itemExp = clone $itemSaved;
90
		$itemExp->setDomain( 'unittest2' );
91
		$resultUpd = $this->object->saveItem( $itemExp );
92
		$itemUpd = $this->object->getItem( $itemExp->getId() );
93
94
		$this->object->deleteItem( $itemSaved->getId() );
95
96
97
		$this->assertTrue( $item->getId() !== null );
98
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
99
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
100
		$this->assertEquals( $item->getParentId(), $itemSaved->getParentId() );
101
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
102
		$this->assertEquals( $item->getRefId(), $itemSaved->getRefId() );
103
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
104
		$this->assertEquals( $item->getDateStart(), $itemSaved->getDateStart() );
105
		$this->assertEquals( $item->getDateEnd(), $itemSaved->getDateEnd() );
106
		$this->assertEquals( $item->getPosition(), $itemSaved->getPosition() );
107
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
108
		$this->assertStringStartsWith( date( 'Y-m-d', time() ), $itemSaved->getTimeCreated() );
109
		$this->assertStringStartsWith( date( 'Y-m-d', time() ), $itemSaved->getTimeModified() );
110
111
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
112
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
113
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
114
115
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
116
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
117
		$this->assertEquals( $itemExp->getParentId(), $itemUpd->getParentId() );
118
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
119
		$this->assertEquals( $itemExp->getRefId(), $itemUpd->getRefId() );
120
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
121
		$this->assertEquals( $itemExp->getDateStart(), $itemUpd->getDateStart() );
122
		$this->assertEquals( $itemExp->getDateEnd(), $itemUpd->getDateEnd() );
123
		$this->assertEquals( $itemExp->getPosition(), $itemUpd->getPosition() );
124
125
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
126
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
127
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
128
129
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
130
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
131
132
		$this->expectException( '\\Aimeos\\MShop\\Exception' );
133
		$this->object->getItem( $itemSaved->getId() );
134
	}
135
136
137
	public function testSearchItems()
138
	{
139
		$total = 0;
140
		$search = $this->object->createSearch();
141
142
		$expr = [];
143
		$expr[] = $search->compare( '!=', 'customer.lists.id', null );
144
		$expr[] = $search->compare( '!=', 'customer.lists.siteid', null );
145
		$expr[] = $search->compare( '!=', 'customer.lists.parentid', null );
146
		$expr[] = $search->compare( '!=', 'customer.lists.key', null );
147
		$expr[] = $search->compare( '==', 'customer.lists.domain', 'text' );
148
		$expr[] = $search->compare( '==', 'customer.lists.type', 'default' );
149
		$expr[] = $search->compare( '>', 'customer.lists.refid', 0 );
150
		$expr[] = $search->compare( '==', 'customer.lists.datestart', '2010-01-01 00:00:00' );
151
		$expr[] = $search->compare( '==', 'customer.lists.dateend', '2098-01-01 00:00:00' );
152
		$expr[] = $search->compare( '!=', 'customer.lists.config', null );
153
		$expr[] = $search->compare( '>', 'customer.lists.position', 0 );
154
		$expr[] = $search->compare( '==', 'customer.lists.status', 1 );
155
		$expr[] = $search->compare( '>=', 'customer.lists.mtime', '1970-01-01 00:00:00' );
156
		$expr[] = $search->compare( '>=', 'customer.lists.ctime', '1970-01-01 00:00:00' );
157
		$expr[] = $search->compare( '==', 'customer.lists.editor', $this->editor );
158
159
		$search->setConditions( $search->combine( '&&', $expr ) );
160
		$search->setSlice( 0, 2 );
161
		$results = $this->object->searchItems( $search, [], $total );
162
		$this->assertEquals( 2, count( $results ) );
163
		$this->assertEquals( 3, $total );
164
165
		foreach( $results as $itemId => $item ) {
166
			$this->assertEquals( $itemId, $item->getId() );
167
		}
168
	}
169
170
171
	public function testSearchItemsAll()
172
	{
173
		//search without base criteria
174
		$search = $this->object->createSearch();
175
		$search->setConditions( $search->compare( '==', 'customer.lists.editor', $this->editor ) );
176
		$result = $this->object->searchItems( $search );
177
		$this->assertEquals( 5, count( $result ) );
178
	}
179
180
181
	public function testSearchItemsBase()
182
	{
183
		//search with base criteria
184
		$search = $this->object->createSearch( true );
185
		$conditions = array(
186
			$search->compare( '==', 'customer.lists.editor', $this->editor ),
187
			$search->getConditions()
188
		);
189
		$search->setConditions( $search->combine( '&&', $conditions ) );
190
		$this->assertEquals( 5, count( $this->object->searchItems( $search ) ) );
191
	}
192
193
194
	public function testGetSubManager()
195
	{
196
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type' ) );
197
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager( 'type', 'Standard' ) );
198
199
		$this->expectException( '\\Aimeos\\MShop\\Exception' );
200
		$this->object->getSubManager( 'unknown' );
201
	}
202
}
203