1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Buzz\Test\Unit\Middleware\History; |
6
|
|
|
|
7
|
|
|
use Buzz\Middleware\History\Journal; |
8
|
|
|
use Nyholm\Psr7\Request; |
9
|
|
|
use Nyholm\Psr7\Response; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class JournalTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
protected $request1; |
15
|
|
|
protected $request2; |
16
|
|
|
protected $request3; |
17
|
|
|
|
18
|
|
|
protected $response1; |
19
|
|
|
protected $response2; |
20
|
|
|
protected $response3; |
21
|
|
|
|
22
|
|
|
protected function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->request1 = new Request('GET', '/r1', [], 'request1'); |
25
|
|
|
$this->request2 = new Request('GET', '/r2', [], 'request2'); |
26
|
|
|
$this->request3 = new Request('GET', '/r3', [], 'request3'); |
27
|
|
|
$this->response1 = new Response(200, [], 'response1'); |
28
|
|
|
$this->response2 = new Response(200, [], 'response2'); |
29
|
|
|
$this->response3 = new Response(200, [], 'response3'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function tearDown() |
33
|
|
|
{ |
34
|
|
|
$this->request1 = null; |
35
|
|
|
$this->request2 = null; |
36
|
|
|
$this->request3 = null; |
37
|
|
|
|
38
|
|
|
$this->response1 = null; |
39
|
|
|
$this->response2 = null; |
40
|
|
|
$this->response3 = null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testRecordEnforcesLimit() |
44
|
|
|
{ |
45
|
|
|
$journal = new Journal(); |
46
|
|
|
$journal->setLimit(2); |
47
|
|
|
|
48
|
|
|
$journal->record($this->request1, $this->response1); |
49
|
|
|
$journal->record($this->request2, $this->response2); |
50
|
|
|
$journal->record($this->request3, $this->response3); |
51
|
|
|
|
52
|
|
|
$this->assertEquals(2, count($journal)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testGetLastReturnsTheLastEntry() |
56
|
|
|
{ |
57
|
|
|
$journal = new Journal(); |
58
|
|
|
|
59
|
|
|
$journal->record($this->request1, $this->response1); |
60
|
|
|
$journal->record($this->request2, $this->response2); |
61
|
|
|
|
62
|
|
|
$this->assertEquals($this->request2, $journal->getLast()->getRequest()); |
63
|
|
|
|
64
|
|
|
return $journal; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @depends testGetLastReturnsTheLastEntry |
69
|
|
|
*/ |
70
|
|
|
public function testGetLastRequestReturnsTheLastRequest(Journal $journal) |
71
|
|
|
{ |
72
|
|
|
$this->assertEquals($this->request2->getBody()->__toString(), $journal->getLastRequest()->getBody()->__toString()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @depends testGetLastReturnsTheLastEntry |
77
|
|
|
*/ |
78
|
|
|
public function testGetLastResponseReturnsTheLastResponse(Journal $journal) |
79
|
|
|
{ |
80
|
|
|
$this->assertEquals($this->response2->getBody()->__toString(), $journal->getLastResponse()->getBody()->__toString()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @depends testGetLastReturnsTheLastEntry |
85
|
|
|
*/ |
86
|
|
|
public function testForeachIteratesReversedEntries(Journal $journal) |
87
|
|
|
{ |
88
|
|
|
$requests = [$this->request2, $this->request1]; |
89
|
|
|
$responses = [$this->response2, $this->response1]; |
90
|
|
|
$this->assertNotEmpty($journal); |
91
|
|
|
|
92
|
|
|
foreach ($journal as $index => $entry) { |
93
|
|
|
$this->assertEquals($requests[$index]->getBody()->__toString(), $entry->getRequest()->getBody()->__toString()); |
94
|
|
|
$this->assertEquals($responses[$index]->getBody()->__toString(), $entry->getResponse()->getBody()->__toString()); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @depends testGetLastReturnsTheLastEntry |
100
|
|
|
*/ |
101
|
|
|
public function testClearRemovesEntries(Journal $journal) |
102
|
|
|
{ |
103
|
|
|
$journal->clear(); |
104
|
|
|
$this->assertEquals(0, count($journal)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @depends testGetLastReturnsTheLastEntry |
109
|
|
|
*/ |
110
|
|
|
public function testDuration() |
111
|
|
|
{ |
112
|
|
|
$journal = new Journal(); |
113
|
|
|
$journal->record($this->request1, $this->response1, 100); |
114
|
|
|
|
115
|
|
|
$this->assertEquals($journal->getLast()->getDuration(), 100); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|