Passed
Push — master ( b7775a...4e1b50 )
by Aimeos
03:15
created

StandardTest::testSortPriceDesc()   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
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
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-2020
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() : void
18
	{
19
		$this->context = \TestHelperFrontend::getContext();
0 ignored issues
show
Bug Best Practice introduced by
The property context does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
		$this->object = new \Aimeos\Controller\Frontend\Product\Standard( $this->context );
21
	}
22
23
24
	protected function tearDown() : void
25
	{
26
		unset( $this->object );
27
	}
28
29
30
	public function testAggregate()
31
	{
32
		$list = $this->object->aggregate( 'index.attribute.id' );
33
34
		$this->assertGreaterThan( 0, count( $list ) );
35
	}
36
37
38
	public function testAllOf()
39
	{
40
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
41
42
		$length = $manager->find( '30', [], 'product', 'length' )->getId();
43
		$width = $manager->find( '29', [], 'product', 'width' )->getId();
44
45
		$this->assertEquals( 1, count( $this->object->allOf( [$length, $width] )->search() ) );
46
	}
47
48
49
	public function testCategory()
50
	{
51
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
52
		$catId = $manager->find( 'cafe' )->getId();
53
54
		$this->assertEquals( 2, count( $this->object->category( $catId, 'promotion' )->search() ) );
55
	}
56
57
58
	public function testCategoryTree()
59
	{
60
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
61
62
		$catId = $manager->find( 'categories' )->getId();
63
		$grpId = $manager->find( 'group' )->getId();
64
65
		$this->object->category( [$catId, $grpId], 'promotion', \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE );
66
		$this->assertEquals( 3, count( $this->object->search() ) );
67
	}
68
69
70
	public function testCompare()
71
	{
72
		$this->assertEquals( 1, count( $this->object->compare( '==', 'product.type', 'bundle' )->search() ) );
73
	}
74
75
76
	public function testFind()
77
	{
78
		$item = $this->object->uses( ['product'] )->find( 'U:BUNDLE' );
79
80
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $item );
81
		$this->assertEquals( 2, count( $item->getRefItems( 'product' ) ) );
82
	}
83
84
85
	public function testFunction()
86
	{
87
		$str = $this->object->function( 'product:has', ['domain', 'type', 'refid'] );
88
		$this->assertEquals( 'product:has("domain","type","refid")', $str );
89
	}
90
91
92
	public function testGet()
93
	{
94
		$item = \Aimeos\MShop::create( $this->context, 'product' )->find( 'U:BUNDLE' );
95
		$item = $this->object->uses( ['product'] )->get( $item->getId() );
96
97
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $item );
98
		$this->assertEquals( 2, count( $item->getRefItems( 'product' ) ) );
99
	}
100
101
102
	public function testHas()
103
	{
104
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
105
		$attrId = $manager->find( '30', [], 'product', 'length' )->getId();
106
107
		$this->assertEquals( 1, count( $this->object->has( 'attribute', 'variant', $attrId )->search() ) );
108
	}
109
110
111
	public function testOneOf()
112
	{
113
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
114
115
		$length = $manager->find( '30', [], 'product', 'length' )->getId();
116
		$width = $manager->find( '30', [], 'product', 'width' )->getId();
117
118
		$this->assertEquals( 2, count( $this->object->oneOf( [$length, $width] )->search() ) );
119
	}
120
121
122
	public function testOneOfList()
123
	{
124
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
125
126
		$length = $manager->find( '30', [], 'product', 'length' )->getId();
127
		$width = $manager->find( '30', [], 'product', 'width' )->getId();
128
129
		$this->assertEquals( 1, count( $this->object->oneOf( [[$length], [$width]] )->search() ) );
130
	}
131
132
133
	public function testParse()
134
	{
135
		$cond = ['&&' => [['>' => ['product.status' => 0]], ['==' => ['product.type' => 'default']]]];
136
		$this->assertEquals( 4, count( $this->object->parse( $cond )->search() ) );
137
	}
138
139
140
	public function testPrice()
141
	{
142
		$this->assertEquals( 5, count( $this->object->price( 20 )->search() ) );
143
	}
144
145
146
	public function testPriceArray()
147
	{
148
		$this->assertEquals( 5, count( $this->object->price( [20] )->search() ) );
149
	}
150
151
152
	public function testPriceBoth()
153
	{
154
		$this->assertEquals( 4, count( $this->object->price( [15, 20] )->search() ) );
155
	}
156
157
158
	public function testProduct()
159
	{
160
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
161
162
		$cncId = $manager->find( 'CNC' )->getId();
163
		$cneId = $manager->find( 'CNE' )->getId();
164
165
		$this->assertEquals( 2, count( $this->object->product( [$cncId, $cneId] )->search() ) );
166
	}
167
168
169
	public function testProperty()
170
	{
171
		$this->assertEquals( 1, count( $this->object->property( 'package-weight', '1.25' )->search() ) );
172
	}
173
174
175
	public function testResolve()
176
	{
177
		$item = $this->object->resolve( 'Cafe-Noire-Cappuccino' );
178
179
		$this->assertInstanceOf( \Aimeos\MShop\Product\Item\Iface::class, $item );
180
		$this->assertEquals( 'Cafe Noire Cappuccino', $item->getLabel() );
181
	}
182
183
184
	public function testSearch()
185
	{
186
		$total = 0;
187
		$items = $this->object->uses( ['price'] )->sort( 'code' )->search( $total );
188
189
		$this->assertEquals( 8, count( $items ) );
190
		$this->assertEquals( 8, $total );
191
		$this->assertEquals( 2, count( $items->first()->getRefItems( 'price' ) ) );
192
	}
193
194
195
	public function testSlice()
196
	{
197
		$this->assertEquals( 2, count( $this->object->slice( 0, 2 )->search() ) );
198
	}
199
200
201
	public function testSort()
202
	{
203
		$this->assertEquals( 8, count( $this->object->sort( '+relevance' )->search() ) );
204
	}
205
206
207
	public function testSortGeneric()
208
	{
209
		$this->assertEquals( 8, count( $this->object->sort( '-product.status' )->search() ) );
210
	}
211
212
213
	public function testSortMultiple()
214
	{
215
		$this->assertEquals( 8, count( $this->object->sort( '-product.status,product.id' )->search() ) );
216
	}
217
218
219
	public function testSortCode()
220
	{
221
		$result = $this->object->sort( 'code' )->search();
222
		$this->assertEquals( 'ABCD', $result->first()->getCode() );
223
	}
224
225
226
	public function testSortCodeDesc()
227
	{
228
		$result = $this->object->sort( '-code' )->search();
229
		$this->assertStringStartsWith( 'U:', $result->first()->getCode() );
230
	}
231
232
233
	public function testSortCtime()
234
	{
235
		$this->assertEquals( 8, count( $this->object->sort( 'ctime' )->search() ) );
236
	}
237
238
239
	public function testSortCtimeDesc()
240
	{
241
		$this->assertEquals( 8, count( $this->object->sort( '-ctime' )->search() ) );
242
	}
243
244
245
	public function testSortName()
246
	{
247
		$result = $this->object->uses( ['text'] )->sort( 'name' )->search();
248
		$this->assertEquals( 'Cafe Noire Cappuccino', $result->first()->getName() );
249
	}
250
251
252
	public function testSortNameDesc()
253
	{
254
		$result = $this->object->uses( ['text'] )->sort( '-name' )->search();
255
		$this->assertEquals( 'Unterproduct 1', $result->first()->getName() );
256
	}
257
258
259
	public function testSortPrice()
260
	{
261
		$result = $this->object->uses( ['price'] )->sort( 'price' )->search();
262
		$prices = $result->first()->getRefItems( 'price', 'default', 'default' );
263
264
		$this->assertEquals( '12.00', $prices->first()->getValue() );
265
	}
266
267
268
	public function testSortPriceDesc()
269
	{
270
		$result = $this->object->uses( ['price'] )->sort( '-price' )->search();
271
		$prices = $result->first()->getRefItems( 'price', 'default', 'default' );
272
273
		$this->assertEquals( '600.00', $prices->first()->getValue() );
274
	}
275
276
277
	public function testSortRelevanceCategory()
278
	{
279
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
280
		$catId = $manager->find( 'new' )->getId();
281
282
		$result = $this->object->category( $catId )->sort( 'relevance' )->search();
283
284
		$this->assertEquals( 3, count( $result ) );
285
		$this->assertEquals( 'CNE', $result->first()->getCode() );
286
		$this->assertEquals( 'U:BUNDLE', $result->last()->getCode() );
287
	}
288
289
290
	public function testSortRelevanceSupplier()
291
	{
292
		$manager = \Aimeos\MShop::create( $this->context, 'supplier' );
293
		$supId = $manager->find( 'unitCode001' )->getId();
294
295
		$result = $this->object->supplier( $supId )->sort( 'relevance' )->search();
296
297
		$this->assertEquals( 2, count( $result ) );
298
		$this->assertEquals( 'CNC', $result->first()->getCode() );
299
		$this->assertEquals( 'CNE', $result->last()->getCode() );
300
	}
301
302
303
	public function testStock()
304
	{
305
		$this->assertEquals( 6, count( $this->object->stock( 0 )->search() ) );
306
		$this->assertEquals( 6, count( $this->object->stock( '1' )->search() ) );
307
		$this->assertEquals( 6, count( $this->object->stock( 1.5 )->search() ) );
308
		$this->assertEquals( 6, count( $this->object->stock( null )->search() ) );
309
	}
310
311
312
	public function testSupplier()
313
	{
314
		$manager = \Aimeos\MShop::create( $this->context, 'supplier' );
315
		$supId = $manager->find( 'unitCode001' )->getId();
316
317
		$this->assertEquals( 2, count( $this->object->supplier( $supId )->search() ) );
318
	}
319
320
321
	public function testText()
322
	{
323
		$this->assertEquals( 2, count( $this->object->text( 'Cafe' )->search() ) );
324
	}
325
326
327
	public function testUses()
328
	{
329
		$this->assertSame( $this->object, $this->object->uses( ['text'] ) );
330
	}
331
}
332