Completed
Pull Request — master (#336)
by Stefan
05:11
created

AutoCategoryResolverTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 139
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Tests\ShopwarePlugins\Connect\Component\CategoryResolver;
4
5
use ShopwarePlugins\Connect\Components\CategoryResolver\AutoCategoryResolver;
6
use Tests\ShopwarePlugins\Connect\ConnectTestHelper;
7
use Shopware\Models\Category\Category;
8
9
class AutoCategoryResolverTest extends ConnectTestHelper
10
{
11
    /** @var  \ShopwarePlugins\Connect\Components\CategoryResolver\AutoCategoryResolver */
12
    private $categoryResolver;
13
    private $manager;
14
    private $categoryRepo;
15
16
    /** @var \ShopwarePlugins\Connect\Components\Config   */
17
    private $config;
18
19
    public function setUp()
20
    {
21
        parent::setUp();
22
23
        $this->manager = Shopware()->Models();
24
        $this->config = new \ShopwarePlugins\Connect\Components\Config($this->manager);
25
        $this->categoryRepo = $this->manager->getRepository('Shopware\Models\Category\Category');
26
27
        $this->categoryResolver = new AutoCategoryResolver(
28
            $this->manager,
29
            $this->categoryRepo,
30
            $this->manager->getRepository('Shopware\CustomModels\Connect\RemoteCategory'),
31
            $this->config
32
        );
33
    }
34
35
    public function testResolve()
36
    {
37
        $defaultCategoryId = $this->config->getDefaultShopCategory()->getId();
38
        $defaultCategory = $this->categoryRepo->findOneBy([
39
            'id' => $defaultCategoryId
40
        ]);
41
42
        $nikeCategory = $this->categoryRepo->findOneBy([
43
            'name' => 'Nike',
44
            'parentId' => $defaultCategoryId
45
        ]);
46
47
        if (!$nikeCategory) {
48
            $nikeCategory = $this->categoryResolver->convertNodeToEntity([
49
                'name' => 'Nike',
50
                'categoryId' => '/spanish/nike'
51
            ], $defaultCategory);
52
        }
53
54
        $nikeBootsCategory = $this->categoryRepo->findOneBy([
55
            'name' => 'Boots',
56
            'parentId' => $nikeCategory
57
        ]);
58
59
        if (!$nikeBootsCategory){
60
            $nikeBootsCategory = $this->categoryResolver->convertNodeToEntity([
61
                'name' => 'Boots',
62
                'categoryId' => '/spanish/nike/boots'
63
            ], $nikeCategory);
64
        }
65
66
        $categories = [
67
            '/spanish/nike/tshirts' => 'Tshirts',
68
            '/spanish/nike' => 'Nike',
69
            '/spanish/adidas/boots' => 'Boots',
70
            '/spanish/adidas' => 'Adidas',
71
            '/spanish' => 'Spanish',
72
        ];
73
74
        $categoryModels = $this->categoryResolver->resolve($categories);
75
76
        //Spanish category must not be created
77
        $this->assertNull($this->categoryRepo->findOneByName('Spanish'));
78
        $this->assertEquals(
79
            $defaultCategoryId,
80
            Shopware()->Db()->fetchOne('SELECT parent FROM s_categories WHERE description = ?', array('Adidas'))
81
        );
82
        // only addidas/boots and nike/tshirts must be returned - CON-4549.
83
        $this->assertCount(2, $categoryModels);
84
        $this->assertEquals($nikeCategory->getId(), $categoryModels[0]->getParent()->getId());
85
        $this->assertEquals('Tshirts', $categoryModels[0]->getName());
86
        $this->assertEquals('Adidas', $categoryModels[1]->getParent()->getName());
87
        $this->assertEquals('Boots', $categoryModels[1]->getName());
88
89
        Shopware()->Db()->exec('DELETE FROM s_categories WHERE description IN ("'. implode('","', $categories) .'")');
90
    }
91
92
    public function testGenerateTree()
93
    {
94
        $categories = array(
95
            '/Kleidung' => 'Kleidung',
96
            '/Kleidung/Hosen' => 'Hosen',
97
            '/Kleidung/Hosen/Hosentraeger' => 'Hosenträger',
98
            '/Nahrung & Getraenke' => 'Nahrung & Getränke',
99
            '/Nahrung & Getraenke/Alkoholische Getraenke' => 'Alkoholische Getränke',
100
            '/Nahrung & Getraenke/Alkoholische Getraenke/Bier' => 'Bier',
101
        );
102
103
        $expected = array(
104
            '/Kleidung' => array(
105
                'name' => 'Kleidung',
106
                'categoryId' => '/Kleidung',
107
                'leaf' => false,
108
                'children' => array(
109
                    '/Kleidung/Hosen' => array(
110
                        'name' => 'Hosen',
111
                        'categoryId' => '/Kleidung/Hosen',
112
                        'leaf' => false,
113
                        'children' => array(
114
                            '/Kleidung/Hosen/Hosentraeger' => array(
115
                                'name' => 'Hosenträger',
116
                                'categoryId' => '/Kleidung/Hosen/Hosentraeger',
117
                                'leaf' => true,
118
                                'children' => array(),
119
                            )
120
                        ),
121
                    ),
122
                ),
123
            ),
124
            '/Nahrung & Getraenke' => array(
125
                'name' => 'Nahrung & Getränke',
126
                'categoryId' => '/Nahrung & Getraenke',
127
                'leaf' => false,
128
                'children' => array(
129
                    '/Nahrung & Getraenke/Alkoholische Getraenke' => array(
130
                        'name' => 'Alkoholische Getränke',
131
                        'categoryId' => '/Nahrung & Getraenke/Alkoholische Getraenke',
132
                        'leaf' => false,
133
                        'children' => array(
134
                            '/Nahrung & Getraenke/Alkoholische Getraenke/Bier' => array(
135
                                'name' => 'Bier',
136
                                'categoryId' => '/Nahrung & Getraenke/Alkoholische Getraenke/Bier',
137
                                'leaf' => true,
138
                                'children' => array(),
139
                            ),
140
                        ),
141
                    ),
142
                ),
143
            ),
144
        );
145
        $this->assertEquals($expected, $this->categoryResolver->generateTree($categories));
146
    }
147
}
148