Browse Source

Ajout de tests pour home

bastien 13 years ago
parent
commit
13a5a0ebe1

+ 3 - 0
app/config/config_dev.yml View File

@@ -21,3 +21,6 @@ monolog:
21 21
 
22 22
 assetic:
23 23
     use_controller: true
24
+
25
+parameters:
26
+    env: dev

+ 3 - 0
app/config/config_prod.yml View File

@@ -16,3 +16,6 @@ monolog:
16 16
             type:  stream
17 17
             path:  %kernel.logs_dir%/%kernel.environment%.log
18 18
             level: debug
19
+
20
+parameters:
21
+    env: prod

+ 3 - 0
app/config/config_test.yml View File

@@ -12,3 +12,6 @@ web_profiler:
12 12
 
13 13
 swiftmailer:
14 14
     disable_delivery: true
15
+
16
+parameters:
17
+    env: test

+ 13 - 0
src/Muzich/CoreBundle/Controller/CoreController.php View File

@@ -138,6 +138,19 @@ class CoreController extends Controller
138 138
       $form->bindRequest($this->getRequest());
139 139
       if ($form->isValid())
140 140
       {
141
+        
142
+        /**
143
+         * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
144
+         * Docrine le voit si on faire une requete directe.
145
+         */
146
+        if ($this->container->getParameter('env') == 'test')
147
+        {
148
+          $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
149
+            $this->container->get('security.context')->getToken()->getUser()->getId(),
150
+            array()
151
+          )->getSingleResult();
152
+        }
153
+        
141 154
         // On utilise le gestionnaire d'élément
142 155
         $factory = new ElementManager($element, $em, $this->container);
143 156
         $factory->proceedFill($user);

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

@@ -132,4 +132,153 @@ class HomeControllerTest extends FunctionalTest
132 132
     }
133 133
   }
134 134
   
135
+  /**
136
+   * Ajouts d'éléments et tests de cas refusés
137
+   */
138
+  public function testAddElementSuccess()
139
+  {
140
+    $this->connectUser('bux', 'toor');
141
+    
142
+    $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
143
+    $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
144
+    
145
+    // Un groupe open
146
+    $fan_de_psy = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
147
+    
148
+    /*
149
+     *  Ajout d'un élément avec succés
150
+     */
151
+    $this->procedure_add_element(
152
+      'Mon bel element', 
153
+      'http://www.youtube.com/watch?v=WC8qb_of04E', 
154
+      array($hardtek->getId(), $tribe->getId())
155
+    );
156
+    
157
+    $this->isResponseRedirection();
158
+    $this->followRedirection();
159
+    $this->isResponseSuccess();
160
+    
161
+    $this->exist('li:contains("Mon bel element")');
162
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
163
+      ->findOneByName('Mon bel element')
164
+    ;
165
+    $this->assertTrue(!is_null($element));
166
+        
167
+  }
168
+  
169
+  /**
170
+   * Ajouts d'éléments et tests de cas refusés
171
+   */
172
+  public function testAddElementFailure()
173
+  {
174
+    $this->connectUser('bux', 'toor');
175
+    
176
+    $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
177
+    $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
178
+    
179
+    // Un groupe no open
180
+    $dudeldrum = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('DUDELDRUM');
181
+    
182
+    /*
183
+     *  Ajouts d'éléments avec echec
184
+     */
185
+    
186
+    // Nom trop court
187
+    $this->procedure_add_element(
188
+      'Mo', 
189
+      'http://www.youtube.com/watch?v=WC8qb_of04E', 
190
+      array($hardtek->getId(), $tribe->getId())
191
+    );
192
+    
193
+    $this->isResponseSuccess();
194
+    
195
+    $this->notExist('li:contains("Mon bel element")');
196
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
197
+      ->findOneByName('Mon bel element')
198
+    ;
199
+    $this->assertTrue(is_null($element));
200
+    
201
+    // Nom trop long
202
+    $this->procedure_add_element(
203
+      'Mon bel element mais qui a un nom trop court la vache oui trop long hohoho', 
204
+      'http://www.youtube.com/watch?v=WC8qb_of04E', 
205
+      array($hardtek->getId(), $tribe->getId())
206
+    );
207
+    
208
+    $this->isResponseSuccess();
209
+    
210
+    $this->notExist('li:contains("Mon bel element mais qui a un nom trop court la vache oui trop long hohoho")');
211
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
212
+      ->findOneByName('Mon bel element mais qui a un nom trop court la vache oui trop long hohoho')
213
+    ;
214
+    $this->assertTrue(is_null($element));
215
+    
216
+    // Pas d'url
217
+    $this->procedure_add_element(
218
+      'Mon bel element', 
219
+      '', 
220
+      array($hardtek->getId(), $tribe->getId())
221
+    );
222
+    
223
+    $this->isResponseSuccess();
224
+    
225
+    $this->notExist('li:contains("Mon bel element")');
226
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
227
+      ->findOneByName('Mon bel element')
228
+    ;
229
+    $this->assertTrue(is_null($element));
230
+    
231
+    // Pas de nom
232
+    $this->procedure_add_element(
233
+      '', 
234
+      'http://www.youtube.com/watch?v=WC8qb_of04E', 
235
+      array($hardtek->getId(), $tribe->getId())
236
+    );
237
+    
238
+    $this->isResponseSuccess();
239
+    
240
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
241
+      ->findOneByName('')
242
+    ;
243
+    $this->assertTrue(is_null($element));
244
+    
245
+  }
246
+  
247
+  public function testAddElementAtGroupFailure()
248
+  {
249
+    $this->connectUser('bux', 'toor');
250
+    
251
+    /*
252
+     * Ajout d'un élément lié a un groupe (success)
253
+     */
254
+    $this->procedure_add_element(
255
+      'Mon bel element', 
256
+      'http://www.youtube.com/watch?v=WC8qb_of04E', 
257
+      array($hardtek->getId(), $tribe->getId()),
258
+      $fan_de_psy->getId()
259
+    );
260
+    
261
+    $this->isResponseRedirection();
262
+    $this->followRedirection();
263
+    $this->isResponseSuccess();
264
+    
265
+    $this->exist('li:contains("Mon bel element")');
266
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
267
+      ->findOneByName('Mon bel element')
268
+    ;
269
+    $this->assertTrue(!is_null($element));
270
+    if (!is_null($element))
271
+    {
272
+      $this->assertEquals($fan_de_psy->getId(), $element->getGroup()->getId());
273
+    }
274
+  }
275
+  
276
+  /**
277
+   * L'ajout d'un Element a un de ses groupe ne doit pas poser de problème
278
+   */
279
+  public function testAddElementAtMyGroupSuccess()
280
+  {
281
+    
282
+  }
283
+  
135 284
 }

+ 41 - 0
src/Muzich/CoreBundle/lib/FunctionalTest.php View File

@@ -20,6 +20,13 @@ class FunctionalTest extends WebTestCase
20 20
    */
21 21
   protected $crawler;
22 22
   
23
+  protected function outputDebug()
24
+  {
25
+    unlink('/home/bux/.debug/out.html');
26
+    $monfichier = fopen('/home/bux/.debug/out.html', 'a+');
27
+    fwrite($monfichier, $this->client->getResponse()->getContent());
28
+  }
29
+  
23 30
   /**
24 31
    * Retourne l'objet User
25 32
    * 
@@ -107,6 +114,30 @@ class FunctionalTest extends WebTestCase
107 114
   }
108 115
   
109 116
   /**
117
+   * Procédure d'ajout d'un élément a partir de la page home
118
+   * 
119
+   * @param string $name
120
+   * @param string $url
121
+   * @param array $tags
122
+   * @param int $group_id 
123
+   */
124
+  protected function procedure_add_element($name, $url, $tags, $group_id = '')
125
+  {
126
+    $this->crawler = $this->client->request('GET', $this->generateUrl('home'));
127
+    $this->isResponseSuccess();
128
+    
129
+    $form = $this->selectForm('form[action="'.$this->generateUrl('element_add').'"] input[type="submit"]');
130
+    $form['element_add[name]'] = $name;
131
+    $form['element_add[url]'] = $url;
132
+    foreach ($tags as $tag_id)
133
+    {
134
+      $form['element_add[tags]['.$tag_id.']'] = $tag_id;
135
+    }
136
+    $form['element_add[group]'] = $group_id;
137
+    $this->submit($form);
138
+  }
139
+  
140
+  /**
110 141
    * Generates a URL from the given parameters.
111 142
    *
112 143
    * @param string $route
@@ -150,6 +181,16 @@ class FunctionalTest extends WebTestCase
150 181
   }
151 182
   
152 183
   /**
184
+   * Test l'inexistance d'un element
185
+   * 
186
+   * @param string $filter 
187
+   */
188
+  protected function notExist($filter)
189
+  {
190
+    $this->assertFalse($this->crawler->filter($filter)->count() > 0);
191
+  }
192
+  
193
+  /**
153 194
    * Retourne un objet lien
154 195
    * 
155 196
    * @param string $filter

+ 24 - 24
src/Muzich/CoreBundle/lib/Test/Client.php View File

@@ -9,32 +9,32 @@ class Client extends BaseClient
9 9
   static protected $connection;
10 10
   protected $requested;
11 11
 
12
-    protected function doRequest($request)
13
-    {
14
-        if ($this->requested) {
15
-            $this->kernel->shutdown();
16
-            $this->kernel->boot();
17
-        }
18
-
19
-        $this->injectConnection();
20
-        $this->requested = true;
12
+  protected function doRequest($request)
13
+  {
14
+    if ($this->requested) {
15
+      $this->kernel->shutdown();
16
+      $this->kernel->boot();
17
+    }
21 18
 
22
-        return $this->kernel->handle($request);
19
+    $this->injectConnection();
20
+    $this->requested = true;
21
+
22
+    return $this->kernel->handle($request);
23
+  }
24
+
25
+  protected function injectConnection()
26
+  {
27
+    if (null === self::$connection) {
28
+      self::$connection = $this->getContainer()->get('doctrine.dbal.default_connection');
29
+    } else {
30
+      if (! $this->requested) {
31
+          self::$connection->rollback();
32
+      }
33
+      $this->getContainer()->set('doctrine.dbal.default_connection', self::$connection);
23 34
     }
24 35
 
25
-    protected function injectConnection()
26
-    {
27
-        if (null === self::$connection) {
28
-            self::$connection = $this->getContainer()->get('doctrine.dbal.default_connection');
29
-        } else {
30
-            if (! $this->requested) {
31
-                self::$connection->rollback();
32
-            }
33
-            $this->getContainer()->set('doctrine.dbal.default_connection', self::$connection);
34
-        }
35
-
36
-        if (! $this->requested) {
37
-            self::$connection->beginTransaction();
38
-        }
36
+    if (! $this->requested) {
37
+      self::$connection->beginTransaction();
39 38
     }
39
+  }
40 40
 }