|
5 | 5 | namespace App\Tests\Unit\RequestHandler;
|
6 | 6 |
|
7 | 7 | use App\RequestHandler\PingRequestHandler;
|
8 |
| -use Chubbyphp\Mock\Argument\ArgumentCallback; |
9 |
| -use Chubbyphp\Mock\Call; |
10 |
| -use Chubbyphp\Mock\MockByCallsTrait; |
11 |
| -use PHPUnit\Framework\MockObject\MockObject; |
| 8 | +use Chubbyphp\Mock\MockMethod\WithCallback; |
| 9 | +use Chubbyphp\Mock\MockMethod\WithReturn; |
| 10 | +use Chubbyphp\Mock\MockMethod\WithReturnSelf; |
| 11 | +use Chubbyphp\Mock\MockObjectBuilder; |
12 | 12 | use PHPUnit\Framework\TestCase;
|
13 | 13 | use Psr\Http\Message\ResponseFactoryInterface;
|
14 | 14 | use Psr\Http\Message\ResponseInterface;
|
|
22 | 22 | */
|
23 | 23 | final class PingRequestHandlerTest extends TestCase
|
24 | 24 | {
|
25 |
| - use MockByCallsTrait; |
26 |
| - |
27 | 25 | public function testHandle(): void
|
28 | 26 | {
|
29 |
| - /** @var MockObject|ServerRequestInterface $request */ |
30 |
| - $request = $this->getMockByCalls(ServerRequestInterface::class); |
31 |
| - |
32 |
| - $bodyLength = 0; |
| 27 | + $builder = new MockObjectBuilder(); |
33 | 28 |
|
34 |
| - /** @var MockObject|StreamInterface $body */ |
35 |
| - $body = $this->getMockByCalls(StreamInterface::class, [ |
36 |
| - Call::create('write')->with(new ArgumentCallback(static function (string $body) use (&$bodyLength): void { |
37 |
| - $data = json_decode($body, true, 512, JSON_THROW_ON_ERROR); |
| 29 | + $request = $builder->create(ServerRequestInterface::class, []); |
38 | 30 |
|
| 31 | + $responseBody = $builder->create(StreamInterface::class, [ |
| 32 | + new WithCallback('write', static function (string $string): int { |
| 33 | + $data = json_decode($string, true); |
39 | 34 | self::assertArrayHasKey('datetime', $data);
|
40 | 35 |
|
41 |
| - $bodyLength = \strlen($body); |
42 |
| - }))->willReturn($bodyLength), |
| 36 | + return \strlen($string); |
| 37 | + }), |
43 | 38 | ]);
|
44 | 39 |
|
45 |
| - /** @var MockObject|ResponseInterface $response */ |
46 |
| - $response = $this->getMockByCalls(ResponseInterface::class, [ |
47 |
| - Call::create('withHeader')->with('Content-Type', 'application/json')->willReturnSelf(), |
48 |
| - Call::create('withHeader') |
49 |
| - ->with('Cache-Control', 'no-cache, no-store, must-revalidate') |
50 |
| - ->willReturnSelf(), |
51 |
| - Call::create('withHeader')->with('Pragma', 'no-cache')->willReturnSelf(), |
52 |
| - Call::create('withHeader')->with('Expires', '0')->willReturnSelf(), |
53 |
| - Call::create('getBody')->with()->willReturn($body), |
| 40 | + $response = $builder->create(ResponseInterface::class, [ |
| 41 | + new WithReturnSelf('withHeader', ['Content-Type', 'application/json']), |
| 42 | + new WithReturnSelf('withHeader', ['Cache-Control', 'no-cache, no-store, must-revalidate']), |
| 43 | + new WithReturnSelf('withHeader', ['Pragma', 'no-cache']), |
| 44 | + new WithReturnSelf('withHeader', ['Expires', '0']), |
| 45 | + new WithReturn('getBody', [], $responseBody), |
54 | 46 | ]);
|
55 | 47 |
|
56 |
| - /** @var MockObject|ResponseFactoryInterface $responseFactory */ |
57 |
| - $responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class, [ |
58 |
| - Call::create('createResponse')->with(200, '')->willReturn($response), |
| 48 | + $responseFactory = $builder->create(ResponseFactoryInterface::class, [ |
| 49 | + new WithReturn('createResponse', [200, ''], $response), |
59 | 50 | ]);
|
60 | 51 |
|
61 |
| - $RequestHandler = new PingRequestHandler($responseFactory); |
| 52 | + $requestHandler = new PingRequestHandler($responseFactory); |
62 | 53 |
|
63 |
| - self::assertSame($response, $RequestHandler->handle($request)); |
| 54 | + self::assertSame($response, $requestHandler->handle($request)); |
64 | 55 | }
|
65 | 56 | }
|
0 commit comments