Passed
Pull Request — master (#297)
by Aimeos
10:30
created

StandardTest::testIterate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 14
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 */
8
9
10
namespace Aimeos\MShop\Index\Manager\Attribute;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $context;
16
	private $object;
17
18
19
	protected function setUp() : void
20
	{
21
		$this->context = \TestHelper::context();
22
		$this->object = new \Aimeos\MShop\Index\Manager\Attribute\Standard( $this->context );
23
	}
24
25
26
	protected function tearDown() : void
27
	{
28
		unset( $this->object );
29
	}
30
31
32
	public function testClear()
33
	{
34
		$this->assertInstanceOf( \Aimeos\MShop\Index\Manager\Iface::class, $this->object->clear( array( -1 ) ) );
35
	}
36
37
38
	public function testAggregate()
39
	{
40
		$item = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'white', [], 'product', 'color' );
41
42
		$search = $this->object->filter( true );
43
		$result = $this->object->aggregate( $search, 'index.attribute.id' );
44
45
		$this->assertEquals( 15, $result->count() );
46
		$this->assertEquals( 5, $result->get( $item->getId() ) );
47
	}
48
49
50
	public function testAggregateMultiple()
51
	{
52
		$item = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'white', [], 'product', 'color' );
53
54
		$search = $this->object->filter( true );
55
		$result = $this->object->aggregate( $search, ['product.status', 'index.attribute.id'] );
56
57
		$this->assertEquals( 15, count( $result[1] ) );
58
		$this->assertEquals( 5, $result[1][$item->getId()] );
59
	}
60
61
62
	public function testGetResourceType()
63
	{
64
		$result = $this->object->getResourceType();
65
66
		$this->assertContains( 'index/attribute', $result );
67
	}
68
69
70
	public function testGetSearchAttributes()
71
	{
72
		foreach( $this->object->getSearchAttributes() as $attribute ) {
73
			$this->assertInstanceOf( \Aimeos\Base\Criteria\Attribute\Iface::class, $attribute );
74
		}
75
	}
76
77
78
	public function testSaveDeleteItem()
79
	{
80
		$productManager = \Aimeos\MShop::create( $this->context, 'product' );
81
		$product = $productManager->find( 'CNC', ['attribute'] );
82
		$attrItem = $product->getRefItems( 'attribute' )->first();
83
84
		$product = $this->object->save( $product->setId( null )->setCode( 'ModifiedCNC' ) );
85
86
87
		$search = $this->object->filter();
88
		$search->setConditions( $search->compare( '==', 'index.attribute.id', $attrItem->getId() ) );
89
		$result = $this->object->search( $search )->toArray();
90
91
92
		$this->object->delete( $product->getId() );
93
		$productManager->delete( $product->getId() );
94
95
96
		$search = $this->object->filter();
97
		$search->setConditions( $search->compare( '==', 'index.attribute.id', $attrItem->getId() ) );
98
		$result2 = $this->object->search( $search )->toArray();
99
100
		$this->assertTrue( in_array( $product->getId(), array_keys( $result ) ) );
101
		$this->assertFalse( in_array( $product->getId(), array_keys( $result2 ) ) );
102
	}
103
104
105
	public function testGetSubManager()
106
	{
107
		$this->expectException( \Aimeos\MShop\Exception::class );
108
		$this->object->getSubManager( 'unknown' );
109
	}
110
111
112
	public function testIterate()
113
	{
114
		$attributeManager = \Aimeos\MShop::create( $this->context, 'attribute' );
115
		$id = $attributeManager->find( '30', [], 'product', 'length' )->getId();
116
117
		$filter = $this->object->filter()->add( 'index.attribute.id', '==', $id );
118
119
		$iterator = $this->object->iterator( $filter );
120
		$products = $this->object->iterate( $iterator, [], 10 );
121
122
		$this->assertEquals( 4, count( $products ) );
123
124
		foreach( $products as $itemId => $item ) {
125
			$this->assertEquals( $itemId, $item->getId() );
126
		}
127
	}
128
129
130
	public function testRemove()
131
	{
132
		$this->assertEquals( $this->object, $this->object->remove( [-1] ) );
133
	}
134
135
136
	public function testSearchItems()
137
	{
138
		$attributeManager = \Aimeos\MShop::create( $this->context, 'attribute' );
139
		$id = $attributeManager->find( '30', [], 'product', 'length' )->getId();
140
141
		$search = $this->object->filter();
142
		$search->setConditions( $search->compare( '==', 'index.attribute.id', $id ) );
143
		$result = $this->object->search( $search, [] );
144
145
		$this->assertEquals( 4, count( $result ) );
146
	}
147
148
149
	public function testSearchItemsNoId()
150
	{
151
		$search = $this->object->filter();
152
		$search->setConditions( $search->compare( '!=', 'index.attribute.id', null ) );
153
		$result = $this->object->search( $search, [] );
154
155
		$this->assertGreaterThanOrEqual( 2, count( $result ) );
156
	}
157
158
159
	public function testSearchItemsAllof()
160
	{
161
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
162
163
		$attrIds = [
164
			$manager->find( '30', [], 'product', 'length' )->getId(),
165
			$manager->find( '29', [], 'product', 'width' )->getId()
166
		];
167
168
		$search = $this->object->filter()->order( 'product.code' );
169
170
		$func = $search->make( 'index.attribute:allof', [$attrIds] );
171
		$search->setConditions( $search->compare( '!=', $func, null ) );
172
173
		$result = $this->object->search( $search, [] );
174
175
		$this->assertEquals( 2, count( $result ) );
176
		$this->assertEquals( 'CNE', $result->first()->getCode() );
177
	}
178
179
180
	public function testSearchItemsAllofArticle()
181
	{
182
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
183
184
		$attrIds = [
185
			$manager->find( '30', [], 'product', 'length' )->getId(),
186
			$manager->find( '30', [], 'product', 'width' )->getId()
187
		];
188
189
		$search = $this->object->filter()->order( 'product.code' );
190
191
		$func = $search->make( 'index.attribute:allof', [$attrIds] );
192
		$search->add( $func, '!=', null );
193
194
		$result = $this->object->search( $search, [] );
195
196
		$this->assertEquals( 2, count( $result ) );
197
		$this->assertEquals( 'U:TEST', $result->first()->getCode() );
198
	}
199
200
201
	public function testSearchItemsOneof()
202
	{
203
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
204
205
		$length = $manager->find( '30', [], 'product', 'length' )->getId();
206
		$width = $manager->find( '29', [], 'product', 'width' )->getId();
207
208
		$search = $this->object->filter()->order( 'product.code' );
209
		$search->add( [$search->make( 'index.attribute:oneof', [$length, $width] ) => null], '!=' );
210
211
		$result = $this->object->search( $search );
212
213
		$this->assertEquals( 4, count( $result ) );
214
		$this->assertEquals( 'CNE', $result->first()->getCode() );
215
	}
216
217
218
	public function testSearchItemsOneofArticle()
219
	{
220
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
221
222
		$color = $manager->find( 'white', [], 'product', 'color' )->getId();
223
		$size = $manager->find( 'm', [], 'product', 'size' )->getId();
224
225
		$search = $this->object->filter()->order( 'product.code' );
226
		$search->add( [
227
			$search->make( 'index.attribute:oneof', [$color] ) => null,
228
			$search->make( 'index.attribute:oneof', [$size] ) => null
229
		], '!=' );
230
231
		$result = $this->object->search( $search, [] );
232
233
		$this->assertEquals( 2, count( $result ) );
234
		$this->assertEquals( 'U:TEST', $result->first()->getCode() );
235
	}
236
237
238
	public function testCleanup()
239
	{
240
		$this->assertInstanceOf( \Aimeos\MShop\Index\Manager\Iface::class, $this->object->cleanup( '1970-01-01 00:00:00' ) );
241
	}
242
243
}
244