Skip to content

Commit

Permalink
Merge pull request #1312 from jim-parry/enhance/testing
Browse files Browse the repository at this point in the history
Add headerEmited (or not) assertions to CIUnitTestCase
  • Loading branch information
jim-parry authored Oct 26, 2018
2 parents bd7b0e6 + e0c8fd4 commit e6a6d64
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 73 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ Please read the [*Contributing to CodeIgniter*](https://github.com/bcit-ci/CodeI
## Server Requirements
PHP version 7.1 or higher is required, with the following extensions installed:

- intl
- [intl](http://php.net/manual/en/intl.requirements.php)
- [libcurl](http://php.net/manual/en/curl.requirements.php) if you plan to use the HTTP\CURLRequest library

Additionally, make sure that the following extensions are enabled in your PHP:

- json (enabled by default - don't turn it off)
- [mbstring](http://php.net/manual/en/mbstring.installation.php)
- [mysqlnd](http://php.net/manual/en/mysqlnd.install.php)
- xml (enabled by default - don't turn it off)

## Running CodeIgniter Tests
Information on running CodeIgniter test suite can be found in the [README.md](tests/README.md) file in the tests directory.
47 changes: 23 additions & 24 deletions application/Config/Boot/development.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
<?php

/*
|--------------------------------------------------------------------------
| ERROR DISPLAY
|--------------------------------------------------------------------------
| In development, we want to show as many errors as possible to help
| make sure they don't make it to production. And save us hours of
| painful debugging.
*/
|--------------------------------------------------------------------------
| ERROR DISPLAY
|--------------------------------------------------------------------------
| In development, we want to show as many errors as possible to help
| make sure they don't make it to production. And save us hours of
| painful debugging.
*/
error_reporting(-1);
ini_set('display_errors', 1);

/*
|--------------------------------------------------------------------------
| DEBUG BACKTRACES
|--------------------------------------------------------------------------
| If true, this constant will tell the error screens to display debug
| backtraces along with the other error information. If you would
| prefer to not see this, set this value to false.
*/
define('SHOW_DEBUG_BACKTRACE', true);
|--------------------------------------------------------------------------
| DEBUG BACKTRACES
|--------------------------------------------------------------------------
| If true, this constant will tell the error screens to display debug
| backtraces along with the other error information. If you would
| prefer to not see this, set this value to false.
*/
defined('SHOW_DEBUG_BACKTRACE') || define('SHOW_DEBUG_BACKTRACE', true);

/*
|--------------------------------------------------------------------------
| DEBUG MODE
|--------------------------------------------------------------------------
| Debug mode is an experimental flag that can allow changes throughout
| the system. This will control whether Kint is loaded, and a few other
| items. It can always be used within your own application too.
*/
|--------------------------------------------------------------------------
| DEBUG MODE
|--------------------------------------------------------------------------
| Debug mode is an experimental flag that can allow changes throughout
| the system. This will control whether Kint is loaded, and a few other
| items. It can always be used within your own application too.
*/

define('CI_DEBUG', 1);
defined('CI_DEBUG') || define('CI_DEBUG', 1);
28 changes: 14 additions & 14 deletions application/Config/Boot/production.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?php

/*
|--------------------------------------------------------------------------
| ERROR DISPLAY
|--------------------------------------------------------------------------
| Don't show ANY in production environments. Instead, let the system catch
| it and display a generic error message.
*/
|--------------------------------------------------------------------------
| ERROR DISPLAY
|--------------------------------------------------------------------------
| Don't show ANY in production environments. Instead, let the system catch
| it and display a generic error message.
*/
ini_set('display_errors', 0);
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);

/*
|--------------------------------------------------------------------------
| DEBUG MODE
|--------------------------------------------------------------------------
| Debug mode is an experimental flag that can allow changes throughout
| the system. It's not widely used currently, and may not survive
| release of the framework.
*/
|--------------------------------------------------------------------------
| DEBUG MODE
|--------------------------------------------------------------------------
| Debug mode is an experimental flag that can allow changes throughout
| the system. It's not widely used currently, and may not survive
| release of the framework.
*/

define('CI_DEBUG', 0);
defined('CI_DEBUG') || define('CI_DEBUG', 0);
46 changes: 23 additions & 23 deletions application/Config/Boot/testing.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<?php

/*
|--------------------------------------------------------------------------
| ERROR DISPLAY
|--------------------------------------------------------------------------
| In development, we want to show as many errors as possible to help
| make sure they don't make it to production. And save us hours of
| painful debugging.
*/
|--------------------------------------------------------------------------
| ERROR DISPLAY
|--------------------------------------------------------------------------
| In development, we want to show as many errors as possible to help
| make sure they don't make it to production. And save us hours of
| painful debugging.
*/
error_reporting(-1);
ini_set('display_errors', 1);

/*
|--------------------------------------------------------------------------
| DEBUG BACKTRACES
|--------------------------------------------------------------------------
| If true, this constant will tell the error screens to display debug
| backtraces along with the other error information. If you would
| prefer to not see this, set this value to false.
*/
define('SHOW_DEBUG_BACKTRACE', true);
|--------------------------------------------------------------------------
| DEBUG BACKTRACES
|--------------------------------------------------------------------------
| If true, this constant will tell the error screens to display debug
| backtraces along with the other error information. If you would
| prefer to not see this, set this value to false.
*/
defined('SHOW_DEBUG_BACKTRACE') || define('SHOW_DEBUG_BACKTRACE', true);

/*
|--------------------------------------------------------------------------
| DEBUG MODE
|--------------------------------------------------------------------------
| Debug mode is an experimental flag that can allow changes throughout
| the system. It's not widely used currently, and may not survive
| release of the framework.
*/
|--------------------------------------------------------------------------
| DEBUG MODE
|--------------------------------------------------------------------------
| Debug mode is an experimental flag that can allow changes throughout
| the system. It's not widely used currently, and may not survive
| release of the framework.
*/

define('CI_DEBUG', 1);
defined('CI_DEBUG') || define('CI_DEBUG', 1);
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"php": ">=7.1",
"zendframework/zend-escaper": "^2.5",
"kint-php/kint": "^2.1",
"ext-intl": "*"
"ext-intl": "*",
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
Expand Down
67 changes: 61 additions & 6 deletions system/Test/CIUnitTestCase.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php namespace CodeIgniter\Test;
<?php
namespace CodeIgniter\Test;

/**
* CodeIgniter
Expand Down Expand Up @@ -45,6 +46,7 @@
*/
class CIUnitTestCase extends TestCase
{

use ReflectionHelper;

/**
Expand All @@ -63,7 +65,7 @@ public function setUp()
{
parent::setUp();

if (! $this->app)
if ( ! $this->app)
{
$this->app = $this->createApplication();
}
Expand Down Expand Up @@ -104,7 +106,8 @@ public function assertEventTriggered(string $eventName): bool

foreach (Events::getPerformanceLogs() as $log)
{
if ($log['event'] !== $eventName) continue;
if ($log['event'] !== $eventName)
continue;

$found = true;
break;
Expand All @@ -114,6 +117,57 @@ public function assertEventTriggered(string $eventName): bool
return $found;
}

/**
* Hooks into xdebug's headers capture, looking for a specific header
* emitted
*
* @param string $header The leading portion of the header we are looking for
* @param bool $ignoreCase
*
* @throws \Exception
*/
public function assertHeaderEmitted(string $header, bool $ignoreCase = false): void
{
$found = false;

foreach (xdebug_get_headers() as $emitted)
{
$found = $ignoreCase ?
(stripos($emitted, $header) === 0) :
(strpos($emitted, $header) === 0);
if ($found)
break;
}

$this->assertTrue($found,"Didn't find header for {$header}");
}

/**
* Hooks into xdebug's headers capture, looking for a specific header
* emitted
*
* @param string $header The leading portion of the header we don't want to find
* @param bool $ignoreCase
*
* @throws \Exception
*/
public function assertHeaderNotEmitted(string $header, bool $ignoreCase = false): void
{
$found = false;

foreach (xdebug_get_headers() as $emitted)
{
$found = $ignoreCase ?
(stripos($emitted, $header) === 0) :
(strpos($emitted, $header) === 0);
if ($found)
break;
}

$success = ! $found;
$this->assertTrue($success,"Found header for {$header}");
}

/**
* Loads up an instance of CodeIgniter
* and gets the environment setup.
Expand All @@ -122,12 +176,12 @@ public function assertEventTriggered(string $eventName): bool
*/
protected function createApplication()
{
$systemPath = realpath(__DIR__.'/../');
$systemPath = realpath(__DIR__ . '/../');

require_once $systemPath.'/'.$this->configPath.'/Paths.php';
require_once $systemPath . '/' . $this->configPath . '/Paths.php';
$paths = $this->adjustPaths(new \Config\Paths());

$app = require $systemPath.'/bootstrap.php';
$app = require $systemPath . '/bootstrap.php';
return $app;
}

Expand Down Expand Up @@ -162,4 +216,5 @@ protected function adjustPaths(Paths $paths)

return $paths;
}

}
99 changes: 99 additions & 0 deletions tests/system/Test/TestCaseEmissionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
namespace CodeIgniter\Test;

use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use Config\App;

/**
* This test suite has been created separately from
* TestCaseTest because it messes with output
* buffering from PHPUnit, and the individual
* test cases need to be run as separate processes.
*/
class TestCaseEmissionsTest extends \CIUnitTestCase
{

//--------------------------------------------------------------------
/**
* This needs to be run as a separate process, since phpunit
* has already captured the "normal" output, and we will get
* a "Cannot modify headers" message if we try to change
* headers or cookies now.
*
* Furthermore, this test needs to flush the output buffering
* that might be in progress, and start our own output buffer
* capture.
*
* This test includes a basic sanity check, to make sure that
* the body we thought would be sent actually was.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testHeadersEmitted()
{

$response = new Response(new App());
$response->pretend(FALSE);

$body = 'Hello';
$response->setBody($body);

$response->setCookie('foo', 'bar');
$this->assertTrue($response->hasCookie('foo'));
$this->assertTrue($response->hasCookie('foo', 'bar'));

// send it
ob_start();
$response->send();

$buffer = ob_clean();
if (ob_get_level() > 0)
ob_end_clean();

// and what actually got sent?; test both ways
$this->assertHeaderEmitted("Set-Cookie: foo=bar;");
$this->assertHeaderEmitted("set-cookie: FOO=bar", true);
}

/**
* This needs to be run as a separate process, since phpunit
* has already captured the "normal" output, and we will get
* a "Cannot modify headers" message if we try to change
* headers or cookies now.
*
* Furthermore, this test needs to flush the output buffering
* that might be in progress, and start our own output buffer
* capture.
*
* This test includes a basic sanity check, to make sure that
* the body we thought would be sent actually was.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testHeadersNotEmitted()
{
$response = new Response(new App());
$response->pretend(FALSE);

$body = 'Hello';
$response->setBody($body);

// what do we think we're about to send?
$response->setCookie('foo', 'bar');
$this->assertTrue($response->hasCookie('foo'));
$this->assertTrue($response->hasCookie('foo', 'bar'));

// send it
ob_start();
$response->send();
$output = ob_clean(); // what really was sent
if (ob_get_level() > 0)
ob_end_clean();

$this->assertHeaderNotEmitted("Set-Cookie: pop=corn", true);
}

}
Loading

0 comments on commit e6a6d64

Please sign in to comment.