Skip to content

Commit 1668023

Browse files
committed
moved template recipes to the cookbook (reorganized files)
1 parent a4105f4 commit 1668023

File tree

14 files changed

+169
-171
lines changed

14 files changed

+169
-171
lines changed
File renamed without changes.

cookbook/index.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ Dive into Symfony2 cookbook:
88

99
email
1010
gmail
11-
autoloader
12-
finder
13-
varnish
11+
tools/autoloader
12+
tools/finder
13+
cache/varnish
14+
testing/http_authorization
15+
testing/insulating_clients
16+
testing/profiling
1417
symfony1
1518

1619
.. include:: map.rst.inc

cookbook/map.rst.inc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
* :doc:`/cookbook/email`
22
* :doc:`/cookbook/gmail`
3-
* :doc:`/cookbook/autoloader`
4-
* :doc:`/cookbook/finder`
5-
* :doc:`/cookbook/varnish`
3+
* :doc:`/cookbook/tools/autoloader`
4+
* :doc:`/cookbook/tools/finder`
5+
* :doc:`/cookbook/cache/varnish`
6+
* :doc:`/cookbook/testing/http_authorization`
7+
* :doc:`/cookbook/testing/insulating_clients`
8+
* :doc:`/cookbook/testing/profiling`
69
* :doc:`/cookbook/symfony1`
7-
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.. index::
2+
single: Tests; HTTP Authorization
3+
4+
How to write Tests when using HTTP Authorization
5+
================================================
6+
7+
If your application needs HTTP authentication, pass the username and password
8+
as server variables to ``createClient()``::
9+
10+
$client = $this->createClient(array(), array(
11+
'PHP_AUTH_USER' => 'username',
12+
'PHP_AUTH_PW' => 'pa$$word',
13+
));
14+
15+
You can also override it on a per request basis::
16+
17+
$client->request('DELETE', '/post/12', array(), array(
18+
'PHP_AUTH_USER' => 'username',
19+
'PHP_AUTH_PW' => 'pa$$word',
20+
));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.. index::
2+
single: Tests
3+
4+
How to test the Interaction of several Clients
5+
==============================================
6+
7+
If you need to simulate an interaction between different Clients (think of a
8+
chat for instance), create several Clients::
9+
10+
$harry = $this->createClient();
11+
$sally = $this->createClient();
12+
13+
$harry->request('POST', '/say/sally/Hello');
14+
$sally->request('GET', '/messages');
15+
16+
$this->assertEquals(201, $harry->getResponse()->getStatusCode());
17+
$this->assertRegExp('/Hello/', $sally->getResponse()->getContent());
18+
19+
This works except when your code maintains a global state or if it depends on
20+
third-party libraries that has some kind of global state. In such a case, you
21+
can insulate your clients::
22+
23+
$harry = $this->createClient();
24+
$sally = $this->createClient();
25+
26+
$harry->insulate();
27+
$sally->insulate();
28+
29+
$harry->request('POST', '/say/sally/Hello');
30+
$sally->request('GET', '/messages');
31+
32+
$this->assertEquals(201, $harry->getResponse()->getStatusCode());
33+
$this->assertRegExp('/Hello/', $sally->getResponse()->getContent());
34+
35+
Insulated clients transparently execute their requests in a dedicated and
36+
clean PHP process, thus avoiding any side-effects.
37+
38+
.. tip::
39+
40+
As an insulated client is slower, you can keep one client in the main
41+
process, and insulate the other ones.

cookbook/testing/profiling.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.. index::
2+
single: Tests; Profiling
3+
4+
How to use the Profiling Data in a Test
5+
=======================================
6+
7+
It's highly recommended that a functional test only tests the Response. But if
8+
you write functional tests that monitor your production servers, you might
9+
want to write tests on the profiling data.
10+
11+
The Symfony2 :doc:`Profiler </guides/internals/profiler>` gathers a lot of
12+
data for each request. Use these data to check the number of database calls,
13+
the time spent in the framework, ... But before writing assertions, always
14+
check that the profiler is indeed available (it is enabled by default in the
15+
``test`` environment)::
16+
17+
if ($profiler = $client->getProfiler()) {
18+
// check the number of requests
19+
$this->assertTrue($profiler->get('db')->getQueryCount() < 10);
20+
21+
// check the time spent in the framework
22+
$this->assertTrue( $profiler->get('timer')->getTime() < 0.5);
23+
}
24+
25+
.. note::
26+
27+
The profiler information are available even if you insulate the client or
28+
if you use an HTTP layer for your tests.
File renamed without changes.
File renamed without changes.

guides/internals/overview.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ on top of the previous one.
2222
independently with the help of the
2323
:class:`Symfony\\Component\\HttpFoundation\\UniversalClassLoader` class
2424
and the ``src/autoload.php`` file. Read the :doc:`dedicated chapter
25-
</cookbook/autoloader>` for more information.
25+
</cookbook/tools/autoloader>` for more information.
2626

2727
``HttpFoundation`` Component
2828
----------------------------

guides/map.rst.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
* :doc:`Overview </guides/testing/overview>` |
1717
* :doc:`Configuration </guides/testing/configuration>` |
1818
* :doc:`Crawler </guides/testing/crawler>` |
19-
* :doc:`Client </guides/testing/client>` |
20-
* :doc:`Recipes </guides/testing/recipes>`
19+
* :doc:`Client </guides/testing/client>`
2120

2221
* **Validation**:
2322

guides/testing/client.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,39 @@ the ``Kernel``::
102102

103103
$container = $client->getContainer();
104104
$kernel = $client->getKernel();
105+
106+
Accessing the Container
107+
-----------------------
108+
109+
It's highly recommended that a functional test only tests the Response. But
110+
under certain very rare circumstances, you might want to access some internal
111+
objects to write assertions. In such cases, you can access the dependency
112+
injection container::
113+
114+
$container = $client->getContainer();
115+
116+
Be warned that this does not work if you insulate the client or if you use an
117+
HTTP layer.
118+
119+
.. tip::
120+
121+
If the information you need to check are available from the profiler, use
122+
them instead.
123+
124+
Redirections
125+
------------
126+
127+
By default, the Client follows HTTP redirects. But if you want to get the
128+
Response before the redirection and redirect yourself, calls the
129+
``followRedirects()`` method::
130+
131+
$client->followRedirects(false);
132+
133+
$crawler = $client->request('GET', '/');
134+
135+
// do something with the redirect response
136+
137+
// follow the redirection manually
138+
$crawler = $client->followRedirect();
139+
140+
$client->followRedirects(true);

guides/testing/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ Testing
88
configuration
99
crawler
1010
client
11-
recipes

guides/testing/overview.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,34 @@ document::
183183

184184
$this->assertRegExp('/Hello Fabien/', $client->getResponse()->getContent());
185185

186+
.. index::
187+
single: Tests; Assertions
188+
189+
Useful Assertions
190+
-----------------
191+
192+
After some time, you will notice that you always write the same kind of
193+
assertions. To get you started faster, here is a list of the most common and
194+
useful assertions::
195+
196+
// Assert that the response matches a given CSS selector.
197+
$this->assertTrue(count($crawler->filter($selector)) > 0);
198+
199+
// Assert that the response matches a given CSS selector n times.
200+
$this->assertEquals($count, $crawler->filter($selector)->count());
201+
202+
// Assert the a response header has the given value.
203+
$this->assertTrue($client->getResponse()->headers->contains($key, $value));
204+
205+
// Assert that the response content matches a regexp.
206+
$this->assertRegExp($regexp, $client->getResponse()->getContent());
207+
208+
// Assert the response status code.
209+
$this->assertTrue($client->getResponse()->isSuccessful());
210+
$this->assertTrue($client->getResponse()->isNotFound());
211+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
212+
213+
// Assert that the response status code is a redirect.
214+
$this->assertTrue($client->getResponse()->isRedirected('google.com'));
215+
186216
.. _documentation: http://www.phpunit.de/manual/3.5/en/

guides/testing/recipes.rst

Lines changed: 0 additions & 160 deletions
This file was deleted.

0 commit comments

Comments
 (0)