1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the SF Forward Bundle. |
||
5 | * |
||
6 | * (c) DAOUDI Soufian <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace SfForward\Tests\Manager; |
||
13 | |||
14 | use PHPUnit\Framework\TestCase; |
||
15 | use Psr\Http\Message\ResponseInterface; |
||
16 | use Psr\Http\Message\StreamInterface; |
||
17 | use SfForward\Manager\ResponseManager; |
||
18 | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
||
19 | use Symfony\Component\HttpFoundation\Response; |
||
20 | |||
21 | class ResponseManagerTest extends TestCase |
||
22 | { |
||
23 | protected $projectDir; |
||
24 | |||
25 | protected function setUp() |
||
26 | { |
||
27 | $this->projectDir = __DIR__.'/../..'; |
||
28 | } |
||
29 | |||
30 | public function testGetResponseSuccess() |
||
31 | { |
||
32 | $response = $this->getMockedResponse(); |
||
33 | $responseManager = new ResponseManager($response, $this->projectDir); |
||
34 | |||
35 | $this->assertInstanceOf(Response::class, $responseManager->getResponse()); |
||
36 | $this->assertSame(Response::HTTP_OK, $responseManager->getResponse()->getStatusCode()); |
||
37 | } |
||
38 | |||
39 | public function testGetResponseNotFound() |
||
40 | { |
||
41 | $response = $this->getMockedResponse('test content', Response::HTTP_NOT_FOUND); |
||
42 | $responseManager = new ResponseManager($response, $this->projectDir); |
||
43 | |||
44 | $this->assertInstanceOf(Response::class, $responseManager->getResponse()); |
||
45 | $this->assertSame(Response::HTTP_NOT_FOUND, $responseManager->getResponse()->getStatusCode()); |
||
46 | } |
||
47 | |||
48 | public function testGetBinaryFileResponse() |
||
49 | { |
||
50 | $response = $this->getMockedResponse('test content', Response::HTTP_OK, 'content-disposition', 1); |
||
51 | $responseManager = new ResponseManager($response, $this->projectDir); |
||
52 | $responseReturn = $responseManager->getResponse(); |
||
53 | unlink($responseReturn->getFile()->getPathname()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
54 | |||
55 | $this->assertInstanceOf(BinaryFileResponse::class, $responseReturn); |
||
56 | $this->assertSame(Response::HTTP_OK, $responseReturn->getStatusCode()); |
||
57 | |||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string $body |
||
62 | * @param int $statusCode |
||
63 | * @param string $contentDisposition |
||
64 | * |
||
65 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
66 | */ |
||
67 | protected function getMockedResponse($body = '', $statusCode = Response::HTTP_OK, $contentDisposition = null, $exactly = 2) |
||
68 | { |
||
69 | $stream = $this->createMock(StreamInterface::class); |
||
70 | $stream->method('getContents') |
||
71 | ->willReturn($body); |
||
72 | |||
73 | $response = $this->createMock(ResponseInterface::class); |
||
74 | |||
75 | $response->expects($this->exactly($exactly)) |
||
76 | ->method('getBody') |
||
77 | ->willReturn($stream); |
||
78 | |||
79 | $response->expects($this->exactly($exactly)) |
||
80 | ->method('getStatusCode') |
||
81 | ->willReturn($statusCode); |
||
82 | |||
83 | $response->expects($this->exactly($exactly)) |
||
84 | ->method('hasHeader') |
||
85 | ->with('Content-Disposition') |
||
86 | ->willReturn($contentDisposition); |
||
87 | |||
88 | $response->method('getHeaders') |
||
89 | ->willReturn([]); |
||
90 | |||
91 | return $response; |
||
92 | } |
||
93 | } |
||
94 |