1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alekseytupichenkov\GuzzleStub\Tests\Unit\Handler; |
4
|
|
|
|
5
|
|
|
use Alekseytupichenkov\GuzzleStub\Handler\StubHandler; |
6
|
|
|
use Alekseytupichenkov\GuzzleStub\Model\Fixture; |
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
use GuzzleHttp\Psr7\Response; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class StubHandlerTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** @var Request */ |
14
|
|
|
private $getRequest; |
15
|
|
|
|
16
|
|
|
/** @var Response */ |
17
|
|
|
private $getResponse; |
18
|
|
|
|
19
|
|
|
/** @var Request */ |
20
|
|
|
private $postRequest; |
21
|
|
|
|
22
|
|
|
/** @var Response */ |
23
|
|
|
private $postResponse; |
24
|
|
|
|
25
|
|
|
/** @var StubHandler */ |
26
|
|
|
private $handler; |
27
|
|
|
|
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
$this->handler = new StubHandler(); |
31
|
|
|
|
32
|
|
|
$this->getRequest = new Request('GET', '/foo/bar?test=1', ['foo' => 'bar']); |
33
|
|
|
$this->getResponse = new Response(200, ['baz' => 'POST'], '{"get": "ok"}'); |
34
|
|
|
$this->postRequest = new Request('POST', '/foo/bar', ['foo' => 'bar'], 'SuperBody'); |
35
|
|
|
$this->postResponse = new Response(200, ['baz' => 'POST'], '{"post": "ok"}'); |
36
|
|
|
|
37
|
|
|
$this->handler->append(new Fixture($this->getRequest, $this->getResponse)); |
38
|
|
|
$this->handler->append(new Fixture($this->postRequest, $this->postResponse)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testSuccessSimpleGet() |
42
|
|
|
{ |
43
|
|
|
$promise = $this->handler->__invoke($this->getRequest, []); |
44
|
|
|
|
45
|
|
|
$this->assertEquals($this->getResponse, $promise->wait()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testSuccessSimplePost() |
49
|
|
|
{ |
50
|
|
|
$promise = $this->handler->__invoke($this->postRequest, []); |
51
|
|
|
|
52
|
|
|
$this->assertEquals($this->postResponse, $promise->wait()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testSuccessParticleGet() |
56
|
|
|
{ |
57
|
|
|
$request = new Request('GET', '/foo/bar?test=1'); |
58
|
|
|
$response = new Response(200); |
59
|
|
|
$this->handler->append(new Fixture($request, $response)); |
60
|
|
|
|
61
|
|
|
$promise = $this->handler->__invoke($this->getRequest, []); |
62
|
|
|
|
63
|
|
|
$this->assertEquals($response, $promise->wait()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testSuccessParticlePost() |
67
|
|
|
{ |
68
|
|
|
$request = new Request('POST', '/foo/bar'); |
69
|
|
|
$response = new Response(200); |
70
|
|
|
$this->handler->append(new Fixture($request, $response)); |
71
|
|
|
|
72
|
|
|
$promise = $this->handler->__invoke($this->postRequest, []); |
73
|
|
|
|
74
|
|
|
$this->assertEquals($this->postResponse, $promise->wait()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testSuccessRegexGet() |
78
|
|
|
{ |
79
|
|
|
$request = new Request('GET|POST', '\/foo\/.*', ['foo' => '.*']); |
80
|
|
|
$response = new Response(200); |
81
|
|
|
$this->handler->append(new Fixture($request, $response)); |
82
|
|
|
|
83
|
|
|
$promise = $this->handler->__invoke($this->getRequest, []); |
84
|
|
|
|
85
|
|
|
$this->assertEquals($response, $promise->wait()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testSuccessRegexPost() |
89
|
|
|
{ |
90
|
|
|
$request = new Request('GET|POST', '\/foo\/.*', ['foo' => '.*'], 'Super.*'); |
91
|
|
|
$response = new Response(200); |
92
|
|
|
$this->handler->append(new Fixture($request, $response)); |
93
|
|
|
|
94
|
|
|
$promise = $this->handler->__invoke($this->postRequest, []); |
95
|
|
|
|
96
|
|
|
$this->assertEquals($response, $promise->wait()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testReplaceResponseByExistingRequest() |
100
|
|
|
{ |
101
|
|
|
$response = new Response(200); |
102
|
|
|
$this->handler->append(new Fixture($this->getRequest, $response)); |
103
|
|
|
|
104
|
|
|
$promise = $this->handler->__invoke($this->getRequest, []); |
105
|
|
|
|
106
|
|
|
$this->assertEquals($response, $promise->wait()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @expectedException \Alekseytupichenkov\GuzzleStub\Exception\GuzzleStubException |
111
|
|
|
* @expectedExceptionMessageRegExp "Can`t find suitable response for request .*" |
112
|
|
|
*/ |
113
|
|
|
public function testException() |
114
|
|
|
{ |
115
|
|
|
$this->handler->__invoke(new Request('not', 'valid'), []); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|