Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 7 | class CategoryExtractorTest extends \Tests\ShopwarePlugins\Connect\ConnectTestHelper |
||
| 8 | { |
||
| 9 | const RANDOM_STRING = '9999abcxyz'; |
||
| 10 | /** |
||
| 11 | * @var \ShopwarePlugins\Connect\Components\CategoryExtractor |
||
| 12 | */ |
||
| 13 | private $categoryExtractor; |
||
| 14 | |||
| 15 | private $configurationGateway; |
||
| 16 | |||
| 17 | private $attributeRepository; |
||
| 18 | |||
| 19 | private $db; |
||
| 20 | private $em; |
||
| 21 | private $articleA; |
||
| 22 | private $articleB; |
||
| 23 | |||
| 24 | private function createArticleA() |
||
| 25 | { |
||
| 26 | $minimalTestArticle = array( |
||
| 27 | 'name' => 'TurnschuhA', |
||
| 28 | 'active' => true, |
||
| 29 | 'tax' => 19, |
||
| 30 | 'supplier' => 'Turnschuh Inc.', |
||
| 31 | 'categories' => array( |
||
| 32 | array('id' => 15), |
||
| 33 | ), |
||
| 34 | 'mainDetail' => array( |
||
| 35 | 'number' => '9898', |
||
| 36 | ), |
||
| 37 | ); |
||
| 38 | |||
| 39 | $articleResource = \Shopware\Components\Api\Manager::getResource('article'); |
||
| 40 | /** @var \Shopware\Models\Article\Article $article */ |
||
| 41 | $this->articleA = $articleResource->create($minimalTestArticle); |
||
| 42 | } |
||
| 43 | |||
| 44 | private function createArticleB() |
||
| 45 | { |
||
| 46 | $minimalTestArticle = array( |
||
| 47 | 'name' => 'TurnschuhB', |
||
| 48 | 'active' => true, |
||
| 49 | 'tax' => 19, |
||
| 50 | 'supplier' => 'Turnschuh Inc.', |
||
| 51 | 'categories' => array( |
||
| 52 | array('id' => 15), |
||
| 53 | ), |
||
| 54 | 'mainDetail' => array( |
||
| 55 | 'number' => '9897', |
||
| 56 | ), |
||
| 57 | ); |
||
| 58 | |||
| 59 | $articleResource = \Shopware\Components\Api\Manager::getResource('article'); |
||
| 60 | /** @var \Shopware\Models\Article\Article $article */ |
||
| 61 | $this->articleB = $articleResource->create($minimalTestArticle); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function setUp() |
||
| 65 | { |
||
| 66 | parent::setUp(); |
||
| 67 | |||
| 68 | $this->db = Shopware()->Db(); |
||
| 69 | $this->em = Shopware()->Models(); |
||
| 70 | |||
| 71 | $this->createArticleA(); |
||
| 72 | $this->createArticleB(); |
||
| 73 | |||
| 74 | /** @var \Shopware\Models\Article\Detail $detailA */ |
||
| 75 | $detailA = $this->articleA->getMainDetail(); |
||
| 76 | $detailB = $this->articleB->getMainDetail(); |
||
| 77 | $this->db->exec( |
||
| 78 | 'INSERT INTO s_plugin_connect_items (article_id, article_detail_id, shop_id, source_id, category) VALUES |
||
| 79 | (' . $this->articleA->getId() . ',' . $detailA->getId() .',1,' . $detailA->getNumber() .', "/bücher"), |
||
| 80 | (' . $this->articleB->getId() . ',' . $detailB->getId() .',1,' . $detailB->getNumber() .', "/bücher") |
||
| 81 | ' |
||
| 82 | ); |
||
| 83 | |||
| 84 | $categories = array( |
||
| 85 | array('category_key' => '/Ski-unit', 'label' => 'Ski'), |
||
| 86 | array('category_key' => '/Kleidung-unit', 'label' => 'Kleidung'), |
||
| 87 | array('category_key' => '/Kleidung-unit/Hosen-unit', 'label' => 'Hosen', 'local_category_id' => 1), |
||
| 88 | array('category_key' => '/Kleidung-unit/Hosen-unit/Hosentraeger-unit', 'label' => 'Hosentraeger'), |
||
| 89 | ); |
||
| 90 | |||
| 91 | //inserts a category with map product |
||
| 92 | $this->db->exec(" |
||
| 93 | INSERT INTO `s_plugin_connect_categories`(`category_key`, `label`, `local_category_id`) VALUES |
||
| 94 | ('/Kleidung-unit/SchuheA-unit','SchuheA', NULL) |
||
| 95 | "); |
||
| 96 | $this->db->exec(" |
||
| 97 | INSERT INTO `s_plugin_connect_categories`(`category_key`, `label`, `local_category_id`) VALUES |
||
| 98 | ('/Kleidung-unit/SchuheA-unit/SchuheB-unit','SchuheB', NULL) |
||
| 99 | "); |
||
| 100 | $this->db->insert( |
||
| 101 | 's_plugin_connect_product_to_categories', |
||
| 102 | array( |
||
| 103 | 'connect_category_id' => $this->db->lastInsertId(), |
||
| 104 | 'articleID' => $this->articleB->getId(), |
||
| 105 | ) |
||
| 106 | ); |
||
| 107 | $this->db->update( |
||
| 108 | 's_articles_attributes', |
||
| 109 | array('connect_mapped_category' => 1), |
||
| 110 | array('articleID=' . $this->articleB->getId()) |
||
| 111 | ); |
||
| 112 | |||
| 113 | // todo@sb: Improve me with mocks |
||
| 114 | foreach ($categories as $category) { |
||
| 115 | $this->db->insert('s_plugin_connect_categories', $category); |
||
| 116 | $categoryId = $this->db->lastInsertId(); |
||
| 117 | |||
| 118 | $this->db->insert('s_plugin_connect_product_to_categories', array( |
||
| 119 | 'connect_category_id' => $categoryId, |
||
| 120 | 'articleID' => $this->articleA->getId(), |
||
| 121 | )); |
||
| 122 | } |
||
| 123 | |||
| 124 | $this->configurationGateway = $this->getMockBuilder('\\Shopware\\Connect\\Gateway\\PDO') |
||
| 125 | ->disableOriginalConstructor() |
||
| 126 | ->getMock(); |
||
| 127 | |||
| 128 | $this->attributeRepository = $this->getMockBuilder('\\Shopware\\CustomModels\\Connect\\AttributeRepository') |
||
| 129 | ->disableOriginalConstructor() |
||
| 130 | ->getMock(); |
||
| 131 | |||
| 132 | $randomStringGenerator = $this->getMockBuilder(RandomStringGenerator::class) |
||
| 133 | ->disableOriginalConstructor() |
||
| 134 | ->getMock(); |
||
| 135 | |||
| 136 | $this->categoryExtractor = new \ShopwarePlugins\Connect\Components\CategoryExtractor( |
||
| 137 | $this->attributeRepository, |
||
| 138 | new \ShopwarePlugins\Connect\Components\CategoryResolver\AutoCategoryResolver( |
||
| 139 | $this->em, |
||
| 140 | $this->em->getRepository('Shopware\Models\Category\Category'), |
||
| 141 | $this->em->getRepository('Shopware\CustomModels\Connect\RemoteCategory'), |
||
| 142 | new \ShopwarePlugins\Connect\Components\Config($this->em) |
||
| 143 | ), |
||
| 144 | $this->configurationGateway, |
||
| 145 | $randomStringGenerator |
||
| 146 | ); |
||
| 147 | |||
| 148 | $randomStringGenerator->expects($this->any()) |
||
| 149 | ->method('generate') |
||
| 150 | ->willReturn(self::RANDOM_STRING); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function tearDown() |
||
| 154 | { |
||
| 155 | $this->db->exec("DELETE FROM `s_plugin_connect_categories`"); |
||
| 156 | $this->db->exec("DELETE FROM `s_plugin_connect_product_to_categories`"); |
||
| 157 | $this->db->exec('DELETE FROM sw_connect_change WHERE c_entity_id LIKE "9898%"'); |
||
| 158 | $this->db->exec('DELETE FROM sw_connect_change WHERE c_entity_id LIKE "9897%"'); |
||
| 159 | $this->db->exec('DELETE FROM s_articles WHERE name = "Turnschuh"'); |
||
| 160 | $this->db->exec('DELETE FROM s_articles_details WHERE ordernumber LIKE "9898%"'); |
||
| 161 | $this->db->exec('DELETE FROM s_plugin_connect_items WHERE source_id LIKE "9898%"'); |
||
| 162 | $this->db->exec('DELETE FROM s_articles_details WHERE ordernumber LIKE "9897%"'); |
||
| 163 | $this->db->exec('DELETE FROM s_plugin_connect_items WHERE source_id LIKE "9897%"'); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function testExtractImportedCategories() |
||
| 167 | { |
||
| 168 | $attribute1 = new \Shopware\CustomModels\Connect\Attribute(); |
||
| 169 | $attribute1->setCategory(array('/Ski' => 'Ski')); |
||
| 170 | $attribute1->setStream('Awesome products'); |
||
| 171 | |||
| 172 | $attribute2 = new \Shopware\CustomModels\Connect\Attribute(); |
||
| 173 | $attribute2->setCategory(array( |
||
| 174 | '/Kleidung' => 'Kleidung', |
||
| 175 | '/Kleidung/Hosen' => 'Hosen', |
||
| 176 | '/Kleidung/Hosentraeger' => 'Hosentraeger', |
||
| 177 | )); |
||
| 178 | $attribute2->setStream('Awesome products'); |
||
| 179 | |||
| 180 | $attribute3 = new \Shopware\CustomModels\Connect\Attribute(); |
||
| 181 | $attribute3->setCategory(array( |
||
| 182 | '/Kleidung/Hosentraeger' => 'Hosentraeger', |
||
| 183 | '/Kleidung/Nahrung & Getraenke' => 'Nahrung & Getraenke', |
||
| 184 | '/Kleidung/Nahrung & Getraenke/Alkoholische Getränke' => 'Alkoholische Getränke', |
||
| 185 | )); |
||
| 186 | $attribute3->setStream('Awesome products'); |
||
| 187 | |||
| 188 | $this->attributeRepository->expects($this->once()) |
||
| 189 | ->method('findRemoteArticleAttributes') |
||
| 190 | ->willReturn(array( |
||
| 191 | $attribute1, |
||
| 192 | $attribute2, |
||
| 193 | $attribute3, |
||
| 194 | )); |
||
| 195 | |||
| 196 | $expected = array( |
||
| 197 | array( |
||
| 198 | 'name' => 'Ski', |
||
| 199 | 'id' => self::RANDOM_STRING, |
||
| 200 | 'categoryId' => '/Ski', |
||
| 201 | 'leaf' => true, |
||
| 202 | 'children' => array(), |
||
| 203 | 'cls' => "sc-tree-node", |
||
| 204 | 'expanded' => false, |
||
| 205 | ), |
||
| 206 | array( |
||
| 207 | 'name' => 'Kleidung', |
||
| 208 | 'id' => self::RANDOM_STRING, |
||
| 209 | 'categoryId' => '/Kleidung', |
||
| 210 | 'leaf' => false, |
||
| 211 | 'children' => array( |
||
| 212 | array( |
||
| 213 | 'name' => 'Hosen', |
||
| 214 | 'id' => self::RANDOM_STRING, |
||
| 215 | 'categoryId' => '/Kleidung/Hosen', |
||
| 216 | 'leaf' => true, |
||
| 217 | 'children' => array(), |
||
| 218 | 'cls' => "sc-tree-node", |
||
| 219 | 'expanded' => false, |
||
| 220 | ), |
||
| 221 | array( |
||
| 222 | 'name' => 'Hosentraeger', |
||
| 223 | 'id' => self::RANDOM_STRING, |
||
| 224 | 'categoryId' => '/Kleidung/Hosentraeger', |
||
| 225 | 'leaf' => true, |
||
| 226 | 'children' => array(), |
||
| 227 | 'cls' => "sc-tree-node", |
||
| 228 | 'expanded' => false, |
||
| 229 | ), |
||
| 230 | array( |
||
| 231 | 'name' => 'Nahrung & Getraenke', |
||
| 232 | 'id' => self::RANDOM_STRING, |
||
| 233 | 'categoryId' => '/Kleidung/Nahrung & Getraenke', |
||
| 234 | 'leaf' => false, |
||
| 235 | 'children' => array( |
||
| 236 | array( |
||
| 237 | 'name' => 'Alkoholische Getränke', |
||
| 238 | 'id' => self::RANDOM_STRING, |
||
| 239 | 'categoryId' => '/Kleidung/Nahrung & Getraenke/Alkoholische Getränke', |
||
| 240 | 'leaf' => true, |
||
| 241 | 'children' => array(), |
||
| 242 | 'cls' => "sc-tree-node", |
||
| 243 | 'expanded' => false, |
||
| 244 | ), |
||
| 245 | ), |
||
| 246 | 'cls' => "sc-tree-node", |
||
| 247 | 'expanded' => false, |
||
| 248 | ) |
||
| 249 | ), |
||
| 250 | 'cls' => "sc-tree-node", |
||
| 251 | 'expanded' => false, |
||
| 252 | ), |
||
| 253 | ); |
||
| 254 | |||
| 255 | |||
| 256 | $result = $this->categoryExtractor->extractImportedCategories(); |
||
| 257 | $this->assertTrue(is_array($result), 'Extracted categories must be array'); |
||
| 258 | $this->assertEquals($expected, $result); |
||
| 259 | } |
||
| 260 | |||
| 261 | public function testGetShopNamesAsCategoriesTree() |
||
| 262 | { |
||
| 263 | $this->configurationGateway->expects($this->once()) |
||
| 264 | ->method('getConnectedShopIds') |
||
| 265 | ->willReturn(array(1, 2, 5,)); |
||
| 266 | |||
| 267 | $this->configurationGateway->expects($this->at(1)) |
||
| 268 | ->method('getShopConfiguration') |
||
| 269 | ->with(1) |
||
| 270 | ->willReturn( |
||
| 271 | new \Shopware\Connect\Struct\ShopConfiguration(array( |
||
| 272 | 'displayName' => 'Shop 1' |
||
| 273 | )) |
||
| 274 | ); |
||
| 275 | |||
| 276 | $expected = array( |
||
| 277 | array( |
||
| 278 | 'id' => self::RANDOM_STRING, |
||
| 279 | 'categoryId' => 1, |
||
| 280 | 'name' => 'Shop 1', |
||
| 281 | 'leaf' => false, |
||
| 282 | 'children' => array(), |
||
| 283 | 'iconCls' => 'sc-tree-node-icon', |
||
| 284 | 'cls' => "sc-tree-node", |
||
| 285 | 'expanded' => false, |
||
| 286 | ), |
||
| 287 | ); |
||
| 288 | $result = $this->categoryExtractor->getMainNodes(); |
||
| 289 | $this->assertEquals($expected, $result); |
||
| 290 | } |
||
| 291 | |||
| 292 | public function testGetTreeWithoutParent() |
||
| 293 | { |
||
| 294 | $expected = array( |
||
| 295 | array( |
||
| 296 | 'id' => self::RANDOM_STRING, |
||
| 297 | 'categoryId' => '/Ski-unit', |
||
| 298 | 'name' => 'Ski', |
||
| 299 | 'leaf' => true, |
||
| 300 | 'children' => array(), |
||
| 301 | 'cls' => "sc-tree-node", |
||
| 302 | 'expanded' => false |
||
| 303 | ), |
||
| 304 | array( |
||
| 305 | 'id' => self::RANDOM_STRING, |
||
| 306 | 'categoryId' => '/Kleidung-unit', |
||
| 307 | 'name' => 'Kleidung', |
||
| 308 | 'leaf' => false, |
||
| 309 | 'children' => array(), |
||
| 310 | 'cls' => "sc-tree-node", |
||
| 311 | 'expanded' => false |
||
| 312 | ), |
||
| 313 | ); |
||
| 314 | $result = $this->categoryExtractor->getRemoteCategoriesTree(); |
||
| 315 | $this->assertEquals($expected, $result); |
||
| 316 | } |
||
| 317 | |||
| 318 | public function testGetTreeWithoutParentAndIncludeChildren() |
||
| 319 | { |
||
| 320 | $expected = array( |
||
| 321 | array( |
||
| 322 | 'id' => self::RANDOM_STRING, |
||
| 323 | 'categoryId' => '/Ski-unit', |
||
| 324 | 'name' => 'Ski', |
||
| 325 | 'leaf' => true, |
||
| 326 | 'children' => array(), |
||
| 327 | 'cls' => "sc-tree-node", |
||
| 328 | 'expanded' => false |
||
| 329 | ), |
||
| 330 | array( |
||
| 331 | 'id' => self::RANDOM_STRING, |
||
| 332 | 'categoryId' => '/Kleidung-unit', |
||
| 333 | 'name' => 'Kleidung', |
||
| 334 | 'leaf' => false, |
||
| 335 | 'children' => array( |
||
| 336 | array( |
||
| 337 | 'id' => self::RANDOM_STRING, |
||
| 338 | 'categoryId' => '/Kleidung-unit/Hosen-unit', |
||
| 339 | 'name' => 'Hosen', |
||
| 340 | 'leaf' => false, |
||
| 341 | 'children' => array( |
||
| 342 | array( |
||
| 343 | 'id' => self::RANDOM_STRING, |
||
| 344 | 'categoryId' => '/Kleidung-unit/Hosen-unit/Hosentraeger-unit', |
||
| 345 | 'name' => 'Hosentraeger', |
||
| 346 | 'leaf' => true, |
||
| 347 | 'children' => array(), |
||
| 348 | 'cls' => "sc-tree-node", |
||
| 349 | 'expanded' => false |
||
| 350 | ), |
||
| 351 | ), |
||
| 352 | 'cls' => "sc-tree-node", |
||
| 353 | 'expanded' => false |
||
| 354 | ), |
||
| 355 | ), |
||
| 356 | 'cls' => "sc-tree-node", |
||
| 357 | 'expanded' => false |
||
| 358 | ), |
||
| 359 | ); |
||
| 360 | $parent = null; |
||
| 361 | $includeChildren = true; |
||
| 362 | $result = $this->categoryExtractor->getRemoteCategoriesTree($parent, $includeChildren); |
||
| 363 | $this->assertEquals($expected, $result); |
||
| 364 | } |
||
| 365 | |||
| 366 | public function testGetTreeWithoutParentAndExcludeMapped() |
||
| 367 | { |
||
| 368 | $expected = array( |
||
| 369 | array( |
||
| 370 | 'id' => self::RANDOM_STRING, |
||
| 371 | 'categoryId' => '/Ski-unit', |
||
| 372 | 'name' => 'Ski', |
||
| 373 | 'leaf' => true, |
||
| 374 | 'children' => array(), |
||
| 375 | 'cls' => "sc-tree-node", |
||
| 376 | 'expanded' => false |
||
| 377 | ), |
||
| 378 | array( |
||
| 379 | 'id' => self::RANDOM_STRING, |
||
| 380 | 'categoryId' => '/Kleidung-unit', |
||
| 381 | 'name' => 'Kleidung', |
||
| 382 | 'leaf' => false, |
||
| 383 | 'children' => array( |
||
| 384 | array( |
||
| 385 | 'id' => self::RANDOM_STRING, |
||
| 386 | 'categoryId' => '/Kleidung-unit/Hosen-unit', |
||
| 387 | 'name' => 'Hosen', |
||
| 388 | 'leaf' => false, |
||
| 389 | 'children' => array( |
||
| 390 | array( |
||
| 391 | 'id' => self::RANDOM_STRING, |
||
| 392 | 'categoryId' => '/Kleidung-unit/Hosen-unit/Hosentraeger-unit', |
||
| 393 | 'name' => 'Hosentraeger', |
||
| 394 | 'leaf' => true, |
||
| 395 | 'children' => array(), |
||
| 396 | 'cls' => "sc-tree-node", |
||
| 397 | 'expanded' => false |
||
| 398 | ), |
||
| 399 | ), |
||
| 400 | 'cls' => "sc-tree-node", |
||
| 401 | 'expanded' => false |
||
| 402 | ), |
||
| 403 | ), |
||
| 404 | 'cls' => "sc-tree-node", |
||
| 405 | 'expanded' => false |
||
| 406 | ), |
||
| 407 | ); |
||
| 408 | $parent = null; |
||
| 409 | $includeChildren = true; |
||
| 410 | $excludeMapped = true; |
||
| 411 | $result = $this->categoryExtractor->getRemoteCategoriesTree($parent, $includeChildren, $excludeMapped); |
||
| 412 | $this->assertEquals($expected, $result); |
||
| 413 | } |
||
| 414 | |||
| 415 | public function testGetTreeWithParent() |
||
| 416 | { |
||
| 417 | $expected = array( |
||
| 418 | array( |
||
| 419 | 'id' => self::RANDOM_STRING, |
||
| 420 | 'categoryId' => '/Kleidung-unit/Hosen-unit', |
||
| 421 | 'name' => 'Hosen', |
||
| 422 | 'leaf' => false, |
||
| 423 | 'children' => array(), |
||
| 424 | 'cls' => "sc-tree-node", |
||
| 425 | 'expanded' => false |
||
| 426 | ), |
||
| 427 | ); |
||
| 428 | $parent = '/Kleidung-unit'; |
||
| 429 | $result = $this->categoryExtractor->getRemoteCategoriesTree($parent); |
||
| 430 | $this->assertEquals($expected, $result); |
||
| 431 | } |
||
| 432 | |||
| 433 | public function testGetTreeWithParentAndIncludeChildren() |
||
| 434 | { |
||
| 435 | $expected = array( |
||
| 436 | array( |
||
| 437 | 'id' => self::RANDOM_STRING, |
||
| 438 | 'categoryId' => '/Kleidung-unit/Hosen-unit', |
||
| 439 | 'name' => 'Hosen', |
||
| 440 | 'leaf' => false, |
||
| 441 | 'children' => array( |
||
| 442 | array( |
||
| 443 | 'id' => self::RANDOM_STRING, |
||
| 444 | 'categoryId' => '/Kleidung-unit/Hosen-unit/Hosentraeger-unit', |
||
| 445 | 'name' => 'Hosentraeger', |
||
| 446 | 'leaf' => true, |
||
| 447 | 'children' => array(), |
||
| 448 | 'cls' => "sc-tree-node", |
||
| 449 | 'expanded' => false |
||
| 450 | ), |
||
| 451 | ), |
||
| 452 | 'cls' => "sc-tree-node", |
||
| 453 | 'expanded' => false |
||
| 454 | ), |
||
| 455 | ); |
||
| 456 | $parent = '/Kleidung-unit'; |
||
| 457 | $includeChildren = true; |
||
| 458 | $result = $this->categoryExtractor->getRemoteCategoriesTree($parent, $includeChildren); |
||
| 459 | $this->assertEquals($expected, $result); |
||
| 460 | } |
||
| 461 | |||
| 462 | public function testGetTreeWithParentAndExcludeMapped() |
||
| 463 | { |
||
| 464 | $parent = '/Kleidung-unit/SchuheA-unit'; |
||
| 465 | $includeChildren = true; |
||
| 466 | $excludeMapped = true; |
||
| 467 | $result = $this->categoryExtractor->getRemoteCategoriesTree($parent, $includeChildren, $excludeMapped); |
||
| 468 | $this->assertEmpty($result); |
||
| 469 | } |
||
| 470 | |||
| 471 | public function testExtractByShopId() |
||
| 472 | { |
||
| 473 | $product = $this->getProduct(); |
||
| 474 | $product->categories = array( |
||
| 475 | '/Ski-unit' => 'Ski', |
||
| 476 | '/Kleidung-unit' => 'Kleidung', |
||
| 477 | '/Kleidung-unit/Hosen-unit' => 'Hosen', |
||
| 478 | '/Kleidung-unit/Hosen-unit/Hosentraeger-unit' => 'Hosentraeger', |
||
| 479 | |||
| 480 | ); |
||
| 481 | |||
| 482 | $this->getProductToShop()->insertOrUpdate($product); |
||
| 483 | |||
| 484 | $expected = array( |
||
| 485 | array( |
||
| 486 | 'id' => self::RANDOM_STRING, |
||
| 487 | 'categoryId' => '/Ski-unit', |
||
| 488 | 'name' => 'Ski', |
||
| 489 | 'leaf' => true, |
||
| 490 | 'children' => array(), |
||
| 491 | 'cls' => "sc-tree-node", |
||
| 492 | 'expanded' => false |
||
| 493 | ), |
||
| 494 | array( |
||
| 495 | 'id' => self::RANDOM_STRING, |
||
| 496 | 'categoryId' => '/Kleidung-unit', |
||
| 497 | 'name' => 'Kleidung', |
||
| 498 | 'leaf' => false, |
||
| 499 | 'children' => array(), |
||
| 500 | 'cls' => "sc-tree-node", |
||
| 501 | 'expanded' => false |
||
| 502 | ), |
||
| 503 | ); |
||
| 504 | |||
| 505 | $result = $this->categoryExtractor->extractByShopId(3); |
||
| 506 | $this->assertEquals($expected, $result); |
||
| 507 | } |
||
| 508 | |||
| 509 | public function testExtractByShopIdAndIncludeChildren() |
||
| 510 | { |
||
| 511 | $product = $this->getProduct(); |
||
| 512 | $product->categories = array( |
||
| 513 | '/Ski-unit' => 'Ski', |
||
| 514 | '/Kleidung-unit' => 'Kleidung', |
||
| 515 | '/Kleidung-unit/Hosen-unit' => 'Hosen', |
||
| 516 | '/Kleidung-unit/Hosen-unit/Hosentraeger-unit' => 'Hosentraeger', |
||
| 517 | |||
| 518 | ); |
||
| 519 | |||
| 520 | $this->getProductToShop()->insertOrUpdate($product); |
||
| 521 | |||
| 522 | $expected = array( |
||
| 523 | array( |
||
| 524 | 'id' => self::RANDOM_STRING, |
||
| 525 | 'categoryId' => '/Ski-unit', |
||
| 526 | 'name' => 'Ski', |
||
| 527 | 'leaf' => true, |
||
| 528 | 'children' => array(), |
||
| 529 | 'cls' => "sc-tree-node", |
||
| 530 | 'expanded' => false |
||
| 531 | ), |
||
| 532 | array( |
||
| 533 | 'id' => self::RANDOM_STRING, |
||
| 534 | 'categoryId' => '/Kleidung-unit', |
||
| 535 | 'name' => 'Kleidung', |
||
| 536 | 'leaf' => false, |
||
| 537 | 'children' => array( |
||
| 538 | array( |
||
| 539 | 'id' => self::RANDOM_STRING, |
||
| 540 | 'categoryId' => '/Kleidung-unit/Hosen-unit', |
||
| 541 | 'name' => 'Hosen', |
||
| 542 | 'leaf' => false, |
||
| 543 | 'children' => array( |
||
| 544 | array( |
||
| 545 | 'id' => self::RANDOM_STRING, |
||
| 546 | 'categoryId' => '/Kleidung-unit/Hosen-unit/Hosentraeger-unit', |
||
| 547 | 'name' => 'Hosentraeger', |
||
| 548 | 'leaf' => true, |
||
| 549 | 'children' => array(), |
||
| 550 | 'cls' => "sc-tree-node", |
||
| 551 | 'expanded' => false |
||
| 552 | ), |
||
| 553 | ), |
||
| 554 | 'cls' => "sc-tree-node", |
||
| 555 | 'expanded' => false |
||
| 556 | ), |
||
| 557 | ), |
||
| 558 | 'cls' => "sc-tree-node", |
||
| 559 | 'expanded' => false |
||
| 560 | ), |
||
| 561 | ); |
||
| 562 | |||
| 563 | $result = $this->categoryExtractor->extractByShopId(3, $includeChildren = true); |
||
| 564 | $this->assertEquals($expected, $result); |
||
| 565 | } |
||
| 566 | |||
| 567 | public function testGetStreamByShopId() |
||
| 568 | { |
||
| 569 | $shopId = 1; |
||
| 570 | for ($i=0; $i < 3; $i++) { |
||
| 571 | $product = $this->getProduct(); |
||
| 572 | $product->shopId = $shopId; |
||
| 573 | $product->stream = 'Awesome products'; |
||
| 574 | |||
| 575 | $this->getProductToShop()->insertOrUpdate($product); |
||
| 576 | } |
||
| 577 | |||
| 578 | for ($i=0; $i < 3; $i++) { |
||
| 579 | $product = $this->getProduct(); |
||
| 580 | $product->shopId = $shopId; |
||
| 581 | $product->stream = 'Mobile devices'; |
||
| 582 | |||
| 583 | $this->getProductToShop()->insertOrUpdate($product); |
||
| 584 | } |
||
| 585 | |||
| 586 | $streams = $this->categoryExtractor->getStreamsByShopId($shopId); |
||
| 587 | $expected = array( |
||
| 588 | array( |
||
| 589 | 'id' => self::RANDOM_STRING, |
||
| 590 | 'categoryId' => '1_stream_Awesome products', |
||
| 591 | 'name' => 'Awesome products', |
||
| 592 | 'leaf' => false, |
||
| 593 | 'children' => array(), |
||
| 594 | 'iconCls' => 'sprite-product-streams', |
||
| 595 | 'cls' => "sc-tree-node", |
||
| 596 | 'expanded' => false |
||
| 597 | ), |
||
| 598 | array( |
||
| 599 | 'id' => self::RANDOM_STRING, |
||
| 600 | 'categoryId' => '1_stream_Mobile devices', |
||
| 601 | 'name' => 'Mobile devices', |
||
| 602 | 'leaf' => false, |
||
| 603 | 'children' => array(), |
||
| 604 | 'iconCls' => 'sprite-product-streams', |
||
| 605 | 'cls' => "sc-tree-node", |
||
| 606 | 'expanded' => false |
||
| 607 | ), |
||
| 608 | ); |
||
| 609 | |||
| 610 | $this->assertEquals($expected, $streams); |
||
| 611 | } |
||
| 612 | |||
| 613 | public function testGetRemoteCategoriesTreeByStream() |
||
| 614 | { |
||
| 615 | $shopId = 3; |
||
| 616 | $stream = 'Media devices'; |
||
| 617 | for ($i=0; $i < 5; $i++) { |
||
| 618 | $product = $this->getProduct(); |
||
| 619 | $product->shopId = $shopId; |
||
| 620 | $product->stream = $stream; |
||
| 621 | $product->categories = array( |
||
| 622 | '/Ski-unit' => 'Ski', |
||
| 623 | '/Kleidung-unit' => 'Kleidung', |
||
| 624 | '/Kleidung-unit/Hosen-unit' => 'Hosen', |
||
| 625 | '/Kleidung-unit/Hosen-unit/Hosentraeger-unit' => 'Hosentraeger', |
||
| 626 | |||
| 627 | ); |
||
| 628 | |||
| 629 | $this->getProductToShop()->insertOrUpdate($product); |
||
| 630 | } |
||
| 631 | |||
| 632 | $expected = array( |
||
| 633 | array( |
||
| 634 | 'id' => self::RANDOM_STRING, |
||
| 635 | 'categoryId' => '/Ski-unit', |
||
| 636 | 'name' => 'Ski', |
||
| 637 | 'leaf' => true, |
||
| 638 | 'children' => array(), |
||
| 639 | 'cls' => "sc-tree-node", |
||
| 640 | 'expanded' => false |
||
| 641 | ), |
||
| 642 | array( |
||
| 643 | 'id' => self::RANDOM_STRING, |
||
| 644 | 'categoryId' => '/Kleidung-unit', |
||
| 645 | 'name' => 'Kleidung', |
||
| 646 | 'leaf' => false, |
||
| 647 | 'children' => array(), |
||
| 648 | 'cls' => "sc-tree-node", |
||
| 649 | 'expanded' => false |
||
| 650 | ), |
||
| 651 | ); |
||
| 652 | |||
| 653 | $remoteCategories = $this->categoryExtractor->getRemoteCategoriesTreeByStream($stream, $shopId); |
||
| 654 | $this->assertEquals($expected, $remoteCategories); |
||
| 655 | } |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Test concat shopId, categoryId and unique number |
||
| 659 | */ |
||
| 660 | public function testConcatShopIdAndCategoryId() |
||
| 661 | { |
||
| 662 | $shopId = 1; |
||
| 663 | |||
| 664 | $randomStringGenerator = $this->getMockBuilder(RandomStringGenerator::class) |
||
| 665 | ->disableOriginalConstructor() |
||
| 666 | ->getMock(); |
||
| 667 | |||
| 668 | $categoryExtractor = new \ShopwarePlugins\Connect\Components\CategoryExtractor( |
||
| 669 | $this->attributeRepository, |
||
| 670 | new \ShopwarePlugins\Connect\Components\CategoryResolver\AutoCategoryResolver( |
||
| 671 | $this->em, |
||
| 672 | $this->em->getRepository('Shopware\Models\Category\Category'), |
||
| 673 | $this->em->getRepository('Shopware\CustomModels\Connect\RemoteCategory'), |
||
| 674 | new \ShopwarePlugins\Connect\Components\Config($this->em) |
||
| 675 | ), |
||
| 676 | $this->configurationGateway, |
||
| 677 | $randomStringGenerator |
||
| 678 | ); |
||
| 679 | |||
| 680 | $argument = sprintf('shopId%s~%s', $shopId, '/Kleidung-unit/Hosen-unit/Hosentraeger-unit'); |
||
| 681 | $randomStringGenerator->expects($this->once()) |
||
| 682 | ->method('generate') |
||
| 683 | ->with($argument) |
||
| 684 | ->willReturn($argument . '1040'); |
||
| 685 | |||
| 686 | $parent = '/Kleidung-unit/Hosen-unit'; |
||
| 687 | $includeChildren = true; |
||
| 688 | $result = $categoryExtractor->getRemoteCategoriesTree($parent, $includeChildren, false, $shopId); |
||
| 689 | $this->assertEquals( |
||
| 690 | [ |
||
| 691 | [ |
||
| 692 | 'id' => 'shopId1~/Kleidung-unit/Hosen-unit/Hosentraeger-unit1040', |
||
| 693 | 'categoryId' => '/Kleidung-unit/Hosen-unit/Hosentraeger-unit', |
||
| 694 | 'name' => 'Hosentraeger', |
||
| 695 | 'leaf' => true, |
||
| 696 | 'children' => [], |
||
| 697 | 'cls' => "sc-tree-node", |
||
| 698 | 'expanded' => false |
||
| 699 | ] |
||
| 700 | ], |
||
| 701 | $result |
||
| 702 | ); |
||
| 703 | } |
||
| 704 | } |
||
| 705 |