Passed
Push — master ( 5d05b9...5af5d7 )
by Aimeos
05:15
created

StandardTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 103
c 1
b 0
f 0
dl 0
loc 190
rs 10
wmc 14

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testSaveUpdateDeleteItem() 0 61 1
A testGetSubManager() 0 7 1
A testGetSubManagerInvalidName() 0 4 1
A testGetResourceType() 0 5 1
A testGetItem() 0 11 1
A testClear() 0 3 1
A setUp() 0 11 1
A testCreateItemType() 0 4 1
A tearDown() 0 3 1
A testSearchItems() 0 22 1
A testSearchItemsBase() 0 8 2
A testCreateItem() 0 3 1
A testDeleteItems() 0 3 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
6
 */
7
8
9
namespace Aimeos\MShop\Rule\Manager;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $exampleRule;
16
	private $exampleRule2;
17
	private $editor = '';
18
19
20
	protected function setUp() : void
21
	{
22
		$this->editor = \TestHelperMShop::getContext()->getEditor();
23
		$this->object = \Aimeos\MShop\Rule\Manager\Factory::create( \TestHelperMShop::getContext() );
24
25
		$this->exampleRule = $this->object->create();
26
		$this->exampleRule->setType( 'catalog' );
27
		$this->exampleRule->setProvider( 'Percent' );
28
		$this->exampleRule->setConfig( ['percent' => '10'] );
29
30
		$this->exampleRule2 = clone $this->exampleRule;
31
	}
32
33
34
	protected function tearDown() : void
35
	{
36
		unset( $this->object );
37
	}
38
39
40
	public function testClear()
41
	{
42
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( [-1] ) );
43
	}
44
45
46
	public function testDeleteItems()
47
	{
48
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->delete( [-1] ) );
49
	}
50
51
52
	public function testGetResourceType()
53
	{
54
		$result = $this->object->getResourceType();
55
56
		$this->assertContains( 'rule', $result );
57
	}
58
59
60
	public function testCreateItem()
61
	{
62
		$this->assertInstanceOf( \Aimeos\MShop\Rule\Item\Iface::class, $this->object->create() );
63
	}
64
65
66
	public function testCreateItemType()
67
	{
68
		$item = $this->object->create( ['rule.type' => 'catalog'] );
69
		$this->assertEquals( 'catalog', $item->getType() );
70
	}
71
72
73
	public function testGetItem()
74
	{
75
		$search = $this->object->filter()->slice( 0, 1 )
76
			->add( ['rule.provider' => 'Percent,Category', 'rule.editor' => $this->editor] );
77
78
		$expected = $this->object->search( $search )
79
			->first( new \RuntimeException( sprintf( 'No rule item including "%1$s" found', 'Percent,Category' ) ));
80
81
		$actual = $this->object->get( $expected->getId() );
82
83
		$this->assertEquals( $expected, $actual );
84
	}
85
86
87
	public function testSaveUpdateDeleteItem()
88
	{
89
		$search = $this->object->filter()->slice( 0, 1 )
90
			->add( ['rule.provider' => 'Percent,Category', 'rule.editor' => $this->editor] );
91
92
		$item = $this->object->search( $search )
93
			->first( new \RuntimeException( sprintf( 'No rule item including "%1$s" found', 'Percent,Category' ) ));
94
95
		$item->setId( null );
96
		$item->setProvider( 'Example1' );
97
		$resultSaved = $this->object->save( $item );
98
		$itemSaved = $this->object->get( $item->getId() );
99
100
		$itemExp = clone $itemSaved;
101
		$itemExp->setProvider( 'Example' );
102
		$itemExp->setPosition( 5 );
103
		$itemExp->setStatus( -1 );
104
		$resultUpd = $this->object->save( $itemExp );
105
		$itemUpd = $this->object->get( $itemExp->getId() );
106
107
		$this->object->delete( $itemSaved->getId() );
108
109
110
		$this->assertTrue( $item->getId() !== null );
111
		$this->assertTrue( $itemSaved->getType() !== null );
112
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
113
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
114
		$this->assertEquals( $item->getProvider(), $itemSaved->getProvider() );
115
		$this->assertEquals( $item->getDateStart(), $itemSaved->getDateStart() );
116
		$this->assertEquals( $item->getDateEnd(), $itemSaved->getDateEnd() );
117
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
118
		$this->assertEquals( $item->getLabel(), $itemSaved->getLabel() );
119
		$this->assertEquals( $item->getConfig(), $itemSaved->getConfig() );
120
		$this->assertEquals( $item->getPosition(), $itemSaved->getPosition() );
121
		$this->assertEquals( $item->getStatus(), $itemSaved->getStatus() );
122
123
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
124
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
125
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
126
127
		$this->assertTrue( $itemUpd->getType() !== null );
128
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
129
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
130
		$this->assertEquals( $itemExp->getProvider(), $itemUpd->getProvider() );
131
		$this->assertEquals( $itemExp->getDateStart(), $itemUpd->getDateStart() );
132
		$this->assertEquals( $itemExp->getDateEnd(), $itemUpd->getDateEnd() );
133
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
134
		$this->assertEquals( $itemExp->getLabel(), $itemUpd->getLabel() );
135
		$this->assertEquals( $itemExp->getConfig(), $itemUpd->getConfig() );
136
		$this->assertEquals( $itemExp->getPosition(), $itemUpd->getPosition() );
137
		$this->assertEquals( $itemExp->getStatus(), $itemUpd->getStatus() );
138
139
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
140
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
141
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
142
143
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
144
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );
145
146
		$this->expectException( \Aimeos\MShop\Exception::class );
147
		$this->object->get( $itemSaved->getId() );
148
	}
149
150
151
	public function testSearchItems()
152
	{
153
		$total = 0;
154
		$search = $this->object->filter();
155
156
		$expr = [];
157
		$expr[] = $search->compare( '!=', 'rule.id', null );
158
		$expr[] = $search->compare( '!=', 'rule.siteid', null );
159
		$expr[] = $search->compare( '==', 'rule.type', 'catalog' );
160
		$expr[] = $search->compare( '!=', 'rule.label', null );
161
		$expr[] = $search->compare( '~=', 'rule.provider', 'Percent,Category' );
162
		$expr[] = $search->compare( '>=', 'rule.datestart', '1970-01-01 00:00:00' );
163
		$expr[] = $search->compare( '==', 'rule.dateend', null );
164
		$expr[] = $search->compare( '~=', 'rule.config', 'home' );
165
		$expr[] = $search->compare( '==', 'rule.position', 0 );
166
		$expr[] = $search->compare( '==', 'rule.status', 1 );
167
		$expr[] = $search->compare( '>=', 'rule.mtime', '1970-01-01 00:00:00' );
168
		$expr[] = $search->compare( '>=', 'rule.ctime', '1970-01-01 00:00:00' );
169
		$expr[] = $search->compare( '==', 'rule.editor', $this->editor );
170
171
		$search->setConditions( $search->and( $expr ) );
172
		$this->assertEquals( 1, $this->object->search( $search, [], $total )->count() );
173
	}
174
175
176
	public function testSearchItemsBase()
177
	{
178
		$search = $this->object->filter( true )->slice( 0, 1 );
179
		$results = $this->object->search( $search, [] );
180
		$this->assertEquals( 1, count( $results ) );
181
182
		foreach( $results as $itemId => $item ) {
183
			$this->assertEquals( $itemId, $item->getId() );
184
		}
185
	}
186
187
188
	public function testGetSubManager()
189
	{
190
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type' ) );
191
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->getSubManager( 'type', 'Standard' ) );
192
193
		$this->expectException( \Aimeos\MShop\Exception::class );
194
		$this->object->getSubManager( 'unknown' );
195
	}
196
197
198
	public function testGetSubManagerInvalidName()
199
	{
200
		$this->expectException( \Aimeos\MShop\Exception::class );
201
		$this->object->getSubManager( 'type', 'unknown' );
202
	}
203
}
204