PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/features/bootstrap/DataContext.php

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