1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CCDNForum ForumBundle |
5
|
|
|
* |
6
|
|
|
* (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/> |
7
|
|
|
* |
8
|
|
|
* Available on github <http://www.github.com/codeconsortium/> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace CCDNForum\ForumBundle\features\bootstrap; |
15
|
|
|
|
16
|
|
|
use Behat\Behat\Context\BehatContext; |
17
|
|
|
use Behat\Gherkin\Node\TableNode; |
18
|
|
|
use Behat\Symfony2Extension\Context\KernelAwareInterface; |
19
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
20
|
|
|
use CCDNForum\ForumBundle\Tests\Functional\src\Entity\User; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* Features context. |
25
|
|
|
* |
26
|
|
|
* @category CCDNForum |
27
|
|
|
* @package ForumBundle |
28
|
|
|
* |
29
|
|
|
* @author Reece Fowell <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
31
|
|
|
* @version Release: 2.0 |
32
|
|
|
* @link https://github.com/codeconsortium/CCDNForumForumBundle |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
class DataContext extends BehatContext implements KernelAwareInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
* Kernel. |
40
|
|
|
* |
41
|
|
|
* @var KernelInterface |
42
|
|
|
*/ |
43
|
|
|
protected $kernel; |
44
|
|
|
|
45
|
|
|
public function __construct() |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function setKernel(KernelInterface $kernel) |
55
|
|
|
{ |
56
|
|
|
$this->kernel = $kernel; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
* Get entity manager. |
62
|
|
|
* |
63
|
|
|
* @return EntityManager |
64
|
|
|
*/ |
65
|
|
|
public function getEntityManager() |
66
|
|
|
{ |
67
|
|
|
return $this->getContainer()->get('doctrine')->getManager(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
* Returns Container instance. |
73
|
|
|
* |
74
|
|
|
* @return ContainerInterface |
|
|
|
|
75
|
|
|
*/ |
76
|
|
|
protected function getContainer() |
77
|
|
|
{ |
78
|
|
|
return $this->kernel->getContainer(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
* Get service by id. |
84
|
|
|
* |
85
|
|
|
* @param string $serviceName |
86
|
|
|
* |
87
|
|
|
* @return object |
88
|
|
|
*/ |
89
|
|
|
protected function getService($serviceName) |
90
|
|
|
{ |
91
|
|
|
return $this->getContainer()->get($serviceName); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected $users = array(); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
* @Given /^there are following users defined:$/ |
99
|
|
|
*/ |
100
|
|
|
public function thereAreFollowingUsersDefined(TableNode $table) |
101
|
|
|
{ |
102
|
|
|
foreach ($table->getHash() as $data) { |
103
|
|
|
$this->users[] = $this->thereIsUser( |
104
|
|
|
$data['email'], |
105
|
|
|
$data['email'], |
106
|
|
|
isset($data['password']) ? $data['password'] : 'password', |
107
|
|
|
isset($data['role']) ? $data['role'] : 'ROLE_USER', |
108
|
|
|
isset($data['enabled']) ? $data['enabled'] : true |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->getEntityManager()->flush(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function thereIsUser($username, $email, $password, $role = 'ROLE_USER', $enabled = true) |
116
|
|
|
{ |
117
|
|
|
$user = new User(); |
118
|
|
|
|
119
|
|
|
$user->setUsername($username); |
120
|
|
|
$user->setEmail($email); |
121
|
|
|
$user->setEnabled($enabled); |
122
|
|
|
$user->setPlainPassword($password); |
123
|
|
|
|
124
|
|
|
if (null !== $role) { |
125
|
|
|
$user->addRole($role); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->getEntityManager()->persist($user); |
129
|
|
|
|
130
|
|
|
return $user; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected $forums = array(); |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* |
137
|
|
|
* @Given /^there are following forums defined:$/ |
138
|
|
|
*/ |
139
|
|
|
public function thereAreFollowingForumsDefined(TableNode $table) |
140
|
|
|
{ |
141
|
|
|
foreach ($table->getHash() as $data) { |
142
|
|
|
$this->forums[] = $this->thereIsForum( |
143
|
|
|
isset($data['name']) ? $data['name'] : sha1(uniqid(mt_rand(), true)) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->getEntityManager()->flush(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function thereIsForum($name) |
151
|
|
|
{ |
152
|
|
|
$forum = $this->getForumModel()->createForum(); |
153
|
|
|
|
154
|
|
|
$forum->setName($name); |
155
|
|
|
|
156
|
|
|
$this->getEntityManager()->persist($forum); |
157
|
|
|
|
158
|
|
|
return $forum; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
protected $categories = array(); |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* |
165
|
|
|
* @Given /^there are following categories defined:$/ |
166
|
|
|
*/ |
167
|
|
|
public function thereAreFollowingCategoriesDefined(TableNode $table) |
168
|
|
|
{ |
169
|
|
|
foreach ($table->getHash() as $index => $data) { |
170
|
|
|
$this->categories[] = $this->thereIsCategory( |
171
|
|
|
isset($data['name']) ? $data['name'] : sha1(uniqid(mt_rand(), true)), |
172
|
|
|
isset($data['order']) ? $data['order'] : $index, |
173
|
|
|
isset($data['forum']) ? $data['forum'] : null |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$this->getEntityManager()->flush(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function thereIsCategory($name, $order, $forumName = null) |
181
|
|
|
{ |
182
|
|
|
$category = $this->getCategoryModel()->createCategory(); |
183
|
|
|
|
184
|
|
|
$category->setName($name); |
185
|
|
|
$category->setListOrderPriority($order); |
186
|
|
|
|
187
|
|
|
foreach ($this->forums as $forum) { |
188
|
|
|
if ($forum->getName() == $forumName) { |
189
|
|
|
$category->setForum($forum); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$this->getEntityManager()->persist($category); |
194
|
|
|
|
195
|
|
|
return $category; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
protected $boards = array(); |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* |
202
|
|
|
* @Given /^there are following boards defined:$/ |
203
|
|
|
*/ |
204
|
|
|
public function thereAreFollowingBoardsDefined(TableNode $table) |
205
|
|
|
{ |
206
|
|
|
foreach ($table->getHash() as $index => $data) { |
207
|
|
|
$this->boards[] = $this->thereIsBoard( |
208
|
|
|
isset($data['name']) ? $data['name'] : sha1(uniqid(mt_rand(), true)), |
209
|
|
|
isset($data['description']) ? $data['description'] : sha1(uniqid(mt_rand(), true)), |
210
|
|
|
isset($data['order']) ? $data['order'] : $index, |
211
|
|
|
isset($data['category']) ? $data['category'] : null |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$this->getEntityManager()->flush(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function thereIsBoard($name, $description, $order, $categoryName = null) |
219
|
|
|
{ |
220
|
|
|
$board = $this->getBoardModel()->createBoard(); |
221
|
|
|
|
222
|
|
|
$board->setName($name); |
223
|
|
|
$board->setDescription($description); |
224
|
|
|
$board->setListOrderPriority($order); |
225
|
|
|
|
226
|
|
|
foreach ($this->categories as $category) { |
227
|
|
|
if ($category->getName() == $categoryName) { |
228
|
|
|
$board->setCategory($category); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$this->getEntityManager()->persist($board); |
233
|
|
|
|
234
|
|
|
return $board; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
protected $topics = array(); |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* |
241
|
|
|
* @Given /^there are following topics defined:$/ |
242
|
|
|
*/ |
243
|
|
|
public function thereAreFollowingTopicsDefined(TableNode $table) |
244
|
|
|
{ |
245
|
|
|
foreach ($table->getHash() as $data) { |
246
|
|
|
$this->topics[] = $this->thereIsTopic( |
247
|
|
|
isset($data['title']) ? $data['title'] : sha1(uniqid(mt_rand(), true)), |
248
|
|
|
isset($data['body']) ? $data['body'] : sha1(uniqid(mt_rand(), true)), |
249
|
|
|
isset($data['board']) ? $data['board'] : null, |
250
|
|
|
isset($data['user']) ? $data['user'] : null, |
251
|
|
|
isset($data['subscribed']) ? $data['subscribed'] : false |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$this->getEntityManager()->flush(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function thereIsTopic($title, $body, $boardName, $userEmail, $subscribed = false) |
259
|
|
|
{ |
260
|
|
|
$user = null; |
261
|
|
|
|
262
|
|
|
foreach ($this->users as $userScan) { |
263
|
|
|
if ($userScan->getEmail() == $userEmail) { |
264
|
|
|
$user = $userScan; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$board = null; |
269
|
|
|
|
270
|
|
|
foreach ($this->boards as $boardScan) { |
271
|
|
|
if ($boardScan->getName() == $boardName) { |
272
|
|
|
$board = $boardScan; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$topic = $this->getTopicModel()->createTopic(); |
277
|
|
|
$topic->setTitle($title); |
278
|
|
|
$topic->setBoard($board); |
279
|
|
|
|
280
|
|
|
$post = $this->getPostModel()->createPost(); |
281
|
|
|
$post->setBody($body); |
282
|
|
|
$post->setCreatedDate(new \DateTime('now')); |
283
|
|
|
$post->setCreatedBy($user); |
284
|
|
|
$post->setTopic($topic); |
285
|
|
|
|
286
|
|
|
$topic->setFirstPost($post); |
287
|
|
|
$topic->setLastPost($post); |
288
|
|
|
|
289
|
|
|
if ($subscribed) { |
290
|
|
|
$subscription = $this->getSubscriptionModel()->createSubscription(); |
291
|
|
|
$subscription->setForum($board->getCategory()->getForum()); |
292
|
|
|
$subscription->setTopic($topic); |
293
|
|
|
$subscription->setOwnedBy($user); |
294
|
|
|
$subscription->setRead(false); |
295
|
|
|
$subscription->setSubscribed(true); |
296
|
|
|
|
297
|
|
|
$this->getEntityManager()->persist($subscription); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$this->getEntityManager()->persist($topic); |
301
|
|
|
|
302
|
|
|
return $topic; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* |
307
|
|
|
* @var \CCDNForum\ForumBundle\Model\FrontModel\ForumModel $forumModel |
308
|
|
|
*/ |
309
|
|
|
private $forumModel; |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* |
313
|
|
|
* @var \CCDNForum\ForumBundle\Model\FrontModel\CategoryModel $categoryModel |
314
|
|
|
*/ |
315
|
|
|
private $categoryModel; |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* |
319
|
|
|
* @var \CCDNForum\ForumBundle\Model\FrontModel\BoardModel $boardModel |
320
|
|
|
*/ |
321
|
|
|
private $boardModel; |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* |
325
|
|
|
* @var \CCDNForum\ForumBundle\Model\FrontModel\TopicModel $topicModel |
326
|
|
|
*/ |
327
|
|
|
private $topicModel; |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* |
331
|
|
|
* @var \CCDNForum\ForumBundle\Model\FrontModel\PostModel $postModel |
332
|
|
|
*/ |
333
|
|
|
private $postModel; |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* |
337
|
|
|
* @var \CCDNForum\ForumBundle\Model\FrontModel\RegistryModel $registryModel |
338
|
|
|
*/ |
339
|
|
|
private $registryModel; |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* |
343
|
|
|
* @var \CCDNForum\ForumBundle\Model\FrontModel\SubscriptionModel $subscriptionModel |
344
|
|
|
*/ |
345
|
|
|
private $subscriptionModel; |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* |
349
|
|
|
* @access protected |
350
|
|
|
* @return \CCDNForum\ForumBundle\Model\FrontModel\ForumModel |
351
|
|
|
*/ |
352
|
|
|
protected function getForumModel() |
353
|
|
|
{ |
354
|
|
|
if (null == $this->forumModel) { |
355
|
|
|
$this->forumModel = $this->getService('ccdn_forum_forum.model.forum'); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
return $this->forumModel; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* |
363
|
|
|
* @access protected |
364
|
|
|
* @return \CCDNForum\ForumBundle\Model\FrontModel\CategoryModel |
365
|
|
|
*/ |
366
|
|
|
protected function getCategoryModel() |
367
|
|
|
{ |
368
|
|
|
if (null == $this->categoryModel) { |
369
|
|
|
$this->categoryModel = $this->getService('ccdn_forum_forum.model.category'); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
return $this->categoryModel; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* |
377
|
|
|
* @access protected |
378
|
|
|
* @return \CCDNForum\ForumBundle\Model\FrontModel\BoardModel |
379
|
|
|
*/ |
380
|
|
|
protected function getBoardModel() |
381
|
|
|
{ |
382
|
|
|
if (null == $this->boardModel) { |
383
|
|
|
$this->boardModel = $this->getService('ccdn_forum_forum.model.board'); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
return $this->boardModel; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* |
391
|
|
|
* @access protected |
392
|
|
|
* @return \CCDNForum\ForumBundle\Model\FrontModel\TopicModel |
393
|
|
|
*/ |
394
|
|
|
protected function getTopicModel() |
395
|
|
|
{ |
396
|
|
|
if (null == $this->topicModel) { |
397
|
|
|
$this->topicModel = $this->getService('ccdn_forum_forum.model.topic'); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
return $this->topicModel; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* |
405
|
|
|
* @access protected |
406
|
|
|
* @return \CCDNForum\ForumBundle\Model\FrontModel\PostModel |
407
|
|
|
*/ |
408
|
|
|
protected function getPostModel() |
409
|
|
|
{ |
410
|
|
|
if (null == $this->postModel) { |
411
|
|
|
$this->postModel = $this->getService('ccdn_forum_forum.model.post'); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
return $this->postModel; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* |
419
|
|
|
* @access protected |
420
|
|
|
* @return \CCDNForum\ForumBundle\Model\FrontModel\RegistryModel |
421
|
|
|
*/ |
422
|
|
|
protected function getRegistryModel() |
423
|
|
|
{ |
424
|
|
|
if (null == $this->registryModel) { |
425
|
|
|
$this->registryModel = $this->getService('ccdn_forum_forum.model.registry'); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
return $this->registryModel; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* |
433
|
|
|
* @access protected |
434
|
|
|
* @return \CCDNForum\ForumBundle\Model\FrontModel\SubscriptionModel |
435
|
|
|
*/ |
436
|
|
|
protected function getSubscriptionModel() |
437
|
|
|
{ |
438
|
|
|
if (null == $this->subscriptionModel) { |
439
|
|
|
$this->subscriptionModel = $this->getService('ccdn_forum_forum.model.subscription'); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
return $this->subscriptionModel; |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.