kodedphp /
http
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Tests\Koded\Http; |
||
| 4 | |||
| 5 | use Koded\Http\FileStream; |
||
| 6 | use Koded\Http\HttpFactory; |
||
| 7 | use PHPUnit\Framework\TestCase; |
||
| 8 | use Psr\Http\Message\RequestInterface; |
||
| 9 | use Psr\Http\Message\StreamInterface; |
||
| 10 | |||
| 11 | |||
| 12 | class FactoriesTest extends TestCase |
||
| 13 | { |
||
| 14 | public function test_request_factory() |
||
| 15 | { |
||
| 16 | $request = (new HttpFactory)->createRequest('get', '/'); |
||
| 17 | $this->assertInstanceOf(RequestInterface::class, $request); |
||
| 18 | } |
||
| 19 | |||
| 20 | public function test_server_request_factory() |
||
| 21 | { |
||
| 22 | $request = (new HttpFactory)->createServerRequest( |
||
| 23 | 'head', '/', ['X_Request_Id' => '123'] |
||
| 24 | ); |
||
| 25 | |||
| 26 | $this->assertSame('/', $request->getUri()->getPath()); |
||
| 27 | $this->assertArrayHasKey('X_Request_Id', $request->getServerParams()); |
||
| 28 | $this->assertSame('123', $request->getServerParams()['X_Request_Id']); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function test_response_factory() |
||
| 32 | { |
||
| 33 | $reason = 'My custom reason phrase'; |
||
| 34 | $response = (new HttpFactory)->createResponse(201, $reason); |
||
| 35 | $this->assertSame(201, $response->getStatusCode()); |
||
| 36 | $this->assertSame($reason, $response->getReasonPhrase()); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function test_create_stream() |
||
| 40 | { |
||
| 41 | $stream = (new HttpFactory)->createStream('hello'); |
||
| 42 | $this->assertSame('hello', $stream->getContents()); |
||
| 43 | $this->assertTrue($stream->isSeekable()); |
||
| 44 | $this->assertTrue($stream->isWritable()); |
||
| 45 | $this->assertTrue($stream->isReadable()); |
||
| 46 | $stream->close(); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function test_create_stream_from_file() |
||
| 50 | { |
||
| 51 | $stream = (new HttpFactory)->createStreamFromFile(__DIR__ . '/../LICENSE'); |
||
| 52 | $this->assertInstanceOf(FileStream::class, $stream); |
||
| 53 | $this->assertGreaterThan(0, $stream->getSize()); |
||
| 54 | $this->assertTrue($stream->isSeekable()); |
||
| 55 | $this->assertFalse($stream->isWritable()); |
||
| 56 | $this->assertTrue($stream->isReadable()); |
||
| 57 | $stream->close(); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function test_create_stream_from_resource() |
||
| 61 | { |
||
| 62 | $resource = fopen('php://memory', 'r'); |
||
| 63 | $stream = (new HttpFactory)->createStreamFromResource($resource); |
||
| 64 | |||
| 65 | $this->assertInstanceOf(StreamInterface::class, $stream); |
||
| 66 | $this->assertTrue($stream->isSeekable()); |
||
| 67 | $this->assertFalse($stream->isWritable()); |
||
| 68 | $this->assertTrue($stream->isReadable()); |
||
| 69 | |||
| 70 | $stream->close(); |
||
| 71 | $resource = null; |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 72 | } |
||
| 73 | } |
||
| 74 |