GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TestBase::addNewPost()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 27
rs 8.439
cc 6
eloc 17
nc 9
nop 6
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\Tests;
15
16
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
17
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
18
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
19
use CCDNForum\ForumBundle\Tests\Functional\src\Entity\User;
20
use CCDNForum\ForumBundle\Entity\Forum;
21
use CCDNForum\ForumBundle\Entity\Category;
22
use CCDNForum\ForumBundle\Entity\Board;
23
use CCDNForum\ForumBundle\Entity\Topic;
24
use CCDNForum\ForumBundle\Entity\Post;
25
use CCDNForum\ForumBundle\Entity\Subscription;
26
27
class TestBase extends WebTestCase
28
{
29
    /**
30
	 *
31
     * @var \Doctrine\ORM\EntityManager
32
     */
33
    protected $em;
34
35
	/**
36
	 *
37
	 * @var $container
38
	 */
39
	private $container;
40
41
	/**
42
	 *
43
	 * @access public
44
	 */
45
    public function setUp()
46
    {
47
        $kernel = static::createKernel();
48
49
        $kernel->boot();
50
		
51
		$this->container = $kernel->getContainer();
52
53
        $this->em = $this->container->get('doctrine.orm.entity_manager');
54
		
55
		$this->purge();
56
    }
57
58
	/*
59
     *
60
     * Close doctrine connections to avoid having a 'too many connections'
61
     * message when running many tests
62
     */
63
	public function tearDown(){
64
		$this->container->get('doctrine')->getConnection()->close();
65
	
66
		parent::tearDown();
67
	}
68
69
    protected function purge()
70
    {
71
        $purger = new ORMPurger($this->em);
72
        $executor = new ORMExecutor($this->em, $purger);
73
        $executor->purge();
74
	}
75
76
	protected function addNewUser($username, $email, $password, $persist = true, $andFlush = true)
77
	{
78
		$user = new User();
79
		$user->setUsername($username);
80
		$user->setEmail($email);
81
		$user->setPlainPassword($password);
82
		
83
		if ($persist) {
84
			$this->em->persist($user);
85
			if ($andFlush) {
86
				$this->em->flush();
87
				$this->em->refresh($user);
88
			}
89
		}
90
		
91
		return $user;
92
	}
93
94
	protected function addFixturesForUsers()
95
	{
96
		$userNames = array('admin', 'tom', 'dick', 'harry');
97
		$users = array();
98
		foreach ($userNames as $username) {
99
			$users[$username] = $this->addNewUser($username, $username . '@foobar.com', 'password', true, false);
100
		}
101
102
		$this->em->flush();
103
	
104
		return $users;
105
	}
106
107
	protected function addNewForum($forumName, $persist = true, $andFlush = true)
108
	{
109
		$forum = $this->getForumModel()->createForum();
110
		$forum->setName($forumName);
111
		
112
		if ($persist) {
113
			$this->em->persist($forum);
114
			if ($andFlush) {
115
				$this->em->flush();
116
				$this->em->refresh($forum);
117
			}
118
		}
119
		
120
		return $forum;
121
	}
122
123
	protected function addFixturesForForums()
124
	{
125
		$forumNames = array('test_forum_1', 'test_forum_2', 'test_forum_3');
126
		$forums = array();
127
		foreach ($forumNames as $forumName) {
128
			$forums[] = $this->addNewForum($forumName, true, false);
129
		}
130
		
131
		$this->em->flush();
132
		
133
		return $forums;
134
	}
135
136
	protected function addNewCategory($categoryName, $order, Forum $forum = null, $persist = true, $andFlush = true)
137
	{
138
		$category = $this->getCategoryModel()->createCategory();
139
		$category->setName($categoryName);
140
		$category->setListOrderPriority($order);
141
		$category->setForum($forum);
142
		
143
		if ($persist) {
144
			$this->em->persist($category);
145
			if ($andFlush) {
146
				$this->em->flush();
147
				$this->em->refresh($category);
148
			}
149
		}
150
		
151
		return $category;
152
	}
153
154
	protected function addFixturesForCategories($forums)
155
	{
156
		$categoryNames = array('test_category_1', 'test_category_2', 'test_category_3');
157
		$categories = array();
158
		foreach ($forums as $forum) {
159
			foreach ($categoryNames as $index => $categoryName) {
160
				$categories[] = $this->addNewCategory($categoryName, $index, $forum, true, true);
161
			}
162
		}
163
		
164
		$this->em->flush();
165
		
166
		return $categories;
167
	}
168
169
	protected function addNewBoard($boardName, $boardDescription, $order, Category $category = null, $persist = true, $andFlush = true)
170
	{
171
		$board = $this->getBoardModel()->createBoard();
172
		$board->setName($boardName);
173
		$board->setDescription($boardDescription);
174
		$board->setListOrderPriority($order);
175
		$board->setCategory($category);
176
		
177
		if ($persist) {
178
			$this->em->persist($board);
179
			if ($andFlush) {
180
				$this->em->flush();
181
				$this->em->refresh($board);
182
			}
183
		}
184
		
185
		return $board;
186
	}
187
188
	protected function addFixturesForBoards($categories)
189
	{
190
		$boardNames = array('test_board_1', 'test_board_2', 'test_board_3');
191
		$boards = array();
192
		foreach ($categories as $category) {
193
			foreach ($boardNames as $index => $boardName) {
194
				$boards[] = $this->addNewBoard($boardName, $boardName, $index, $category, true, true);
195
			}
196
		}
197
		
198
		$this->em->flush();
199
200
		return $boards;
201
	}
202
203
	protected function addNewTopic($title, Board $board = null, $persist = true, $andFlush = true)
204
	{
205
		$topic = $this->getTopicModel()->createTopic();
206
		$topic->setTitle($title);
207
		$topic->setBoard($board);
208
		
209
		if ($persist) {
210
			$this->em->persist($topic);
211
			if ($andFlush) {
212
				$this->em->flush();
213
				$this->em->refresh($topic);
214
			}
215
		}
216
		
217
		return $topic;
218
	}
219
220
	protected function addFixturesForTopics($boards)
221
	{
222
		$topicTitles = array('test_topic_1', 'test_topic_2', 'test_topic_3');
223
		$topics = array();
224
		foreach ($boards as $board) {
225
			foreach ($topicTitles as $topicTitle) {
226
				$topics[] = $this->addNewTopic($topicTitle, $board, true, false);
227
			}
228
		}
229
		
230
		$this->em->flush();
231
		
232
		return $topics;
233
	}
234
235
	protected function addNewPost($body, $topic, $user, \Datetime $createdDate = null, $persist = true, $andFlush = true)
236
	{
237
		$post = $this->getPostModel()->createPost();
238
		$post->setTopic($topic);
239
		$post->setBody($body);
240
        $post->setCreatedDate($createdDate ?: new \DateTime());
241
        $post->setCreatedBy($user);
242
        $post->setDeleted(false);
243
		
244
		if ($topic) {
245
			$topic->setLastPost($post);
246
			
247
			if (! $topic->getFirstPost()) {
248
				$topic->setFirstPost($post);
249
			}
250
		}
251
		
252
		if ($persist) {
253
			$this->em->persist($post);
254
			if ($andFlush) {
255
				$this->em->flush();
256
				$this->em->refresh($post);
257
			}
258
		}
259
		
260
		return $post;
261
	}
262
263
	protected function addFixturesForPosts($topics, $user)
264
	{
265
		$postBodies = array('test_post_1', 'test_post_2', 'test_post_3');
266
		$posts = array();
267
		foreach ($topics as $topicIndex => $topic) {
268
			foreach ($postBodies as $postIndex => $postBody) {
269
				$datetime = new \DateTime('now + ' . (int)(($topicIndex + 1) . ($postIndex + 1)) . ' minutes');
270
				$posts[] = $this->addNewPost($postBody, $topics[$topicIndex], $user, $datetime, true, false);
271
				
272
				if ($postIndex == 0) {
273
					$topics[$topicIndex]->setFirstPost($posts[count($posts) - 1]);
274
				}
275
			}
276
			
277
			$topics[$topicIndex]->setLastPost($posts[count($posts) - 1]);
278
			$this->em->persist($topic);
279
		}
280
		
281
		$this->em->flush();
282
		
283
		return $posts;
284
	}
285
286
	protected function addNewSubscription($forum, $topic, $user, $isRead = false, $persist = true, $andFlush = true)
287
	{
288
		$subscription = $this->getSubscriptionModel()->createSubscription();
289
		$subscription->setTopic($topic);
290
        $subscription->setOwnedBy($user);
291
		$subscription->setForum($forum);
292
        $subscription->setRead($isRead);
293
        $subscription->setSubscribed(true);
294
		
295
		if ($persist) {
296
			$this->em->persist($subscription);
297
			if ($andFlush) {
298
				$this->em->flush();
299
				$this->em->refresh($subscription);
300
			}
301
		}
302
		
303
		return $subscription;
304
	}
305
306
	protected function addFixturesForSubscriptions($forum, $topics, $user, $isRead = false)
307
	{
308
		$subscriptions = array();
309
		foreach ($topics as $topic) {
310
			$subscriptions[] = $this->addNewSubscription($forum, $topic, $user, $isRead, true, false);
311
		}
312
		
313
		$this->em->flush();
314
		
315
		return $subscriptions;
316
	}
317
318
    /**
319
     *
320
     * @var \CCDNForum\ForumBundle\Model\FrontModel\ForumModel $forumModel
321
     */
322
    private $forumModel;
323
324
    /**
325
     *
326
     * @var \CCDNForum\ForumBundle\Model\FrontModel\CategoryModel $categoryModel
327
     */
328
    private $categoryModel;
329
330
    /**
331
     *
332
     * @var \CCDNForum\ForumBundle\Model\FrontModel\BoardModel $boardModel
333
     */
334
    private $boardModel;
335
336
    /**
337
     *
338
     * @var \CCDNForum\ForumBundle\Model\FrontModel\TopicModel $topicModel
339
     */
340
    private $topicModel;
341
342
    /**
343
     *
344
     * @var \CCDNForum\ForumBundle\Model\FrontModel\PostModel $postModel
345
     */
346
    private $postModel;
347
348
    /**
349
     *
350
     * @var \CCDNForum\ForumBundle\Model\FrontModel\RegistryModel $registryModel
351
     */
352
    private $registryModel;
353
354
    /**
355
     *
356
     * @var \CCDNForum\ForumBundle\Model\FrontModel\SubscriptionModel $subscriptionModel
357
     */
358
    private $subscriptionModel;
359
360
    /**
361
     *
362
     * @access protected
363
     * @return \CCDNForum\ForumBundle\Model\FrontModel\ForumModel
364
     */
365
    protected function getForumModel()
366
    {
367
        if (null == $this->forumModel) {
368
            $this->forumModel = $this->container->get('ccdn_forum_forum.model.forum');
369
        }
370
371
        return $this->forumModel;
372
    }
373
374
    /**
375
     *
376
     * @access protected
377
     * @return \CCDNForum\ForumBundle\Model\FrontModel\CategoryModel
378
     */
379
    protected function getCategoryModel()
380
    {
381
        if (null == $this->categoryModel) {
382
            $this->categoryModel = $this->container->get('ccdn_forum_forum.model.category');
383
        }
384
385
        return $this->categoryModel;
386
    }
387
388
    /**
389
     *
390
     * @access protected
391
     * @return \CCDNForum\ForumBundle\Model\FrontModel\BoardModel
392
     */
393
    protected function getBoardModel()
394
    {
395
        if (null == $this->boardModel) {
396
            $this->boardModel = $this->container->get('ccdn_forum_forum.model.board');
397
        }
398
399
        return $this->boardModel;
400
    }
401
402
    /**
403
     *
404
     * @access protected
405
     * @return \CCDNForum\ForumBundle\Model\FrontModel\TopicModel
406
     */
407
    protected function getTopicModel()
408
    {
409
        if (null == $this->topicModel) {
410
            $this->topicModel = $this->container->get('ccdn_forum_forum.model.topic');
411
        }
412
413
        return $this->topicModel;
414
    }
415
416
    /**
417
     *
418
     * @access protected
419
     * @return \CCDNForum\ForumBundle\Model\FrontModel\PostModel
420
     */
421
    protected function getPostModel()
422
    {
423
        if (null == $this->postModel) {
424
            $this->postModel = $this->container->get('ccdn_forum_forum.model.post');
425
        }
426
427
        return $this->postModel;
428
    }
429
430
    /**
431
     *
432
     * @access protected
433
     * @return \CCDNForum\ForumBundle\Model\FrontModel\RegistryModel
434
     */
435
    protected function getRegistryModel()
436
    {
437
        if (null == $this->registryModel) {
438
            $this->registryModel = $this->container->get('ccdn_forum_forum.model.registry');
439
        }
440
441
        return $this->registryModel;
442
    }
443
444
    /**
445
     *
446
     * @access protected
447
     * @return \CCDNForum\ForumBundle\Model\FrontModel\SubscriptionModel
448
     */
449
    protected function getSubscriptionModel()
450
    {
451
        if (null == $this->subscriptionModel) {
452
            $this->subscriptionModel = $this->container->get('ccdn_forum_forum.model.subscription');
453
        }
454
455
        return $this->subscriptionModel;
456
    }
457
}
458