Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: ControllerTester::execute. Fixes #1834 #1855

Merged
merged 1 commit into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}