Skip to content

Commit 973f99b

Browse files
committed
Header testing works
1 parent f91b642 commit 973f99b

File tree

4 files changed

+67
-68
lines changed

4 files changed

+67
-68
lines changed

application/Config/Boot/development.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/*
43
|--------------------------------------------------------------------------
54
| ERROR DISPLAY

system/Test/CIUnitTestCase.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace CodeIgniter\Test;
43

54
/**
@@ -125,10 +124,9 @@ public function assertEventTriggered(string $eventName): bool
125124
* @param string $header The leading portion of the header we are looking for
126125
* @param bool $ignoreCase
127126
*
128-
* @return bool
129127
* @throws \Exception
130128
*/
131-
public function assertHeaderEmitted(string $header, bool $ignoreCase = false): bool
129+
public function assertHeaderEmitted(string $header, bool $ignoreCase = false): void
132130
{
133131
$found = false;
134132

@@ -141,7 +139,7 @@ public function assertHeaderEmitted(string $header, bool $ignoreCase = false): b
141139
break;
142140
}
143141

144-
return $found ? $found : $this->fail("Didn't find header for {$header}");
142+
$this->assertTrue($found,"Didn't find header for {$header}");
145143
}
146144

147145
/**
@@ -151,10 +149,9 @@ public function assertHeaderEmitted(string $header, bool $ignoreCase = false): b
151149
* @param string $header The leading portion of the header we don't want to find
152150
* @param bool $ignoreCase
153151
*
154-
* @return bool
155152
* @throws \Exception
156153
*/
157-
public function assertHeaderNotEmitted(string $header, bool $ignoreCase = false): bool
154+
public function assertHeaderNotEmitted(string $header, bool $ignoreCase = false): void
158155
{
159156
$found = false;
160157

@@ -168,7 +165,7 @@ public function assertHeaderNotEmitted(string $header, bool $ignoreCase = false)
168165
}
169166

170167
$success = ! $found;
171-
return $success ? $success : $this->fail("Found header for {$header}");
168+
$this->assertTrue($success,"Found header for {$header}");
172169
}
173170

174171
/**
Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace CodeIgniter\Test;
43

54
use CodeIgniter\HTTP\Response;
@@ -15,20 +14,6 @@
1514
class TestCaseEmissionsTest extends \CIUnitTestCase
1615
{
1716

18-
// public function setUp()
19-
// {
20-
// while( count( ob_list_handlers() ) > 1 )
21-
// {
22-
// ob_end_clean();
23-
// }
24-
// ob_start(null, 0, PHP_OUTPUT_HANDLER_CLEANABLE);
25-
// }
26-
//
27-
// public function tearDown()
28-
// {
29-
// ob_end_clean();
30-
// }
31-
3217
//--------------------------------------------------------------------
3318
/**
3419
* This needs to be run as a separate process, since phpunit
@@ -44,37 +29,32 @@ class TestCaseEmissionsTest extends \CIUnitTestCase
4429
* the body we thought would be sent actually was.
4530
*
4631
* @runInSeparateProcess
32+
* @preserveGlobalState disabled
4733
*/
48-
public function testHeaderEmitted()
34+
public function testHeadersEmitted()
4935
{
36+
5037
$response = new Response(new App());
5138
$response->pretend(FALSE);
5239

5340
$body = 'Hello';
54-
$expected = $body;
41+
$response->setBody($body);
5542

56-
// what do we think we're about to send?
5743
$response->setCookie('foo', 'bar');
5844
$this->assertTrue($response->hasCookie('foo'));
5945
$this->assertTrue($response->hasCookie('foo', 'bar'));
60-
61-
$response->setBody($body);
62-
63-
//echo 'ob level at '.ob_get_level();
64-
ob_end_clean();
65-
ob_start(null, 0, PHP_OUTPUT_HANDLER_CLEANABLE);
66-
//$buffer = ob_get_clean(); // flush previous
46+
6747
// send it
68-
// $buffer = ob_get_clean();
48+
ob_start();
6949
$response->send();
70-
$buffer = ob_end_clean();
71-
// and what actually got sent?; test both ways
72-
$actual = $response->getBody(); // what we thought was sent
7350

74-
$this->assertEquals($expected, $actual);
51+
$buffer = ob_clean();
52+
if (ob_get_level() > 0)
53+
ob_end_clean();
54+
55+
// and what actually got sent?; test both ways
7556
$this->assertHeaderEmitted("Set-Cookie: foo=bar;");
7657
$this->assertHeaderEmitted("set-cookie: FOO=bar", true);
77-
7858
}
7959

8060
/**
@@ -91,32 +71,29 @@ public function testHeaderEmitted()
9171
* the body we thought would be sent actually was.
9272
*
9373
* @runInSeparateProcess
74+
* @preserveGlobalState disabled
9475
*/
95-
// public function testHeaderNotEmitted()
96-
// {
97-
// $response = new Response(new App());
98-
// $response->pretend(FALSE);
99-
//
100-
// $body = 'Hello';
101-
// $expected = $body;
102-
//
103-
// // what do we think we're about to send?
104-
// $response->setCookie('foo', 'bar');
105-
// $this->assertTrue($response->hasCookie('foo'));
106-
// $this->assertTrue($response->hasCookie('foo', 'bar'));
107-
//
108-
// // send it
109-
// $response->setBody($body);
110-
//
111-
// ob_start();
112-
// $response->send();
113-
// $output = ob_get_clean(); // what really was sent
114-
// // and what actually got sent?; test both ways
115-
// $actual = $response->getBody(); // what we thought was sent
116-
//
117-
// $this->assertEquals($expected, $actual);
118-
// $this->assertEquals($expected, $output);
119-
//
120-
// $this->assertHeaderNotEmitted("Set-Cookie: pop=corn", true);
121-
// }
76+
public function testHeadersNotEmitted()
77+
{
78+
$response = new Response(new App());
79+
$response->pretend(FALSE);
80+
81+
$body = 'Hello';
82+
$response->setBody($body);
83+
84+
// what do we think we're about to send?
85+
$response->setCookie('foo', 'bar');
86+
$this->assertTrue($response->hasCookie('foo'));
87+
$this->assertTrue($response->hasCookie('foo', 'bar'));
88+
89+
// send it
90+
ob_start();
91+
$response->send();
92+
$output = ob_clean(); // what really was sent
93+
if (ob_get_level() > 0)
94+
ob_end_clean();
95+
96+
$this->assertHeaderNotEmitted("Set-Cookie: pop=corn", true);
97+
}
98+
12299
}

tests/system/Test/TestCaseTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace CodeIgniter\Test;
43

54
use CodeIgniter\Events\Events;
@@ -30,7 +29,8 @@ public function testLogging()
3029

3130
public function testEventTriggering()
3231
{
33-
Events::on('foo', function($arg) use(&$result) {
32+
Events::on('foo', function($arg) use(&$result)
33+
{
3434
$result = $arg;
3535
});
3636

@@ -51,4 +51,30 @@ public function testStreamFilter()
5151
stream_filter_remove($this->stream_filter);
5252
}
5353

54+
//--------------------------------------------------------------------
55+
/**
56+
* PHPunit emits headers before we get nominal control of
57+
* the output stream, making header testing awkward, to say
58+
* the least. This test is intended to make sure that this
59+
* is happening as expected.
60+
*
61+
* TestCaseEmissionsTest is intended to circumvent PHPunit,
62+
* and allow us to test our own header emissions.
63+
*
64+
*/
65+
public function testPHPUnitHeadersEmitted()
66+
{
67+
$response = new Response(new App());
68+
$response->pretend(TRUE);
69+
70+
$body = 'Hello';
71+
$response->setBody($body);
72+
73+
$response->send();
74+
75+
// Did PHPunit do its thing?
76+
$this->assertHeaderEmitted("Content-type: text/html;");
77+
$this->assertHeaderNotEmitted("Set-Cookie: foo=bar;");
78+
}
79+
5480
}

0 commit comments

Comments
 (0)