Skip to content

Commit 2e59fd8

Browse files
committed
FeatureResponseTest - added tests, changed getJSON to an accessor not a test
1 parent 1d32be3 commit 2e59fd8

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

system/Test/FeatureResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,15 @@ public function assertSeeInField(string $field, string $value = null)
298298
/**
299299
* Returns the response's body as JSON
300300
*
301-
* @return mixed|string
301+
* @return mixed|false
302302
*/
303303
public function getJSON()
304304
{
305305
$response = $this->response->getJSON();
306306

307307
if (is_null($response))
308308
{
309-
$this->fail('The Response contained invalid JSON.');
309+
return false;
310310
}
311311

312312
return $response;

tests/system/Test/FeatureResponseTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
class FeatureResponseTest extends CIUnitTestCase
88
{
9+
910
/**
1011
* @var FeatureResponse
1112
*/
1213
protected $feature;
14+
1315
/**
1416
* @var Response
1517
*/
@@ -44,6 +46,15 @@ public function testIsOKSuccess()
4446
$this->assertTrue($this->feature->isOK());
4547
}
4648

49+
public function testIsOKEmpty()
50+
{
51+
$this->getFeatureResponse('Hi there');
52+
$this->response->setStatusCode(200);
53+
$this->response->setBody('');
54+
55+
$this->assertFalse($this->feature->isOK());
56+
}
57+
4758
public function testAssertSee()
4859
{
4960
$this->getFeatureResponse('<h1>Hello World</h1>');
@@ -78,6 +89,14 @@ public function testAssertDontSeeElement()
7889
$this->feature->assertDontSeeElement('h1.para');
7990
}
8091

92+
public function testAssertSeeLink()
93+
{
94+
$this->getFeatureResponse('<h1 class="header"><a href="http://example.com/hello">Hello</a> <span>World</span></h1>');
95+
96+
$this->feature->assertSeeElement('h1');
97+
$this->feature->assertSeeLink('Hello');
98+
}
99+
81100
public function testAssertSeeInField()
82101
{
83102
$this->getFeatureResponse('<html><body><input type="text" name="user[name]" value="Foobar"></body></html>');
@@ -147,6 +166,14 @@ public function testAssertHeader()
147166
$this->feature->assertHeader('foo', 'bar');
148167
}
149168

169+
public function testAssertHeaderMissing()
170+
{
171+
$this->getFeatureResponse('<h1>Hello World</h1>', [], ['foo' => 'bar']);
172+
173+
$this->feature->assertHeader('foo');
174+
$this->feature->assertHeaderMissing('banana');
175+
}
176+
150177
public function testAssertCookie()
151178
{
152179
$this->getFeatureResponse('<h1>Hello World</h1>');
@@ -184,6 +211,17 @@ public function testGetJSON()
184211
$this->assertEquals($formatter->format(['foo' => 'bar']), $this->feature->getJSON());
185212
}
186213

214+
public function testInvalidJSON()
215+
{
216+
$this->getFeatureResponse('<h1>Hello World</h1>');
217+
$this->response->setJSON('');
218+
$config = new \Config\Format();
219+
$formatter = $config->getFormatter('application/json');
220+
221+
// this should fail because of empty JSON
222+
$this->assertFalse($this->feature->getJSON());
223+
}
224+
187225
public function testGetXML()
188226
{
189227
$this->getFeatureResponse(['foo' => 'bar']);
@@ -265,4 +303,5 @@ protected function getFeatureResponse($body = null, array $responseOptions = [],
265303

266304
$this->feature = new FeatureResponse($this->response);
267305
}
306+
268307
}

0 commit comments

Comments
 (0)