-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.html
220 lines (141 loc) · 8.01 KB
/
testing.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Unit Testing</title>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/ubuntu-font.css">
<script src="vendor/modernizr-2.6.2.min.js"></script>
<script type="text/javascript" src="vendor/google-code-prettify/prettify.js"></script>
</head><body><section class="docs-content"><div class="container">
<article class="docs-body"><h1>Unit Testing</h1>
<p><a name="introduction"></a></p>
<h2>Introduction</h2>
<p>Laravel is built with unit testing in mind. In fact, support for testing with PHPUnit is included out of the box, and a <code>phpunit.xml</code> file is already setup for your application. In addition to PHPUnit, Laravel also utilizes the Symfony HttpKernel, DomCrawler, and BrowserKit components to allow you to inspect and manipulate your views while testing, allowing to simulate a web browser.</p>
<p>An example test file is provided in the <code>app/tests</code> directory. After installing a new Laravel application, simply run <code>phpunit</code> on the command line to run your tests.</p>
<p><a name="defining-and-running-tests"></a></p>
<h2>Defining & Running Tests</h2>
<p>To create a test case, simply create a new test file in the <code>app/tests</code> directory. The test class should extend <code>TestCase</code>. You may then define test methods as you normally would when using PHPUnit.</p>
<p><strong>An Example Test Class</strong></p>
<pre class="prettyprint"><code>class FooTest extends TestCase {
public function testSomethingIsTrue()
{
$this->assertTrue(true);
}
}
</code></pre>
<p>You may run all of the tests for your application by executing the <code>phpunit</code> command from your terminal.</p>
<blockquote>
<p><strong>Note:</strong> If you define your own <code>setUp</code> method, be sure to call <code>parent::setUp</code>.</p>
</blockquote>
<p><a name="test-environment"></a></p>
<h2>Test Environment</h2>
<p>When running unit tests, Laravel will automatically set the configuration environment to <code>testing</code>. Also, Laravel includes configuration files for <code>session</code> and <code>cache</code> in the test environment. Both of these drivers are set to <code>array</code> while in the test environment, meaning no session or cache data will be persisted while testing. You are free to create other testing environment configurations as necessary.</p>
<p><a name="calling-routes-from-tests"></a></p>
<h2>Calling Routes From Tests</h2>
<p>You may easily call one of your routes for a test using the <code>call</code> method:</p>
<p><strong>Calling A Route From A Test</strong></p>
<pre class="prettyprint"><code>$response = $this->call('GET', 'user/profile');
$response = $this->call($method, $uri, $parameters, $files, $server, $content);
</code></pre>
<p>You may then inspect the <code>Illuminate\Http\Response</code> object:</p>
<pre class="prettyprint"><code>$this->assertEquals('Hello World', $response->getContent());
</code></pre>
<p>You may also call a controller from a test:</p>
<p><strong>Calling A Controller From A Test</strong></p>
<pre class="prettyprint"><code>$response = $this->action('GET', 'HomeController@index');
$response = $this->action('GET', 'UserController@profile', array('user' => 1));
</code></pre>
<p>The <code>getContent</code> method will return the evaluated string contents of the response. If your route returns a <code>View</code>, you may access it using the <code>original</code> property:</p>
<pre class="prettyprint"><code>$view = $response->original;
$this->assertEquals('John', $view['name']);
</code></pre>
<p>To call a HTTPS route, you may use the <code>callSecure</code> method:</p>
<pre class="prettyprint"><code>$response = $this->callSecure('GET', 'foo/bar');
</code></pre>
<h3>DOM Crawler</h3>
<p>You may also call a route and receive a DOM Crawler instance that you may use to inspect the content:</p>
<pre class="prettyprint"><code>$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('h1:contains("Hello World!")'));
</code></pre>
<p>For more information on how to use the crawler, refer to its <a href="http://symfony.com/doc/master/components/dom_crawler.html">official documentation</a>.</p>
<p><a name="mocking-facades"></a></p>
<h2>Mocking Facades</h2>
<p>When testing, you may often want to mock a call to a Laravel static facade. For example, consider the following controller action:</p>
<pre class="prettyprint"><code>public function getIndex()
{
Event::fire('foo', array('name' => 'Dayle'));
return 'All done!';
}
</code></pre>
<p>We can mock the call to the <code>Event</code> class by using the <code>shouldReceive</code> method on the facade, which will return an instance of a <a href="https://github.com/padraic/mockery">Mockery</a> mock.</p>
<p><strong>Mocking A Facade</strong></p>
<pre class="prettyprint"><code>public function testGetIndex()
{
Event::shouldReceive('fire')->once()->with(array('name' => 'Dayle'));
$this->call('GET', '/');
}
</code></pre>
<blockquote>
<p><strong>Note:</strong> You should not mock the <code>Request</code> facade. Instead, pass the input you desire into the <code>call</code> method when running your test.</p>
</blockquote>
<p><a name="framework-assertions"></a></p>
<h2>Framework Assertions</h2>
<p>Laravel ships with several <code>assert</code> methods to make testing a little easier:</p>
<p><strong>Asserting Responses Are OK</strong></p>
<pre class="prettyprint"><code>public function testMethod()
{
$this->call('GET', '/');
$this->assertResponseOk();
}
</code></pre>
<p><strong>Asserting Response Statuses</strong></p>
<pre class="prettyprint"><code>$this->assertResponseStatus(403);
</code></pre>
<p><strong>Asserting Responses Are Redirects</strong></p>
<pre class="prettyprint"><code>$this->assertRedirectedTo('foo');
$this->assertRedirectedToRoute('route.name');
$this->assertRedirectedToAction('Controller@method');
</code></pre>
<p><strong>Asserting A View Has Some Data</strong></p>
<pre class="prettyprint"><code>public function testMethod()
{
$this->call('GET', '/');
$this->assertViewHas('name');
$this->assertViewHas('age', $value);
}
</code></pre>
<p><strong>Asserting The Session Has Some Data</strong></p>
<pre class="prettyprint"><code>public function testMethod()
{
$this->call('GET', '/');
$this->assertSessionHas('name');
$this->assertSessionHas('age', $value);
}
</code></pre>
<p><a name="helper-methods"></a></p>
<h2>Helper Methods</h2>
<p>The <code>TestCase</code> class contains several helper methods to make testing your application easier.</p>
<p>You may set the currently authenticated user using the <code>be</code> method:</p>
<p><strong>Setting The Currently Authenticated User</strong></p>
<pre class="prettyprint"><code>$user = new User(array('name' => 'John'));
$this->be($user);
</code></pre>
<p>You may re-seed your database from a test using the <code>seed</code> method:</p>
<p><strong>Re-Seeding Database From Tests</strong></p>
<pre class="prettyprint"><code>$this->seed();
$this->seed($connection);
</code></pre>
<p>More information on creating seeds may be found in the <a href="/docs/migrations#database-seeding">migrations and seeding</a> section of the documentation.</p>
</article><div class="clearfix"></div>
</div>
</section>
<script src="js/jquery.min.js"></script>
<script src="js/plugins.js"></script>
<script type="text/javascript" src="vendor/google-code-prettify/run_prettify.js"></script>
</body></html>