AttributeTypeActionTest::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the module AttributeType                                */
4
/*                                                                                   */
5
/*      For the full copyright and license information, please view the LICENSE.txt  */
6
/*      file that was distributed with this source code.                             */
7
/*************************************************************************************/
8
9
namespace AttributeType\Tests;
10
11
use AttributeType\Action\AttributeTypeAction;
12
use AttributeType\Event\AttributeTypeAvMetaEvent;
13
use AttributeType\Event\AttributeTypeEvent;
14
use AttributeType\Model\AttributeAttributeTypeQuery;
15
use AttributeType\Model\AttributeTypeAvMeta;
16
use AttributeType\Model\AttributeTypeAvMetaQuery;
17
use Thelia\Model\Attribute;
18
use Thelia\Model\AttributeAv;
19
use Thelia\Model\AttributeQuery;
20
use Thelia\Model\ModuleQuery;
21
use Thelia\Model\Module;
22
use Thelia\Tests\TestCaseWithURLToolSetup;
23
use AttributeType\Model\AttributeType;
24
use AttributeType\Model\AttributeTypeQuery;
25
26
/**
27
 * Class AttributeTypeActionTest
28
 * @package AttributeType\Tests
29
 * @author Gilles Bourgeat <[email protected]>
30
 */
31
class AttributeTypeActionTest extends TestCaseWithURLToolSetup
32
{
33
    /** @var string */
34
    protected $slugTest = 'test-php-unit';
35
36
    /** @var AttributeTypeAction */
37
    protected $action = null;
38
39
    /** @var AttributeType */
40
    protected $attributeType = null;
41
42
    /** @var Attribute */
43
    protected $attribute = null;
44
45
    /** @var bool */
46
    protected $isActive = false;
47
48
    public function __construct()
49
    {
50
        parent::__construct();
51
52
        /** @var Module $module */
53
        if (null !== $module = ModuleQuery::create()->findOneByCode('AttributeType')) {
54
            if ($module->getActivate()) {
55
                $this->isActive = true;
56
            }
57
        }
58
59
        $this->action = new AttributeTypeAction($this->getContainer());
60
61
        $this->attributeType = (new AttributeType())
62
            ->setSlug($this->slugTest)
63
            ->setInputType('text');
64
65
        $this->attribute = AttributeQuery::create()->findOne();
66
    }
67
68
    public function testActions()
69
    {
70
        if (!$this->action) {
71
            return;
72
        }
73
74
        // if last test is wrong
75
        AttributeTypeQuery::create()->filterBySlug($this->slugTest)->delete();
76
77
        $this->createAction();
78
        $this->updateAction();
79
        $this->associateAction();
80
        $this->createMetaAction();
81
        $this->updateMetaAction();
82
        $this->dissociateAction();
83
        $this->deleteAction();
84
    }
85
86
    protected function createAction()
87
    {
88
        $event = new AttributeTypeEvent($this->attributeType);
89
90
        $this->action->create($event);
91
92
        $attributeType = $event->getAttributeType();
93
94
        $this->assertNotEquals(null, $attributeType->getId());
95
    }
96
97
    protected function updateAction()
98
    {
99
        $this->attributeType->setInputType('number');
100
101
        $this->action->update(
102
            new AttributeTypeEvent($this->attributeType)
103
        );
104
105
        $attributeTypeGetSuccessTest = AttributeTypeQuery::create()
106
            ->findOneById($this->attributeType->getId());
107
108
        // test if input type is number after update
109
        $this->assertEquals('number', $attributeTypeGetSuccessTest->getInputType());
110
    }
111
112
    protected function associateAction()
113
    {
114
        $this->action->associate(
115
            (new AttributeTypeEvent($this->attributeType))->setAttribute($this->attribute)
116
        );
117
118
        $attributeAttributeType = AttributeAttributeTypeQuery::create()
119
            ->filterByAttributeId($this->attribute->getId())
120
            ->filterByAttributeTypeId($this->attributeType->getId())
121
            ->findOne();
122
123
        $this->assertNotEquals(null, $attributeAttributeType);
124
    }
125
126
    protected function createMetaAction()
127
    {
128
        /** @var AttributeAv $attributeAv */
129
        $attributeAv = $this->attribute->getAttributeAvs()->getFirst();
130
131
        $attributeAttributeType = AttributeAttributeTypeQuery::create()
132
            ->filterByAttributeId($this->attribute->getId())
133
            ->filterByAttributeTypeId($this->attributeType->getId())
134
            ->findOne();
135
136
        $attributeTypeAvMeta = (new AttributeTypeAvMeta())
137
            ->setAttributeAttributeTypeId($attributeAttributeType->getId())
138
            ->setAttributeAvId($attributeAv->getId())
139
            ->setValue("");
140
141
        $this->action->metaCreate(
142
            new AttributeTypeAvMetaEvent($attributeTypeAvMeta)
143
        );
144
145
        $this->assertNotEquals(null, $attributeTypeAvMeta->getId());
146
    }
147
148
    protected function updateMetaAction()
149
    {
150
        /** @var AttributeAv $attributeAv */
151
        $attributeAv = $this->attribute->getAttributeAvs()->getFirst();
152
153
        $attributeAttributeType = AttributeAttributeTypeQuery::create()
154
            ->filterByAttributeId($this->attribute->getId())
155
            ->filterByAttributeTypeId($this->attributeType->getId())
156
            ->findOne();
157
158
        $attributeTypeAvMeta = AttributeTypeAvMetaQuery::create()
159
            ->filterByAttributeAttributeTypeId($attributeAttributeType->getId())
160
            ->filterByAttributeAvId($attributeAv->getId())
161
            ->findOne();
162
163
        $attributeTypeAvMeta->setValue('test');
164
165
        $this->action->metaUpdate(
166
            new AttributeTypeAvMetaEvent($attributeTypeAvMeta)
167
        );
168
169
        $attributeTypeAvMetaTest = AttributeTypeAvMetaQuery::create()
170
            ->filterByAttributeAttributeTypeId($attributeAttributeType->getId())
171
            ->filterByAttributeAvId($attributeAv->getId())
172
            ->findOne();
173
174
        $this->assertEquals('test', $attributeTypeAvMetaTest->getValue());
175
    }
176
177
    protected function dissociateAction()
178
    {
179
        $this->action->dissociate(
180
            (new AttributeTypeEvent($this->attributeType))->setAttribute($this->attribute)
181
        );
182
183
        $attributeAttributeType = AttributeAttributeTypeQuery::create()
184
            ->filterByAttributeId($this->attribute->getId())
185
            ->filterByAttributeTypeId($this->attributeType->getId())
186
            ->findOne();
187
188
        $this->assertEquals(null, $attributeAttributeType);
189
    }
190
191
    protected function deleteAction()
192
    {
193
        $this->action->delete(
194
            new AttributeTypeEvent($this->attributeType)
195
        );
196
197
        $attributeTypeGetSuccessTest = AttributeTypeQuery::create()
198
            ->findOneById($this->attributeType->getId());
199
200
        // test if input type is number after update
201
        $this->assertEquals(null, $attributeTypeGetSuccessTest);
202
    }
203
}
204