Completed
Push — master ( ec78c4...7e4af5 )
by Aimeos
07:46
created

StandardTest::testFindItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
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, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2016
7
 */
8
9
10
namespace Aimeos\MShop\Stock\Manager;
11
12
13
class StandardTest extends \PHPUnit_Framework_TestCase
14
{
15
	private $object;
16
	private $editor = '';
17
18
19
	protected function setUp()
20
	{
21
		$this->editor = \TestHelperMShop::getContext()->getEditor();
22
		$this->object = new \Aimeos\MShop\Stock\Manager\Standard( \TestHelperMShop::getContext() );
23
	}
24
25
26
	protected function tearDown()
27
	{
28
		unset( $this->object );
29
	}
30
31
32
	public function testCleanup()
33
	{
34
		$this->object->cleanup( array( -1 ) );
35
	}
36
37
38
	public function testCreateItem()
39
	{
40
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Stock\\Item\\Iface', $this->object->createItem() );
41
	}
42
43
44
	public function testFindItem()
45
	{
46
		$item = $this->object->findItem( 'CNC', [], 'product', 'default' );
47
48
		$this->assertEquals( 'CNC', $item->getProductCode() );
49
	}
50
51
52
	public function testSaveInvalid()
53
	{
54
		$this->setExpectedException( '\Aimeos\MShop\Stock\Exception' );
55
		$this->object->saveItem( new \Aimeos\MShop\Locale\Item\Standard() );
56
	}
57
58
59
	public function testSaveUpdateDeleteItem()
60
	{
61
		$search = $this->object->createSearch();
62
		$search->setConditions( $search->compare( '==', 'stock.editor', $this->editor ) );
63
		$search->setSlice( 0, 1 );
64
		$items = $this->object->searchItems( $search );
65
66
		if( ( $item = reset( $items ) ) === false ) {
67
			throw new \RuntimeException( 'No item found' );
68
		}
69
70
		$item->setId( null );
71
		$item->setProductCode( 'XYZ' );
72
		$this->object->saveItem( $item );
73
		$itemSaved = $this->object->getItem( $item->getId() );
74
75
		$itemExp = clone $itemSaved;
76
		$itemExp->setStockLevel( 50 );
77
		$this->object->saveItem( $itemExp );
78
		$itemUpd = $this->object->getItem( $itemExp->getId() );
79
80
		$this->object->deleteItem( $itemSaved->getId() );
81
82
83
		$this->assertTrue( $item->getId() !== null );
84
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
85
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
86
		$this->assertEquals( $item->getTypeId(), $itemSaved->getTypeId() );
87
		$this->assertEquals( $item->getProductCode(), $itemSaved->getProductCode() );
88
		$this->assertEquals( $item->getStockLevel(), $itemSaved->getStockLevel() );
89
		$this->assertEquals( $item->getDateBack(), $itemSaved->getDateBack() );
90
91
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
92
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
93
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
94
95
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
96
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
97
		$this->assertEquals( $itemExp->getTypeId(), $itemUpd->getTypeId() );
98
		$this->assertEquals( $itemExp->getProductCode(), $itemUpd->getProductCode() );
99
		$this->assertEquals( $itemExp->getStockLevel(), $itemUpd->getStockLevel() );
100
		$this->assertEquals( $itemExp->getDateBack(), $itemUpd->getDateBack() );
101
102
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
103
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
104
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
105
106
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
107
		$this->object->getItem( $itemSaved->getId() );
108
	}
109
110
111
	public function testGetItem()
112
	{
113
		$search = $this->object->createSearch();
114
		$conditions = array(
115
			$search->compare( '==', 'stock.stocklevel', 2000 ),
116
			$search->compare( '==', 'stock.editor', $this->editor )
117
		);
118
		$search->setConditions( $search->combine( '&&', $conditions ) );
119
		$result = $this->object->searchItems( $search );
120
121
		if( ( $expected = reset( $result ) ) === false ) {
122
			throw new \RuntimeException( sprintf( 'No stock item found for level "%1$s".', 2000 ) );
123
		}
124
125
		$actual = $this->object->getItem( $expected->getId() );
126
		$this->assertEquals( $expected, $actual );
127
	}
128
129
130
	public function testGetResourceType()
131
	{
132
		$result = $this->object->getResourceType();
133
134
		$this->assertContains( 'stock', $result );
135
		$this->assertContains( 'stock/type', $result );
136
	}
137
138
139
	public function testGetSearchAttributes()
140
	{
141
		foreach( $this->object->getSearchAttributes() as $attribute ) {
142
			$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Attribute\\Iface', $attribute );
143
		}
144
	}
145
146
147
	public function testSearchItems()
148
	{
149
		$total = 0;
150
		$search = $this->object->createSearch();
151
152
		$expr = array();
153
		$expr[] = $search->compare( '!=', 'stock.id', null );
154
		$expr[] = $search->compare( '!=', 'stock.siteid', null );
155
		$expr[] = $search->compare( '!=', 'stock.typeid', null );
156
		$expr[] = $search->compare( '!=', 'stock.productcode', null );
157
		$expr[] = $search->compare( '==', 'stock.stocklevel', 1000 );
158
		$expr[] = $search->compare( '==', 'stock.dateback', '2010-04-01 00:00:00' );
159
		$expr[] = $search->compare( '>=', 'stock.mtime', '1970-01-01 00:00:00' );
160
		$expr[] = $search->compare( '>=', 'stock.ctime', '1970-01-01 00:00:00' );
161
		$expr[] = $search->compare( '==', 'stock.editor', $this->editor );
162
163
		$search->setConditions( $search->combine( '&&', $expr ) );
164
		$search->setSlice( 0, 1 );
165
		$results = $this->object->searchItems( $search, array(), $total );
166
167
		$this->assertEquals( 1, count( $results ) );
168
		$this->assertEquals( 1, $total );
169
170
		foreach( $results as $itemId => $item ) {
171
			$this->assertEquals( $itemId, $item->getId() );
172
		}
173
	}
174
175
176
	public function testDecrease()
177
	{
178
		$typeManager = $this->object->getSubManager( 'type' );
179
		$typeItem = $typeManager->findItem( 'unit_type1', array(), 'product' );
180
181
		$stockItem = $this->object->createItem();
182
		$stockItem->setTypeId( $typeItem->getId() );
183
		$stockItem->setProductCode( 'CNC' );
184
		$stockItem->setStockLevel( 0 );
185
186
		$this->object->saveItem( $stockItem );
187
188
		$this->object->decrease( 'CNC', $typeItem->getCode(), 5 );
189
		$actual = $this->object->getItem( $stockItem->getId() );
190
191
		$this->object->deleteItem( $stockItem->getId() );
192
193
		$this->assertEquals( -5, $actual->getStocklevel() );
194
	}
195
196
197
	public function testIncrease()
198
	{
199
		$typeManager = $this->object->getSubManager( 'type' );
200
		$typeItem = $typeManager->findItem( 'unit_type1', array(), 'product' );
201
202
		$stockItem = $this->object->createItem();
203
		$stockItem->setTypeId( $typeItem->getId() );
204
		$stockItem->setProductCode( 'CNC' );
205
		$stockItem->setStockLevel( 0 );
206
207
		$this->object->saveItem( $stockItem );
208
209
		$this->object->increase( 'CNC', $typeItem->getCode(), 5 );
210
		$actual = $this->object->getItem( $stockItem->getId() );
211
212
		$this->object->deleteItem( $stockItem->getId() );
213
214
		$this->assertEquals( 5, $actual->getStocklevel() );
215
	}
216
217
218
	public function testGetSubManager()
219
	{
220
		$this->setExpectedException( '\\Aimeos\\MShop\\Exception' );
221
		$this->object->getSubManager( 'unknown' );
222
	}
223
}
224