Browse Source

Merge branch 'feature/test' into unstable

bastien 13 years ago
parent
commit
ceec87bf33

+ 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

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

+ 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
 }