Passed
Push — master ( e70d66...82ea4a )
by Aimeos
10:52 queued 33s
created

StandardTest::testProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
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-2021
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 testSearchAll()
196
	{
197
		$total = 0;
198
		$this->context->getConfig()->set( 'controller/frontend/product/show-all', true );
199
		$object = new \Aimeos\Controller\Frontend\Product\Standard( $this->context );
200
201
		$items = $object->search( $total );
202
203
		$this->assertEquals( 22, count( $items ) );
204
		$this->assertEquals( 22, $total );
205
	}
206
207
208
	public function testSlice()
209
	{
210
		$this->assertEquals( 2, count( $this->object->slice( 0, 2 )->search() ) );
211
	}
212
213
214
	public function testSort()
215
	{
216
		$this->assertEquals( 8, count( $this->object->sort( '+relevance' )->search() ) );
217
	}
218
219
220
	public function testSortGeneric()
221
	{
222
		$this->assertEquals( 8, count( $this->object->sort( '-product.status' )->search() ) );
223
	}
224
225
226
	public function testSortMultiple()
227
	{
228
		$this->assertEquals( 8, count( $this->object->sort( 'sort:index.text:relevance("de","test(\"\")"),product.id' )->search() ) );
229
	}
230
231
232
	public function testSortCode()
233
	{
234
		$result = $this->object->sort( 'code' )->search();
235
		$this->assertEquals( 'ABCD', $result->first()->getCode() );
236
	}
237
238
239
	public function testSortCodeDesc()
240
	{
241
		$result = $this->object->sort( '-code' )->search();
242
		$this->assertStringStartsWith( 'U:', $result->first()->getCode() );
243
	}
244
245
246
	public function testSortCtime()
247
	{
248
		$this->assertEquals( 8, count( $this->object->sort( 'ctime' )->search() ) );
249
	}
250
251
252
	public function testSortCtimeDesc()
253
	{
254
		$this->assertEquals( 8, count( $this->object->sort( '-ctime' )->search() ) );
255
	}
256
257
258
	public function testSortName()
259
	{
260
		$result = $this->object->uses( ['text'] )->sort( 'name' )->search();
261
		$this->assertEquals( 'Cafe Noire Cappuccino', $result->first()->getName() );
262
	}
263
264
265
	public function testSortNameDesc()
266
	{
267
		$result = $this->object->uses( ['text'] )->sort( '-name' )->search();
268
		$this->assertEquals( 'Unterproduct 1', $result->first()->getName() );
269
	}
270
271
272
	public function testSortPrice()
273
	{
274
		$result = $this->object->uses( ['price'] )->sort( 'price' )->search();
275
		$prices = $result->first()->getRefItems( 'price', 'default', 'default' );
276
277
		$this->assertEquals( '12.00', $prices->first()->getValue() );
278
	}
279
280
281
	public function testSortPriceDesc()
282
	{
283
		$result = $this->object->uses( ['price'] )->sort( '-price' )->search();
284
		$prices = $result->first()->getRefItems( 'price', 'default', 'default' );
285
286
		$this->assertEquals( '600.00', $prices->first()->getValue() );
287
	}
288
289
290
	public function testSortRelevanceCategory()
291
	{
292
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
293
		$catId = $manager->find( 'new' )->getId();
294
295
		$result = $this->object->category( $catId )->sort( 'relevance' )->search();
296
297
		$this->assertEquals( 3, count( $result ) );
298
		$this->assertEquals( 'CNE', $result->first()->getCode() );
299
		$this->assertEquals( 'U:BUNDLE', $result->last()->getCode() );
300
	}
301
302
303
	public function testSortRelevanceSupplier()
304
	{
305
		$manager = \Aimeos\MShop::create( $this->context, 'supplier' );
306
		$supId = $manager->find( 'unitSupplier001' )->getId();
307
308
		$result = $this->object->supplier( $supId )->sort( 'relevance' )->search();
309
310
		$this->assertEquals( 2, count( $result ) );
311
		$this->assertEquals( 'CNC', $result->first()->getCode() );
312
		$this->assertEquals( 'CNE', $result->last()->getCode() );
313
	}
314
315
316
	public function testSupplier()
317
	{
318
		$manager = \Aimeos\MShop::create( $this->context, 'supplier' );
319
		$supId = $manager->find( 'unitSupplier001' )->getId();
320
321
		$this->assertEquals( 2, count( $this->object->supplier( $supId )->search() ) );
322
	}
323
324
325
	public function testText()
326
	{
327
		$this->assertEquals( 2, count( $this->object->text( 'Cafe' )->search() ) );
328
	}
329
330
331
	public function testUses()
332
	{
333
		$this->assertSame( $this->object, $this->object->uses( ['text'] ) );
334
	}
335
}
336