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