Skip to content

Commit b6efad4

Browse files
authored
Merge pull request #38 from kbond/assert-json
BrowserKitBrowser::assertJson() improvements and bug fix
2 parents e0e59b1 + 9351c6e commit b6efad4

File tree

6 files changed

+58
-7
lines changed

6 files changed

+58
-7
lines changed

src/Browser/BrowserKitBrowser.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,9 @@ final public function assertHeaderContains(string $header, string $expected): se
207207
/**
208208
* @return static
209209
*/
210-
final public function assertJson(): self
210+
final public function assertJson(string $expectedContentType = 'json'): self
211211
{
212-
return $this->wrapMinkExpectation(
213-
fn() => $this->webAssert()->responseHeaderContains('Content-Type', 'application/json')
214-
);
212+
return $this->assertHeaderContains('Content-Type', $expectedContentType);
215213
}
216214

217215
/**

src/Browser/Mink/PantherDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public function getText($xpath): string
7777
if (($element = $crawler->getElement(0)) && 'title' === $element->getTagName()) {
7878
// hack to get the text of the title html element
7979
// for this element, WebDriverElement::getText() returns an empty string
80-
// the only way to get the value is to get the html
81-
return \strip_tags($crawler->html());
80+
// the only way to get the value is to get title from the client
81+
return $this->client->getTitle();
8282
}
8383

8484
$text = $crawler->text();

src/Browser/Response/JsonResponse.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ final class JsonResponse extends Response
1313
{
1414
public function json()
1515
{
16+
if (empty($this->body())) {
17+
return null;
18+
}
19+
1620
return \json_decode($this->body(), true, 512, \JSON_THROW_ON_ERROR);
1721
}
1822

tests/BrowserKitBrowserTests.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,34 @@ public function can_assert_json_matches(): void
256256
;
257257
}
258258

259+
/**
260+
* @test
261+
*/
262+
public function assert_json_alternate_content_types(): void
263+
{
264+
$this->browser()
265+
->get('/json?content-type=application/vnd.custom.json', ['json' => 'foo'])
266+
->assertSuccessful()
267+
->assertJson()
268+
->assertJson('application/vnd.custom.json')
269+
;
270+
}
271+
272+
/**
273+
* @test
274+
*/
275+
public function can_dump_empty_json_request(): void
276+
{
277+
$output = self::catchVarDumperOutput(function() {
278+
$this->browser()
279+
->post('/json')
280+
->dump()
281+
;
282+
});
283+
284+
$this->assertStringContainsString('content-type: application/json', $output[0]);
285+
}
286+
259287
/**
260288
* @test
261289
*/

tests/Fixture/Kernel.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ public function httpMethod(Request $request): Response
7575

7676
public function json(Request $request): JsonResponse
7777
{
78-
return new JsonResponse($request->getContent(), 200, [], true);
78+
return new JsonResponse(
79+
$request->getContent(),
80+
200,
81+
['Content-Type' => $request->query->get('content-type', 'application/json')],
82+
true
83+
);
7984
}
8085

8186
public function exception(): void

tests/PantherBrowserTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,22 @@ public function cannot_follow_invisible_link(): void
187187
;
188188
}
189189

190+
/**
191+
* @test
192+
*/
193+
public function can_dump_html_element(): void
194+
{
195+
$this->markTestIncomplete('TODO: fix.');
196+
}
197+
198+
/**
199+
* @test
200+
*/
201+
public function if_dump_selector_matches_multiple_elements_all_are_dumped(): void
202+
{
203+
$this->markTestIncomplete('TODO: fix.');
204+
}
205+
190206
protected function browser(): PantherBrowser
191207
{
192208
return $this->pantherBrowser();

0 commit comments

Comments
 (0)