Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 529f527

Browse files
committed
Register all arguments of header() on the stack
In order to be able to verify that we're calling the function with the correct values.
1 parent fb7f06e commit 529f527

File tree

5 files changed

+62
-30
lines changed

5 files changed

+62
-30
lines changed

test/Response/AbstractEmitterTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ public function testEmitsResponseHeaders()
4040
ob_start();
4141
$this->emitter->emit($response);
4242
ob_end_clean();
43-
$this->assertContains('HTTP/1.1 200 OK', HeaderStack::stack());
44-
$this->assertContains('Content-Type: text/plain', HeaderStack::stack());
43+
44+
$this->assertTrue(HeaderStack::has('HTTP/1.1 200 OK'));
45+
$this->assertTrue(HeaderStack::has('Content-Type: text/plain'));
4546
}
4647

4748
public function testEmitsMessageBody()
@@ -68,7 +69,7 @@ public function testDoesNotInjectContentLengthHeaderIfStreamSizeIsUnknown()
6869
$this->emitter->emit($response);
6970
ob_end_clean();
7071
foreach (HeaderStack::stack() as $header) {
71-
$this->assertNotContains('Content-Length:', $header);
72+
$this->assertNotContains('Content-Length:', $header['header']);
7273
}
7374
}
7475
}

test/Response/SapiStreamEmitterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testDoesNotInjectContentLengthHeaderIfStreamSizeIsUnknown()
5656
$this->emitter->emit($response);
5757
ob_end_clean();
5858
foreach (HeaderStack::stack() as $header) {
59-
$this->assertNotContains('Content-Length:', $header);
59+
$this->assertNotContains('Content-Length:', $header['header']);
6060
}
6161
}
6262

test/ServerTest.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ public function testListenInvokesCallbackAndSendsResponse()
151151
$this->expectOutputString('FOOBAR');
152152
$server->listen();
153153

154-
$this->assertContains('HTTP/1.1 200 OK', HeaderStack::stack());
155-
$this->assertContains('Content-Type: text/plain', HeaderStack::stack());
154+
$this->assertTrue(HeaderStack::has('HTTP/1.1 200 OK'));
155+
$this->assertTrue(HeaderStack::has('Content-Type: text/plain'));
156156
}
157157

158158
public function testListenEmitsStatusHeaderWithoutReasonPhraseIfNoReasonPhrase()
@@ -176,8 +176,8 @@ public function testListenEmitsStatusHeaderWithoutReasonPhraseIfNoReasonPhrase()
176176
$this->expectOutputString('FOOBAR');
177177
$server->listen();
178178

179-
$this->assertContains('HTTP/1.1 299', HeaderStack::stack());
180-
$this->assertContains('Content-Type: text/plain', HeaderStack::stack());
179+
$this->assertTrue(HeaderStack::has('HTTP/1.1 299'));
180+
$this->assertTrue(HeaderStack::has('Content-Type: text/plain'));
181181
}
182182

183183
public function testEnsurePercentCharactersDoNotResultInOutputError()
@@ -200,8 +200,8 @@ public function testEnsurePercentCharactersDoNotResultInOutputError()
200200
$this->expectOutputString('100%');
201201
$server->listen();
202202

203-
$this->assertContains('HTTP/1.1 200 OK', HeaderStack::stack());
204-
$this->assertContains('Content-Type: text/plain', HeaderStack::stack());
203+
$this->assertTrue(HeaderStack::has('HTTP/1.1 200 OK'));
204+
$this->assertTrue(HeaderStack::has('Content-Type: text/plain'));
205205
}
206206

207207
public function testEmitsHeadersWithMultipleValuesMultipleTimes()
@@ -228,19 +228,16 @@ public function testEmitsHeadersWithMultipleValuesMultipleTimes()
228228

229229
$server->listen();
230230

231-
$this->assertContains('HTTP/1.1 200 OK', HeaderStack::stack());
232-
$this->assertContains('Content-Type: text/plain', HeaderStack::stack());
233-
$this->assertContains(
234-
'Set-Cookie: foo=bar; expires=Wed, 1 Oct 2014 10:30; path=/foo; domain=example.com',
235-
HeaderStack::stack()
231+
$this->assertTrue(HeaderStack::has('HTTP/1.1 200 OK'));
232+
$this->assertTrue(HeaderStack::has('Content-Type: text/plain'));
233+
$this->assertTrue(
234+
HeaderStack::has('Set-Cookie: foo=bar; expires=Wed, 1 Oct 2014 10:30; path=/foo; domain=example.com')
236235
);
237-
$this->assertContains(
238-
'Set-Cookie: bar=baz; expires=Wed, 8 Oct 2014 10:30; path=/foo/bar; domain=example.com',
239-
HeaderStack::stack()
236+
$this->assertTrue(
237+
HeaderStack::has('Set-Cookie: bar=baz; expires=Wed, 8 Oct 2014 10:30; path=/foo/bar; domain=example.com')
240238
);
241239

242-
$stack = HeaderStack::stack();
243-
return $stack;
240+
return HeaderStack::stack();
244241
}
245242

246243
/**

test/TestAsset/Functions.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
class HeaderStack
2828
{
2929
/**
30-
* @var array
30+
* @var string[][]
3131
*/
3232
private static $data = [];
3333

@@ -42,22 +42,40 @@ public static function reset()
4242
/**
4343
* Push a header on the stack
4444
*
45-
* @param string $header
45+
* @param string[] $header
4646
*/
47-
public static function push($header)
47+
public static function push(array $header)
4848
{
4949
self::$data[] = $header;
5050
}
5151

5252
/**
5353
* Return the current header stack
5454
*
55-
* @return array
55+
* @return string[][]
5656
*/
5757
public static function stack()
5858
{
5959
return self::$data;
6060
}
61+
62+
/**
63+
* Verify if there's a header line on the stack
64+
*
65+
* @param string $header
66+
*
67+
* @return bool
68+
*/
69+
public static function has($header)
70+
{
71+
foreach (self::$data as $item) {
72+
if ($item['header'] === $header) {
73+
return true;
74+
}
75+
}
76+
77+
return false;
78+
}
6179
}
6280

6381
/**
@@ -73,9 +91,17 @@ function headers_sent()
7391
/**
7492
* Emit a header, without creating actual output artifacts
7593
*
76-
* @param string $value
94+
* @param string $string
95+
* @param bool $replace
96+
* @param int|null $statusCode
7797
*/
78-
function header($value)
98+
function header($string, $replace = true, $statusCode = null)
7999
{
80-
HeaderStack::push($value);
100+
HeaderStack::push(
101+
[
102+
'header' => $string,
103+
'replace' => $replace,
104+
'status_code' => $statusCode,
105+
]
106+
);
81107
}

test/TestAsset/SapiResponse.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,17 @@ function headers_sent()
3535
/**
3636
* Emit a header, without creating actual output artifacts
3737
*
38-
* @param string $value
38+
* @param string $string
39+
* @param bool $replace
40+
* @param int|null $http_response_code
3941
*/
40-
function header($value)
42+
function header($string, $replace = true, $http_response_code = null)
4143
{
42-
HeaderStack::push($value);
44+
HeaderStack::push(
45+
[
46+
'header' => $string,
47+
'replace' => $replace,
48+
'status_code' => $http_response_code,
49+
]
50+
);
4351
}

0 commit comments

Comments
 (0)