Skip to content

Commit 76e605c

Browse files
committed
chubbyphp-mock-v2
1 parent 4f74f76 commit 76e605c

File tree

2 files changed

+112
-102
lines changed

2 files changed

+112
-102
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
},
2323
"require-dev": {
2424
"chubbyphp/chubbyphp-dev-helper": "dev-master",
25-
"chubbyphp/chubbyphp-mock": "^1.8",
26-
"infection/infection": "^0.29.8",
25+
"chubbyphp/chubbyphp-mock": "^2.0@dev",
26+
"infection/infection": "^0.29.12",
2727
"php-coveralls/php-coveralls": "^2.7.0",
2828
"phpstan/extension-installer": "^1.4.3",
29-
"phpstan/phpstan": "^2.0.3",
30-
"phpunit/phpunit": "^11.5.0"
29+
"phpstan/phpstan": "^2.1.6",
30+
"phpunit/phpunit": "^11.5.9"
3131
},
3232
"autoload": {
3333
"psr-4": { "Chubbyphp\\StaticFile\\": "src/" }

tests/Unit/StaticFileMiddlewareTest.php

Lines changed: 108 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Chubbyphp\Tests\Unit\StaticFile;
66

7-
use Chubbyphp\Mock\Call;
8-
use Chubbyphp\Mock\MockByCallsTrait;
7+
use Chubbyphp\Mock\MockMethod\WithReturn;
8+
use Chubbyphp\Mock\MockMethod\WithReturnSelf;
9+
use Chubbyphp\Mock\MockObjectBuilder;
910
use Chubbyphp\StaticFile\StaticFileMiddleware;
10-
use PHPUnit\Framework\MockObject\MockObject;
1111
use PHPUnit\Framework\TestCase;
1212
use Psr\Http\Message\ResponseFactoryInterface;
1313
use Psr\Http\Message\ResponseInterface;
@@ -23,31 +23,31 @@
2323
*/
2424
final class StaticFileMiddlewareTest extends TestCase
2525
{
26-
use MockByCallsTrait;
27-
2826
public function testIsNotReadable(): void
2927
{
3028
$publicDirectory = sys_get_temp_dir();
3129
$requestTarget = '/'.uniqid().uniqid().'.xml';
3230

33-
/** @var MockObject|ServerRequestInterface $request */
34-
$request = $this->getMockByCalls(ServerRequestInterface::class, [
35-
Call::create('getRequestTarget')->with()->willReturn($requestTarget),
31+
$builder = new MockObjectBuilder();
32+
33+
/** @var ServerRequestInterface $request */
34+
$request = $builder->create(ServerRequestInterface::class, [
35+
new WithReturn('getRequestTarget', [], $requestTarget),
3636
]);
3737

38-
/** @var MockObject|ResponseInterface $response */
39-
$response = $this->getMockByCalls(ResponseInterface::class);
38+
/** @var ResponseInterface $response */
39+
$response = $builder->create(ResponseInterface::class, []);
4040

41-
/** @var MockObject|RequestHandlerInterface $handler */
42-
$handler = $this->getMockByCalls(RequestHandlerInterface::class, [
43-
Call::create('handle')->with($request)->willReturn($response),
41+
/** @var RequestHandlerInterface $handler */
42+
$handler = $builder->create(RequestHandlerInterface::class, [
43+
new WithReturn('handle', [$request], $response),
4444
]);
4545

46-
/** @var MockObject|ResponseFactoryInterface $responseFactory */
47-
$responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class);
46+
/** @var ResponseFactoryInterface $responseFactory */
47+
$responseFactory = $builder->create(ResponseFactoryInterface::class, []);
4848

49-
/** @var MockObject|StreamFactoryInterface $streamFactory */
50-
$streamFactory = $this->getMockByCalls(StreamFactoryInterface::class);
49+
/** @var StreamFactoryInterface $streamFactory */
50+
$streamFactory = $builder->create(StreamFactoryInterface::class, []);
5151

5252
$middleware = new StaticFileMiddleware($responseFactory, $streamFactory, $publicDirectory);
5353

@@ -62,11 +62,13 @@ public function testInvalidHashAlgorythm(): void
6262
$publicDirectory = sys_get_temp_dir();
6363
$hashAlgorithm = 'unknown';
6464

65-
/** @var MockObject|ResponseFactoryInterface $responseFactory */
66-
$responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class);
65+
$builder = new MockObjectBuilder();
6766

68-
/** @var MockObject|StreamFactoryInterface $streamFactory */
69-
$streamFactory = $this->getMockByCalls(StreamFactoryInterface::class);
67+
/** @var ResponseFactoryInterface $responseFactory */
68+
$responseFactory = $builder->create(ResponseFactoryInterface::class, []);
69+
70+
/** @var StreamFactoryInterface $streamFactory */
71+
$streamFactory = $builder->create(StreamFactoryInterface::class, []);
7072

7173
new StaticFileMiddleware($responseFactory, $streamFactory, $publicDirectory, $hashAlgorithm);
7274
}
@@ -85,37 +87,39 @@ public function testIfMatch(string $body, string $contentLength, ?string $conten
8587

8688
$hash = hash_file($hashAlgorithm, $filename);
8789

88-
/** @var MockObject|ServerRequestInterface $request */
89-
$request = $this->getMockByCalls(ServerRequestInterface::class, [
90-
Call::create('getRequestTarget')->with()->willReturn($requestTarget),
91-
Call::create('getHeaderLine')->with('If-None-Match')->willReturn($hash),
90+
$builder = new MockObjectBuilder();
91+
92+
/** @var ServerRequestInterface $request */
93+
$request = $builder->create(ServerRequestInterface::class, [
94+
new WithReturn('getRequestTarget', [], $requestTarget),
95+
new WithReturn('getHeaderLine', ['If-None-Match'], $hash),
9296
]);
9397

9498
if (null !== $contentType) {
95-
/** @var MockObject|ResponseInterface $response */
96-
$response = $this->getMockByCalls(ResponseInterface::class, [
97-
Call::create('withHeader')->with('Content-Length', $contentLength)->willReturnSelf(),
98-
Call::create('withHeader')->with('Content-Type', $contentType)->willReturnSelf(),
99-
Call::create('withHeader')->with('ETag', $hash)->willReturnSelf(),
99+
/** @var ResponseInterface $response */
100+
$response = $builder->create(ResponseInterface::class, [
101+
new WithReturnSelf('withHeader', ['Content-Length', $contentLength]),
102+
new WithReturnSelf('withHeader', ['Content-Type', $contentType]),
103+
new WithReturnSelf('withHeader', ['ETag', $hash]),
100104
]);
101105
} else {
102-
/** @var MockObject|ResponseInterface $response */
103-
$response = $this->getMockByCalls(ResponseInterface::class, [
104-
Call::create('withHeader')->with('Content-Length', $contentLength)->willReturnSelf(),
105-
Call::create('withHeader')->with('ETag', $hash)->willReturnSelf(),
106+
/** @var ResponseInterface $response */
107+
$response = $builder->create(ResponseInterface::class, [
108+
new WithReturnSelf('withHeader', ['Content-Length', $contentLength]),
109+
new WithReturnSelf('withHeader', ['ETag', $hash]),
106110
]);
107111
}
108112

109-
/** @var MockObject|RequestHandlerInterface $handler */
110-
$handler = $this->getMockByCalls(RequestHandlerInterface::class);
113+
/** @var RequestHandlerInterface $handler */
114+
$handler = $builder->create(RequestHandlerInterface::class, []);
111115

112-
/** @var MockObject|ResponseFactoryInterface $responseFactory */
113-
$responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class, [
114-
Call::create('createResponse')->with(304, '')->willReturn($response),
116+
/** @var ResponseFactoryInterface $responseFactory */
117+
$responseFactory = $builder->create(ResponseFactoryInterface::class, [
118+
new WithReturn('createResponse', [304, ''], $response),
115119
]);
116120

117-
/** @var MockObject|StreamFactoryInterface $streamFactory */
118-
$streamFactory = $this->getMockByCalls(StreamFactoryInterface::class);
121+
/** @var StreamFactoryInterface $streamFactory */
122+
$streamFactory = $builder->create(StreamFactoryInterface::class, []);
119123

120124
$middleware = new StaticFileMiddleware($responseFactory, $streamFactory, $publicDirectory, $hashAlgorithm);
121125

@@ -139,37 +143,39 @@ public function testIfMatchWithDefaultHashAlgorithm(
139143

140144
$hash = hash_file('md5', $filename);
141145

142-
/** @var MockObject|ServerRequestInterface $request */
143-
$request = $this->getMockByCalls(ServerRequestInterface::class, [
144-
Call::create('getRequestTarget')->with()->willReturn($requestTarget),
145-
Call::create('getHeaderLine')->with('If-None-Match')->willReturn($hash),
146+
$builder = new MockObjectBuilder();
147+
148+
/** @var ServerRequestInterface $request */
149+
$request = $builder->create(ServerRequestInterface::class, [
150+
new WithReturn('getRequestTarget', [], $requestTarget),
151+
new WithReturn('getHeaderLine', ['If-None-Match'], $hash),
146152
]);
147153

148154
if (null !== $contentType) {
149-
/** @var MockObject|ResponseInterface $response */
150-
$response = $this->getMockByCalls(ResponseInterface::class, [
151-
Call::create('withHeader')->with('Content-Length', $contentLength)->willReturnSelf(),
152-
Call::create('withHeader')->with('Content-Type', $contentType)->willReturnSelf(),
153-
Call::create('withHeader')->with('ETag', $hash)->willReturnSelf(),
155+
/** @var ResponseInterface $response */
156+
$response = $builder->create(ResponseInterface::class, [
157+
new WithReturnSelf('withHeader', ['Content-Length', $contentLength]),
158+
new WithReturnSelf('withHeader', ['Content-Type', $contentType]),
159+
new WithReturnSelf('withHeader', ['ETag', $hash]),
154160
]);
155161
} else {
156-
/** @var MockObject|ResponseInterface $response */
157-
$response = $this->getMockByCalls(ResponseInterface::class, [
158-
Call::create('withHeader')->with('Content-Length', $contentLength)->willReturnSelf(),
159-
Call::create('withHeader')->with('ETag', $hash)->willReturnSelf(),
162+
/** @var ResponseInterface $response */
163+
$response = $builder->create(ResponseInterface::class, [
164+
new WithReturnSelf('withHeader', ['Content-Length', $contentLength]),
165+
new WithReturnSelf('withHeader', ['ETag', $hash]),
160166
]);
161167
}
162168

163-
/** @var MockObject|RequestHandlerInterface $handler */
164-
$handler = $this->getMockByCalls(RequestHandlerInterface::class);
169+
/** @var RequestHandlerInterface $handler */
170+
$handler = $builder->create(RequestHandlerInterface::class, []);
165171

166-
/** @var MockObject|ResponseFactoryInterface $responseFactory */
167-
$responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class, [
168-
Call::create('createResponse')->with(304, '')->willReturn($response),
172+
/** @var ResponseFactoryInterface $responseFactory */
173+
$responseFactory = $builder->create(ResponseFactoryInterface::class, [
174+
new WithReturn('createResponse', [304, ''], $response),
169175
]);
170176

171-
/** @var MockObject|StreamFactoryInterface $streamFactory */
172-
$streamFactory = $this->getMockByCalls(StreamFactoryInterface::class);
177+
/** @var StreamFactoryInterface $streamFactory */
178+
$streamFactory = $builder->create(StreamFactoryInterface::class, []);
173179

174180
$middleware = new StaticFileMiddleware($responseFactory, $streamFactory, $publicDirectory);
175181

@@ -190,43 +196,45 @@ public function testIfNoneMatch(string $body, string $contentLength, ?string $co
190196

191197
$hash = hash_file($hashAlgorithm, $filename);
192198

193-
/** @var MockObject|ServerRequestInterface $request */
194-
$request = $this->getMockByCalls(ServerRequestInterface::class, [
195-
Call::create('getRequestTarget')->with()->willReturn($requestTarget),
196-
Call::create('getHeaderLine')->with('If-None-Match')->willReturn(''),
199+
$builder = new MockObjectBuilder();
200+
201+
/** @var ServerRequestInterface $request */
202+
$request = $builder->create(ServerRequestInterface::class, [
203+
new WithReturn('getRequestTarget', [], $requestTarget),
204+
new WithReturn('getHeaderLine', ['If-None-Match'], ''),
197205
]);
198206

199-
/** @var MockObject|StreamInterface $responseBody */
200-
$responseBody = $this->getMockByCalls(StreamInterface::class);
207+
/** @var StreamInterface $responseBody */
208+
$responseBody = $builder->create(StreamInterface::class, []);
201209

202210
if (null !== $contentType) {
203-
/** @var MockObject|ResponseInterface $response */
204-
$response = $this->getMockByCalls(ResponseInterface::class, [
205-
Call::create('withHeader')->with('Content-Length', $contentLength)->willReturnSelf(),
206-
Call::create('withHeader')->with('Content-Type', $contentType)->willReturnSelf(),
207-
Call::create('withHeader')->with('ETag', $hash)->willReturnSelf(),
208-
Call::create('withBody')->with($responseBody)->willReturnSelf(),
211+
/** @var ResponseInterface $response */
212+
$response = $builder->create(ResponseInterface::class, [
213+
new WithReturnSelf('withHeader', ['Content-Length', $contentLength]),
214+
new WithReturnSelf('withHeader', ['Content-Type', $contentType]),
215+
new WithReturnSelf('withHeader', ['ETag', $hash]),
216+
new WithReturnSelf('withBody', [$responseBody]),
209217
]);
210218
} else {
211-
/** @var MockObject|ResponseInterface $response */
212-
$response = $this->getMockByCalls(ResponseInterface::class, [
213-
Call::create('withHeader')->with('Content-Length', $contentLength)->willReturnSelf(),
214-
Call::create('withHeader')->with('ETag', $hash)->willReturnSelf(),
215-
Call::create('withBody')->with($responseBody)->willReturnSelf(),
219+
/** @var ResponseInterface $response */
220+
$response = $builder->create(ResponseInterface::class, [
221+
new WithReturnSelf('withHeader', ['Content-Length', $contentLength]),
222+
new WithReturnSelf('withHeader', ['ETag', $hash]),
223+
new WithReturnSelf('withBody', [$responseBody]),
216224
]);
217225
}
218226

219-
/** @var MockObject|RequestHandlerInterface $handler */
220-
$handler = $this->getMockByCalls(RequestHandlerInterface::class);
227+
/** @var RequestHandlerInterface $handler */
228+
$handler = $builder->create(RequestHandlerInterface::class, []);
221229

222-
/** @var MockObject|ResponseFactoryInterface $responseFactory */
223-
$responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class, [
224-
Call::create('createResponse')->with(200, '')->willReturn($response),
230+
/** @var ResponseFactoryInterface $responseFactory */
231+
$responseFactory = $builder->create(ResponseFactoryInterface::class, [
232+
new WithReturn('createResponse', [200, ''], $response),
225233
]);
226234

227-
/** @var MockObject|StreamFactoryInterface $streamFactory */
228-
$streamFactory = $this->getMockByCalls(StreamFactoryInterface::class, [
229-
Call::create('createStreamFromFile')->with($filename, 'r')->willReturn($responseBody),
235+
/** @var StreamFactoryInterface $streamFactory */
236+
$streamFactory = $builder->create(StreamFactoryInterface::class, [
237+
new WithReturn('createStreamFromFile', [$filename, 'r'], $responseBody),
230238
]);
231239

232240
$middleware = new StaticFileMiddleware($responseFactory, $streamFactory, $publicDirectory, $hashAlgorithm);
@@ -239,24 +247,26 @@ public function testBaseDirectory(): void
239247
$publicDirectory = sys_get_temp_dir();
240248
$requestTarget = '/';
241249

242-
/** @var MockObject|ServerRequestInterface $request */
243-
$request = $this->getMockByCalls(ServerRequestInterface::class, [
244-
Call::create('getRequestTarget')->with()->willReturn($requestTarget),
250+
$builder = new MockObjectBuilder();
251+
252+
/** @var ServerRequestInterface $request */
253+
$request = $builder->create(ServerRequestInterface::class, [
254+
new WithReturn('getRequestTarget', [], $requestTarget),
245255
]);
246256

247-
/** @var MockObject|ResponseInterface $response */
248-
$response = $this->getMockByCalls(ResponseInterface::class);
257+
/** @var ResponseInterface $response */
258+
$response = $builder->create(ResponseInterface::class, []);
249259

250-
/** @var MockObject|RequestHandlerInterface $handler */
251-
$handler = $this->getMockByCalls(RequestHandlerInterface::class, [
252-
Call::create('handle')->with($request)->willReturn($response),
260+
/** @var RequestHandlerInterface $handler */
261+
$handler = $builder->create(RequestHandlerInterface::class, [
262+
new WithReturn('handle', [$request], $response),
253263
]);
254264

255-
/** @var MockObject|ResponseFactoryInterface $responseFactory */
256-
$responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class);
265+
/** @var ResponseFactoryInterface $responseFactory */
266+
$responseFactory = $builder->create(ResponseFactoryInterface::class, []);
257267

258-
/** @var MockObject|StreamFactoryInterface $streamFactory */
259-
$streamFactory = $this->getMockByCalls(StreamFactoryInterface::class);
268+
/** @var StreamFactoryInterface $streamFactory */
269+
$streamFactory = $builder->create(StreamFactoryInterface::class, []);
260270

261271
$middleware = new StaticFileMiddleware($responseFactory, $streamFactory, $publicDirectory);
262272

0 commit comments

Comments
 (0)