-
Notifications
You must be signed in to change notification settings - Fork 2
/
ResponseInterfaceTestsTrait.php
92 lines (70 loc) · 2.56 KB
/
ResponseInterfaceTestsTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Maks3w\Psr7Assertions\PhpUnit;
use PHPUnit_Framework_Assert as Assert;
use Psr\Http\Message\ResponseInterface;
/**
* Provide PHPUnit test methods for Psr\Http\Message\ResponseInterface constraints.
*/
trait ResponseInterfaceTestsTrait
{
use MessageInterfaceTestsTrait;
// Test methods for default/empty instances
public function testResponseImplementsInterface()
{
Assert::assertInstanceOf('Psr\Http\Message\ResponseInterface', $this->createDefaultResponse());
}
public function testValidDefaultStatusCode()
{
$message = $this->createDefaultResponse();
$statusCode = $message->getStatusCode();
Assert::assertInternalType('integer', $statusCode, 'getStatusCode must return an integer');
}
public function testValidDefaultReasonPhrase()
{
$message = $this->createDefaultResponse();
$reasonPhrase = $message->getReasonPhrase();
Assert::assertInternalType('string', $reasonPhrase, 'getReasonPhrase must return a string');
Assert::assertEmpty($reasonPhrase, 'must return an empty string if none present');
}
// Test methods for change instances status
public function testValidWithStatusDefaultReasonPhrase()
{
$message = $this->createDefaultResponse();
$messageClone = clone $message;
$statusCode = 100;
$newMessage = $message->withStatus($statusCode);
$this->assertImmutable($messageClone, $message, $newMessage);
Assert::assertEquals(
$statusCode,
$newMessage->getStatusCode(),
'getStatusCode does not match code set in withStatus'
);
}
public function testValidWithStatusCustomReasonPhrase()
{
$message = $this->createDefaultResponse();
$messageClone = clone $message;
$statusCode = 100;
$reasonPhrase = 'example';
$newMessage = $message->withStatus($statusCode, $reasonPhrase);
$this->assertImmutable($messageClone, $message, $newMessage);
Assert::assertEquals(
$statusCode,
$newMessage->getStatusCode(),
'getStatusCode does not match code set in withStatus'
);
Assert::assertEquals(
$reasonPhrase,
$newMessage->getReasonPhrase(),
'getReasonPhrase does not match code set in withStatus'
);
}
/**
* @return ResponseInterface
*/
abstract protected function createDefaultResponse();
protected function createDefaultMessage()
{
return $this->createDefaultResponse();
}
}