forked from apigee/apigee-mock-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleMockStorageTest.php
More file actions
152 lines (125 loc) · 4.33 KB
/
Copy pathSimpleMockStorageTest.php
File metadata and controls
152 lines (125 loc) · 4.33 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
* Copyright 2019 The Apigee Mock Client PHP Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Apigee\MockClient\Tests;
use Apigee\MockClient\MatchableResult;
use Apigee\MockClient\SimpleMockStorage;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\RequestMatcher\RequestMatcher;
use PHPUnit\Framework\TestCase;
/**
* Tests the simple mock client storage.
*/
class SimpleMockStorageTest extends TestCase {
/**
* @var \Apigee\MockClient\MockStorageInterface
*/
protected $storage;
/**
* A sample response.
*
* @var \Psr\Http\Message\ResponseInterface
*/
protected $response;
/**
* A sample request.
*
* @var \Psr\Http\Message\RequestInterface
*/
protected $request;
/**
* A sample matchable result.
*
* @var \Apigee\MockClient\MatchableResultInterface
*/
protected $matchableResult;
public function setup(): void {
$this->storage = new SimpleMockStorage();
$generator = MessageFactoryDiscovery::find();
$this->response = $generator->createResponse();
$this->request = $generator->createRequest('GET', 'https://example.com');
$this->matchableResult = new MatchableResult(new RequestMatcher(), function () { return $this->response; });
}
/**
* Tests an uninitialized simple storage.
*/
public function testEmptyStorage() {
static::assertNull($this->storage->default());
static::assertNull($this->storage->claim());
static::assertSame(0, $this->storage->responseCount());
static::assertSame([], $this->storage->requests());
static::assertNull($this->storage->lastRequest());
static::assertSame(0, $this->storage->totalRequests());
static::assertSame([], $this->storage->matchableResults());
}
/**
* Tests the default getters and setters.
*/
public function testDefault() {
$this->storage->setDefault($this->response);
static::assertSame($this->response, $this->storage->default());
}
/**
* Tests the queue in the mock storage.
*/
public function testQueue() {
$this->storage->add($this->response);
$this->storage->add(new \Exception('Test exception.'));
static::assertSame($this->response, $this->storage->claim());
static::assertSame('Test exception.', (string) $this->storage->claim()->getMessage());
static::assertSame(0, $this->storage->responseCount());
}
/**
* Tests the request log.
*/
public function testRequestLog() {
$last_request = clone $this->request;
$this->storage->addRequest($this->request);
$this->storage->addRequest($this->request);
$this->storage->addRequest($last_request);
static::assertSame(3, $this->storage->totalRequests());
static::assertSame($this->storage->requests(), [
$this->request,
$this->request,
$last_request
]);
static::assertSame($last_request, $this->storage->lastRequest());
}
/**
* Tests the matchable results.
*/
public function testMatchablesResults() {
$this->storage->addMatchableResult($this->matchableResult);
static::assertSame([$this->matchableResult], $this->storage->matchableResults());
}
/**
* Tests resetting the mock client storage.
*/
public function testReset() {
$this->storage->setDefault($this->response);
$this->storage->add($this->response);
$this->storage->addRequest($this->request);
$this->storage->addMatchableResult($this->matchableResult);
static::assertNotNull($this->storage->default());
static::assertSame(1, $this->storage->responseCount());
static::assertSame([$this->request], $this->storage->requests());
static::assertSame($this->request, $this->storage->lastRequest());
static::assertSame(1, $this->storage->totalRequests());
static::assertSame([$this->matchableResult], $this->storage->matchableResults());
$this->storage->reset();
$this->testEmptyStorage();
}
}