1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2012 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2018 |
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() |
20
|
|
|
{ |
21
|
|
|
$this->context = \TestHelperMShop::getContext(); |
22
|
|
|
$this->object = new \Aimeos\MShop\Index\Manager\Attribute\Standard( $this->context ); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
protected function tearDown() |
27
|
|
|
{ |
28
|
|
|
unset( $this->object ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
public function testCleanup() |
33
|
|
|
{ |
34
|
|
|
$this->object->cleanup( array( -1 ) ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
public function testAggregate() |
39
|
|
|
{ |
40
|
|
|
$manager = \Aimeos\MShop::create( $this->context, 'attribute' ); |
41
|
|
|
|
42
|
|
|
$search = $manager->createSearch(); |
43
|
|
|
$expr = array( |
44
|
|
|
$search->compare( '==', 'attribute.code', 'white' ), |
45
|
|
|
$search->compare( '==', 'attribute.domain', 'product' ), |
46
|
|
|
$search->compare( '==', 'attribute.type', 'color' ), |
47
|
|
|
); |
48
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
49
|
|
|
|
50
|
|
|
$items = $manager->searchItems( $search ); |
51
|
|
|
|
52
|
|
|
if( ( $item = reset( $items ) ) === false ) { |
53
|
|
|
throw new \RuntimeException( 'No attribute item found' ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
$search = $this->object->createSearch( true ); |
58
|
|
|
$result = $this->object->aggregate( $search, 'index.attribute.id' ); |
59
|
|
|
|
60
|
|
|
$this->assertEquals( 15, count( $result ) ); |
61
|
|
|
$this->assertArrayHasKey( $item->getId(), $result ); |
62
|
|
|
$this->assertEquals( 4, $result[$item->getId()] ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
public function testGetResourceType() |
67
|
|
|
{ |
68
|
|
|
$result = $this->object->getResourceType(); |
69
|
|
|
|
70
|
|
|
$this->assertContains( 'index/attribute', $result ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
public function testGetSearchAttributes() |
75
|
|
|
{ |
76
|
|
|
foreach( $this->object->getSearchAttributes() as $attribute ) { |
77
|
|
|
$this->assertInstanceOf( \Aimeos\MW\Criteria\Attribute\Iface::class, $attribute ); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
public function testSaveDeleteItem() |
83
|
|
|
{ |
84
|
|
|
$productManager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context ); |
85
|
|
|
$search = $productManager->createSearch(); |
86
|
|
|
$search->setConditions( $search->compare( '==', 'product.code', 'CNC' ) ); |
87
|
|
|
|
88
|
|
|
$result = $productManager->searchItems( $search, array( 'attribute' ) ); |
89
|
|
|
|
90
|
|
|
if( ( $product = reset( $result ) ) === false ) { |
91
|
|
|
throw new \RuntimeException( 'No product item with code CNE found!' ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$attributes = $product->getRefItems( 'attribute' ); |
95
|
|
|
if( ( $attrItem = reset( $attributes ) ) === false ) { |
96
|
|
|
throw new \RuntimeException( 'Product doesnt have any attribute item' ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$product = $this->object->saveItem( $product->setId( null )->setCode( 'ModifiedCNC' ) ); |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
$search = $this->object->createSearch(); |
103
|
|
|
$search->setConditions( $search->compare( '==', 'index.attribute.id', $attrItem->getId() ) ); |
104
|
|
|
$result = $this->object->searchItems( $search ); |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
$this->object->deleteItem( $product->getId() ); |
108
|
|
|
$productManager->deleteItem( $product->getId() ); |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
$search = $this->object->createSearch(); |
112
|
|
|
$search->setConditions( $search->compare( '==', 'index.attribute.id', $attrItem->getId() ) ); |
113
|
|
|
$result2 = $this->object->searchItems( $search ); |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
$this->assertContains( $product->getId(), array_keys( $result ) ); |
117
|
|
|
$this->assertFalse( in_array( $product->getId(), array_keys( $result2 ) ) ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
public function testGetSubManager() |
122
|
|
|
{ |
123
|
|
|
$this->setExpectedException( \Aimeos\MShop\Exception::class ); |
124
|
|
|
$this->object->getSubManager( 'unknown' ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
public function testSearchItems() |
129
|
|
|
{ |
130
|
|
|
$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context ); |
131
|
|
|
$id = $attributeManager->findItem( '30', [], 'product', 'length' )->getId(); |
132
|
|
|
|
133
|
|
|
$search = $this->object->createSearch(); |
134
|
|
|
$search->setConditions( $search->compare( '==', 'index.attribute.id', $id ) ); |
135
|
|
|
$result = $this->object->searchItems( $search, [] ); |
136
|
|
|
|
137
|
|
|
$this->assertEquals( 3, count( $result ) ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
public function testSearchItemsNoId() |
142
|
|
|
{ |
143
|
|
|
$search = $this->object->createSearch(); |
144
|
|
|
$search->setConditions( $search->compare( '!=', 'index.attribute.id', null ) ); |
145
|
|
|
$result = $this->object->searchItems( $search, [] ); |
146
|
|
|
|
147
|
|
|
$this->assertGreaterThanOrEqual( 2, count( $result ) ); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
public function testSearchItemsAllof() |
152
|
|
|
{ |
153
|
|
|
$manager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context ); |
154
|
|
|
|
155
|
|
|
$attrIds = [ |
156
|
|
|
$manager->findItem( '30', [], 'product', 'length' )->getId(), |
157
|
|
|
$manager->findItem( '29', [], 'product', 'width' )->getId() |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
$search = $this->object->createSearch(); |
161
|
|
|
|
162
|
|
|
$func = $search->createFunction( 'index.attribute:allof', [$attrIds] ); |
163
|
|
|
$search->setConditions( $search->compare( '!=', $func, null ) ); |
164
|
|
|
$search->setSortations( array( $search->sort( '+', 'product.code' ) ) ); |
165
|
|
|
|
166
|
|
|
$result = $this->object->searchItems( $search, [] ); |
167
|
|
|
|
168
|
|
|
$this->assertEquals( 2, count( $result ) ); |
169
|
|
|
$this->assertEquals( 'CNE', reset( $result )->getCode() ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
public function testSearchItemsOneof() |
174
|
|
|
{ |
175
|
|
|
$manager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context ); |
176
|
|
|
|
177
|
|
|
$attrIds = [ |
178
|
|
|
$manager->findItem( '30', [], 'product', 'length' )->getId(), |
179
|
|
|
$manager->findItem( '30', [], 'product', 'width' )->getId() |
180
|
|
|
]; |
181
|
|
|
|
182
|
|
|
$search = $this->object->createSearch(); |
183
|
|
|
|
184
|
|
|
$func = $search->createFunction( 'index.attribute:oneof', [$attrIds] ); |
185
|
|
|
$search->setConditions( $search->compare( '!=', $func, null ) ); |
186
|
|
|
$search->setSortations( array( $search->sort( '+', 'product.code' ) ) ); |
187
|
|
|
|
188
|
|
|
$result = $this->object->searchItems( $search, [] ); |
189
|
|
|
|
190
|
|
|
$this->assertEquals( 3, count( $result ) ); |
191
|
|
|
$this->assertEquals( 'CNE', reset( $result )->getCode() ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
public function testCleanupIndex() |
196
|
|
|
{ |
197
|
|
|
$this->object->cleanupIndex( '1970-01-01 00:00:00' ); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|