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\DealerShedules; |
19
|
|
|
use Dealer\Model\DealerShedulesQuery; |
20
|
|
|
use Dealer\Service\DealerService; |
21
|
|
|
use Dealer\Service\SchedulesService; |
22
|
|
|
use Propel\Runtime\Propel; |
23
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
24
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
25
|
|
|
|
26
|
|
|
class SchedulesServiceTest extends \PHPUnit_Framework_TestCase |
27
|
|
|
{ |
28
|
|
|
/** @var SchedulesService $schedulesService */ |
29
|
|
|
protected $schedulesService; |
30
|
|
|
|
31
|
|
|
/** @var EventDispatcherInterface $mockEventDispatcher */ |
32
|
|
|
protected $mockEventDispatcher; |
33
|
|
|
|
34
|
|
|
/** @var Dealer $dealer */ |
35
|
|
|
protected $dealer; |
36
|
|
|
|
37
|
|
|
public function setUp() |
38
|
|
|
{ |
39
|
|
|
$this->schedulesService = new SchedulesService(); |
40
|
|
|
$this->mockEventDispatcher = $this->getMockBuilder(EventDispatcher::class) |
41
|
|
|
->setMethods(['dispatch']) |
42
|
|
|
->getMock(); |
43
|
|
|
$this->schedulesService->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\SchedulesService::createFromArray() |
59
|
|
|
* @expectedException \Exception |
60
|
|
|
*/ |
61
|
|
|
public function testCreateFromArrayWithEmptyArray() |
62
|
|
|
{ |
63
|
|
|
$this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(SchedulesService::EVENT_CREATE_BEFORE)); |
64
|
|
|
$this->schedulesService->createFromArray([]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @covers \Dealer\Service\SchedulesService::createFromArray() |
69
|
|
|
* @expectedException \Exception |
70
|
|
|
*/ |
71
|
|
|
public function testCreateFromArrayWithErrorInfo() |
72
|
|
|
{ |
73
|
|
|
$this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(SchedulesService::EVENT_CREATE_BEFORE)); |
74
|
|
|
$this->schedulesService->createFromArray($this->dataMissingRequire(), "fr_FR"); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @covers \Dealer\Service\SchedulesService::createFromArray() |
80
|
|
|
* @expectedException \Exception |
81
|
|
|
*/ |
82
|
|
|
public function testCreateFromArrayWithNull() |
83
|
|
|
{ |
84
|
|
|
$this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(SchedulesService::EVENT_CREATE_BEFORE)); |
85
|
|
|
$this->schedulesService->createFromArray(null, "fr_FR"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @covers \Dealer\Service\SchedulesService::createFromArray() |
90
|
|
|
* @return DealerShedules |
91
|
|
|
*/ |
92
|
|
|
public function testCreateFromArrayWithDefaultBaseInfo() |
93
|
|
|
{ |
94
|
|
|
$data = $this->dataRequire(); |
95
|
|
|
|
96
|
|
|
$this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch') |
97
|
|
|
->withConsecutive( |
98
|
|
|
[$this->equalTo(SchedulesService::EVENT_CREATE_BEFORE)], |
99
|
|
|
[$this->equalTo(SchedulesService::EVENT_CREATE_AFTER)] |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$dealer = $this->schedulesService->createFromArray($data, "fr_FR"); |
103
|
|
|
|
104
|
|
|
$this->assertEquals($dealer, DealerShedulesQuery::create()->findOneById($dealer->getId())); |
105
|
|
|
|
106
|
|
|
return $dealer; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @covers \Dealer\Service\SchedulesService::createFromArray() |
111
|
|
|
* @return DealerShedules |
112
|
|
|
*/ |
113
|
|
|
public function testCreateFromArrayWithExtraBaseInfo() |
114
|
|
|
{ |
115
|
|
|
$data = $this->dataExtraRequire(); |
116
|
|
|
|
117
|
|
|
$this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch') |
118
|
|
|
->withConsecutive( |
119
|
|
|
[$this->equalTo(SchedulesService::EVENT_CREATE_BEFORE)], |
120
|
|
|
[$this->equalTo(SchedulesService::EVENT_CREATE_AFTER)] |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$dealer = $this->schedulesService->createFromArray($data, "fr_FR"); |
124
|
|
|
|
125
|
|
|
$this->assertEquals($dealer, DealerShedulesQuery::create()->findOneById($dealer->getId())); |
126
|
|
|
|
127
|
|
|
return $dealer; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @covers \Dealer\Service\SchedulesService::updateFromArray() |
132
|
|
|
* @param DealerShedules $dealer |
133
|
|
|
* @depends testCreateFromArrayWithDefaultBaseInfo |
134
|
|
|
*/ |
135
|
|
|
public function testUpdateFromArrayAfterCreateFromArrayWithDefaultBaseInfo(DealerShedules $dealer) |
136
|
|
|
{ |
137
|
|
|
$data = $this->dataUpdate(); |
138
|
|
|
$data['id'] = $dealer->getId(); |
139
|
|
|
|
140
|
|
|
$this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch') |
141
|
|
|
->withConsecutive( |
142
|
|
|
[$this->equalTo(SchedulesService::EVENT_UPDATE_BEFORE)], |
143
|
|
|
[$this->equalTo(SchedulesService::EVENT_UPDATE_AFTER)] |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$dealer = $this->schedulesService->updateFromArray($data, "fr_FR"); |
147
|
|
|
$this->assertEquals($dealer, DealerShedulesQuery::create()->findOneById($dealer->getId())); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @covers \Dealer\Service\SchedulesService::updateFromArray() |
152
|
|
|
* @param DealerShedules $dealer |
153
|
|
|
* @depends testCreateFromArrayWithExtraBaseInfo |
154
|
|
|
*/ |
155
|
|
|
public function testUpdateFromArrayAfterCreateFromArrayWithExtraBaseInfo(DealerShedules $dealer) |
156
|
|
|
{ |
157
|
|
|
$data = $this->dataExtraUpdate(); |
158
|
|
|
$data['id'] = $dealer->getId(); |
159
|
|
|
|
160
|
|
|
$this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch') |
161
|
|
|
->withConsecutive( |
162
|
|
|
[$this->equalTo(SchedulesService::EVENT_UPDATE_BEFORE)], |
163
|
|
|
[$this->equalTo(SchedulesService::EVENT_UPDATE_AFTER)] |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
$dealer = $this->schedulesService->updateFromArray($data, "fr_FR"); |
167
|
|
|
$this->assertEquals($dealer, DealerShedulesQuery::create()->findOneById($dealer->getId())); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @covers \Dealer\Service\SchedulesService::updateFromArray() |
172
|
|
|
* @expectedException \Exception |
173
|
|
|
*/ |
174
|
|
|
public function testUpdateFromEmptyIdWithoutAllRequireField() |
175
|
|
|
{ |
176
|
|
|
$data = $this->dataUpdate(); |
177
|
|
|
|
178
|
|
|
$this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(SchedulesService::EVENT_UPDATE_BEFORE)); |
179
|
|
|
|
180
|
|
|
$dealer = $this->schedulesService->updateFromArray($data, "fr_FR"); |
181
|
|
|
$this->assertEquals($dealer, DealerShedulesQuery::create()->findOneById($dealer->getId())); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @covers \Dealer\Service\SchedulesService::updateFromArray() |
186
|
|
|
*/ |
187
|
|
|
public function testUpdateFromEmptyIdWithAllRequireField() |
188
|
|
|
{ |
189
|
|
|
$data = $this->dataRequire(); |
190
|
|
|
|
191
|
|
|
$this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch') |
192
|
|
|
->withConsecutive( |
193
|
|
|
[$this->equalTo(SchedulesService::EVENT_UPDATE_BEFORE)], |
194
|
|
|
[$this->equalTo(SchedulesService::EVENT_UPDATE_AFTER)] |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
$dealer = $this->schedulesService->updateFromArray($data, "fr_FR"); |
198
|
|
|
$this->assertEquals($dealer, DealerShedulesQuery::create()->findOneById($dealer->getId())); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @covers \Dealer\Service\SchedulesService::updateFromArray() |
204
|
|
|
* @expectedException \Exception |
205
|
|
|
*/ |
206
|
|
|
public function testUpdateFromEmptyIdNull() |
207
|
|
|
{ |
208
|
|
|
$this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(SchedulesService::EVENT_UPDATE_BEFORE)); |
209
|
|
|
$this->schedulesService->updateFromArray(null); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @covers \Dealer\Service\SchedulesService::updateFromArray() |
214
|
|
|
* @expectedException \Exception |
215
|
|
|
*/ |
216
|
|
|
public function testUpdateFromEmptyArray() |
217
|
|
|
{ |
218
|
|
|
$this->mockEventDispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(SchedulesService::EVENT_UPDATE_BEFORE)); |
219
|
|
|
|
220
|
|
|
$dealer = $this->schedulesService->updateFromArray([], "fr_FR"); |
221
|
|
|
$this->assertEquals($dealer, DealerShedulesQuery::create()->findOneById($dealer->getId())); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @covers \Dealer\Service\SchedulesService::deleteFromId() |
227
|
|
|
* @param DealerShedules $dealer |
228
|
|
|
* @depends testCreateFromArrayWithDefaultBaseInfo |
229
|
|
|
*/ |
230
|
|
|
public function testDeleteDealerAfterCreateFromArrayWithBaseInfo(DealerShedules $dealer) |
231
|
|
|
{ |
232
|
|
|
$this->mockEventDispatcher->expects($this->exactly(2))->method('dispatch') |
233
|
|
|
->withConsecutive( |
234
|
|
|
[$this->equalTo(SchedulesService::EVENT_DELETE_BEFORE)], |
235
|
|
|
[$this->equalTo(SchedulesService::EVENT_DELETE_AFTER)] |
236
|
|
|
); |
237
|
|
|
$id = $dealer->getId(); |
238
|
|
|
$this->schedulesService->deleteFromId(['id' => $id]); |
239
|
|
|
|
240
|
|
|
$this->assertEmpty(DealerShedulesQuery::create()->findOneById($id)); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @covers \Dealer\Service\SchedulesService::deleteFromId() |
245
|
|
|
*/ |
246
|
|
|
public function testDeleteDealerWithoutPassId() |
247
|
|
|
{ |
248
|
|
|
$this->mockEventDispatcher->expects($this->exactly(0))->method('dispatch'); |
249
|
|
|
$this->schedulesService->deleteFromId([]); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @covers \Dealer\Service\SchedulesService::deleteFromId() |
254
|
|
|
*/ |
255
|
|
|
public function testDeleteDealerWithIdNull() |
256
|
|
|
{ |
257
|
|
|
$this->mockEventDispatcher->expects($this->exactly(0))->method('dispatch'); |
258
|
|
|
$this->schedulesService->deleteFromId(null); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
public static function tearDownAfterClass() |
263
|
|
|
{ |
264
|
|
|
Propel::getConnection()->rollBack(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
protected function dataRequire() |
268
|
|
|
{ |
269
|
|
|
return [ |
270
|
|
|
"day" => 0, |
271
|
|
|
"begin" => "8:30", |
272
|
|
|
"end" => "15:30", |
273
|
|
|
"dealer_id" => $this->dealer->getId() |
274
|
|
|
]; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
protected function dataExtraRequire() |
278
|
|
|
{ |
279
|
|
|
return [ |
280
|
|
|
"day" => 0, |
281
|
|
|
"begin" => "8:30", |
282
|
|
|
"end" => "15:30", |
283
|
|
|
"period_begin" => "2015-03-12", |
284
|
|
|
"period_end" => "2015-09-12", |
285
|
|
|
"dealer_id" => $this->dealer->getId() |
286
|
|
|
]; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
protected function dataUpdate() |
290
|
|
|
{ |
291
|
|
|
return [ |
292
|
|
|
"day" => 1, |
293
|
|
|
]; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
protected function dataExtraUpdate() |
297
|
|
|
{ |
298
|
|
|
return [ |
299
|
|
|
"period_end" => "2015-12-12", |
300
|
|
|
]; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
protected function dataMissingRequire() |
305
|
|
|
{ |
306
|
|
|
return [ |
307
|
|
|
"day" => 3, |
308
|
|
|
]; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
protected function dataDealerRequire() |
312
|
|
|
{ |
313
|
|
|
return [ |
314
|
|
|
"title" => "Openstudio", |
315
|
|
|
"address1" => "5 rue jean rochon", |
316
|
|
|
"zipcode" => "63000", |
317
|
|
|
"city" => "test", |
318
|
|
|
"country_id" => "64", |
319
|
|
|
]; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|