Browse Source

Evolution #124: Du tessst et encore du test: Voir nouveau éléments, voir plus d'éléments.

bastien 13 years ago
parent
commit
142ffdf8ac

+ 1 - 0
src/Muzich/CoreBundle/Controller/SearchController.php View File

@@ -28,6 +28,7 @@ class SearchController extends Controller
28 28
     }
29 29
     
30 30
     return $this->jsonResponse(array(
31
+      'status'  => 'success',
31 32
       'count'   => $count,
32 33
       'message' => $message,
33 34
       'html'    => $html,

+ 198 - 0
src/Muzich/CoreBundle/Tests/Controller/HomeControllerTest.php View File

@@ -4,6 +4,7 @@ namespace Muzich\CoreBundle\Tests\Controller;
4 4
 
5 5
 use Muzich\CoreBundle\lib\FunctionalTest;
6 6
 use Muzich\CoreBundle\Searcher\ElementSearcher;
7
+use Muzich\CoreBundle\Entity\Element;
7 8
 
8 9
 class HomeControllerTest extends FunctionalTest
9 10
 {
@@ -537,4 +538,201 @@ class HomeControllerTest extends FunctionalTest
537 538
     
538 539
   }
539 540
   
541
+  /**
542
+   * Test de la récupération de nouveaux éléments
543
+   * 
544
+   */
545
+  public function testSeeNew()
546
+  {
547
+    $this->client = self::createClient();
548
+    $this->connectUser('bux', 'toor');
549
+    
550
+    $bux = $this->getUser();
551
+    $bob = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
552
+      ->findOneByUsername('bob')
553
+    ;
554
+    $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
555
+    $tribe_id   = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
556
+
557
+    // On récupère l'id du dernier element affiché
558
+    $extract = $this->crawler->filter('ul.elements li.element')
559
+       ->extract(array('id'));
560
+    
561
+    $first_id = (int)str_replace('element_', '', $extract[0]);
562
+    
563
+    $url = $this->generateUrl('element_new_count', array('refid' => $first_id));
564
+    // On effectue la kekete ajax
565
+    $crawler = $this->client->request(
566
+      'GET', 
567
+      $url, 
568
+      array(), 
569
+      array(), 
570
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
571
+    );
572
+    
573
+    $this->isResponseSuccess();
574
+    
575
+    $response = json_decode($this->client->getResponse()->getContent(), true);
576
+    $this->assertEquals($response['status'], 'success');
577
+    $this->assertEquals($response['count'], '0');
578
+    $this->assertEquals($response['message'], '');
579
+    
580
+    $this->addElementAjax('NewElement One', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
581
+    $this->addElementAjax('NewElement Two', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
582
+    
583
+    // On refait la même demande, deux éléments sont nouveaux
584
+    $url = $this->generateUrl('element_new_count', array('refid' => $first_id));
585
+    // On effectue la kekete ajax
586
+    $crawler = $this->client->request(
587
+      'GET', 
588
+      $url, 
589
+      array(), 
590
+      array(), 
591
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
592
+    );
593
+    
594
+    $response = json_decode($this->client->getResponse()->getContent(), true);
595
+    $this->assertEquals($response['status'], 'success');
596
+    $this->assertEquals($response['count'], '2');
597
+    
598
+    // Si on demande la récupération des nouveaux éléments on doit les obtenirs
599
+    $url = $this->generateUrl('element_new_get', array('refid' => $first_id));
600
+    // On effectue la kekete ajax
601
+    $crawler = $this->client->request(
602
+      'GET', 
603
+      $url, 
604
+      array(), 
605
+      array(), 
606
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
607
+    );
608
+    
609
+    $this->isResponseSuccess();
610
+    
611
+    $response = json_decode($this->client->getResponse()->getContent(), true);
612
+    $this->assertEquals($response['status'], 'success');
613
+    $this->assertEquals($response['count'], '0');
614
+    $this->assertTrue(!is_null($response['html']));
615
+    $this->assertTrue(strpos($response['html'], 'NewElement One') !== false);
616
+    $this->assertTrue(strpos($response['html'], 'NewElement Two') !== false);
617
+    
618
+    // On ajoute 10 autres éléments (NOTE: le 10 est hardcodé dans ce test
619
+    // , c'est la limite d'affichage d'éléments)
620
+    $this->addElementAjax('NewElement 3', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
621
+    $this->addElementAjax('NewElement 4', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
622
+    $this->addElementAjax('NewElement 5', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
623
+    $this->addElementAjax('NewElement 6', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
624
+    $this->addElementAjax('NewElement 7', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
625
+    $this->addElementAjax('NewElement 8', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
626
+    $this->addElementAjax('NewElement 9', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
627
+    $this->addElementAjax('NewElement 10', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
628
+    $this->addElementAjax('NewElement 11', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
629
+    $this->addElementAjax('NewElement 12', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
630
+  
631
+    // On va refaire un count des nouveaux éléments
632
+    // Ca devrat nous répondree 12 puisque on utilise l'id de référence du début
633
+    
634
+    // On refait la même demande, deux éléments sont nouveaux
635
+    $url = $this->generateUrl('element_new_count', array('refid' => $first_id));
636
+    // On effectue la kekete ajax
637
+    $crawler = $this->client->request(
638
+      'GET', 
639
+      $url, 
640
+      array(), 
641
+      array(), 
642
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
643
+    );
644
+    
645
+    $response = json_decode($this->client->getResponse()->getContent(), true);
646
+    $this->assertEquals($response['status'], 'success');
647
+    $this->assertEquals($response['count'], '12');
648
+    
649
+    // Si on demande la récupération des nouveaux éléments on doit en obtenir 10
650
+    // et en rester 2
651
+    $url = $this->generateUrl('element_new_get', array('refid' => $first_id));
652
+    // On effectue la kekete ajax
653
+    $crawler = $this->client->request(
654
+      'GET', 
655
+      $url, 
656
+      array(), 
657
+      array(), 
658
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
659
+    );
660
+    
661
+    $this->isResponseSuccess();
662
+    
663
+    $response = json_decode($this->client->getResponse()->getContent(), true);
664
+    $this->assertEquals($response['status'], 'success');
665
+    $this->assertEquals($response['count'], '2');
666
+    $this->assertTrue(!is_null($response['html']));
667
+    $this->assertTrue(strpos($response['html'], 'NewElement One') !== false);
668
+    $this->assertTrue(strpos($response['html'], 'NewElement Two') !== false);
669
+    $this->assertTrue(strpos($response['html'], 'NewElement 3') !== false);
670
+    $this->assertTrue(strpos($response['html'], 'NewElement 4') !== false);
671
+    $this->assertTrue(strpos($response['html'], 'NewElement 5') !== false);
672
+    $this->assertTrue(strpos($response['html'], 'NewElement 6') !== false);
673
+    $this->assertTrue(strpos($response['html'], 'NewElement 7') !== false);
674
+    $this->assertTrue(strpos($response['html'], 'NewElement 8') !== false);
675
+    $this->assertTrue(strpos($response['html'], 'NewElement 9') !== false);
676
+    $this->assertTrue(strpos($response['html'], 'NewElement 10') !== false);
677
+    $this->assertTrue(strpos($response['html'], 'NewElement 11') === false);
678
+    $this->assertTrue(strpos($response['html'], 'NewElement 12') === false);
679
+    
680
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
681
+      ->findOneByName('NewElement 10')
682
+    ;
683
+    // notre nouvel id référent en celui de NewElement 10
684
+    // On renouvelle la demande, il ne doit y avoir que 2 élément nouveau a afficher
685
+    $url = $this->generateUrl('element_new_count', array('refid' => $element->getId()));
686
+    // On effectue la kekete ajax
687
+    $crawler = $this->client->request(
688
+      'GET', 
689
+      $url, 
690
+      array(), 
691
+      array(), 
692
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
693
+    );
694
+    
695
+    $response = json_decode($this->client->getResponse()->getContent(), true);
696
+    $this->assertEquals($response['status'], 'success');
697
+    $this->assertEquals($response['count'], '2');
698
+    
699
+  }
700
+  
701
+  public function testMoreElements()
702
+  {
703
+    $this->client = self::createClient();
704
+    $this->connectUser('bux', 'toor');
705
+    
706
+    $bux = $this->getUser();
707
+    
708
+    // On récupère l'id du dernier element affiché
709
+    $extract = $this->crawler->filter('ul.elements li.element')
710
+       ->extract(array('id'));
711
+    
712
+    // !!!!!! NOTE !!!!! : 9 est hardcodé ici: la config est de 10 éléments en affichage
713
+    $id_limit = (int)str_replace('element_', '', $extract[9]);
714
+    
715
+    $url = $this->generateUrl('search_elements_more', array(
716
+      'id_limit'  => $id_limit,
717
+      'invertcolors' => 0
718
+    ));
719
+    
720
+    // We want mooooore
721
+    // On effectue la kekete ajax
722
+    $crawler = $this->client->request(
723
+      'GET', 
724
+      $url, 
725
+      array(), 
726
+      array(), 
727
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
728
+    );
729
+    
730
+    $response = json_decode($this->client->getResponse()->getContent(), true);
731
+    $this->assertEquals($response['status'], 'success');
732
+    $this->assertEquals($response['count'], '4');  // HARDCODE fixtures
733
+    $this->assertEquals($response['end'], true);  // HARDCODE fixtures
734
+    
735
+  }
736
+  
737
+  
540 738
 }

+ 156 - 0
src/Muzich/CoreBundle/Tests/Controller/ShowControllerTest.php View File

@@ -0,0 +1,156 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Controller;
4
+
5
+use Muzich\CoreBundle\lib\FunctionalTest;
6
+
7
+class ShowControllerTest extends FunctionalTest
8
+{
9
+  
10
+  public function testViewMoreOnProfile()
11
+  {
12
+    $this->client = self::createClient();
13
+    $this->connectUser('bux', 'toor');
14
+    
15
+    $bux = $this->getUser();
16
+    
17
+    $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
18
+    $tribe_id   = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
19
+
20
+    // En premier lieux on va devoir ajouter des éléments.
21
+    $this->addElementAjax('PoPElement 1', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
22
+    $this->addElementAjax('PoPElement 2', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
23
+    $this->addElementAjax('PoPElement 3', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
24
+    $this->addElementAjax('PoPElement 4', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
25
+    
26
+    // D'après les fixtures on aura 2 éléments en plus des 10 (paramétre) de la page
27
+    // Ouverture de la page profil
28
+    $this->crawler = $this->client->request('GET', $this->generateUrl('show_user', array('slug' => $bux->getSlug())));
29
+    $this->isResponseSuccess();
30
+    
31
+    $this->exist('span.element_name:contains("PoPElement 4")'); 
32
+    $this->exist('span.element_name:contains("PoPElement 3")'); 
33
+    $this->exist('span.element_name:contains("PoPElement 2")'); 
34
+    $this->exist('span.element_name:contains("PoPElement 1")'); 
35
+    $this->exist('span.element_name:contains("Ed Cox - La fanfare des teuffeurs (Hardcordian)")'); 
36
+    $this->exist('span.element_name:contains("Babylon Pression - Des Tasers et des Pauvres")'); 
37
+    $this->exist('span.element_name:contains("AZYD AZYLUM Live au Café Provisoire")'); 
38
+    $this->exist('span.element_name:contains("SOULFLY - Prophecy")'); 
39
+    $this->exist('span.element_name:contains("KoinkOin - H5N1")'); 
40
+    $this->exist('span.element_name:contains("Antropod - Polakatek")'); 
41
+    $this->notExist('span.element_name:contains("Dtc che passdrop")'); 
42
+    $this->notExist('span.element_name:contains("Heretik System Popof - Resistance")'); 
43
+    
44
+    // L'élément de référence est celui en bas de la liste
45
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
46
+      ->findOneByName('Antropod - Polakatek')
47
+    ;
48
+    $tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
49
+      ->getElementsTags($bux->getId())      
50
+    ;
51
+    
52
+    $ids = array();
53
+    foreach ($tags as $tag)
54
+    {
55
+      $ids[] = $tag->getId();
56
+    }
57
+    
58
+    // On fait la demande pour en voir plus
59
+    $url = $this->generateUrl('show_elements_get', array(
60
+      'type' => 'user',
61
+      'object_id' => $bux->getId(),
62
+      'tags_ids_json' => json_encode($ids),
63
+      'id_limit' => $element->getId()
64
+    ));
65
+    $crawler = $this->client->request(
66
+      'GET', 
67
+      $url, 
68
+      array(), 
69
+      array(), 
70
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
71
+    );
72
+    
73
+    $response = json_decode($this->client->getResponse()->getContent(), true);
74
+    $this->assertEquals($response['status'], 'success');
75
+    $this->assertTrue(strpos($response['html'], 'Dtc che passdrop') !== false);
76
+    $this->assertTrue(strpos($response['html'], 'Heretik System Popof - Resistance') !== false);
77
+  }
78
+  
79
+  public function testViewMoreOnGroup()
80
+  {
81
+    $this->client = self::createClient();
82
+    $this->connectUser('bux', 'toor');
83
+    
84
+    $bux = $this->getUser();    
85
+    $fan_de_psy = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
86
+
87
+    $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
88
+    $tribe_id   = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
89
+
90
+    // En premier lieux on va devoir ajouter des éléments.
91
+    $this->addElementAjax('PsyElement 1', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)), $fan_de_psy->getSlug());
92
+    $this->addElementAjax('PsyElement 2', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)), $fan_de_psy->getSlug());
93
+    $this->addElementAjax('PsyElement 3', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)), $fan_de_psy->getSlug());
94
+    $this->addElementAjax('PsyElement 4', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)), $fan_de_psy->getSlug());
95
+    $this->addElementAjax('PsyElement 5', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)), $fan_de_psy->getSlug());
96
+    $this->addElementAjax('PsyElement 6', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)), $fan_de_psy->getSlug());
97
+    $this->addElementAjax('PsyElement 7', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)), $fan_de_psy->getSlug());
98
+    $this->addElementAjax('PsyElement 8', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)), $fan_de_psy->getSlug());
99
+    $this->addElementAjax('PsyElement 9', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)), $fan_de_psy->getSlug());
100
+    $this->addElementAjax('PsyElement 10', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)), $fan_de_psy->getSlug());
101
+    
102
+    // D'après les fixtures on aura 2 éléments en plus des 10 (paramétre) de la page
103
+    // Ouverture de la page profil
104
+    $this->crawler = $this->client->request('GET', $this->generateUrl('show_group', array('slug' => $fan_de_psy->getSlug())));
105
+    $this->isResponseSuccess();
106
+    
107
+    $this->exist('span.element_name:contains("PsyElement 10")'); 
108
+    $this->exist('span.element_name:contains("PsyElement 9")'); 
109
+    $this->exist('span.element_name:contains("PsyElement 8")'); 
110
+    $this->exist('span.element_name:contains("PsyElement 7")'); 
111
+    $this->exist('span.element_name:contains("PsyElement 6")'); 
112
+    $this->exist('span.element_name:contains("PsyElement 5")'); 
113
+    $this->exist('span.element_name:contains("PsyElement 4")'); 
114
+    $this->exist('span.element_name:contains("PsyElement 3")'); 
115
+    $this->exist('span.element_name:contains("PsyElement 5")'); 
116
+    $this->exist('span.element_name:contains("PsyElement 1")'); 
117
+    $this->notExist('span.element_name:contains("Infected mushroom - Muse Breaks")'); 
118
+    $this->notExist('span.element_name:contains("Infected Mushroom - Psycho")'); 
119
+    
120
+    // L'élément de référence est celui en bas de la liste, donc le premier ajouté
121
+    // la haut (vuq ue l'on en a ajouté 10)
122
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
123
+      ->findOneByName('PsyElement 1')
124
+    ;
125
+    $tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
126
+      ->getElementsTags($fan_de_psy->getId())      
127
+    ;
128
+    
129
+    $ids = array();
130
+    foreach ($tags as $tag)
131
+    {
132
+      $ids[] = $tag->getId();
133
+    }
134
+    
135
+    // On fait la demande pour en voir plus
136
+    $url = $this->generateUrl('show_elements_get', array(
137
+      'type' => 'group',
138
+      'object_id' => $fan_de_psy->getId(),
139
+      'tags_ids_json' => json_encode($ids),
140
+      'id_limit' => $element->getId()
141
+    ));
142
+    $crawler = $this->client->request(
143
+      'GET', 
144
+      $url, 
145
+      array(), 
146
+      array(), 
147
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
148
+    );
149
+    
150
+    $response = json_decode($this->client->getResponse()->getContent(), true);
151
+    $this->assertEquals($response['status'], 'success');
152
+    $this->assertTrue(strpos($response['html'], 'Infected mushroom - Muse Breaks') !== false);
153
+    $this->assertTrue(strpos($response['html'], 'Infected Mushroom - Psycho') !== false);
154
+  }
155
+  
156
+}

+ 49 - 2
src/Muzich/CoreBundle/lib/FunctionalTest.php View File

@@ -63,9 +63,14 @@ class FunctionalTest extends WebTestCase
63 63
     ;
64 64
   }
65 65
   
66
-  protected function connectUser($login, $password)
66
+  protected function connectUser($login, $password, $client = null)
67 67
   {
68
-    $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
68
+    if (!$client)
69
+    {
70
+      $client = $this->client;
71
+    }
72
+    
73
+    $this->crawler = $client->request('GET', $this->generateUrl('index'));
69 74
     $this->isResponseSuccess();
70 75
 
71 76
     $this->assertEquals('anon.', $this->getUser());
@@ -393,4 +398,46 @@ class FunctionalTest extends WebTestCase
393 398
   {
394 399
     $this->assertTrue($this->client->getResponse()->isNotFound());
395 400
   }
401
+  
402
+  
403
+  
404
+  protected function addElementAjax($name, $url, $tags = '', $group_slug = null)
405
+  {
406
+    $this->crawler = $this->client->request('GET', $this->generateUrl('home'));
407
+    
408
+    $extract = $this->crawler->filter('input[name="element_add[_token]"]')
409
+      ->extract(array('value'));
410
+    $csrf = $extract[0];
411
+    
412
+    $url_ajax = $this->generateUrl('element_add');
413
+    if ($group_slug)
414
+    {
415
+      $url_ajax = $this->generateUrl('element_add', array('group_slug' => $group_slug));
416
+    }
417
+    
418
+    $this->crawler = $this->client->request(
419
+      'POST', 
420
+      $url_ajax, 
421
+      array(
422
+          'element_add' => array(
423
+              '_token' => $csrf,
424
+              'name'   => $name,
425
+              'url'    => $url,
426
+              'tags'   => $tags
427
+          )
428
+        
429
+      ), 
430
+      array(), 
431
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
432
+    );
433
+    
434
+    $this->isResponseSuccess();
435
+    $response = json_decode($this->client->getResponse()->getContent(), true);
436
+    $this->assertEquals($response['status'], 'success');
437
+    
438
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
439
+      ->findOneByName($name)
440
+    ;
441
+    $this->assertTrue(!is_null($element));
442
+  }
396 443
 }

+ 1 - 0
src/Muzich/HomeBundle/Controller/ShowController.php View File

@@ -151,6 +151,7 @@ class ShowController extends Controller
151 151
     }
152 152
     
153 153
     return $this->jsonResponse(array(
154
+      'status'  => 'success',
154 155
       'count'   => $count,
155 156
       'message' => $message,
156 157
       'html'    => $html