Преглед на файлове

Ecriture du premier test fonctionel: identification

bastien преди 13 години
родител
ревизия
4eb3096814
променени са 2 файла, в които са добавени 174 реда и са изтрити 0 реда
  1. 41 0
      src/Muzich/CoreBundle/Tests/Controller/IndexControllerTest.php
  2. 133 0
      src/Muzich/CoreBundle/lib/FunctionalTest.php

+ 41 - 0
src/Muzich/CoreBundle/Tests/Controller/IndexControllerTest.php Целия файл

@@ -0,0 +1,41 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Controller;
4
+
5
+use Muzich\CoreBundle\lib\FunctionalTest;
6
+
7
+class IndexControllerTest extends FunctionalTest
8
+{
9
+  public function testIdentification()
10
+  {
11
+    /**
12
+     * Test de l'identification de paul
13
+     */
14
+    $this->client = self::createClient();
15
+
16
+    $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
17
+    $this->isResponseSuccess();
18
+
19
+    $this->assertEquals('anon.', $this->getUser());
20
+
21
+    $this->exist('div.login');
22
+    $this->exist('form[action="'.($url = $this->generateUrl('fos_user_security_check')).'"]');
23
+    $this->exist('form[action="'.$url.'"] input[id="username"]');
24
+    $this->exist('form[action="'.$url.'"] input[id="password"]');
25
+    $this->exist('form[action="'.$url.'"] input[id="remember_me"]');
26
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
27
+
28
+    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
29
+    $form['_username'] = 'paul';
30
+    $form['_password'] = 'toor';
31
+    $form['_remember_me'] = true;
32
+    $this->submit($form);
33
+
34
+    $this->isResponseRedirection();
35
+    $this->followRedirection();
36
+    $this->isResponseSuccess();
37
+
38
+    $user = $this->getUser();
39
+    $this->assertEquals('paul', $user->getUsername());
40
+  }
41
+}

+ 133 - 0
src/Muzich/CoreBundle/lib/FunctionalTest.php Целия файл

@@ -0,0 +1,133 @@
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
+  /**
24
+   * Retourne l'objet User
25
+   * 
26
+   * @return \Muzich\CoreBundle\Entity\User 
27
+   */
28
+  protected function getUser()
29
+  {
30
+    return $this->client->getContainer()->get('security.context')->getToken()->getUser();
31
+  }
32
+  
33
+  /**
34
+   * Generates a URL from the given parameters.
35
+   *
36
+   * @param string $route
37
+   * @param array $parameters
38
+   * @param boolean $absolute 
39
+   * 
40
+   * @return string (url generated)
41
+   */
42
+  protected function generateUrl($route, $parameters = array(), $absolute = false)
43
+  {
44
+    return $this->client->getContainer()->get('router')->generate($route, $parameters, $absolute);
45
+  }
46
+  
47
+  /**
48
+   * Test l'existance d'un element
49
+   * 
50
+   * @param string $filter 
51
+   */
52
+  protected function exist($filter)
53
+  {
54
+    $this->assertTrue($this->crawler->filter($filter)->count() > 0);
55
+  }
56
+  
57
+  /**
58
+   * Retourne un objet lien
59
+   * 
60
+   * @param string $filter
61
+   * @return \Symfony\Component\DomCrawler\Link
62
+   */
63
+  protected function selectLink($filter)
64
+  {
65
+    return $this->crawler->filter($filter)->eq(1)->link();
66
+  }
67
+  
68
+//  /**
69
+//   * Clique sur un link
70
+//   *  
71
+//   * @param type $link 
72
+//   */
73
+//  protected function click($link)
74
+//  {
75
+//    $this->crawler = $this->client->click($link);
76
+//  }
77
+  
78
+  /**
79
+   * Retourne un formulaire
80
+   * 
81
+   * @param string $filter
82
+   * @return \Symfony\Component\DomCrawler\Form 
83
+   */
84
+  protected function selectForm($filter)
85
+  {
86
+    return $this->crawler->filter($filter)->form();
87
+  }
88
+  
89
+  /**
90
+   * Soumet un formulaire
91
+   * 
92
+   * @param type $form 
93
+   */
94
+  protected function submit($form)
95
+  {
96
+    $this->crawler = $this->client->submit($form);
97
+  }
98
+  
99
+  /**
100
+   * Ordonne au client de suivre la redirection
101
+   */
102
+  protected function followRedirection()
103
+  {
104
+    $this->client->followRedirect();
105
+  }
106
+  
107
+  /**
108
+   * Contrôle le Codestatus de la réponse
109
+   * 
110
+   * @param int $code 
111
+   */
112
+  protected function isStatusCode($code)
113
+  {
114
+    $this->assertEquals($code, $this->client->getResponse()->getStatusCode());
115
+  }
116
+  
117
+  /**
118
+   * Contrôle que le CodeStatus de la Response correspond bien a celle d'une
119
+   *  redirection
120
+   */
121
+  protected function isResponseRedirection()
122
+  {
123
+    $this->client->getResponse()->isRedirection();
124
+  }
125
+  
126
+  /**
127
+   * Contrôle que le CodeStatus de la Response correspond bien a celle d'un Ok
128
+   */
129
+  protected function isResponseSuccess()
130
+  {
131
+    $this->client->getResponse()->isSuccessful();
132
+  }
133
+}