Skip to content

Commit 8335646

Browse files
committed
Extract Laravel trait
1 parent d69ca02 commit 8335646

File tree

2 files changed

+161
-151
lines changed

2 files changed

+161
-151
lines changed

src/Extensions/Laravel.php

Lines changed: 2 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -2,160 +2,11 @@
22

33
namespace Laracasts\Integrated\Extensions;
44

5-
use PHPUnit_Framework_ExpectationFailedException as PHPUnitException;
6-
use Laracasts\Integrated\Extensions\Traits\ApiRequests;
5+
use Laracasts\Integrated\Extensions\Traits\LaravelTestCase;
76
use Illuminate\Foundation\Testing\TestCase;
8-
use Symfony\Component\DomCrawler\Crawler;
9-
use Symfony\Component\DomCrawler\Form;
107
use Laracasts\Integrated\Emulator;
118

129
abstract class Laravel extends TestCase implements Emulator
1310
{
14-
use IntegrationTrait, ApiRequests;
15-
16-
/**
17-
* Enable method spoofing for HTML forms with a "_method" attribute.
18-
*
19-
* @setUp
20-
*/
21-
protected function enableMethodSpoofing()
22-
{
23-
$this->app['request']->enableHttpMethodParameterOverride();
24-
}
25-
26-
/**
27-
* Get the base url for all requests.
28-
*
29-
* @return string
30-
*/
31-
public function baseUrl()
32-
{
33-
return "http://localhost";
34-
}
35-
36-
/**
37-
* Submit a form on the page.
38-
*
39-
* @param string $buttonText
40-
* @param array $formData
41-
* @return static
42-
*/
43-
public function submitForm($buttonText, $formData = [])
44-
{
45-
$this->makeRequestUsingForm(
46-
$this->fillForm($buttonText, $formData)
47-
);
48-
49-
return $this;
50-
}
51-
52-
/**
53-
* Call a URI in the application.
54-
*
55-
* @param string $requestType
56-
* @param string $uri
57-
* @param array $parameters
58-
* @param array $cookies
59-
* @param array $files
60-
* @return static
61-
*/
62-
protected function makeRequest($requestType, $uri, $parameters = [], $cookies = [], $files = [])
63-
{
64-
$this->call($requestType, $uri, $parameters, $cookies, $files);
65-
66-
$this->clearInputs()->followRedirects()->assertPageLoaded($uri);
67-
68-
// We'll set the current page again here, since it's possible
69-
// that the user was redirected.
70-
71-
$this->currentPage = $this->app['request']->fullUrl();
72-
73-
$this->crawler = new Crawler($this->response(), $this->currentPage());
74-
75-
return $this;
76-
}
77-
78-
/**
79-
* Follow 302 redirections.
80-
*
81-
* @return void
82-
*/
83-
protected function followRedirects()
84-
{
85-
while ($this->response->isRedirect()) {
86-
$this->makeRequest('GET', $this->response->getTargetUrl());
87-
}
88-
89-
return $this;
90-
}
91-
92-
/**
93-
* Make a request to a URL using form parameters.
94-
*
95-
* @param Form $form
96-
* @return static
97-
*/
98-
protected function makeRequestUsingForm(Form $form)
99-
{
100-
return $this->makeRequest(
101-
$form->getMethod(), $form->getUri(), $form->getValues(), [], $form->getFiles()
102-
);
103-
}
104-
105-
/**
106-
* Get the number of rows that match the given condition.
107-
*
108-
* @param string $table
109-
* @param array $data
110-
* @return integer
111-
*/
112-
protected function seeRowsWereReturned($table, $data)
113-
{
114-
return $this->app['db']->table($table)->where($data)->count();
115-
}
116-
117-
/**
118-
* Get the content from the reponse.
119-
*
120-
* @return string
121-
*/
122-
protected function response()
123-
{
124-
return $this->response->getContent();
125-
}
126-
127-
/**
128-
* Get the status code from the last request.
129-
*
130-
* @return string
131-
*/
132-
protected function statusCode()
133-
{
134-
return $this->response->getStatusCode();
135-
}
136-
137-
/**
138-
* Provide additional messaging for 500 errors.
139-
*
140-
* @param string|null $message
141-
* @throws PHPUnitException
142-
* @return void
143-
*/
144-
protected function handleInternalError($message = null)
145-
{
146-
$crawler = new Crawler($this->response(), $this->currentPage());
147-
148-
// A little weird, but we need to parse the output HTML to
149-
// figure out the specifics of where the error occurred.
150-
// There might be an easier way to figure this out.
151-
152-
$crawler = $crawler->filter('.exception_title');
153-
$exception = $crawler->filter('abbr')->html();
154-
$location = $crawler->filter('a')->extract('title')[0];
155-
156-
$message .= "\n\n{$exception} on {$location}";
157-
158-
throw new PHPUnitException($message);
159-
}
160-
11+
use LaravelTestCase;
16112
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace Laracasts\Integrated\Extensions\Traits;
4+
5+
use Laracasts\Integrated\Extensions\IntegrationTrait;
6+
use Laracasts\Integrated\Extensions\Traits\ApiRequests;
7+
use Symfony\Component\DomCrawler\Crawler;
8+
use Symfony\Component\DomCrawler\Form;
9+
10+
trait LaravelTestCase
11+
{
12+
13+
use IntegrationTrait, ApiRequests;
14+
15+
/**
16+
* Enable method spoofing for HTML forms with a "_method" attribute.
17+
*
18+
* @setUp
19+
*/
20+
protected function enableMethodSpoofing()
21+
{
22+
$this->app['request']->enableHttpMethodParameterOverride();
23+
}
24+
25+
/**
26+
* Get the base url for all requests.
27+
*
28+
* @return string
29+
*/
30+
public function baseUrl()
31+
{
32+
return "http://localhost";
33+
}
34+
35+
/**
36+
* Submit a form on the page.
37+
*
38+
* @param string $buttonText
39+
* @param array $formData
40+
* @return static
41+
*/
42+
public function submitForm($buttonText, $formData = [])
43+
{
44+
$this->makeRequestUsingForm(
45+
$this->fillForm($buttonText, $formData)
46+
);
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* Call a URI in the application.
53+
*
54+
* @param string $requestType
55+
* @param string $uri
56+
* @param array $parameters
57+
* @param array $cookies
58+
* @param array $files
59+
* @return static
60+
*/
61+
protected function makeRequest($requestType, $uri, $parameters = [], $cookies = [], $files = [])
62+
{
63+
$this->call($requestType, $uri, $parameters, $cookies, $files);
64+
65+
$this->clearInputs()->followRedirects()->assertPageLoaded($uri);
66+
67+
// We'll set the current page again here, since it's possible
68+
// that the user was redirected.
69+
70+
$this->currentPage = $this->app['request']->fullUrl();
71+
72+
$this->crawler = new Crawler($this->response(), $this->currentPage());
73+
74+
return $this;
75+
}
76+
77+
/**
78+
* Follow 302 redirections.
79+
*
80+
* @return void
81+
*/
82+
protected function followRedirects()
83+
{
84+
while ($this->response->isRedirect()) {
85+
$this->makeRequest('GET', $this->response->getTargetUrl());
86+
}
87+
88+
return $this;
89+
}
90+
91+
/**
92+
* Make a request to a URL using form parameters.
93+
*
94+
* @param Form $form
95+
* @return static
96+
*/
97+
protected function makeRequestUsingForm(Form $form)
98+
{
99+
return $this->makeRequest(
100+
$form->getMethod(), $form->getUri(), $form->getValues(), [], $form->getFiles()
101+
);
102+
}
103+
104+
/**
105+
* Get the number of rows that match the given condition.
106+
*
107+
* @param string $table
108+
* @param array $data
109+
* @return integer
110+
*/
111+
protected function seeRowsWereReturned($table, $data)
112+
{
113+
return $this->app['db']->table($table)->where($data)->count();
114+
}
115+
116+
/**
117+
* Get the content from the reponse.
118+
*
119+
* @return string
120+
*/
121+
protected function response()
122+
{
123+
return $this->response->getContent();
124+
}
125+
126+
/**
127+
* Get the status code from the last request.
128+
*
129+
* @return string
130+
*/
131+
protected function statusCode()
132+
{
133+
return $this->response->getStatusCode();
134+
}
135+
136+
/**
137+
* Provide additional messaging for 500 errors.
138+
*
139+
* @param string|null $message
140+
* @throws PHPUnitException
141+
* @return void
142+
*/
143+
protected function handleInternalError($message = null)
144+
{
145+
$crawler = new Crawler($this->response(), $this->currentPage());
146+
147+
// A little weird, but we need to parse the output HTML to
148+
// figure out the specifics of where the error occurred.
149+
// There might be an easier way to figure this out.
150+
151+
$crawler = $crawler->filter('.exception_title');
152+
$exception = $crawler->filter('abbr')->html();
153+
$location = $crawler->filter('a')->extract('title')[0];
154+
155+
$message .= "\n\n{$exception} on {$location}";
156+
157+
throw new PHPUnitException($message);
158+
}
159+
}

0 commit comments

Comments
 (0)