Skip to content

Commit

Permalink
Merge pull request #1855 from jim-parry/testing29
Browse files Browse the repository at this point in the history
Fix: ControllerTester::execute. Fixes #1834
  • Loading branch information
jim-parry authored Mar 22, 2019
2 parents f5563e6 + e8bbe4b commit 57b0bc4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
13 changes: 10 additions & 3 deletions system/Test/ControllerTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,17 @@ public function execute(string $method, ...$params)
if (is_string($response))
{
$output = $response;
$result->response()->setBody($output);
$result->setBody($output);
}
elseif (! empty($response) && ! empty($response->getBody()))
{
$result->setBody($response->getBody());
}
else
{
$result->setBody('');
}

$result->response()->setBody($output);
$result->setBody($output);
}

// If not response code has been sent, assume a success
Expand Down
14 changes: 13 additions & 1 deletion tests/_support/Controllers/Popcorn.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function popper()

public function weasel()
{
$this->respond(['Nothing to see here'], 200);
$this->respond('', 200);
}

public function oops()
Expand All @@ -43,4 +43,16 @@ public function goaway()
{
return redirect()->to('/');
}

// @see https://github.com/codeigniter4/CodeIgniter4/issues/1834
public function index3()
{
$response = $this->response->setJSON([
'lang' => $this->request->getLocale(),
]);

// echo var_dump($this->response->getBody());
return $response;
}

}
14 changes: 12 additions & 2 deletions tests/system/Test/ControllerTesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public function testPopcornFailure()
->controller(\Tests\Support\Controllers\Popcorn::class)
->execute('pop');

$body = $result->getBody();
$this->assertEquals(567, $result->response()->getStatusCode());
}

Expand All @@ -107,7 +106,6 @@ public function testPopcornException()
->controller(\Tests\Support\Controllers\Popcorn::class)
->execute('popper');

$body = $result->getBody();
$this->assertEquals(500, $result->response()->getStatusCode());
}

Expand Down Expand Up @@ -163,6 +161,7 @@ public function testEmptyResponse()
->execute('weasel');

$body = $result->getBody(); // empty
$this->assertEmpty($body);
$this->assertFalse($result->isOK());
}

Expand Down Expand Up @@ -200,4 +199,15 @@ public function testFailsForward()
$this->assertNull($result->ohno('Hi'));
}

// @see https://github.com/codeigniter4/CodeIgniter4/issues/1834
public function testResponseOverriding()
{
$result = $this->withURI('http://example.com/rest/')
->controller(\Tests\Support\Controllers\Popcorn::class)
->execute('index3');

$response = json_decode($result->getBody());
$this->assertEquals('en', $response->lang);
}

}

0 comments on commit 57b0bc4

Please sign in to comment.