Skip to content

Commit c171769

Browse files
committed
Add headerEmited (or not) assertions to CUUnitTestCase
1 parent a5b9917 commit c171769

8 files changed

Lines changed: 275 additions & 71 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,15 @@ Please read the [*Contributing to CodeIgniter*](https://github.com/bcit-ci/CodeI
6868
## Server Requirements
6969
PHP version 7.1 or higher is required, with the following extensions installed:
7070

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

74+
Additionally, make sure that the following extensions are enabled in your PHP:
75+
76+
- json (enabled by default - don't turn it off)
77+
- [mbstring](http://php.net/manual/en/mbstring.installation.php)
78+
- [mysqlnd](http://php.net/manual/en/mysqlnd.install.php)
79+
- xml (enabled by default - don't turn it off)
7380

7481
## Running CodeIgniter Tests
7582
Information on running CodeIgniter test suite can be found in the [README.md](tests/README.md) file in the tests directory.
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
<?php
22

33
/*
4-
|--------------------------------------------------------------------------
5-
| ERROR DISPLAY
6-
|--------------------------------------------------------------------------
7-
| In development, we want to show as many errors as possible to help
8-
| make sure they don't make it to production. And save us hours of
9-
| painful debugging.
10-
*/
4+
|--------------------------------------------------------------------------
5+
| ERROR DISPLAY
6+
|--------------------------------------------------------------------------
7+
| In development, we want to show as many errors as possible to help
8+
| make sure they don't make it to production. And save us hours of
9+
| painful debugging.
10+
*/
1111
error_reporting(-1);
1212
ini_set('display_errors', 1);
1313

1414
/*
15-
|--------------------------------------------------------------------------
16-
| DEBUG BACKTRACES
17-
|--------------------------------------------------------------------------
18-
| If true, this constant will tell the error screens to display debug
19-
| backtraces along with the other error information. If you would
20-
| prefer to not see this, set this value to false.
21-
*/
22-
define('SHOW_DEBUG_BACKTRACE', true);
15+
|--------------------------------------------------------------------------
16+
| DEBUG BACKTRACES
17+
|--------------------------------------------------------------------------
18+
| If true, this constant will tell the error screens to display debug
19+
| backtraces along with the other error information. If you would
20+
| prefer to not see this, set this value to false.
21+
*/
22+
defined('SHOW_DEBUG_BACKTRACE') || define('SHOW_DEBUG_BACKTRACE', true);
2323

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

33-
define('CI_DEBUG', 1);
33+
defined('CI_DEBUG') || define('CI_DEBUG', 1);
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<?php
22

33
/*
4-
|--------------------------------------------------------------------------
5-
| ERROR DISPLAY
6-
|--------------------------------------------------------------------------
7-
| Don't show ANY in production environments. Instead, let the system catch
8-
| it and display a generic error message.
9-
*/
4+
|--------------------------------------------------------------------------
5+
| ERROR DISPLAY
6+
|--------------------------------------------------------------------------
7+
| Don't show ANY in production environments. Instead, let the system catch
8+
| it and display a generic error message.
9+
*/
1010
ini_set('display_errors', 0);
1111
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
1212

1313
/*
14-
|--------------------------------------------------------------------------
15-
| DEBUG MODE
16-
|--------------------------------------------------------------------------
17-
| Debug mode is an experimental flag that can allow changes throughout
18-
| the system. It's not widely used currently, and may not survive
19-
| release of the framework.
20-
*/
14+
|--------------------------------------------------------------------------
15+
| DEBUG MODE
16+
|--------------------------------------------------------------------------
17+
| Debug mode is an experimental flag that can allow changes throughout
18+
| the system. It's not widely used currently, and may not survive
19+
| release of the framework.
20+
*/
2121

22-
define('CI_DEBUG', 0);
22+
defined('CI_DEBUG') || define('CI_DEBUG', 0);
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
<?php
22

33
/*
4-
|--------------------------------------------------------------------------
5-
| ERROR DISPLAY
6-
|--------------------------------------------------------------------------
7-
| In development, we want to show as many errors as possible to help
8-
| make sure they don't make it to production. And save us hours of
9-
| painful debugging.
10-
*/
4+
|--------------------------------------------------------------------------
5+
| ERROR DISPLAY
6+
|--------------------------------------------------------------------------
7+
| In development, we want to show as many errors as possible to help
8+
| make sure they don't make it to production. And save us hours of
9+
| painful debugging.
10+
*/
1111
error_reporting(-1);
1212
ini_set('display_errors', 1);
1313

1414
/*
15-
|--------------------------------------------------------------------------
16-
| DEBUG BACKTRACES
17-
|--------------------------------------------------------------------------
18-
| If true, this constant will tell the error screens to display debug
19-
| backtraces along with the other error information. If you would
20-
| prefer to not see this, set this value to false.
21-
*/
22-
define('SHOW_DEBUG_BACKTRACE', true);
15+
|--------------------------------------------------------------------------
16+
| DEBUG BACKTRACES
17+
|--------------------------------------------------------------------------
18+
| If true, this constant will tell the error screens to display debug
19+
| backtraces along with the other error information. If you would
20+
| prefer to not see this, set this value to false.
21+
*/
22+
defined('SHOW_DEBUG_BACKTRACE') || define('SHOW_DEBUG_BACKTRACE', true);
2323

2424
/*
25-
|--------------------------------------------------------------------------
26-
| DEBUG MODE
27-
|--------------------------------------------------------------------------
28-
| Debug mode is an experimental flag that can allow changes throughout
29-
| the system. It's not widely used currently, and may not survive
30-
| release of the framework.
31-
*/
25+
|--------------------------------------------------------------------------
26+
| DEBUG MODE
27+
|--------------------------------------------------------------------------
28+
| Debug mode is an experimental flag that can allow changes throughout
29+
| the system. It's not widely used currently, and may not survive
30+
| release of the framework.
31+
*/
3232

33-
define('CI_DEBUG', 1);
33+
defined('CI_DEBUG') || define('CI_DEBUG', 1);

system/Test/CIUnitTestCase.php

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace CodeIgniter\Test;
1+
<?php
2+
3+
namespace CodeIgniter\Test;
24

35
/**
46
* CodeIgniter
@@ -45,6 +47,7 @@
4547
*/
4648
class CIUnitTestCase extends TestCase
4749
{
50+
4851
use ReflectionHelper;
4952

5053
/**
@@ -63,7 +66,7 @@ public function setUp()
6366
{
6467
parent::setUp();
6568

66-
if (! $this->app)
69+
if ( ! $this->app)
6770
{
6871
$this->app = $this->createApplication();
6972
}
@@ -104,7 +107,8 @@ public function assertEventTriggered(string $eventName): bool
104107

105108
foreach (Events::getPerformanceLogs() as $log)
106109
{
107-
if ($log['event'] !== $eventName) continue;
110+
if ($log['event'] !== $eventName)
111+
continue;
108112

109113
$found = true;
110114
break;
@@ -114,6 +118,59 @@ public function assertEventTriggered(string $eventName): bool
114118
return $found;
115119
}
116120

121+
/**
122+
* Hooks into xdebug's headers capture, looking for a specific header
123+
* emitted
124+
*
125+
* @param string $header The leading portion of the header we are looking for
126+
* @param bool $ignoreCase
127+
*
128+
* @return bool
129+
* @throws \Exception
130+
*/
131+
public function assertHeaderEmitted(string $header, bool $ignoreCase = false): bool
132+
{
133+
$found = false;
134+
135+
foreach (xdebug_get_headers() as $emitted)
136+
{
137+
$found = $ignoreCase ?
138+
(stripos($emitted, $header) === 0) :
139+
(strpos($emitted, $header) === 0);
140+
if ($found)
141+
break;
142+
}
143+
144+
return $found ? $found : $this->fail("Didn't find header for {$header}");
145+
}
146+
147+
/**
148+
* Hooks into xdebug's headers capture, looking for a specific header
149+
* emitted
150+
*
151+
* @param string $header The leading portion of the header we don't want to find
152+
* @param bool $ignoreCase
153+
*
154+
* @return bool
155+
* @throws \Exception
156+
*/
157+
public function assertHeaderNotEmitted(string $header, bool $ignoreCase = false): bool
158+
{
159+
$found = false;
160+
161+
foreach (xdebug_get_headers() as $emitted)
162+
{
163+
$found = $ignoreCase ?
164+
(stripos($emitted, $header) === 0) :
165+
(strpos($emitted, $header) === 0);
166+
if ($found)
167+
break;
168+
}
169+
170+
$success = ! $found;
171+
return $success ? $success : $this->fail("Found header for {$header}");
172+
}
173+
117174
/**
118175
* Loads up an instance of CodeIgniter
119176
* and gets the environment setup.
@@ -122,12 +179,12 @@ public function assertEventTriggered(string $eventName): bool
122179
*/
123180
protected function createApplication()
124181
{
125-
$systemPath = realpath(__DIR__.'/../');
182+
$systemPath = realpath(__DIR__ . '/../');
126183

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

130-
$app = require $systemPath.'/bootstrap.php';
187+
$app = require $systemPath . '/bootstrap.php';
131188
return $app;
132189
}
133190

@@ -162,4 +219,5 @@ protected function adjustPaths(Paths $paths)
162219

163220
return $paths;
164221
}
222+
165223
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace CodeIgniter\Test;
4+
5+
use CodeIgniter\HTTP\Response;
6+
use CodeIgniter\HTTP\Exceptions\HTTPException;
7+
use Config\App;
8+
9+
/**
10+
* This test suite has been created separately from
11+
* TestCaseTest because it messes with output
12+
* buffering from PHPUnit, and the individual
13+
* test cases need to be run as separate processes.
14+
*/
15+
class TestCaseEmissionsTest extends \CIUnitTestCase
16+
{
17+
//--------------------------------------------------------------------
18+
19+
/**
20+
* This needs to be run as a separate process, since phpunit
21+
* has already captured the "normal" output, and we will get
22+
* a "Cannot modify headers" message if we try to change
23+
* headers or cookies now.
24+
*
25+
* Furthermore, this test needs to flush the output buffering
26+
* that might be in progress, and start our own output buffer
27+
* capture.
28+
*
29+
* This test includes a basic sanity check, to make sure that
30+
* the body we thought would be sent actually was.
31+
*
32+
* @runInSeparateProcess
33+
*/
34+
public function testHeaderEmitted()
35+
{
36+
$response = new Response(new App());
37+
$response->pretend(FALSE);
38+
39+
$body = 'Hello';
40+
$expected = $body;
41+
42+
// what do we think we're about to send?
43+
$response->setCookie('foo', 'bar');
44+
$this->assertTrue($response->hasCookie('foo'));
45+
$this->assertTrue($response->hasCookie('foo', 'bar'));
46+
47+
// send it
48+
$response->setBody($body);
49+
50+
ob_start();
51+
$response->send();
52+
$output = ob_get_clean(); // what really was sent
53+
// and what actually got sent?; test both ways
54+
$actual = $response->getBody(); // what we thought was sent
55+
56+
$this->assertEquals($expected, $actual);
57+
$this->assertEquals($expected, $output);
58+
59+
$this->assertHeaderEmitted("Set-Cookie: foo=bar;");
60+
$this->assertHeaderEmitted("set-cookie: FOO=bar", true);
61+
}
62+
63+
/**
64+
* This needs to be run as a separate process, since phpunit
65+
* has already captured the "normal" output, and we will get
66+
* a "Cannot modify headers" message if we try to change
67+
* headers or cookies now.
68+
*
69+
* Furthermore, this test needs to flush the output buffering
70+
* that might be in progress, and start our own output buffer
71+
* capture.
72+
*
73+
* This test includes a basic sanity check, to make sure that
74+
* the body we thought would be sent actually was.
75+
*
76+
* @runInSeparateProcess
77+
*/
78+
public function testHeaderNotEmitted()
79+
{
80+
$response = new Response(new App());
81+
$response->pretend(FALSE);
82+
83+
$body = 'Hello';
84+
$expected = $body;
85+
86+
// what do we think we're about to send?
87+
$response->setCookie('foo', 'bar');
88+
$this->assertTrue($response->hasCookie('foo'));
89+
$this->assertTrue($response->hasCookie('foo', 'bar'));
90+
91+
// send it
92+
$response->setBody($body);
93+
94+
ob_start();
95+
$response->send();
96+
$output = ob_get_clean(); // what really was sent
97+
// and what actually got sent?; test both ways
98+
$actual = $response->getBody(); // what we thought was sent
99+
100+
$this->assertEquals($expected, $actual);
101+
$this->assertEquals($expected, $output);
102+
103+
$this->assertHeaderNotEmitted("Set-Cookie: pop=corn", true);
104+
}
105+
106+
}

0 commit comments

Comments
 (0)