Skip to content

refactor: add test for DownloadResponse #6375

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

Merged
merged 4 commits into from
Aug 14, 2022
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
6 changes: 4 additions & 2 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -982,8 +982,10 @@ protected function gatherOutput(?Cache $cacheConfig = null, $returned = null)

if ($returned instanceof DownloadResponse) {
// Turn off output buffering completely, even if php.ini output_buffering is not off
while (ob_get_level() > 0) {
ob_end_clean();
if (ENVIRONMENT !== 'testing') {
while (ob_get_level() > 0) {
ob_end_clean();
}
}

$this->response = $returned;
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Filters/Customfilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Customfilter implements \CodeIgniter\Filters\FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
$request->url = 'http://hellowworld.com';
$request->appendBody('http://hellowworld.com');

return $request;
}
Expand Down
31 changes: 30 additions & 1 deletion tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,33 @@ public function testControllersCanReturnResponseObject()
$this->assertStringContainsString("You want to see 'about' page.", $output);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6358
*/
public function testControllersCanReturnDownloadResponseObject()
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;

$_SERVER['REQUEST_URI'] = '/pages/about';

// Inject mock router.
$routes = Services::routes();
$routes->add('pages/(:segment)', static function ($segment) {
$response = Services::response();

return $response->download('some.txt', 'some text', true);
});
$router = Services::router($routes, Services::request());
Services::injectMock('router', $router);

ob_start();
$this->codeigniter->useSafeOutput(true)->run();
$output = ob_get_clean();

$this->assertSame('some text', $output);
}

public function testControllersRunFilterByClassName()
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
Expand All @@ -200,7 +227,7 @@ public function testControllersRunFilterByClassName()

// Inject mock router.
$routes = Services::routes();
$routes->add('pages/about', static fn () => Services::request()->url, ['filter' => Customfilter::class]);
$routes->add('pages/about', static fn () => Services::request()->getBody(), ['filter' => Customfilter::class]);

$router = Services::router($routes, Services::request());
Services::injectMock('router', $router);
Expand All @@ -210,6 +237,8 @@ public function testControllersRunFilterByClassName()
$output = ob_get_clean();

$this->assertStringContainsString('http://hellowworld.com', $output);

$this->resetServices();
}

public function testResponseConfigEmpty()
Expand Down
4 changes: 3 additions & 1 deletion tests/system/Filters/FiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,9 @@ public function testCustomFiltersLoad()
$uri = 'admin/foo/bar';
$request = $filters->run($uri, 'before');

$this->assertSame('http://hellowworld.com', $request->url);
$this->assertSame('http://hellowworld.com', $request->getBody());

$this->resetServices();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,8 @@ public function testRouteWorksWithClassnameFilter()
$this->assertSame('\TestController', $router->controllerName());
$this->assertSame('foo', $router->methodName());
$this->assertSame(Customfilter::class, $router->getFilter());

$this->resetServices();
}

public function testRouteWorksWithMultipleFilters()
Expand Down
5 changes: 2 additions & 3 deletions tests/system/Test/FilterTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ public function testCallerUsesClonedInstance()
$caller = $this->getFilterCaller('test-customfilter', 'before');
$result = $caller();

$this->assertObjectNotHasAttribute('url', $this->request);
$this->assertSame('http://hellowworld.com', $result->getBody());

$this->assertObjectHasAttribute('url', $result);
$this->assertSame('http://hellowworld.com', $result->url);
$this->resetServices();
}

public function testGetFiltersForRoute()
Expand Down