Skip to content

Commit c35ee08

Browse files
committed
Refactor web tests too have setUp
1 parent fd8ce0c commit c35ee08

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

tests/functional/WebTest.php

+29-32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
class WebTest extends WebTestCase
77
{
8+
private $client;
9+
810
public function createApplication()
911
{
1012
$app = require __DIR__.'/../../src/app.php';
@@ -20,12 +22,17 @@ public function createApplication()
2022
return $app;
2123
}
2224

23-
public function testRoot()
25+
public function setUp()
2426
{
25-
$client = $this->createClient();
27+
parent::setUp();
2628

27-
$client->request('GET', '/');
28-
$response = $client->getResponse();
29+
$this->client = $this->createClient();
30+
}
31+
32+
public function testRoot()
33+
{
34+
$this->client->request('GET', '/');
35+
$response = $this->client->getResponse();
2936
$this->assertTrue($response->isOk(), '/ should return ok response');
3037
$this->assertContains('trashbin', $response->getContent());
3138
$this->assertContains('simple pastebin', $response->getContent());
@@ -35,55 +42,49 @@ public function testRoot()
3542

3643
public function testCreatePaste()
3744
{
38-
$client = $this->createClient();
39-
$client->setServerParameters(array('REQUEST_TIME' => 1337882841));
45+
$this->client->setServerParameters(array('REQUEST_TIME' => 1337882841));
4046

41-
$crawler = $client->request('GET', '/');
47+
$crawler = $this->client->request('GET', '/');
4248

4349
$form = $crawler->filter('form')->form();
4450
$form['content'] = 'foobar';
4551

46-
$crawler = $client->submit($form);
47-
$crawler = $client->followRedirect();
52+
$crawler = $this->client->submit($form);
53+
$crawler = $this->client->followRedirect();
4854

49-
$response = $client->getResponse();
55+
$response = $this->client->getResponse();
5056
$this->assertTrue($response->isOk());
5157
$this->assertContains('foobar', $response->getContent());
5258
}
5359

5460
/** @test */
5561
public function createPasteWithoutContentShouldFail()
5662
{
57-
$client = $this->createClient();
58-
59-
$crawler = $client->request('GET', '/');
63+
$crawler = $this->client->request('GET', '/');
6064

6165
$form = $crawler->filter('form')->form();
6266
$form['content'] = '';
6367

64-
$client->submit($form);
68+
$this->client->submit($form);
6569

66-
$response = $client->getResponse();
70+
$response = $this->client->getResponse();
6771
$this->assertSame(400, $response->getStatusCode());
6872
}
6973

7074
public function testViewPaste()
7175
{
72-
$client = $this->createClient();
73-
74-
$client->request('GET', '/abcdef12');
75-
$response = $client->getResponse();
76+
$this->client->request('GET', '/abcdef12');
77+
$response = $this->client->getResponse();
7678
$this->assertTrue($response->isOk());
7779
$this->assertContains('foobar', $response->getContent());
7880
}
7981

8082
public function testCreatePasteWithParent()
8183
{
82-
$client = $this->createClient();
83-
$crawler = $client->request('GET', '/abcdef12');
84+
$crawler = $this->client->request('GET', '/abcdef12');
8485

8586
$link = $crawler->selectLink('copy')->link();
86-
$crawler = $client->click($link);
87+
$crawler = $this->client->click($link);
8788

8889
$form = $crawler->filter('form')->form();
8990
$this->assertSame('foobar', $form['content']->getValue());
@@ -95,17 +96,13 @@ public function testCreatePasteWithParent()
9596
*/
9697
public function testViewPasteWithInvalidId()
9798
{
98-
$client = $this->createClient();
99-
100-
$client->request('GET', '/00000000');
99+
$this->client->request('GET', '/00000000');
101100
}
102101

103102
public function testAbout()
104103
{
105-
$client = $this->createClient();
106-
107-
$client->request('GET', '/about');
108-
$response = $client->getResponse();
104+
$this->client->request('GET', '/about');
105+
$response = $this->client->getResponse();
109106
$this->assertTrue($response->isOk(), '/about should return ok response');
110107
$this->assertContains('trashbin', $response->getContent());
111108
$this->assertContains('github', $response->getContent());
@@ -116,10 +113,10 @@ public function testErrorHandler()
116113
{
117114
$this->app['debug'] = false;
118115

119-
$client = $this->createClient();
116+
$this->client = $this->createClient();
120117

121-
$client->request('GET', '/non-existent');
122-
$response = $client->getResponse();
118+
$this->client->request('GET', '/non-existent');
119+
$response = $this->client->getResponse();
123120
$this->assertSame(404, $response->getStatusCode());
124121
}
125122
}

0 commit comments

Comments
 (0)