Completed
Push — master ( cdddba...d80da6 )
by Aimeos
07:09
created

StandardTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2018
6
 */
7
8
9
namespace Aimeos\Controller\Frontend\Product;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
16
17
	protected function setUp()
18
	{
19
		$this->context = \TestHelperFrontend::getContext();
20
		$this->object = new \Aimeos\Controller\Frontend\Product\Standard( $this->context );
21
	}
22
23
24
	protected function tearDown()
25
	{
26
		unset( $this->object );
27
	}
28
29
30
	public function testAggregate()
31
	{
32
		$filter = $this->object->createFilter();
33
		$list = $this->object->aggregate( $filter, 'index.attribute.id' );
34
35
		$this->assertGreaterThan( 0, count( $list ) );
36
	}
37
38
39
	public function testCreateFilter()
40
	{
41
		$filter = $this->object->createFilter();
42
43
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );
44
		$this->assertEquals( [], $filter->getSortations() );
45
		$this->assertEquals( 0, $filter->getSliceStart() );
46
		$this->assertEquals( 100, $filter->getSliceSize() );
47
	}
48
49
50
	public function testCreateFilterIgnoreDates()
51
	{
52
		$this->context->getConfig()->set( 'controller/frontend/product/ignore-dates', true );
53
54
		$filter = $this->object->createFilter();
55
56
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );
57
	}
58
59
60
	public function testAddFilterAttribute()
61
	{
62
		$filter = $this->object->createFilter();
63
		$filter = $this->object->addFilterAttribute( $filter, array( 1, 2 ), [], [] );
64
65
		$list = $filter->getConditions()->getExpressions();
66
67
		if( !isset( $list[0] ) || !( $list[0] instanceof \Aimeos\MW\Criteria\Expression\Compare\Iface ) ) {
68
			throw new \RuntimeException( 'Wrong expression' );
69
		}
70
71
		$this->assertEquals( 'index.attribute:all(["1","2"])', $list[0]->getName() );
72
	}
73
74
75
	public function testAddFilterAttributeOptions()
76
	{
77
		$filter = $this->object->createFilter();
78
		$filter = $this->object->addFilterAttribute( $filter, [], array( 1 ), [] );
79
80
		$list = $filter->getConditions()->getExpressions();
81
82
		if( !isset( $list[0] ) || !( $list[0] instanceof \Aimeos\MW\Criteria\Expression\Compare\Iface ) ) {
83
			throw new \RuntimeException( 'Wrong expression' );
84
		}
85
86
		$this->assertEquals( 'index.attribute.id', $list[0]->getName() );
87
		$this->assertEquals( [1], $list[0]->getValue() );
88
	}
89
90
91
	public function testAddFilterAttributeOne()
92
	{
93
		$filter = $this->object->createFilter();
94
		$filter = $this->object->addFilterAttribute( $filter, [], [], array( 'test' => array( 2 ) ) );
95
96
		$list = $filter->getConditions()->getExpressions();
97
98
		if( !isset( $list[0] ) || !( $list[0] instanceof \Aimeos\MW\Criteria\Expression\Compare\Iface ) ) {
99
			throw new \RuntimeException( 'Wrong expression' );
100
		}
101
102
		$this->assertEquals( 'index.attribute.id', $list[0]->getName() );
103
		$this->assertEquals( [2], $list[0]->getValue() );
104
	}
105
106
107
	public function testAddFilterCategory()
108
	{
109
		$context = \TestHelperFrontend::getContext();
110
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'catalog' );
111
112
		$catId = $manager->findItem( 'root' )->getId();
113
		$level = \Aimeos\MW\Tree\Manager\Base::LEVEL_LIST;
114
115
		$filter = $this->object->createFilter();
116
		$filter = $this->object->addFilterCategory( $filter, [$catId], 'default', $level );
117
118
		$list = $filter->getConditions()->getExpressions();
119
120
		if( !isset( $list[0] ) || !( $list[0] instanceof \Aimeos\MW\Criteria\Expression\Combine\Iface ) ) {
121
			throw new \RuntimeException( 'Not a "combine" expression' );
122
		}
123
124
		if( !isset( $list[1] ) || !( $list[1] instanceof \Aimeos\MW\Criteria\Expression\Compare\Iface ) ) {
125
			throw new \RuntimeException( 'Not a "compare" expression' );
126
		}
127
128
		$this->assertEquals( 'index.catalog.id', $list[1]->getName() );
129
		$this->assertEquals( 3, count( $list[1]->getValue() ) );
130
		$this->assertEquals( [], $filter->getSortations() );
131
	}
132
133
134
	public function testAddFilterSupplier()
135
	{
136
		$filter = $this->object->createFilter();
137
		$filter = $this->object->addFilterSupplier( $filter, [1, 2] );
138
139
		$list = $filter->getConditions()->getExpressions();
140
141
		if( !isset( $list[0] ) || !( $list[0] instanceof \Aimeos\MW\Criteria\Expression\Combine\Iface ) ) {
142
			throw new \RuntimeException( 'Not a "combine" expression' );
143
		}
144
145
		if( !isset( $list[1] ) || !( $list[1] instanceof \Aimeos\MW\Criteria\Expression\Compare\Iface ) ) {
146
			throw new \RuntimeException( 'Not a "compare" expression' );
147
		}
148
149
		$this->assertEquals( 'index.supplier.id', $list[1]->getName() );
150
		$this->assertEquals( 2, count( $list[1]->getValue() ) );
151
		$this->assertEquals( [], $filter->getSortations() );
152
	}
153
154
155
	public function testAddFilterText()
156
	{
157
		$filter = $this->object->createFilter();
158
		$filter = $this->object->addFilterText( $filter, 'Espresso' );
159
160
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );
161
162
		$list = $filter->getConditions()->getExpressions();
163
164
165
		if( !isset( $list[0] ) || !( $list[0] instanceof \Aimeos\MW\Criteria\Expression\Compare\Iface ) ) {
166
			throw new \RuntimeException( 'Wrong expression' );
167
		}
168
		$this->assertEquals( 'index.text:relevance("de","Espresso")', $list[0]->getName() );
169
		$this->assertEquals( 0, $list[0]->getValue() );
170
171
		$this->assertEquals( [], $filter->getSortations() );
172
		$this->assertEquals( 0, $filter->getSliceStart() );
173
		$this->assertEquals( 100, $filter->getSliceSize() );
174
	}
175
176
177
	public function testCreateFilterSortRelevanceCategory()
178
	{
179
		$level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE;
180
181
		$filter = $this->object->createFilter( 'relevance', '-' );
182
		$filter = $this->object->addFilterCategory( $filter, [-1], 'test', $level );
183
184
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );
185
186
		$sort = $filter->getSortations();
187
		if( ( $item = reset( $sort ) ) === false ) {
188
			throw new \RuntimeException( 'Sortation not set' );
189
		}
190
191
		$this->assertEquals( 'sort:index.catalog:position("test",["-1"])', $item->getName() );
192
		$this->assertEquals( '-', $item->getOperator() );
193
	}
194
195
196
	public function testCreateFilterSortRelevanceSupplier()
197
	{
198
		$filter = $this->object->createFilter( 'relevance', '-' );
199
		$filter = $this->object->addFilterSupplier( $filter, [-1], 'test' );
200
201
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );
202
203
		$sort = $filter->getSortations();
204
		if( ( $item = reset( $sort ) ) === false ) {
205
			throw new \RuntimeException( 'Sortation not set' );
206
		}
207
208
		$this->assertEquals( 'sort:index.supplier:position("test",["-1"])', $item->getName() );
209
		$this->assertEquals( '-', $item->getOperator() );
210
	}
211
212
213
	public function testCreateFilterSortRelevanceText()
214
	{
215
		$filter = $this->object->createFilter( 'relevance', '-', 1, 2 );
216
		$filter = $this->object->addFilterText( $filter, 'Espresso' );
217
218
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );
219
		$this->assertEquals( [], $filter->getSortations() );
220
		$this->assertEquals( 1, $filter->getSliceStart() );
221
		$this->assertEquals( 2, $filter->getSliceSize() );
222
	}
223
224
225
	public function testCreateFilterSortCode()
226
	{
227
		$filter = $this->object->createFilter( 'code' );
228
229
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );
230
231
		$sort = $filter->getSortations();
232
		if( ( $item = reset( $sort ) ) === false ) {
233
			throw new \RuntimeException( 'Sortation not set' );
234
		}
235
236
		$this->assertEquals( 'product.code', $item->getName() );
237
	}
238
239
240
	public function testCreateFilterSortCtime()
241
	{
242
		$filter = $this->object->createFilter( 'ctime' );
243
244
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );
245
246
		$sort = $filter->getSortations();
247
		if( ( $item = reset( $sort ) ) === false ) {
248
			throw new \RuntimeException( 'Sortation not set' );
249
		}
250
251
		$this->assertEquals( 'product.ctime', $item->getName() );
252
	}
253
254
255
	public function testCreateFilterSortName()
256
	{
257
		$filter = $this->object->createFilter( 'name' );
258
259
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );
260
261
		$sort = $filter->getSortations();
262
		if( ( $item = reset( $sort ) ) === false ) {
263
			throw new \RuntimeException( 'Sortation not set' );
264
		}
265
266
		$this->assertEquals( 'sort:index.text:name("de")', $item->getName() );
267
	}
268
269
270
	public function testCreateFilterSortPrice()
271
	{
272
		$filter = $this->object->createFilter( 'price' );
273
274
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );
275
276
		$sort = $filter->getSortations();
277
		if( ( $item = reset( $sort ) ) === false ) {
278
			throw new \RuntimeException( 'Sortation not set' );
279
		}
280
281
		$this->assertStringStartsWith( 'sort:index.price:value("default","EUR","default")', $item->getName() );
282
	}
283
284
285
	public function testCreateFilterSortInvalid()
286
	{
287
		$filter = $this->object->createFilter( '', 'failure' );
288
289
		$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );
290
		$this->assertEquals( [], $filter->getSortations() );
291
	}
292
293
294
	public function testGetItem()
295
	{
296
		$context = \TestHelperFrontend::getContext();
297
		$id = \Aimeos\MShop\Factory::createManager( $context, 'product' )->findItem( 'CNC' )->getId();
298
299
		$result = $this->object->getItem( $id );
300
301
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $result );
302
		$this->assertGreaterThan( 0, $result->getPropertyItems() );
303
		$this->assertGreaterThan( 0, $result->getRefItems( 'attribute' ) );
304
		$this->assertGreaterThan( 0, $result->getRefItems( 'media' ) );
305
		$this->assertGreaterThan( 0, $result->getRefItems( 'price' ) );
306
		$this->assertGreaterThan( 0, $result->getRefItems( 'product' ) );
307
		$this->assertGreaterThan( 0, $result->getRefItems( 'text' ) );
308
	}
309
310
311
	public function testGetItems()
312
	{
313
		$context = \TestHelperFrontend::getContext();
314
		$context->getConfig()->set( 'controller/frontend/product/ignore-dates', true );
315
316
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'product' );
317
318
		$search = $manager->createSearch();
319
		$search->setConditions( $search->compare( '==', 'product.code', array( 'CNC', 'CNE' ) ) );
320
321
		$ids = [];
322
		foreach( $manager->searchItems( $search ) as $productItem ) {
323
			$ids[] = $productItem->getId();
324
		}
325
326
327
		$result = $this->object->getItems( $ids );
328
329
		$this->assertEquals( 2, count( $result ) );
330
331
		foreach( $result as $productItem ) {
332
			$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $productItem );
333
		}
334
	}
335
336
337
	public function testSearchItemsCategory()
338
	{
339
		$catalogManager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
340
341
		$search = $catalogManager->createSearch()->setSlice( 0, 1 );
342
		$search->setConditions( $search->compare( '==', 'catalog.code', 'new' ) );
343
		$items = $catalogManager->searchItems( $search );
344
345
		if( ( $item = reset( $items ) ) === false ) {
346
			throw new \RuntimeException( 'Product item not found' );
347
		}
348
349
		$filter = $this->object->createFilter( 'position', '+', 1, 1 );
350
		$filter = $this->object->addFilterCategory( $filter, [$item->getId()] );
351
352
		$total = 0;
353
		$results = $this->object->searchItems( $filter, [], $total );
354
355
		$this->assertEquals( 3, $total );
356
		$this->assertEquals( 1, count( $results ) );
357
	}
358
359
360
	public function testSearchItemsText()
361
	{
362
		$filter = $this->object->createFilter( 'relevance', '+', 0, 1 );
363
		$filter = $this->object->addFilterText( $filter, 'Expresso' );
364
365
		$total = 0;
366
		$results = $this->object->searchItems( $filter, [], $total );
367
368
		$this->assertEquals( 2, $total );
369
		$this->assertEquals( 1, count( $results ) );
370
	}
371
}
372