Browse Source

Ecriture de tests: Ajout et mise a jour d'un Groupe.

bastien 13 years ago
parent
commit
fa733a6c6a

+ 9 - 0
src/Muzich/CoreBundle/Managers/GroupManager.php View File

@@ -43,6 +43,15 @@ class GroupManager
43 43
   {
44 44
     // La procédure se charge pour le moment des tags
45 45
     $this->group->setTags(null);
46
+    // Il faut supprimer tous les liens vers les tags
47
+    foreach ($this->em->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
48
+      ->findBy(array(
49
+        'group' => $this->group->getId()
50
+      )) as $group_tag)     
51
+    {
52
+      $this->em->remove($group_tag);
53
+    }
54
+    // Pour etablir les nouveaux liens
46 55
     $this->group->setTagsWithIds($this->em, $tags_ids);
47 56
   }
48 57
   

+ 175 - 0
src/Muzich/CoreBundle/Tests/Controller/GroupControllerTest.php View File

@@ -0,0 +1,175 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Controller;
4
+
5
+use Muzich\CoreBundle\lib\FunctionalTest;
6
+
7
+class GroupControllerTest extends FunctionalTest
8
+{
9
+ 
10
+  /**
11
+   * Test de création d'un groupe
12
+   */
13
+  public function testGroupAdd()
14
+  {
15
+    $this->connectUser('bob', 'toor');
16
+    
17
+    $Fans_de_psytrance = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
18
+    $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
19
+    $tribe_id   = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
20
+    
21
+    $this->crawler = $this->client->request('GET', $this->generateUrl('groups_own_list'));
22
+    
23
+    // Le groupe que nous voulons créer n'existe pas dans la base
24
+    $group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
25
+      ->findOneByName('HardtekMania')
26
+    ;
27
+    
28
+    $this->assertTrue(is_null($group));
29
+    
30
+    // bob administre le groupe Fan de psutrance
31
+    $this->exist('a[href="'.$this->generateUrl('show_group', array('slug' => $Fans_de_psytrance->getSlug())).'"]');
32
+    // On a le formulaire de création sur la page
33
+    $this->exist('form[action="'.($url = $this->generateUrl('group_add')).'"]');
34
+    $this->exist('form[action="'.$url.'"] input[id="group_name"]');
35
+    $this->exist('form[action="'.$url.'"] textarea[id="group_description"]');
36
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
37
+    
38
+    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
39
+    $form['group[name]'] = 'HardtekMania';
40
+    $form['group[description]'] = 'Des bass, des bpm, des gros caissons !';
41
+    $form['group[tags]['.$hardtek_id.']'] = $hardtek_id;
42
+    $form['group[tags]['.$tribe_id.']'] = $tribe_id;
43
+    $this->submit($form);
44
+    
45
+    $this->isResponseRedirection();
46
+    $this->followRedirection();
47
+    $this->isResponseSuccess();
48
+    
49
+    // Le groupe que créé existe bien dans la base
50
+    $group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
51
+      ->findOneBy(array(
52
+        'name'        => 'HardtekMania',
53
+        'description' => 'Des bass, des bpm, des gros caissons !'
54
+      ))
55
+    ;
56
+    
57
+    $this->assertTrue(!is_null($group));
58
+    
59
+    // On vérifie egallement le lien avec les tags
60
+    if (!is_null($group))
61
+    {
62
+      $group_tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
63
+        ->findBy(array(
64
+          'group' => $group->getId(),
65
+          'tag'   => $hardtek_id
66
+        ))
67
+      ;
68
+      
69
+      $this->assertEquals(1, count($group_tags));
70
+      
71
+      $group_tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
72
+        ->findBy(array(
73
+          'group' => $group->getId(),
74
+          'tag'   => $tribe_id
75
+        ))
76
+      ;
77
+      
78
+      $this->assertEquals(1, count($group_tags));      
79
+    }
80
+    
81
+  }
82
+  
83
+  /**
84
+   * Test de la mise a jour d'un groupe
85
+   */
86
+  public function testGroupUpdate()
87
+  {
88
+    $this->connectUser('bob', 'toor');
89
+    
90
+    $Fans_de_psytrance = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
91
+    $psytrance_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Psytrance')->getId();
92
+    $electro_id   = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Electro')->getId();
93
+    
94
+    // Le groupe n'a pas encore les modifs que nous voulons effectuer
95
+    $group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
96
+      ->findOneBy(array(
97
+          'name' => 'Les Fans de Psytrance'
98
+      ))
99
+    ;
100
+    
101
+    $this->assertTrue(is_null($group));
102
+    
103
+    // On se rend sur la page de listing des groupes possédés
104
+    $this->crawler = $this->client->request('GET', $this->generateUrl('groups_own_list'));
105
+    
106
+    // Il y a bien le lien vers son groupe
107
+    $this->exist('a[href="'.($url = $this->generateUrl('show_group', array('slug' => $Fans_de_psytrance->getSlug()))).'"]');
108
+    $link = $this->selectLink('a[href="'.$url.'"]');
109
+    // On clique dessus
110
+    $this->clickOnLink($link);
111
+    $this->isResponseSuccess();
112
+    
113
+    // On est sur la page du groupe, bob doit voir le lien vers la page d'edition
114
+    $this->exist('a[href="'.($url = $this->generateUrl('group_edit', array('slug' => $Fans_de_psytrance->getSlug()))).'"]');
115
+    $link = $this->selectLink('a[href="'.$url.'"]');
116
+    // On clique dessus
117
+    $this->clickOnLink($link);
118
+    $this->isResponseSuccess();
119
+    
120
+    // On vérifie al présence du forumaire
121
+    $this->exist('form[action="'.($url = $this->generateUrl('group_update', array('slug' => $Fans_de_psytrance->getSlug()))).'"]');
122
+    $this->exist('form[action="'.$url.'"] input[id="group_name"]');
123
+    $this->exist('form[action="'.$url.'"] textarea[id="group_description"]');
124
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
125
+    
126
+    // On valide ce formulaire
127
+    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
128
+    $form['group[name]'] = 'Les Fans de Psytrance';
129
+    $form['group[description]'] = 'Ca va swiguer !';
130
+    $form['group[tags]['.$psytrance_id.']'] = $psytrance_id;
131
+    // On rajoute le lien vers le tag electro
132
+    $form['group[tags]['.$electro_id.']'] = $electro_id;
133
+    $this->submit($form);
134
+    
135
+    $this->isResponseRedirection();
136
+    $this->followRedirection();
137
+    $this->isResponseSuccess();
138
+    
139
+    // Le groupe est bien modifié en base
140
+    $group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
141
+      ->findOneBy(array(
142
+        'name'        => 'Les Fans de Psytrance',
143
+        'description' => 'Ca va swiguer !'
144
+      ))
145
+    ;
146
+    
147
+    $this->assertTrue(!is_null($group));
148
+    
149
+    // On vérifie egallement le lien avec les tags
150
+    if (!is_null($group))
151
+    {
152
+      $group_tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
153
+        ->findBy(array(
154
+          'group' => $group->getId(),
155
+          'tag'   => $psytrance_id
156
+        ))
157
+      ;
158
+      
159
+      $this->assertEquals(1, count($group_tags));
160
+      
161
+      $group_tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
162
+        ->findBy(array(
163
+          'group' => $group->getId(),
164
+          'tag'   => $electro_id
165
+        ))
166
+      ;
167
+      
168
+      $this->assertEquals(1, count($group_tags));      
169
+    }
170
+    
171
+    // Et sur la page aussi
172
+    $this->exist('h2:contains("Les Fans de Psytrance")');
173
+  }
174
+  
175
+}

+ 13 - 0
src/Muzich/GroupBundle/Controller/DefaultController.php View File

@@ -46,6 +46,19 @@ class DefaultController extends Controller
46 46
   public function addAction(Request $request)
47 47
   {
48 48
     $user = $this->getUser();
49
+    
50
+    /**
51
+     * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
52
+     * Docrine le voit si on faire une requete directe.
53
+     */
54
+    if ($this->container->getParameter('env') == 'test')
55
+    {
56
+      $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
57
+        $this->container->get('security.context')->getToken()->getUser()->getId(),
58
+        array()
59
+      )->getSingleResult();
60
+    }
61
+    
49 62
     $em = $this->getDoctrine()->getEntityManager();
50 63
     
51 64
     $new_group = new Group();