Browse Source

Ajout de tests sur l'ajout d'élément.

bastien 13 years ago
parent
commit
37fc25385e

+ 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

+ 30 - 11
src/Muzich/CoreBundle/Controller/CoreController.php View File

@@ -122,6 +122,20 @@ class CoreController extends Controller
122 122
     $user = $this->getUser();
123 123
     $em = $this->getDoctrine()->getEntityManager();
124 124
     
125
+    /*
126
+     * Contrôle préléminaire si groupe précisé
127
+     */
128
+    $group = null;
129
+    if ($group_slug)
130
+    {
131
+      $group = $this->findGroupWithSlug($group_slug);
132
+      if (!$group->userCanAddElement($this->getUserId()))
133
+      {
134
+        $group = null;
135
+        throw  $this->createNotFoundException('Vous ne pouvez pas ajouter d\'éléments a ce groupe');
136
+      }
137
+    }
138
+    
125 139
     $element = new Element();
126 140
     $form = $this->createForm(
127 141
       new ElementAddForm(),
@@ -138,23 +152,28 @@ class CoreController extends Controller
138 152
       $form->bindRequest($this->getRequest());
139 153
       if ($form->isValid())
140 154
       {
155
+        
156
+        /**
157
+         * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
158
+         * Docrine le voit si on faire une requete directe.
159
+         */
160
+        if ($this->container->getParameter('env') == 'test')
161
+        {
162
+          $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
163
+            $this->container->get('security.context')->getToken()->getUser()->getId(),
164
+            array()
165
+          )->getSingleResult();
166
+        }
167
+        
141 168
         // On utilise le gestionnaire d'élément
142 169
         $factory = new ElementManager($element, $em, $this->container);
143 170
         $factory->proceedFill($user);
144 171
         
145 172
         // Si on a précisé un groupe dans lequel mettre l'element
146
-        if ($group_slug)
173
+        if ($group)
147 174
         {
148
-          $group = $this->findGroupWithSlug($group_slug);
149
-          if ($group->userCanAddElement($this->getUserId()))
150
-          {
151
-            $element->setGroup($group);
152
-          }
153
-          else
154
-          {
155
-            throw $this->createNotFoundException('Vous ne pouvez ajouter d\'element a ce groupe.');
156
-          }
157
-          $redirect_url = $this->generateUrl('show_group', array('slug' => $group->getSlug()));
175
+          $element->setGroup($group);
176
+          $redirect_url = $this->generateUrl('show_group', array('slug' => $group_slug));
158 177
         }
159 178
         else
160 179
         {

+ 19 - 8
src/Muzich/CoreBundle/ElementFactory/Site/base/VideoSiteFactory.php View File

@@ -21,14 +21,25 @@ class VideoSiteFactory extends BaseFactory
21 21
   {
22 22
     parent::__construct($element, $container);
23 23
     
24
-    // Configuration de VideoEmbed
25
-    define('SITEBASE', $container->getParameter('sitebase'));
26
-    define('VIDEO_EMBED_CONFIG_FILE', SITEBASE.$container->getParameter('video_embed_config_file'));
27
-    //to activate debug mode and false for production usage. it will write 
28
-    //to a log file when something goes wrong but should not produce 
29
-    //exceptions in production enviroment
30
-    define('DEBUG', $container->getParameter('video_embed_debug')); 
31
-    
24
+    //if ($container->getParameter('env') != 'test')
25
+    //{
26
+      // Configuration de VideoEmbed
27
+      if (!defined('SITEBASE')) 
28
+      {
29
+        define('SITEBASE', $container->getParameter('sitebase'));
30
+      }
31
+    //}
32
+      if (!defined('VIDEO_EMBED_CONFIG_FILE')) 
33
+      {
34
+      define('VIDEO_EMBED_CONFIG_FILE', SITEBASE.$container->getParameter('video_embed_config_file'));
35
+      }
36
+      //to activate debug mode and false for production usage. it will write 
37
+      //to a log file when something goes wrong but should not produce 
38
+      //exceptions in production enviroment
39
+      if (!defined('DEBUG')) 
40
+      {
41
+        define('DEBUG', $container->getParameter('video_embed_debug')); 
42
+      }
32 43
     try {
33 44
       $this->video_engine =  new VideoEmbed($this->element->getUrl());
34 45
     } catch (Exception $exc) {

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

@@ -0,0 +1,346 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Controller;
4
+
5
+use Muzich\CoreBundle\lib\FunctionalTest;
6
+use Muzich\CoreBundle\Searcher\ElementSearcher;
7
+
8
+class HomeControllerTest extends FunctionalTest
9
+{
10
+  /**
11
+   * Ce test contrôle l'affichage des elements sur la page d'accueil
12
+   * Il modifie egallement le filtre d'éléments
13
+   */
14
+  public function testFilter()
15
+  {
16
+    $this->connectUser('bux', 'toor');
17
+
18
+    // Présence du formulaire d'ajout d'un élément
19
+    $this->exist('form[action="'.($url = $this->generateUrl('element_add')).'"]');
20
+    $this->exist('form[action="'.$url.'"] input[id="element_add_name"]');
21
+    $this->exist('form[action="'.$url.'"] input[id="element_add_url"]');
22
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
23
+    
24
+    // Présence du formulaire de filtrage
25
+    $this->exist('form[action="'.($url = $this->generateUrl('search_elements')).'"]');
26
+    $this->exist('form[action="'.$url.'"] select[id="element_search_form_network"]');
27
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
28
+    
29
+    $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
30
+    $tribe_id   = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
31
+    
32
+    // On récupére le formulaire de filtrage
33
+    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
34
+    
35
+    // On décoche les tags
36
+    foreach ($this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findAll() as $tag)
37
+    {
38
+      $form['element_search_form[tags]['.$tag->getId().']']->untick();
39
+    }
40
+    
41
+    // On met ce que l'on veut dans le form
42
+    $form['element_search_form[network]'] = ElementSearcher::NETWORK_PUBLIC;
43
+    $form['element_search_form[tags]['.$hardtek_id.']'] = $hardtek_id;
44
+    $form['element_search_form[tags]['.$tribe_id.']'] = $tribe_id;
45
+    $this->submit($form);
46
+    
47
+    $this->client->submit($form);
48
+    
49
+    $this->isResponseRedirection();
50
+    $this->followRedirection();
51
+    $this->isResponseSuccess();
52
+    
53
+    $this->assertTrue($this->getSession()->has('user.element_search.params'));
54
+    $this->assertEquals(array(
55
+        'network'   => ElementSearcher::NETWORK_PUBLIC,
56
+        'tags'      => array(
57
+          $hardtek_id, $tribe_id
58
+        ),
59
+        'count'     => $this->getContainer()->getParameter('search_default_count'),
60
+        'user_id'   => null,
61
+        'group_id'  => null,
62
+        'favorite' => false
63
+    ), $this->getSession()->get('user.element_search.params'));
64
+    
65
+    // On fabrique l'ElementSearcher correspondant
66
+    $es = new ElementSearcher();
67
+    $es->init($this->getSession()->get('user.element_search.params'));
68
+    
69
+    foreach ($es->getElements($this->getDoctrine(), $this->getUser()->getId()) as $element)
70
+    {
71
+      $this->exist('html:contains("'.$element->getName().'")');
72
+    }
73
+  }
74
+  
75
+  /**
76
+   * Test de la présence des elements sur la page d'un utilisateur
77
+   */
78
+  public function testUserPage()
79
+  {
80
+    $this->connectUser('bux', 'toor');
81
+    $jean = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
82
+      ->findOneByUsername('jean')
83
+    ;
84
+    
85
+    $this->crawler = $this->client->request(
86
+      'GET', 
87
+      $this->generateUrl('show_user', array('slug' => $jean->getSlug()))
88
+    );
89
+    
90
+    $this->isResponseSuccess();
91
+    $this->exist('h2:contains("'.$jean->getName().'")');
92
+    
93
+    $es = new ElementSearcher();
94
+    $es->init(array(
95
+      'user_id' => $jean->getId()
96
+    ));
97
+    
98
+    foreach ($es->getElements($this->getDoctrine(), $this->getUser()->getId()) as $element)
99
+    {
100
+      $this->exist('html:contains("'.$element->getName().'")');
101
+    }
102
+  }
103
+  
104
+  /**
105
+   * Test de la présence des elements sur la page d'un utilisateur
106
+   */
107
+  public function testGroupPage()
108
+  {
109
+    $this->connectUser('bux', 'toor');
110
+    $fdp = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
111
+      ->findOneBySlug('fans-de-psytrance')
112
+      ->getSingleResult()
113
+    ;
114
+    
115
+    $this->crawler = $this->client->request(
116
+      'GET', 
117
+      $this->generateUrl('show_group', array('slug' => $fdp->getSlug()))
118
+    );
119
+    
120
+    $this->isResponseSuccess();
121
+    $this->exist('h2:contains("'.$fdp->getName().'")');
122
+    
123
+    $es = new ElementSearcher();
124
+    $es->init(array(
125
+      'group_id' => $fdp->getId()
126
+    ));
127
+    
128
+    foreach ($es->getElements($this->getDoctrine(), $this->getUser()->getId()) as $element)
129
+    {
130
+      $this->exist('html:contains("'.$element->getName().'")');
131
+    }
132
+  }
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
+    /*
145
+     *  Ajout d'un élément avec succés
146
+     */
147
+    $this->procedure_add_element(
148
+      'Mon bel element', 
149
+      'http://www.youtube.com/watch?v=WC8qb_of04E', 
150
+      array($hardtek->getId(), $tribe->getId())
151
+    );
152
+    
153
+    $this->isResponseRedirection();
154
+    $this->followRedirection();
155
+    $this->isResponseSuccess();
156
+    
157
+    $this->exist('li:contains("Mon bel element")');
158
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
159
+      ->findOneByName('Mon bel element')
160
+    ;
161
+    $this->assertTrue(!is_null($element));
162
+        
163
+  }
164
+  
165
+  /**
166
+   * Ajouts d'éléments et tests de cas refusés
167
+   */
168
+  public function testAddElementFailure()
169
+  {
170
+    $this->connectUser('bux', 'toor');
171
+    
172
+    $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
173
+    $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
174
+    
175
+    /*
176
+     *  Ajouts d'éléments avec echec
177
+     */
178
+    
179
+    // Nom trop court
180
+    $this->procedure_add_element(
181
+      'Mo', 
182
+      'http://www.youtube.com/watch?v=WC8qb_of04E', 
183
+      array($hardtek->getId(), $tribe->getId())
184
+    );
185
+    
186
+    $this->isResponseSuccess();
187
+        
188
+    $this->notExist('li:contains("Mon bel element a4er563a1r")');
189
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
190
+      ->findOneByName('Mon bel element a4er563a1r')
191
+    ;
192
+    $this->assertTrue(is_null($element));
193
+    
194
+    // Nom trop long
195
+    $this->procedure_add_element(
196
+      'Mon bel element mais qui a un nom trop court la vache oui trop long hohoho', 
197
+      'http://www.youtube.com/watch?v=WC8qb_of04E', 
198
+      array($hardtek->getId(), $tribe->getId())
199
+    );
200
+    
201
+    $this->isResponseSuccess();
202
+    
203
+    $this->notExist('li:contains("Mon bel element mais qui a un nom trop court la vache oui trop long hohoho")');
204
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
205
+      ->findOneByName('Mon bel element mais qui a un nom trop court la vache oui trop long hohoho')
206
+    ;
207
+    $this->assertTrue(is_null($element));
208
+    
209
+    // Pas d'url
210
+    $this->procedure_add_element(
211
+      'Mon bel element', 
212
+      '', 
213
+      array($hardtek->getId(), $tribe->getId())
214
+    );
215
+    
216
+    $this->isResponseSuccess();
217
+    
218
+    $this->notExist('li:contains("Mon bel element gfez7f")');
219
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
220
+      ->findOneByName('Mon bel element gfez7f')
221
+    ;
222
+    $this->assertTrue(is_null($element));
223
+    
224
+    // Pas de nom
225
+    $this->procedure_add_element(
226
+      '', 
227
+      'http://www.youtube.com/watch?v=WC8qb_of04E', 
228
+      array($hardtek->getId(), $tribe->getId())
229
+    );
230
+    
231
+    $this->isResponseSuccess();
232
+    
233
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
234
+      ->findOneByName('')
235
+    ;
236
+    $this->assertTrue(is_null($element));
237
+    
238
+  }
239
+  
240
+  /**
241
+   * L'ajout d'un Element a un de ses groupe ne doit pas poser de problème
242
+   */
243
+  public function testAddElementAtMyGroupSuccess()
244
+  {
245
+    $this->connectUser('bux', 'toor');
246
+    // Un groupe open, donc pas de soucis
247
+    $fan_de_psy = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
248
+    
249
+    $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
250
+    $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
251
+        
252
+    $this->isResponseSuccess();
253
+    $this->procedure_add_element(
254
+      'Element mis dans le groupe de psytrance', 
255
+      'http://www.youtube.com/watch?v=WC8qb_of04E', 
256
+      array($hardtek->getId(), $tribe->getId()),
257
+      $fan_de_psy->getSlug()
258
+    );
259
+    
260
+    $this->isResponseRedirection();
261
+    $this->followRedirection();
262
+    $this->isResponseSuccess();
263
+    
264
+    $this->exist('li:contains("Element mis dans le groupe de psytrance")');
265
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
266
+      ->findOneByName('Element mis dans le groupe de psytrance')
267
+    ;
268
+    $this->assertTrue(!is_null($element));
269
+    
270
+    if (!is_null($element))
271
+    {
272
+      $this->assertEquals($fan_de_psy->getId(), $element->getGroup()->getId());
273
+    }
274
+    else
275
+    {
276
+      $this->assertTrue(false);
277
+    }
278
+    
279
+    $this->disconnectUser();
280
+    
281
+    /*
282
+     * Ajout d'un element dans un groupe que l'on posséde.
283
+     */
284
+    $this->connectUser('joelle', 'toor');
285
+    $this->isResponseSuccess();
286
+    
287
+    // Ce groupe appartient a joelle
288
+    $groupe_de_joelle = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Le groupe de joelle');
289
+    
290
+    $this->procedure_add_element(
291
+      'Element mis dans le groupe de joelle', 
292
+      'http://www.youtube.com/watch?v=WC8qb_of04E', 
293
+      array($hardtek->getId(), $tribe->getId()),
294
+      $groupe_de_joelle->getSlug()
295
+    );
296
+    
297
+    $this->isResponseRedirection();
298
+    $this->followRedirection();
299
+    $this->isResponseSuccess();
300
+    
301
+    $this->exist('li:contains("Element mis dans le groupe de joelle")');
302
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
303
+      ->findOneByName('Element mis dans le groupe de joelle')
304
+    ;
305
+    $this->assertTrue(!is_null($element));
306
+    
307
+    if (!is_null($element))
308
+    {
309
+      $this->assertEquals($groupe_de_joelle->getId(), $element->getGroup()->getId());
310
+    }
311
+    else
312
+    {
313
+      $this->assertTrue(false);
314
+    }
315
+  }
316
+  
317
+  /**
318
+   * L'ajout a un group qui n'est pas a sois, ou qui n'est pas open
319
+   * doit être impossible.
320
+   */
321
+  public function testAddElementAtGroupFailure()
322
+  {
323
+    $this->connectUser('bux', 'toor');
324
+    // Un groupe no open
325
+    $dudeldrum = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('DUDELDRUM');
326
+    
327
+    $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
328
+    $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
329
+        
330
+    // Nous tentons d'ouvrir l'url d'ajout d'élément avec un groupe qui n'est pas ouvert
331
+    // et qui n'appartient pas a l'utilisateur connecté
332
+    $this->crawler = $this->client->request(
333
+      'POST', 
334
+      $this->generateUrl('element_add', array('group_slug' => $dudeldrum->getSlug())),
335
+      array(
336
+        'element_add[name]' => 'Yohoho trululu',
337
+        'element_add[url]'  => 'http://www.youtube.com/watch?v=WC8qb_of04E',
338
+        'element_add[tags]['.$hardtek->getId().']' => $hardtek->getId(),
339
+        'element_add[tags]['.$tribe->getId().']' => $tribe->getId()
340
+      )
341
+    );
342
+    
343
+    $this->isResponseNotFound();
344
+  }
345
+  
346
+}

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

@@ -0,0 +1,297 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\lib;
4
+
5
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
+use Symfony\Bundle\FrameworkBundle\Client;
7
+use Symfony\Component\DomCrawler\Crawler;
8
+
9
+class FunctionalTest extends WebTestCase
10
+{
11
+  /**
12
+   *
13
+   * @var Client 
14
+   */
15
+  protected $client;
16
+  
17
+  /**
18
+   *
19
+   * @var Crawler 
20
+   */
21
+  protected $crawler;
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
+  
30
+  /**
31
+   * Retourne l'objet User
32
+   * 
33
+   * @return \Muzich\CoreBundle\Entity\User 
34
+   */
35
+  protected function getUser()
36
+  {
37
+    return $this->client->getContainer()->get('security.context')->getToken()->getUser();
38
+  }
39
+  
40
+  protected function connectUser($login, $password)
41
+  {
42
+    $this->client = self::createClient();
43
+
44
+    $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
45
+    $this->isResponseSuccess();
46
+
47
+    $this->assertEquals('anon.', $this->getUser());
48
+
49
+    $this->exist('div.login');
50
+    $this->exist('form[action="'.($url = $this->generateUrl('fos_user_security_check')).'"]');
51
+    $this->exist('form[action="'.$url.'"] input[id="username"]');
52
+    $this->exist('form[action="'.$url.'"] input[id="password"]');
53
+    $this->exist('form[action="'.$url.'"] input[id="remember_me"]');
54
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
55
+
56
+    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
57
+    $form['_username'] = $login;
58
+    $form['_password'] = $password;
59
+    $form['_remember_me'] = true;
60
+    $this->submit($form);
61
+
62
+    $this->isResponseRedirection();
63
+    $this->followRedirection();
64
+    $this->isResponseSuccess();
65
+
66
+    $user = $this->getUser();
67
+    $this->assertEquals($login, $user->getUsername());
68
+  }
69
+  
70
+  protected function disconnectUser()
71
+  {
72
+    $this->crawler = $this->client->request('GET', $this->generateUrl('fos_user_security_logout'));
73
+  }
74
+  
75
+  protected function validate_registrate_user_form($form, $username, $email, $pass1, $pass2)
76
+  {
77
+    $form['fos_user_registration_form[username]'] = $username;
78
+    $form['fos_user_registration_form[email]'] = $email;
79
+    $form['fos_user_registration_form[plainPassword][first]'] = $pass1;
80
+    // Un des mots de passe est incorrect
81
+    $form['fos_user_registration_form[plainPassword][second]'] = $pass2;
82
+    $this->submit($form);
83
+  }
84
+  
85
+  protected function procedure_registration_failure($username, $email, $pass1, $pass2)
86
+  {
87
+    $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
88
+    $this->isResponseSuccess();
89
+    $this->assertEquals('anon.', $this->getUser());
90
+    
91
+    $url = $this->generateUrl('register');
92
+    // Les mots de passes sont différents
93
+    $this->validate_registrate_user_form(
94
+      $this->selectForm('form[action="'.$url.'"] input[type="submit"]'), 
95
+      $username, 
96
+      $email, 
97
+      $pass1,
98
+      $pass2
99
+    );
100
+    
101
+    $this->isResponseSuccess();
102
+
103
+    if ('anon.' === ($user = $this->getUser()))
104
+    {
105
+      // Nous ne sommes pas identifiés
106
+      $this->assertEquals('anon.', $user);
107
+
108
+      // L'utilisateur n'est pas enregistré, il ne doit donc pas être en base
109
+      $db_user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
110
+        ->findOneByUsername($username)
111
+      ;
112
+
113
+      $this->assertTrue(is_null($db_user));
114
+    }
115
+    else
116
+    {
117
+      $this->assertTrue(false);
118
+    }
119
+  }
120
+  
121
+  /**
122
+   * Procédure d'ajout d'un élément
123
+   * 
124
+   * @param string $name
125
+   * @param string $url
126
+   * @param array $tags
127
+   * @param int $group_id 
128
+   */
129
+  protected function procedure_add_element($name, $url, $tags, $group_slug = null)
130
+  {
131
+    if (!$group_slug)
132
+    {
133
+      $this->crawler = $this->client->request('GET', $this->generateUrl('home'));
134
+      $form_url = $this->generateUrl('element_add');
135
+    }
136
+    else
137
+    {
138
+      $this->crawler = $this->client->request('GET', $this->generateUrl('show_group', array('slug' => $group_slug)));
139
+      $form_url = $this->generateUrl('element_add', array('group_slug' => $group_slug));
140
+    }
141
+    $this->isResponseSuccess();
142
+    
143
+    $form = $this->selectForm('form[action="'.$form_url.'"] input[type="submit"]');
144
+    $form['element_add[name]'] = $name;
145
+    $form['element_add[url]'] = $url;
146
+    foreach ($tags as $tag_id)
147
+    {
148
+      $form['element_add[tags]['.$tag_id.']'] = $tag_id;
149
+    }
150
+    
151
+    $this->submit($form);
152
+  }
153
+  
154
+  /**
155
+   * Generates a URL from the given parameters.
156
+   *
157
+   * @param string $route
158
+   * @param array $parameters
159
+   * @param boolean $absolute 
160
+   * 
161
+   * @return string (url generated)
162
+   */
163
+  protected function generateUrl($route, $parameters = array(), $absolute = false)
164
+  {
165
+    return $this->client->getContainer()->get('router')->generate($route, $parameters, $absolute);
166
+  }
167
+  
168
+  protected function getContainer()
169
+  {
170
+    return $this->client->getContainer();
171
+  }
172
+  
173
+  protected function getSession()
174
+  {
175
+    return $this->getContainer()->get('session');
176
+  }
177
+  
178
+  protected function clickOnLink($link)
179
+  {
180
+    $this->crawler = $this->client->click($link);
181
+  }
182
+
183
+
184
+  /**
185
+   *
186
+   * @return \Symfony\Bundle\DoctrineBundle\Registry
187
+   */
188
+  protected function getDoctrine()
189
+  {
190
+    return $this->client->getContainer()->get('doctrine');
191
+  }
192
+  
193
+  /**
194
+   * Test l'existance d'un element
195
+   * 
196
+   * @param string $filter 
197
+   */
198
+  protected function exist($filter)
199
+  {
200
+    $this->assertTrue($this->crawler->filter($filter)->count() > 0);
201
+  }
202
+  
203
+  /**
204
+   * Test l'inexistance d'un element
205
+   * 
206
+   * @param string $filter 
207
+   */
208
+  protected function notExist($filter)
209
+  {
210
+    $this->assertFalse($this->crawler->filter($filter)->count() > 0);
211
+  }
212
+  
213
+  /**
214
+   * Retourne un objet lien
215
+   * 
216
+   * @param string $filter
217
+   * @return \Symfony\Component\DomCrawler\Link
218
+   */
219
+  protected function selectLink($filter)
220
+  {
221
+    return $this->crawler->filter($filter)->eq(1)->link();
222
+  }
223
+  
224
+//  /**
225
+//   * Clique sur un link
226
+//   *  
227
+//   * @param type $link 
228
+//   */
229
+//  protected function click($link)
230
+//  {
231
+//    $this->crawler = $this->client->click($link);
232
+//  }
233
+  
234
+  /**
235
+   * Retourne un formulaire
236
+   * 
237
+   * @param string $filter
238
+   * @return \Symfony\Component\DomCrawler\Form 
239
+   */
240
+  protected function selectForm($filter)
241
+  {
242
+    return $this->crawler->filter($filter)->form();
243
+  }
244
+  
245
+  /**
246
+   * Soumet un formulaire
247
+   * 
248
+   * @param type $form 
249
+   */
250
+  protected function submit($form)
251
+  {
252
+    $this->crawler = $this->client->submit($form);
253
+  }
254
+  
255
+  /**
256
+   * Ordonne au client de suivre la redirection
257
+   */
258
+  protected function followRedirection()
259
+  {
260
+    $this->crawler = $this->client->followRedirect();
261
+  }
262
+  
263
+  /**
264
+   * Contrôle le Codestatus de la réponse
265
+   * 
266
+   * @param int $code 
267
+   */
268
+  protected function isStatusCode($code)
269
+  {
270
+    $this->assertEquals($code, $this->client->getResponse()->getStatusCode());
271
+  }
272
+  
273
+  /**
274
+   * Contrôle que le CodeStatus de la Response correspond bien a celle d'une
275
+   *  redirection
276
+   */
277
+  protected function isResponseRedirection()
278
+  {
279
+    $this->assertTrue($this->client->getResponse()->isRedirection());
280
+  }
281
+  
282
+  /**
283
+   * Contrôle que le CodeStatus de la Response correspond bien a celle d'un Ok
284
+   */
285
+  protected function isResponseSuccess()
286
+  {
287
+    $this->assertTrue($this->client->getResponse()->isSuccessful());
288
+  }
289
+  
290
+  /**
291
+   * Contrôle que le CodeStatus de la Response correspond bien a celle d'un Ok
292
+   */
293
+  protected function isResponseNotFound()
294
+  {
295
+    $this->assertTrue($this->client->getResponse()->isNotFound());
296
+  }
297
+}

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

@@ -0,0 +1,40 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\lib\Test;
4
+
5
+use Symfony\Bundle\FrameworkBundle\Client as BaseClient;
6
+
7
+class Client extends BaseClient
8
+{
9
+  static protected $connection;
10
+  protected $requested;
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;
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);
34
+    }
35
+
36
+    if (! $this->requested) {
37
+      self::$connection->beginTransaction();
38
+    }
39
+  }
40
+}