-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponseFactoryTest.php
75 lines (59 loc) · 2.22 KB
/
ResponseFactoryTest.php
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
<?php
declare(strict_types=1);
namespace Qubus\Tests\Routing;
use Laminas\Diactoros\Response\EmptyResponse;
use Laminas\Diactoros\Response\TextResponse;
use Laminas\Diactoros\ServerRequest;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Qubus\Routing\Factories\ResponseFactory;
use Qubus\Routing\Interfaces\Responsable;
class ResponseFactoryTest extends TestCase
{
use MockeryPHPUnitIntegration;
protected function setUp(): void
{
parent::setUp();
$this->request = new ServerRequest([], [], '/test/123', 'GET');
}
/** @test */
public function whenPassedaResponseInstanceTheSameObjectIsReturned()
{
$response = new TextResponse('Testing', 200);
Assert::assertSame($response, ResponseFactory::create($this->request, $response));
}
/** @test */
public function whenPassedaNonResponseInstanceaResponseObjectIsReturned()
{
$response = ResponseFactory::create($this->request, 'Testing');
Assert::assertInstanceOf(ResponseInterface::class, $response);
Assert::assertSame('Testing', $response->getBody()->getContents());
}
/** @test */
public function whenNothingIsPassedAnEmptyResponseObjectIsReturned()
{
$response = ResponseFactory::create($this->request, '');
Assert::assertInstanceOf(EmptyResponse::class, $response);
}
/** @test */
public function whenaResponsableObjectIsPassedTheResponseObjectIsReturned()
{
$textResponse = new TextResponse('testing123');
$object = Mockery::mock(ResponsableObject::class);
$object->shouldReceive('toResponse')->with($this->request)->once()->andReturn($textResponse);
$response = ResponseFactory::create($this->request, $object);
Assert::assertInstanceOf(TextResponse::class, $response);
Assert::assertSame('testing123', $response->getBody()->getContents());
}
}
class ResponsableObject implements Responsable
{
public function toResponse(RequestInterface $request): ResponseInterface
{
return new TextResponse('testing123');
}
}