Skip to content

Commit b29cb39

Browse files
committed
Fix tests
1 parent cbb9b70 commit b29cb39

File tree

2 files changed

+143
-5
lines changed

2 files changed

+143
-5
lines changed

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
"intervention/image": "^2.3"
2727
},
2828
"require-dev": {
29-
"phpunit/phpunit": "~4.8.20",
30-
"orchestra/database": "~3.2.0|~3.3.0|~3.4.0-dev",
31-
"orchestra/testbench": "~3.2.4|~3.3.0|~3.4.0-dev",
29+
"phpunit/phpunit": "~4.8.20|~5.0",
30+
"orchestra/testbench": "~3.2.4|~3.3.0|~3.4.0",
3231
"mockery/mockery": "^0.9.5",
3332
"hamcrest/hamcrest-php": "~1.2",
3433
"league/flysystem-aws-s3-v3": "^1.0.1",

tests/TestCase.php

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
<?php
22

33

4+
use Illuminate\Http\UploadedFile;
5+
use Illuminate\Support\Arr;
6+
use Laravel\BrowserKitTesting\Concerns\InteractsWithSession;
7+
use Laravel\BrowserKitTesting\Constraints\HasSource;
8+
use Laravel\BrowserKitTesting\Constraints\PageConstraint;
9+
use Laravel\BrowserKitTesting\Constraints\ReversePageConstraint;
10+
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
11+
use PHPUnit_Framework_Assert as PHPUnit;
12+
413
class TestCase extends \Orchestra\Testbench\TestCase
514
{
6-
use \Laravel\BrowserKitTesting\Concerns\MakesHttpRequests;
7-
use \Laravel\BrowserKitTesting\Concerns\InteractsWithSession;
15+
use InteractsWithSession;
16+
17+
protected $crawler;
18+
protected $response;
819

920
/** @before */
1021
public function injectDependencies()
@@ -55,4 +66,132 @@ protected function addDependencyForCallParameter(ReflectionParameter $parameter,
5566
$dependencies[] = $parameter->getDefaultValue();
5667
}
5768
}
69+
70+
public function assertRedirectedTo($uri, $with = [])
71+
{
72+
PHPUnit::assertInstanceOf('Illuminate\Http\RedirectResponse', $this->response);
73+
74+
PHPUnit::assertEquals($this->app['url']->to($uri), $this->response->headers->get('Location'));
75+
76+
$this->assertSessionHasAll($with);
77+
78+
return $this;
79+
}
80+
81+
public function see($text, $negate = false)
82+
{
83+
return $this->_assertInPage(new HasSource($text), $negate);
84+
}
85+
86+
public function seeJson(array $data = null, $negate = false)
87+
{
88+
if (is_null($data)) {
89+
$this->assertJson(
90+
$this->response->getContent(), "JSON was not returned from [{$this->currentUri}]."
91+
);
92+
93+
return $this;
94+
}
95+
96+
try {
97+
return $this->seeJsonEquals($data);
98+
} catch (PHPUnit_Framework_ExpectationFailedException $e) {
99+
return $this->seeJsonContains($data, $negate);
100+
}
101+
}
102+
103+
public function seeJsonEquals(array $data)
104+
{
105+
$actual = json_encode(Arr::sortRecursive(
106+
(array) $this->decodeResponseJson()
107+
));
108+
109+
$this->assertEquals(json_encode(Arr::sortRecursive($data)), $actual);
110+
111+
return $this;
112+
}
113+
114+
protected function _assertInPage(PageConstraint $constraint, $reverse = false, $message = '')
115+
{
116+
if ($reverse) {
117+
$constraint = new ReversePageConstraint($constraint);
118+
}
119+
120+
self::assertThat(
121+
$this->crawler() ?: $this->response->getContent(),
122+
$constraint, $message
123+
);
124+
125+
return $this;
126+
}
127+
128+
protected function crawler()
129+
{
130+
if (! empty($this->subCrawlers)) {
131+
return end($this->subCrawlers);
132+
}
133+
134+
return $this->crawler;
135+
}
136+
137+
public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
138+
{
139+
$kernel = $this->app->make('Illuminate\Contracts\Http\Kernel');
140+
141+
$this->currentUri = $this->prepareUrlForRequest($uri);
142+
143+
$this->resetPageContext();
144+
145+
$symfonyRequest = SymfonyRequest::create(
146+
$this->currentUri, $method, $parameters,
147+
$cookies, $this->filterFiles($files), array_replace($this->serverVariables, $server), $content
148+
);
149+
150+
$request = Request::createFromBase($symfonyRequest);
151+
152+
$response = $kernel->handle($request);
153+
154+
$kernel->terminate($request, $response);
155+
156+
return $this->response = $response;
157+
}
158+
159+
protected function resetPageContext()
160+
{
161+
$this->crawler = null;
162+
163+
$this->subCrawlers = [];
164+
}
165+
protected function filterFiles($files)
166+
{
167+
foreach ($files as $key => $file) {
168+
if ($file instanceof UploadedFile) {
169+
continue;
170+
}
171+
172+
if (is_array($file)) {
173+
if (! isset($file['name'])) {
174+
$files[$key] = $this->filterFiles($files[$key]);
175+
} elseif (isset($files[$key]['error']) && $files[$key]['error'] !== 0) {
176+
unset($files[$key]);
177+
}
178+
179+
continue;
180+
}
181+
182+
unset($files[$key]);
183+
}
184+
185+
return $files;
186+
}
187+
protected function decodeResponseJson()
188+
{
189+
$decodedResponse = json_decode($this->response->getContent(), true);
190+
191+
if (is_null($decodedResponse) || $decodedResponse === false) {
192+
$this->fail('Invalid JSON was returned from the route. Perhaps an exception was thrown?');
193+
}
194+
195+
return $decodedResponse;
196+
}
58197
}

0 commit comments

Comments
 (0)