Completed
Push — master ( 1b60f8...d5b5b7 )
by Aimeos
08:48
created

StandardTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 198
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 8

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A tearDown() 0 4 1
A testAggregate() 0 12 1
A testCleanup() 0 4 1
A testGetResourceType() 0 6 1
A testCreateItem() 0 4 1
A testGetItem() 0 17 2
A testSaveInvalid() 0 5 1
A testSaveUpdateDeleteItem() 0 57 2
A testCreateSearch() 0 4 1
B testSearchItems() 0 43 2
A testGetSubManager() 0 5 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 */
7
8
9
namespace Aimeos\MShop\Subscription\Manager;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $object;
16
	private $editor = '';
17
18
19
	protected function setUp()
20
	{
21
		$this->editor = \TestHelperMShop::getContext()->getEditor();
22
		$this->context = \TestHelperMShop::getContext();
23
24
		$this->object = new \Aimeos\MShop\Subscription\Manager\Standard( $this->context );
25
	}
26
27
28
	protected function tearDown()
29
	{
30
		unset( $this->object );
31
	}
32
33
34
	public function testAggregate()
35
	{
36
		$search = $this->object->createSearch();
37
		$search->setConditions( $search->compare( '==', 'subscription.editor', 'core:unittest' ) );
38
		$result = $this->object->aggregate( $search, 'subscription.status' );
39
40
		$this->assertEquals( 2, count( $result ) );
41
		$this->assertArrayHasKey( '0', $result );
42
		$this->assertArrayHasKey( '1', $result );
43
		$this->assertEquals( 1, $result['0'] );
44
		$this->assertEquals( 1, $result['1'] );
45
	}
46
47
48
	public function testCleanup()
49
	{
50
		$this->object->cleanup( array( -1 ) );
51
	}
52
53
54
	public function testGetResourceType()
55
	{
56
		$result = $this->object->getResourceType();
57
58
		$this->assertContains( 'subscription', $result );
59
	}
60
61
62
	public function testCreateItem()
63
	{
64
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Subscription\\Item\\Iface', $this->object->createItem() );
65
	}
66
67
68
	public function testGetItem()
69
	{
70
		$search = $this->object->createSearch();
71
		$conditions = array(
72
			$search->compare( '==', 'subscription.status', 1 ),
73
			$search->compare( '==', 'subscription.editor', $this->editor )
74
		);
75
		$search->setConditions( $search->combine( '&&', $conditions ) );
76
		$results = $this->object->searchItems( $search );
77
78
		if( ( $expected = reset( $results ) ) === false ) {
79
			throw new \RuntimeException( sprintf( 'No subscription found in mshop_subscription with status "%1$s"', 1 ) );
80
		}
81
82
		$actual = $this->object->getItem( $expected->getId() );
83
		$this->assertEquals( $expected, $actual );
84
	}
85
86
87
	public function testSaveInvalid()
88
	{
89
		$this->setExpectedException( '\Aimeos\MShop\Subscription\Exception' );
90
		$this->object->saveItem( new \Aimeos\MShop\Locale\Item\Standard() );
91
	}
92
93
94
	public function testSaveUpdateDeleteItem()
95
	{
96
		$search = $this->object->createSearch();
97
		$conditions = array(
98
			$search->compare( '==', 'subscription.status', 1 ),
99
			$search->compare( '==', 'subscription.editor', $this->editor )
100
		);
101
		$search->setConditions( $search->combine( '&&', $conditions ) );
102
		$results = $this->object->searchItems( $search );
103
104
		if( ( $item = reset( $results ) ) === false ) {
105
			throw new \RuntimeException( 'No subscription item found.' );
106
		}
107
108
		$item->setId( null );
109
		$resultSaved = $this->object->saveItem( $item );
110
		$itemSaved = $this->object->getItem( $item->getId() );
111
112
		$itemExp = clone $itemSaved;
113
		$itemExp->setInterval( 'P1W' );
114
		$resultUpd = $this->object->saveItem( $itemExp );
115
		$itemUpd = $this->object->getItem( $itemExp->getId() );
116
117
		$this->object->deleteItem( $itemSaved->getId() );
118
119
120
		$this->assertTrue( $item->getId() !== null );
121
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
122
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
123
		$this->assertEquals( $item->getOrderProductId(), $itemSaved->getOrderProductId() );
124
		$this->assertEquals( $item->getDateNext(), $itemSaved->getDateNext() );
125
		$this->assertEquals( $item->getDateEnd(), $itemSaved->getDateEnd() );
126
		$this->assertEquals( $item->getInterval(), $itemSaved->getInterval() );
127
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
128
129
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
130
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
131
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
132
133
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
134
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
135
		$this->assertEquals( $itemExp->getOrderProductId(), $itemUpd->getOrderProductId() );
136
		$this->assertEquals( $itemExp->getDateNext(), $itemUpd->getDateNext() );
137
		$this->assertEquals( $itemExp->getDateEnd(), $itemUpd->getDateEnd() );
138
		$this->assertEquals( $itemExp->getInterval(), $itemUpd->getInterval() );
139
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
140
141
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
142
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
143
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
144
145
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultSaved );
146
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Iface', $resultUpd );
147
148
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
149
		$this->object->getItem( $itemSaved->getId() );
150
	}
151
152
153
	public function testCreateSearch()
154
	{
155
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $this->object->createSearch() );
156
	}
157
158
159
	public function testSearchItems()
160
	{
161
		$siteid = $this->context->getLocale()->getSiteId();
162
163
		$total = 0;
164
		$search = $this->object->createSearch();
165
166
		$expr = [];
167
		$expr[] = $search->compare( '!=', 'subscription.id', null );
168
		$expr[] = $search->compare( '==', 'subscription.siteid', $siteid );
169
		$expr[] = $search->compare( '!=', 'subscription.ordprodid', null );
170
		$expr[] = $search->compare( '==', 'subscription.datenext', '2000-01-01 00:00:00' );
171
		$expr[] = $search->compare( '==', 'subscription.dateend', '2010-01-01 00:00:00' );
172
		$expr[] = $search->compare( '==', 'subscription.interval', 'P1M' );
173
		$expr[] = $search->compare( '==', 'subscription.status', 1 );
174
		$expr[] = $search->compare( '>=', 'subscription.mtime', '1970-01-01 00:00:00' );
175
		$expr[] = $search->compare( '>=', 'subscription.ctime', '1970-01-01 00:00:00' );
176
		$expr[] = $search->compare( '==', 'subscription.editor', $this->editor );
177
178
179
		$search->setConditions( $search->combine( '&&', $expr ) );
180
		$result = $this->object->searchItems( $search, [], $total );
181
182
		$this->assertEquals( 1, count( $result ) );
183
		$this->assertEquals( 1, $total );
184
185
186
		$search = $this->object->createSearch();
187
		$conditions = array(
188
			$search->compare( '==', 'subscription.editor', $this->editor )
189
		);
190
		$search->setConditions( $search->combine( '&&', $conditions ) );
191
		$search->setSlice( 0, 1 );
192
		$total = 0;
193
		$items = $this->object->searchItems( $search, [], $total );
194
195
		$this->assertEquals( 1, count( $items ) );
196
		$this->assertEquals( 2, $total );
197
198
		foreach( $items as $itemId => $item ) {
199
			$this->assertEquals( $itemId, $item->getId() );
200
		}
201
	}
202
203
204
	public function testGetSubManager()
205
	{
206
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
207
		$this->object->getSubManager( 'unknown' );
208
	}
209
}
210