ContactInfoServiceTest   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 0
loc 251
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 19 1
A setUpBeforeClass() 0 4 1
A testCreateFromArrayWithEmptyArray() 0 5 1
A testCreateFromArrayWithErrorInfo() 0 5 1
A testCreateFromArrayWithNull() 0 5 1
A testCreateFromArrayWithBaseInfo() 0 16 1
A testUpdateFromArrayAfterCreateFromArrayWithBaseInfo() 0 14 1
A testUpdateFromEmptyIdWithoutAllRequireField() 0 9 1
A testUpdateFromEmptyIdWithAllRequireField() 0 13 1
A testUpdateFromEmptyIdNull() 0 5 1
A testUpdateFromEmptyArray() 0 7 1
A testDeleteDealerAfterCreateFromArrayWithBaseInfo() 0 12 1
A testDeleteDealerWithoutPassId() 0 5 1
A testDeleteDealerWithIdNull() 0 5 1
A tearDownAfterClass() 0 4 1
A dataRequire() 0 8 1
A dataUpdate() 0 7 1
A dataMissingRequire() 0 7 1
A dataContactRequire() 0 7 1
A dataDealerRequire() 0 10 1
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
17
use Dealer\Model\Dealer;
18
use Dealer\Model\DealerContact;
19
use Dealer\Model\DealerContactInfo;
20
use Dealer\Model\DealerContactInfoQuery;
21
use Dealer\Service\ContactInfoService;
22
use Dealer\Service\ContactService;
23
use Dealer\Service\DealerService;
24
use Propel\Runtime\Propel;
25
use Symfony\Component\EventDispatcher\EventDispatcher;
26
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
27
28
class ContactInfoServiceTest extends \PHPUnit_Framework_TestCase
29
{
30
    /** @var ContactInfoService $dealerService */
31
    protected $contactInfoService;
32
33
    /** @var  EventDispatcherInterface $mockEventDispatcher */
34
    protected $mockEventDispatcher;
35
36
    /** @var Dealer $dealer */
37
    protected $dealer;
38
39
    /** @var DealerContact $contact */
40
    protected $contact;
41
42
    public function setUp()
43
    {
44
        $this->contactInfoService = new ContactInfoService();
45
        $this->mockEventDispatcher = $this->getMockBuilder(EventDispatcher::class)
46
            ->setMethods(['dispatch'])
47
            ->getMock();
48
        $this->contactInfoService->setDispatcher($this->mockEventDispatcher);
49
50
        /* Create Test Dealer */
51
        $dealerService = new DealerService();
52
        $dealerService->setDispatcher($this->mockEventDispatcher);
53
        $this->dealer = $dealerService->createFromArray($this->dataDealerRequire());
54
55
        /* Create Test Contact */
56
        $contactService = new ContactService();
57
        $contactService->setDispatcher($this->mockEventDispatcher);
58
        $this->contact = $contactService->createFromArray($this->dataContactRequire());
59
60
    }
61
62
    public static function setUpBeforeClass()
63
    {
64
        Propel::getConnection()->beginTransaction();
65
    }
66
67
    /**
68
     * @covers \Dealer\Service\ContactInfoService::createFromArray()
69
     * @expectedException \Exception
70
     */
71
    public function testCreateFromArrayWithEmptyArray()
72
    {
73
        $this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(ContactInfoService::EVENT_CREATE_BEFORE));
74
        $this->contactInfoService->createFromArray([]);
75
    }
76
77
    /**
78
     * @covers \Dealer\Service\ContactInfoService::createFromArray()
79
     * @expectedException \Exception
80
     */
81
    public function testCreateFromArrayWithErrorInfo()
82
    {
83
        $this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(ContactInfoService::EVENT_CREATE_BEFORE));
84
        $this->contactInfoService->createFromArray($this->dataMissingRequire(), "fr_FR");
85
    }
86
87
88
    /**
89
     * @covers \Dealer\Service\ContactInfoService::createFromArray()
90
     * @expectedException \Exception
91
     */
92
    public function testCreateFromArrayWithNull()
93
    {
94
        $this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(ContactInfoService::EVENT_CREATE_BEFORE));
95
        $this->contactInfoService->createFromArray(null, "fr_FR");
96
    }
97
98
    /**
99
     * @covers \Dealer\Service\ContactInfoService::createFromArray()
100
     */
101
    public function testCreateFromArrayWithBaseInfo()
102
    {
103
        $data = $this->dataRequire();
104
105
        $this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch')
106
            ->withConsecutive(
107
                [$this->equalTo(ContactInfoService::EVENT_CREATE_BEFORE)],
108
                [$this->equalTo(ContactInfoService::EVENT_CREATE_AFTER)]
109
            );
110
111
        $dealer = $this->contactInfoService->createFromArray($data, "fr_FR");
112
113
        $this->assertEquals($dealer, DealerContactInfoQuery::create()->findOneById($dealer->getId()));
114
115
        return $dealer;
116
    }
117
118
    /**
119
     * @param DealerContact $dealer
120
     * @covers  \Dealer\Service\ContactInfoService::updateFromArray()
121
     * @depends testCreateFromArrayWithBaseInfo
122
     */
123
    public function testUpdateFromArrayAfterCreateFromArrayWithBaseInfo(DealerContactInfo $dealer)
124
    {
125
        $data = $this->dataUpdate();
126
        $data['id'] = $dealer->getId();
127
128
        $this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch')
129
            ->withConsecutive(
130
                [$this->equalTo(ContactInfoService::EVENT_UPDATE_BEFORE)],
131
                [$this->equalTo(ContactInfoService::EVENT_UPDATE_AFTER)]
132
            );
133
134
        $dealer = $this->contactInfoService->updateFromArray($data, "fr_FR");
135
        $this->assertEquals($dealer, DealerContactInfoQuery::create()->findOneById($dealer->getId()));
136
    }
137
138
    /**
139
     * @covers \Dealer\Service\ContactInfoService::updateFromArray()
140
     * @expectedException \Exception
141
     */
142
    public function testUpdateFromEmptyIdWithoutAllRequireField()
143
    {
144
        $data = $this->dataUpdate();
145
146
        $this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(ContactInfoService::EVENT_UPDATE_BEFORE));
147
148
        $dealer = $this->contactInfoService->updateFromArray($data, "fr_FR");
149
        $this->assertEquals($dealer, DealerContactInfoQuery::create()->findOneById($dealer->getId()));
150
    }
151
152
    /**
153
     * @covers \Dealer\Service\ContactInfoService::updateFromArray()
154
     */
155
    public function testUpdateFromEmptyIdWithAllRequireField()
156
    {
157
        $data = $this->dataRequire();
158
159
        $this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch')
160
            ->withConsecutive(
161
                [$this->equalTo(ContactInfoService::EVENT_UPDATE_BEFORE)],
162
                [$this->equalTo(ContactInfoService::EVENT_UPDATE_AFTER)]
163
            );
164
165
        $dealer = $this->contactInfoService->updateFromArray($data, "fr_FR");
166
        $this->assertEquals($dealer, DealerContactInfoQuery::create()->findOneById($dealer->getId()));
167
    }
168
169
170
    /**
171
     * @covers \Dealer\Service\ContactInfoService::updateFromArray()
172
     * @expectedException \Exception
173
     */
174
    public function testUpdateFromEmptyIdNull()
175
    {
176
        $this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(ContactInfoService::EVENT_UPDATE_BEFORE));
177
        $this->contactInfoService->updateFromArray(null);
178
    }
179
180
    /**
181
     * @covers \Dealer\Service\ContactInfoService::updateFromArray()
182
     * @expectedException \Exception
183
     */
184
    public function testUpdateFromEmptyArray()
185
    {
186
        $this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(ContactInfoService::EVENT_UPDATE_BEFORE));
187
188
        $dealer = $this->contactInfoService->updateFromArray([], "fr_FR");
189
        $this->assertEquals($dealer, DealerContactInfoQuery::create()->findOneById($dealer->getId()));
190
    }
191
192
193
    /**
194
     * @param DealerContact $dealer
195
     * @covers  \Dealer\Service\ContactInfoService::deleteFromId()
196
     * @depends testCreateFromArrayWithBaseInfo
197
     */
198
    public function testDeleteDealerAfterCreateFromArrayWithBaseInfo(DealerContactInfo $dealer)
199
    {
200
        $this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch')
201
            ->withConsecutive(
202
                [$this->equalTo(ContactInfoService::EVENT_DELETE_BEFORE)],
203
                [$this->equalTo(ContactInfoService::EVENT_DELETE_AFTER)]
204
            );
205
        $id = $dealer->getId();
206
        $this->contactInfoService->deleteFromId(['id' => $id]);
207
208
        $this->assertEmpty(DealerContactInfoQuery::create()->findOneById($id));
209
    }
210
211
    /**
212
     * @covers \Dealer\Service\ContactInfoService::deleteFromId()
213
     */
214
    public function testDeleteDealerWithoutPassId()
215
    {
216
        $this->mockEventDispatcher->expects($this->exactly(0))->method('dispatch');
217
        $this->contactInfoService->deleteFromId([]);
218
    }
219
220
    /**
221
     * @covers \Dealer\Service\ContactInfoService::deleteFromId()
222
     */
223
    public function testDeleteDealerWithIdNull()
224
    {
225
        $this->mockEventDispatcher->expects($this->exactly(0))->method('dispatch');
226
        $this->contactInfoService->deleteFromId(null);
227
    }
228
229
    public static function tearDownAfterClass()
230
    {
231
        Propel::getConnection()->rollBack();
232
    }
233
234
    protected function dataRequire()
235
    {
236
        return [
237
            "value" => "email",
238
            "type" => 0,
239
            "contact_id" => $this->contact->getId(),
240
        ];
241
    }
242
243
    protected function dataUpdate()
244
    {
245
        return [
246
            "value" => "tel",
247
            "type" => 1,
248
        ];
249
    }
250
251
252
    protected function dataMissingRequire()
253
    {
254
        return [
255
            "value" => "tel",
256
            "type" => 1,
257
        ];
258
    }
259
260
    protected function dataContactRequire()
261
    {
262
        return [
263
            "label" => "Openstudio",
264
            "dealer_id" => $this->dealer->getId()
265
        ];
266
    }
267
268
    protected function dataDealerRequire()
269
    {
270
        return [
271
            "title" => "Openstudio",
272
            "address1" => "5 rue jean rochon",
273
            "zipcode" => "63000",
274
            "city" => "test",
275
            "country_id" => "64",
276
        ];
277
    }
278
}
279