UnitTest.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Muzich\CoreBundle\lib;
  3. require_once(__DIR__ . "/../../../../app/AppKernel.php");
  4. use Muzich\CoreBundle\Entity\Element;
  5. use Muzich\CoreBundle\Managers\ElementManager;
  6. class UnitTest extends \PHPUnit_Framework_TestCase
  7. {
  8. protected $_container;
  9. public function __construct()
  10. {
  11. $kernel = new \AppKernel("test", true);
  12. $kernel->boot();
  13. $this->_container = $kernel->getContainer();
  14. parent::__construct();
  15. }
  16. protected function get($service)
  17. {
  18. return $this->_container->get($service);
  19. }
  20. /**
  21. *
  22. * @return \Doctrine\Bundle\DoctrineBundle\Registry
  23. */
  24. protected function getDoctrine()
  25. {
  26. return $this->get('doctrine');
  27. }
  28. protected function getParam($param)
  29. {
  30. return $this->_container->getParameter($param);
  31. }
  32. protected function proceed_elementAndFill($user, $name, $url, $tag_ids, $final_embed)
  33. {
  34. $element = new Element();
  35. $element->setName($name);
  36. $element->setTags(json_encode($tag_ids));
  37. $element->setUrl($url);
  38. $factory = new ElementManager(
  39. $element,
  40. $this->getDoctrine()->getEntityManager(),
  41. $this->_container
  42. );
  43. $factory->proceedFill($user);
  44. $this->assertEquals($element->getName(), $name);
  45. $this->assertEquals($element->getUrl(), $url);
  46. // check tags
  47. $element_tag_ids = array();
  48. foreach ($element->getTags() as $tag)
  49. {
  50. $element_tag_ids[] = $tag->getId();
  51. }
  52. $this->assertEquals($element_tag_ids, $tag_ids);
  53. $this->assertEquals($element->getEmbed(), $final_embed);
  54. }
  55. protected function proceed_element_datas_api($user, $url)
  56. {
  57. $element = new Element();
  58. $element->setUrl($url);
  59. $factory = new ElementManager($element,
  60. $this->getDoctrine()->getEntityManager(), $this->_container);
  61. $factory->proceedFill($user);
  62. return $element->getDatas();
  63. }
  64. protected function getUser($username)
  65. {
  66. return $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  67. ->findOneByUsername($username)
  68. ;
  69. }
  70. protected function getTag($name)
  71. {
  72. return $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  73. ->findOneByName($name)
  74. ;
  75. }
  76. protected function persist($entity)
  77. {
  78. $this->getDoctrine()->getEntityManager()->persist($entity);
  79. }
  80. protected function flush()
  81. {
  82. $this->getDoctrine()->getEntityManager()->flush();
  83. }
  84. /**
  85. *
  86. * @return \Doctrine\ORM\EntityManager
  87. */
  88. protected function getEntityManager()
  89. {
  90. return $this->getDoctrine()->getEntityManager();
  91. }
  92. /**
  93. * Raccourcis de findOneBy
  94. *
  95. * @param string $entityName
  96. * @param array $params
  97. * @return object
  98. */
  99. protected function findOneBy($entityName, array $params)
  100. {
  101. return $this->getEntityManager()->getRepository('MuzichCoreBundle:'.$entityName)
  102. ->findOneBy($params);
  103. }
  104. }