TagReadTest.php 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace Muzich\CoreBundle\Tests\Searcher;
  3. use Muzich\CoreBundle\lib\UnitTest;
  4. use Muzich\CoreBundle\Util\TagLike;
  5. use Muzich\CoreBundle\lib\Tag as TagLib;
  6. class TagReadTest extends UnitTest
  7. {
  8. private function getTagsNames($response = array())
  9. {
  10. if (count($response))
  11. {
  12. $tags_clean = array();
  13. if (count($response['tags']))
  14. {
  15. foreach ($response['tags'] as $tag)
  16. {
  17. $tags_clean[] = $tag['name'];
  18. }
  19. }
  20. return $tags_clean;
  21. }
  22. }
  23. private function getTagsNamesForQuery($response = array())
  24. {
  25. if (count($response))
  26. {
  27. $tags_clean = array();
  28. foreach ($response as $tag)
  29. {
  30. $tags_clean[] = $tag->getName();
  31. }
  32. return $tags_clean;
  33. }
  34. }
  35. /**
  36. * Simple test des tags retournés
  37. */
  38. public function testSearchTag()
  39. {
  40. $bux = $this->getUser('bux');
  41. $cresults = array(
  42. 'Anarcho-punk', 'Dance-Punk', 'Horror punk', 'Pop-punk', 'Post-Punk',
  43. 'Punk rock', 'Ska-punk', 'Skate punk', 'Synthpunk'
  44. );
  45. $TagLike = new TagLike($this->getDoctrine()->getManager());
  46. $response = $TagLike->getSimilarTags('punk', $bux->getId());
  47. $this->assertEquals(count($cresults), count($response['tags']));
  48. foreach ($response['tags'] as $tag)
  49. {
  50. $this->assertTrue(in_array($tag['name'], $cresults));
  51. }
  52. }
  53. /**
  54. * Simple test des tags retournés
  55. */
  56. public function testSearchTagLike()
  57. {
  58. $bux = $this->getUser('bux');
  59. $cresults = array(
  60. 'Reggae'
  61. );
  62. $TagLike = new TagLike($this->getDoctrine()->getManager());
  63. $response = $TagLike->getSimilarTags('reggea', $bux->getId());
  64. $this->assertEquals(count($cresults), count($response['tags']));
  65. foreach ($response['tags'] as $tag)
  66. {
  67. $this->assertTrue(in_array($tag['name'], $cresults));
  68. }
  69. }
  70. /**
  71. * Test de l'ordre dans lequel les tags sont retourné
  72. */
  73. public function testSearchTagOrdered()
  74. {
  75. $bux = $this->getUser('bux');
  76. //////////////////////////
  77. $cresults = array(
  78. 'Anarcho-punk', 'Dance-Punk', 'Horror punk', 'Pop-punk', 'Post-Punk',
  79. 'Punk rock', 'Ska-punk', 'Skate punk', 'Synthpunk'
  80. );
  81. $TagLike = new TagLike($this->getDoctrine()->getManager());
  82. $tags = $this->getTagsNames($result = $TagLike->getSimilarTags('punk', $bux->getId()));
  83. $this->assertEquals($cresults, $tags);
  84. $this->assertEquals($result['same_found'], false);
  85. //////////////////////////
  86. $cresults = array(
  87. 'Anarcho-punk', 'Dance-Punk', 'Horror punk', 'Pop-punk', 'Post-Punk',
  88. 'Punk rock', 'Ska-punk', 'Skate punk', 'Synthpunk'
  89. );
  90. $TagLike = new TagLike($this->getDoctrine()->getManager());
  91. $tags = $this->getTagsNames($result = $TagLike->getSimilarTags('anarcho punk', $bux->getId()));
  92. $this->assertEquals($cresults, $tags);
  93. $this->assertEquals($result['same_found'], true);
  94. //////////////////////////
  95. $cresults = array(
  96. 'Anarcho-punk', 'Dance-Punk', 'Horror punk', 'Pop-punk', 'Post-Punk',
  97. 'Punk rock', 'Ska-punk', 'Skate punk', 'Synthpunk'
  98. );
  99. $TagLike = new TagLike($this->getDoctrine()->getManager());
  100. $tags = $this->getTagsNames($result = $TagLike->getSimilarTags('punk anarcho', $bux->getId()));
  101. $this->assertEquals($cresults, $tags);
  102. $this->assertEquals($result['same_found'], true);
  103. //////////////////////////
  104. $cresults = array(
  105. 'Anarcho-punk', 'Dance-Punk', 'Horror punk', 'Pop-punk', 'Post-Punk',
  106. 'Punk rock', 'Ska-punk', 'Skate punk', 'Synthpunk'
  107. );
  108. $TagLike = new TagLike($this->getDoctrine()->getManager());
  109. $tags = $this->getTagsNames($result = $TagLike->getSimilarTags('punk anar', $bux->getId()));
  110. $this->assertEquals($cresults, $tags);
  111. $this->assertEquals($result['same_found'], true);
  112. //////////////////////////
  113. $cresults = array(
  114. 'Skate punk', 'Ska-punk', 'Ska', 'Anarcho-punk', 'Dance-Punk', 'Horror punk',
  115. 'Pop-punk', 'Post-Punk', 'Punk rock', 'Ska-jazz', 'Skacore', 'Synthpunk'
  116. );
  117. $TagLike = new TagLike($this->getDoctrine()->getManager());
  118. $tags = $this->getTagsNames($result = $TagLike->getSimilarTags('ska punk', $bux->getId()));
  119. $this->assertEquals($cresults, $tags);
  120. $this->assertEquals($result['same_found'], true);
  121. //////////////////////////
  122. $cresults = array(
  123. 'Horror punk'
  124. );
  125. $TagLike = new TagLike($this->getDoctrine()->getManager());
  126. $tags = $this->getTagsNames($result = $TagLike->getSimilarTags('horror', $bux->getId()));
  127. $this->assertEquals($cresults, $tags);
  128. $this->assertEquals($result['same_found'], false);
  129. }
  130. /**
  131. * Check de la récupération des tags de profil
  132. */
  133. public function testSearchTagProfile()
  134. {
  135. $bux = $this->getUser('bux');
  136. $joelle = $this->getUser('joelle');
  137. $paul = $this->getUser('paul');
  138. ////////////////////////////////
  139. $rtags = array('Electro', 'Hardcore', 'Hardtek', 'Metal');
  140. $tags = $this->getTagsNamesForQuery($this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  141. ->getElementsTags($bux->getId(), $bux->getId())
  142. );
  143. $this->assertEquals($rtags, $tags);
  144. ////////////////////////////////
  145. $rtags = array('Beatbox', 'Chanteuse', 'Dubstep', 'Medieval');
  146. $tags = $this->getTagsNamesForQuery($this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  147. ->getElementsTags($joelle->getId(), $joelle->getId())
  148. );
  149. $this->assertEquals($rtags, $tags);
  150. ////////////////////////////////
  151. $rtags = array('Hardtek', 'Psytrance', 'Tribe');
  152. $tags = $this->getTagsNamesForQuery($this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  153. ->getElementsTags($paul->getId(), $paul->getId())
  154. );
  155. $this->assertEquals($rtags, $tags);
  156. }
  157. public function testSearchTagFavorites()
  158. {
  159. $bux = $this->getUser('bux');
  160. $joelle = $this->getUser('joelle');
  161. $paul = $this->getUser('paul');
  162. ////////////////////////////////
  163. $rtags = array('Hardtek');
  164. $tags = $this->getTagsNamesForQuery($this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  165. ->getTags($bux->getId(), $bux->getId())
  166. );
  167. $this->assertEquals($rtags, $tags);
  168. ////////////////////////////////
  169. $rtags = null;
  170. $tags = $this->getTagsNamesForQuery($this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  171. ->getTags($joelle->getId(), $joelle->getId())
  172. );
  173. $this->assertEquals($rtags, $tags);
  174. ////////////////////////////////
  175. $rtags = array('Hardtek', 'Tribe');
  176. $tags = $this->getTagsNamesForQuery($this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  177. ->getTags($paul->getId(), $paul->getId())
  178. );
  179. $this->assertEquals($rtags, $tags);
  180. }
  181. /*
  182. * Test des opération de création d'une liste ordonné de tags en fonction
  183. * d'une liste d'élèments
  184. */
  185. public function testTagOrder()
  186. {
  187. $bux = $this->getUser('bux');
  188. $joelle = $this->getUser('joelle');
  189. $tag_lib = new TagLib();
  190. $hardtek = $this->findOneBy('Tag', array('name' => 'Hardtek'));
  191. $metal = $this->findOneBy('Tag', array('name' => 'Metal'));
  192. $electro = $this->findOneBy('Tag', array('name' => 'Electro'));
  193. $hardcore = $this->findOneBy('Tag', array('name' => 'Hardcore'));
  194. $chanteuse = $this->findOneBy('Tag', array('name' => 'Chanteuse'));
  195. $dubstep = $this->findOneBy('Tag', array('name' => 'Dubstep'));
  196. $medieval = $this->findOneBy('Tag', array('name' => 'Medieval'));
  197. $beatbox = $this->findOneBy('Tag', array('name' => 'Beatbox'));
  198. /*
  199. * Test de la récuparéation de l'ordre des tags
  200. */
  201. $search = new \Muzich\CoreBundle\Searcher\ElementSearcher();
  202. $search->init(array(
  203. 'user_id' => $bux->getId()
  204. ));
  205. $elements = $search->getElements($this->getDoctrine(), $bux->getId());
  206. $tag_reference = $tag_lib->getOrderedTagsWithElements($elements);
  207. $this->assertEquals(array(
  208. $hardtek->getId(),
  209. $metal->getId(),
  210. $electro->getId(),
  211. $hardcore->getId()
  212. ), $tag_reference);
  213. ////////////
  214. $search = new \Muzich\CoreBundle\Searcher\ElementSearcher();
  215. $search->init(array(
  216. 'user_id' => $joelle->getId()
  217. ));
  218. $elements = $search->getElements($this->getDoctrine(), $bux->getId());
  219. $tag_reference = $tag_lib->getOrderedTagsWithElements($elements);
  220. $this->assertEquals(array(
  221. $chanteuse->getId(),
  222. $dubstep->getId(),
  223. $medieval->getId(),
  224. $beatbox->getId()
  225. ), $tag_reference);
  226. /*
  227. * Test du trie de tags en fonction d'une liste référente
  228. */
  229. // Tag non ordonés
  230. $tags_disordered = array(
  231. $medieval,
  232. $beatbox,
  233. $hardcore,
  234. $dubstep,
  235. $chanteuse
  236. );
  237. // On ordonne tout ça avec la référence calculé juste avant
  238. $tags_ordered = $tag_lib->sortTagWithOrderedReference($tags_disordered, $tag_reference);
  239. $tags_ordered_ids = array();
  240. foreach ($tags_ordered as $tag_ordered)
  241. {
  242. $tags_ordered_ids[] = $tag_ordered->getId();
  243. }
  244. $this->assertEquals(
  245. array(
  246. $chanteuse->getId(),
  247. $dubstep->getId(),
  248. $medieval->getId(),
  249. $beatbox->getId(),
  250. $hardcore->getId()
  251. ), $tags_ordered_ids
  252. );
  253. }
  254. }