FunctionalTest.php 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. namespace Muzich\CoreBundle\lib;
  3. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  4. use Symfony\Bundle\FrameworkBundle\Client;
  5. use Symfony\Component\DomCrawler\Crawler;
  6. class FunctionalTest extends WebTestCase
  7. {
  8. /**
  9. *
  10. * @var Client
  11. */
  12. protected $client;
  13. /**
  14. *
  15. * @var Crawler
  16. */
  17. protected $crawler;
  18. protected function outputDebug()
  19. {
  20. unlink('/home/bux/.debug/out.html');
  21. $monfichier = fopen('/home/bux/.debug/out.html', 'a+');
  22. fwrite($monfichier, $this->client->getResponse()->getContent());
  23. }
  24. /**
  25. * Retourne l'objet User
  26. *
  27. * @return \Muzich\CoreBundle\Entity\User
  28. */
  29. protected function getUser()
  30. {
  31. return $this->client->getContainer()->get('security.context')->getToken()->getUser();
  32. }
  33. protected function connectUser($login, $password)
  34. {
  35. $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
  36. $this->isResponseSuccess();
  37. $this->assertEquals('anon.', $this->getUser());
  38. $this->exist('div.login');
  39. $this->exist('form[action="'.($url = $this->generateUrl('fos_user_security_check')).'"]');
  40. $this->exist('form[action="'.$url.'"] input[id="username"]');
  41. $this->exist('form[action="'.$url.'"] input[id="password"]');
  42. $this->exist('form[action="'.$url.'"] input[id="remember_me"]');
  43. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  44. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  45. $form['_username'] = $login;
  46. $form['_password'] = $password;
  47. $form['_remember_me'] = true;
  48. $this->submit($form);
  49. $this->isResponseRedirection();
  50. $this->followRedirection();
  51. $this->isResponseSuccess();
  52. $user = $this->getUser();
  53. if ('anon.' != $user)
  54. {
  55. $this->assertEquals($login, $user->getUsername());
  56. }
  57. else
  58. {
  59. $this->assertTrue(false);
  60. }
  61. }
  62. protected function disconnectUser()
  63. {
  64. $this->crawler = $this->client->request('GET', $this->generateUrl('fos_user_security_logout'));
  65. }
  66. protected function validate_registrate_user_form($form, $username, $email, $pass1, $pass2, $token)
  67. {
  68. $form['fos_user_registration_form[username]'] = $username;
  69. $form['fos_user_registration_form[email]'] = $email;
  70. $form['fos_user_registration_form[plainPassword][first]'] = $pass1;
  71. // Un des mots de passe est incorrect
  72. $form['fos_user_registration_form[plainPassword][second]'] = $pass2;
  73. $form['fos_user_registration_form[token]'] = $token;
  74. $this->submit($form);
  75. }
  76. protected function procedure_registration_success($username, $email, $pass1, $pass2, $token)
  77. {
  78. $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
  79. $this->isResponseSuccess();
  80. $this->assertEquals('anon.', $this->getUser());
  81. $url = $this->generateUrl('register');
  82. // Les mots de passes sont différents
  83. $this->validate_registrate_user_form(
  84. $this->selectForm('form[action="'.$url.'"] input[type="submit"]'),
  85. $username,
  86. $email,
  87. $pass1,
  88. $pass2,
  89. $token
  90. );
  91. $this->isResponseRedirection();
  92. $this->followRedirection();
  93. $this->isResponseSuccess();
  94. if ('anon.' != ($user = $this->getUser()))
  95. {
  96. // Nous ne sommes pas identifiés
  97. $this->assertEquals($username, $user->getUsername());
  98. // L'utilisateur n'est pas enregistré, il ne doit donc pas être en base
  99. $db_user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  100. ->findOneByUsername($username)
  101. ;
  102. $this->assertTrue(!is_null($db_user));
  103. }
  104. else
  105. {
  106. $this->assertTrue(false);
  107. }
  108. }
  109. protected function procedure_registration_failure($username, $email, $pass1, $pass2, $token)
  110. {
  111. $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
  112. $this->isResponseSuccess();
  113. $this->assertEquals('anon.', $this->getUser());
  114. $url = $this->generateUrl('register');
  115. // Les mots de passes sont différents
  116. $this->validate_registrate_user_form(
  117. $this->selectForm('form[action="'.$url.'"] input[type="submit"]'),
  118. $username,
  119. $email,
  120. $pass1,
  121. $pass2,
  122. $token
  123. );
  124. $this->isResponseSuccess();
  125. if ('anon.' === ($user = $this->getUser()))
  126. {
  127. // Nous ne sommes pas identifiés
  128. $this->assertEquals('anon.', $user);
  129. // L'utilisateur n'est pas enregistré, il ne doit donc pas être en base
  130. $db_user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  131. ->findOneByUsername($username)
  132. ;
  133. $this->assertTrue(is_null($db_user));
  134. }
  135. else
  136. {
  137. $this->assertTrue(false);
  138. }
  139. }
  140. /**
  141. * Procédure d'ajout d'un élément
  142. *
  143. * @param string $name
  144. * @param string $url
  145. * @param array $tags
  146. * @param string $group_slug
  147. */
  148. protected function procedure_add_element($name, $url, $tags, $group_slug = null)
  149. {
  150. if (!$group_slug)
  151. {
  152. $this->crawler = $this->client->request('GET', $this->generateUrl('home'));
  153. $form_url = $this->generateUrl('element_add');
  154. }
  155. else
  156. {
  157. $this->crawler = $this->client->request('GET', $this->generateUrl('show_group', array('slug' => $group_slug)));
  158. $form_url = $this->generateUrl('element_add', array('group_slug' => $group_slug));
  159. }
  160. $this->isResponseSuccess();
  161. $form = $this->selectForm('form[action="'.$form_url.'"] input[type="submit"]');
  162. $form['element_add[name]'] = $name;
  163. $form['element_add[url]'] = $url;
  164. $form['element_add[tags]'] = json_encode($tags);
  165. $this->submit($form);
  166. }
  167. /**
  168. * Retourne un utilisateur en allant le chercher en base.
  169. *
  170. * @param string $username
  171. * @return \Muzich\CoreBundle\Entity\User
  172. */
  173. protected function findUserByUsername($username)
  174. {
  175. return $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:User')
  176. ->findOneByUsername($username)
  177. ;
  178. }
  179. /**
  180. * Generates a URL from the given parameters.
  181. *
  182. * @param string $route
  183. * @param array $parameters
  184. * @param boolean $absolute
  185. *
  186. * @return string (url generated)
  187. */
  188. protected function generateUrl($route, $parameters = array(), $absolute = false)
  189. {
  190. return $this->client->getContainer()->get('router')->generate($route, $parameters, $absolute);
  191. }
  192. protected function getContainer()
  193. {
  194. return $this->client->getContainer();
  195. }
  196. protected function getSession()
  197. {
  198. return $this->getContainer()->get('session');
  199. }
  200. protected function getCollector($name)
  201. {
  202. return$this->client->getProfile()->getCollector($name);
  203. }
  204. /**
  205. * Retourne le MessageDataCollector en cours
  206. *
  207. * @return Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector
  208. */
  209. protected function getMailerMessageDataCollector()
  210. {
  211. return $this->getCollector('swiftmailer');
  212. }
  213. protected function clickOnLink($link)
  214. {
  215. $this->crawler = $this->client->click($link);
  216. }
  217. /**
  218. *
  219. * @return \Symfony\Bundle\DoctrineBundle\Registry
  220. */
  221. protected function getDoctrine()
  222. {
  223. return $this->client->getContainer()->get('doctrine');
  224. }
  225. /**
  226. * Test l'existance d'un element
  227. *
  228. * @param string $filter
  229. */
  230. protected function exist($filter)
  231. {
  232. $this->assertTrue($this->crawler->filter($filter)->count() > 0);
  233. }
  234. /**
  235. * Test l'inexistance d'un element
  236. *
  237. * @param string $filter
  238. */
  239. protected function notExist($filter)
  240. {
  241. $this->assertFalse($this->crawler->filter($filter)->count() > 0);
  242. }
  243. /**
  244. * Retourne un objet lien
  245. *
  246. * @param string $filter
  247. * @return \Symfony\Component\DomCrawler\Link
  248. */
  249. protected function selectLink($filter)
  250. {
  251. return $this->crawler->filter($filter)->link();
  252. }
  253. // /**
  254. // * Clique sur un link
  255. // *
  256. // * @param type $link
  257. // */
  258. // protected function click($link)
  259. // {
  260. // $this->crawler = $this->client->click($link);
  261. // }
  262. /**
  263. * Retourne un formulaire, en filtrant le BOUTON SUBMIT !!
  264. *
  265. * @param string $filter
  266. * @return \Symfony\Component\DomCrawler\Form
  267. */
  268. protected function selectForm($filter)
  269. {
  270. return $this->crawler->filter($filter)->form();
  271. }
  272. /**
  273. * Soumet un formulaire
  274. *
  275. * @param type $form
  276. */
  277. protected function submit($form, $params = array())
  278. {
  279. $this->crawler = $this->client->submit($form, $params);
  280. }
  281. /**
  282. * Ordonne au client de suivre la redirection
  283. */
  284. protected function followRedirection()
  285. {
  286. $this->crawler = $this->client->followRedirect();
  287. }
  288. /**
  289. * Contrôle le Codestatus de la réponse
  290. *
  291. * @param int $code
  292. */
  293. protected function isStatusCode($code)
  294. {
  295. $this->assertEquals($code, $this->client->getResponse()->getStatusCode());
  296. }
  297. /**
  298. * Contrôle que le CodeStatus de la Response correspond bien a celle d'une
  299. * redirection
  300. */
  301. protected function isResponseRedirection()
  302. {
  303. $this->assertTrue($this->client->getResponse()->isRedirection());
  304. }
  305. /**
  306. * Contrôle que le CodeStatus de la Response correspond bien a celle d'un Ok
  307. */
  308. protected function isResponseSuccess()
  309. {
  310. $this->assertTrue($this->client->getResponse()->isSuccessful());
  311. }
  312. /**
  313. * Contrôle que le CodeStatus de la Response correspond bien a celle d'un Ok
  314. */
  315. protected function isResponseNotFound()
  316. {
  317. $this->assertTrue($this->client->getResponse()->isNotFound());
  318. }
  319. }