|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Brainbits\FunctionalTestHelpers\Tests\HttpClientMock; |
| 6 | + |
| 7 | +use Brainbits\FunctionalTestHelpers\HttpClientMock\MockRequestBuilderFactory; |
| 8 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Psr\Http\Message\StreamInterface; |
| 11 | + |
| 12 | +#[CoversClass(MockRequestBuilderFactory::class)] |
| 13 | +final class MockRequestBuilderFactoryTest extends TestCase |
| 14 | +{ |
| 15 | + private readonly MockRequestBuilderFactory $mockRequestBuilderFactory; |
| 16 | + |
| 17 | + public function setUp(): void |
| 18 | + { |
| 19 | + $this->mockRequestBuilderFactory = new MockRequestBuilderFactory(); |
| 20 | + } |
| 21 | + |
| 22 | + public function testBuildsRequestWithoutBody(): void |
| 23 | + { |
| 24 | + $options = [ |
| 25 | + 'headers' => ['Content-Type: application/json'], |
| 26 | + 'json' => ['foo' => 'bar'], |
| 27 | + ]; |
| 28 | + $request = ($this->mockRequestBuilderFactory)('POST', 'https://service.com', $options); |
| 29 | + |
| 30 | + self::assertSame('POST', $request->getMethod()); |
| 31 | + self::assertSame('https://service.com', $request->getUri()); |
| 32 | + self::assertSame(['foo' => 'bar'], $request->getJson()); |
| 33 | + } |
| 34 | + |
| 35 | + public function testBuildsRequestWithJsonInBody(): void |
| 36 | + { |
| 37 | + $options = [ |
| 38 | + 'headers' => [ |
| 39 | + 'Content-Length: 1', |
| 40 | + 'Content-Type: application/json', |
| 41 | + ], |
| 42 | + 'body' => '{"foo": "bar"}', |
| 43 | + ]; |
| 44 | + |
| 45 | + $request = ($this->mockRequestBuilderFactory)('POST', 'https://service.com', $options); |
| 46 | + |
| 47 | + self::assertSame('POST', $request->getMethod()); |
| 48 | + self::assertSame('https://service.com', $request->getUri()); |
| 49 | + self::assertSame(['foo' => 'bar'], $request->getJson()); |
| 50 | + self::assertTrue($request->isJson()); |
| 51 | + } |
| 52 | + |
| 53 | + public function testBuildsRequestWithCallableInBody(): void |
| 54 | + { |
| 55 | + $body = $this->getMockBuilder(StreamInterface::class)->getMock(); |
| 56 | + $body->method('read') |
| 57 | + ->willReturn('{"foo": "bar"}'); |
| 58 | + |
| 59 | + $options = [ |
| 60 | + 'headers' => [ |
| 61 | + 'Content-Length: 1', |
| 62 | + 'Content-Type: application/json', |
| 63 | + ], |
| 64 | + 'body' => static fn (int $size) => $body->read($size), |
| 65 | + ]; |
| 66 | + |
| 67 | + $request = ($this->mockRequestBuilderFactory)('POST', 'https://service.com', $options); |
| 68 | + |
| 69 | + self::assertSame('POST', $request->getMethod()); |
| 70 | + self::assertSame('https://service.com', $request->getUri()); |
| 71 | + self::assertSame(['foo' => 'bar'], $request->getJson()); |
| 72 | + self::assertTrue($request->isJson()); |
| 73 | + } |
| 74 | +} |
0 commit comments