DealerServiceTest::testUpdateFromEmptyIdNull()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
/*************************************************************************************/
13
14
namespace Dealer\Test\Service;
15
16
use Dealer\Model\Dealer;
17
use Dealer\Model\DealerQuery;
18
use Dealer\Service\DealerService;
19
use Propel\Runtime\Propel;
20
use Symfony\Component\EventDispatcher\EventDispatcher;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
23
class DealerServiceTest extends \PHPUnit_Framework_TestCase
24
{
25
    /** @var DealerService $dealerService */
26
    protected $dealerService;
27
28
    /** @var  EventDispatcherInterface $mockEventDispatcher */
29
    protected $mockEventDispatcher;
30
31
    public function setUp()
32
    {
33
        $this->dealerService = new DealerService();
34
        $this->mockEventDispatcher = $this->getMockBuilder(EventDispatcher::class)
35
            ->setMethods(['dispatch'])
36
            ->getMock();
37
        $this->dealerService->setDispatcher($this->mockEventDispatcher);
38
    }
39
40
    public static function setUpBeforeClass()
41
    {
42
        Propel::getConnection()->beginTransaction();
43
    }
44
45
    /**
46
     * @covers \Dealer\Service\DealerService::createFromArray()
47
     * @expectedException \Exception
48
     */
49
    public function testCreateFromArrayWithEmptyArray()
50
    {
51
        $this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(DealerService::EVENT_CREATE_BEFORE));
52
        $this->dealerService->createFromArray([]);
53
    }
54
55
    /**
56
     * @covers \Dealer\Service\DealerService::createFromArray()
57
     * @expectedException \Exception
58
     */
59
    public function testCreateFromArrayWithErrorInfo()
60
    {
61
        $this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(DealerService::EVENT_CREATE_BEFORE));
62
        $this->dealerService->createFromArray($this->dataMissingRequire(), "fr_FR");
63
    }
64
65
66
    /**
67
     * @covers \Dealer\Service\DealerService::createFromArray()
68
     * @expectedException \Exception
69
     */
70
    public function testCreateFromArrayWithNull()
71
    {
72
        $this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(DealerService::EVENT_CREATE_BEFORE));
73
        $this->dealerService->createFromArray(null, "fr_FR");
74
    }
75
76
    /**
77
     * @covers \Dealer\Service\DealerService::createFromArray()
78
     */
79
    public function testCreateFromArrayWithBaseInfo()
80
    {
81
        $this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch')
82
            ->withConsecutive(
83
                [$this->equalTo(DealerService::EVENT_CREATE_BEFORE)],
84
                [$this->equalTo(DealerService::EVENT_CREATE_AFTER)]
85
            );
86
        $dealer = $this->dealerService->createFromArray($this->dataRequire(), "fr_FR");
87
88
        $this->assertEquals($dealer, DealerQuery::create()->findOneById($dealer->getId()));
89
90
        return $dealer;
91
    }
92
93
    /**
94
     * @param Dealer $dealer
95
     * @covers  \Dealer\Service\DealerService::updateFromArray()
96
     * @depends testCreateFromArrayWithBaseInfo
97
     */
98
    public function testUpdateFromArrayAfterCreateFromArrayWithBaseInfo(Dealer $dealer)
99
    {
100
        $data = $this->dataUpdate();
101
        $data['id'] = $dealer->getId();
102
103
        $this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch')
104
            ->withConsecutive(
105
                [$this->equalTo(DealerService::EVENT_UPDATE_BEFORE)],
106
                [$this->equalTo(DealerService::EVENT_UPDATE_AFTER)]
107
            );
108
109
        $dealer = $this->dealerService->updateFromArray($data, "fr_FR");
110
        $this->assertEquals($dealer, DealerQuery::create()->findOneById($dealer->getId()));
111
    }
112
113
    /**
114
     * @covers \Dealer\Service\DealerService::updateFromArray()
115
     * @expectedException \Exception
116
     */
117
    public function testUpdateFromEmptyIdWithoutAllRequireField()
118
    {
119
        $data = $this->dataUpdate();
120
121
        $this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(DealerService::EVENT_UPDATE_BEFORE));
122
123
        $dealer = $this->dealerService->updateFromArray($data, "fr_FR");
124
        $this->assertEquals($dealer, DealerQuery::create()->findOneById($dealer->getId()));
125
    }
126
127
    /**
128
     * @covers \Dealer\Service\DealerService::updateFromArray()
129
     */
130
    public function testUpdateFromEmptyIdWithAllRequireField()
131
    {
132
        $data = $this->dataRequire();
133
134
        $this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch')
135
            ->withConsecutive(
136
                [$this->equalTo(DealerService::EVENT_UPDATE_BEFORE)],
137
                [$this->equalTo(DealerService::EVENT_UPDATE_AFTER)]
138
            );
139
140
        $dealer = $this->dealerService->updateFromArray($data, "fr_FR");
141
        $this->assertEquals($dealer, DealerQuery::create()->findOneById($dealer->getId()));
142
    }
143
144
145
    /**
146
     * @covers \Dealer\Service\DealerService::updateFromArray()
147
     * @expectedException \Exception
148
     */
149
    public function testUpdateFromEmptyIdNull()
150
    {
151
        $this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(DealerService::EVENT_UPDATE_BEFORE));
152
        $this->dealerService->updateFromArray(null);
153
    }
154
155
    /**
156
     * @covers \Dealer\Service\DealerService::updateFromArray()
157
     * @expectedException \Exception
158
     */
159
    public function testUpdateFromEmptyArray()
160
    {
161
        $this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(DealerService::EVENT_UPDATE_BEFORE));
162
163
        $dealer = $this->dealerService->updateFromArray([], "fr_FR");
164
        $this->assertEquals($dealer, DealerQuery::create()->findOneById($dealer->getId()));
165
    }
166
167
    /**
168
     * @param Dealer $dealer
169
     * @covers  \Dealer\Service\DealerService::deleteFromId()
170
     * @depends testCreateFromArrayWithBaseInfo
171
     */
172
    public function testDeleteDealerAfterCreateFromArrayWithBaseInfo(Dealer $dealer)
173
    {
174
        $this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch')
175
            ->withConsecutive(
176
                [$this->equalTo(DealerService::EVENT_DELETE_BEFORE)],
177
                [$this->equalTo(DealerService::EVENT_DELETE_AFTER)]
178
            );
179
        $id = $dealer->getId();
180
        $this->dealerService->deleteFromId(['id' => $id]);
181
182
        $this->assertEmpty(DealerQuery::create()->findOneById($id));
183
    }
184
185
    /**
186
     * @covers \Dealer\Service\DealerService::deleteFromId()
187
     */
188
    public function testDeleteDealerWithoutPassId()
189
    {
190
        $this->mockEventDispatcher->expects($this->exactly(0))->method('dispatch');
191
        $this->dealerService->deleteFromId([]);
192
    }
193
194
    /**
195
     * @covers \Dealer\Service\DealerService::deleteFromId()
196
     */
197
    public function testDeleteDealerWithIdNull()
198
    {
199
        $this->mockEventDispatcher->expects($this->exactly(0))->method('dispatch');
200
        $this->dealerService->deleteFromId(null);
201
    }
202
203
    public static function tearDownAfterClass()
204
    {
205
        Propel::getConnection()->rollBack();
206
    }
207
208
    protected function dataRequire()
209
    {
210
        return [
211
            "title" => "Openstudio",
212
            "address1" => "5 rue jean rochon",
213
            "zipcode" => "63000",
214
            "city" => "test",
215
            "country_id" => "64",
216
        ];
217
    }
218
219
    protected function dataUpdate()
220
    {
221
        return [
222
            "city" => "testUp",
223
        ];
224
    }
225
226
227
    protected function dataMissingRequire()
228
    {
229
        return [
230
            "title" => "Openstudio",
231
        ];
232
    }
233
234
235
}
236