Passed
Push — master ( d34681...4293d9 )
by Aimeos
05:27
created

StandardTest::testSearchItemsTotal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 */
8
9
10
namespace Aimeos\MShop\Price\Manager\Lists\Type;
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
		$manager = \Aimeos\MShop\Price\Manager\Factory::create( \TestHelperMShop::getContext() );
23
24
		$listManager = $manager->getSubManager( 'lists' );
25
		$this->object = $listManager->getSubManager( 'type' );
26
	}
27
28
29
	protected function tearDown()
30
	{
31
		unset( $this->object );
32
	}
33
34
35
	public function testCleanup()
36
	{
37
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->cleanup( [-1] ) );
38
	}
39
40
41
	public function testGetResourceType()
42
	{
43
		$result = $this->object->getResourceType();
44
45
		$this->assertContains( 'price/lists/type', $result );
46
	}
47
48
49
	public function testCreateItem()
50
	{
51
		$item = $this->object->createItem();
52
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Type\Iface::class, $item );
53
	}
54
55
56
	public function testGetItem()
57
	{
58
		$search = $this->object->createSearch()->setSlice( 0, 1 );
59
		$search->setConditions( $search->compare( '==', 'price.lists.type.editor', $this->editor ) );
60
		$results = $this->object->searchItems( $search );
61
62
		if( ( $expected = reset( $results ) ) === false ) {
63
			throw new \RuntimeException( 'No price list type item found' );
64
		}
65
66
		$this->assertEquals( $expected, $this->object->getItem( $expected->getId() ) );
67
	}
68
69
70
	public function testSaveInvalid()
71
	{
72
		$this->setExpectedException( \Aimeos\MW\Common\Exception::class );
73
		$this->object->saveItem( new \Aimeos\MShop\Locale\Item\Standard() );
74
	}
75
76
77
	public function testSaveUpdateDeleteItem()
78
	{
79
		$search = $this->object->createSearch();
80
		$search->setConditions( $search->compare( '==', 'price.lists.type.editor', $this->editor ) );
81
		$results = $this->object->searchItems( $search );
82
83
		if( ( $item = reset( $results ) ) === false ) {
84
			throw new \RuntimeException( 'No type item found' );
85
		}
86
87
		$item->setId( null );
88
		$item->setCode( 'unitTestSave' );
89
		$resultSaved = $this->object->saveItem( $item );
90
		$itemSaved = $this->object->getItem( $item->getId() );
91
92
		$itemExp = clone $itemSaved;
93
		$itemExp->setCode( 'unitTestSave2' );
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->getCode(), $itemSaved->getCode() );
104
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
105
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
106
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
107
108
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
109
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
110
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
111
112
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
113
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
114
		$this->assertEquals( $itemExp->getCode(), $itemUpd->getCode() );
115
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
116
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
117
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
118
119
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
120
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
121
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
122
123
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
124
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
125
126
		$this->setExpectedException( \Aimeos\MShop\Exception::class );
127
		$this->object->getItem( $itemSaved->getId() );
128
	}
129
130
131
	public function testSearchItems()
132
	{
133
		$search = $this->object->createSearch();
134
135
		$expr = [];
136
		$expr[] = $search->compare( '!=', 'price.lists.type.id', null );
137
		$expr[] = $search->compare( '!=', 'price.lists.type.siteid', null );
138
		$expr[] = $search->compare( '==', 'price.lists.type.code', 'default' );
139
		$expr[] = $search->compare( '==', 'price.lists.type.domain', 'product' );
140
		$expr[] = $search->compare( '==', 'price.lists.type.label', 'Standard' );
141
		$expr[] = $search->compare( '>=', 'price.lists.type.position', 0 );
142
		$expr[] = $search->compare( '==', 'price.lists.type.status', 1 );
143
		$expr[] = $search->compare( '==', 'price.lists.type.editor', $this->editor );
144
145
		$search->setConditions( $search->combine( '&&', $expr ) );
146
147
		$results = $this->object->searchItems( $search, [], $total );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $total seems to be never defined.
Loading history...
148
		$this->assertEquals( 1, count( $results ) );
149
	}
150
151
152
	public function testSearchItemsTotal()
153
	{
154
		$total = 0;
155
156
		$search = $this->object->createSearch()->setSlice( 0, 1 );
157
		$search->setConditions( $search->compare( '==', 'price.lists.type.editor', $this->editor ) );
158
		$search->setSortations( [$search->sort( '-', 'price.lists.type.position' )] );
159
160
		$results = $this->object->searchItems( $search, [], $total );
161
162
		$this->assertEquals( 1, count( $results ) );
163
		$this->assertEquals( 2, $total );
164
165
		foreach( $results as $itemId => $item ) {
166
			$this->assertEquals( $itemId, $item->getId() );
167
		}
168
	}
169
}
170