summaryrefslogtreecommitdiff
path: root/tests/Unit/Http/ResponseTest.php
blob: 56d8410f75c4d7259573e160e782fa6258a85e65 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php

namespace Engelsystem\Test\Unit\Http;

use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Engelsystem\Http\Response;
use Engelsystem\Renderer\Renderer;
use InvalidArgumentException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;

class ResponseTest extends TestCase
{
    use ArraySubsetAsserts;

    /**
     * @covers \Engelsystem\Http\Response
     */
    public function testCreate()
    {
        $response = new Response();
        $this->assertInstanceOf(SymfonyResponse::class, $response);
        $this->assertInstanceOf(ResponseInterface::class, $response);
    }

    /**
     * @covers \Engelsystem\Http\Response::getReasonPhrase
     * @covers \Engelsystem\Http\Response::withStatus
     */
    public function testWithStatus()
    {
        $response = new Response();
        $newResponse = $response->withStatus(503);
        $this->assertNotEquals($response, $newResponse);
        $this->assertNotEquals('', $newResponse->getReasonPhrase());
        $this->assertEquals(503, $newResponse->getStatusCode());

        $newResponse = $response->withStatus(503, 'Foo');
        $this->assertEquals('Foo', $newResponse->getReasonPhrase());
    }

    /**
     * @covers \Engelsystem\Http\Response::withContent
     */
    public function testWithContent()
    {
        $response = new Response();
        $newResponse = $response->withContent('Lorem Ipsum?');

        $this->assertNotEquals($response, $newResponse);
        $this->assertEquals('Lorem Ipsum?', $newResponse->getContent());
    }

    /**
     * @covers \Engelsystem\Http\Response::withView
     * @covers \Engelsystem\Http\Response::setRenderer
     */
    public function testWithView()
    {
        /** @var REnderer|MockObject $renderer */
        $renderer = $this->createMock(Renderer::class);

        $renderer->expects($this->once())
            ->method('render')
            ->with('foo', ['lorem' => 'ipsum'])
            ->willReturn('Foo ipsum!');

        $response = new Response('', 200, [], $renderer);
        $newResponse = $response->withView('foo', ['lorem' => 'ipsum'], 505, ['test' => 'er']);

        $this->assertNotEquals($response, $newResponse);
        $this->assertEquals('Foo ipsum!', $newResponse->getContent());
        $this->assertEquals(505, $newResponse->getStatusCode());
        $this->assertArraySubset(['test' => ['er']], $newResponse->getHeaders());

        /** @var REnderer|MockObject $renderer */
        $anotherRenderer = $this->createMock(Renderer::class);
        $anotherRenderer->expects($this->once())
            ->method('render')
            ->with('bar')
            ->willReturn('Stuff');

        $response->setRenderer($anotherRenderer);
        $response = $response->withView('bar');
        $this->assertEquals('Stuff', $response->getContent());
    }

    /**
     * @covers \Engelsystem\Http\Response::withView
     */
    public function testWithViewNoRenderer()
    {
        $this->expectException(InvalidArgumentException::class);

        $response = new Response();
        $response->withView('foo');
    }

    /**
     * @covers \Engelsystem\Http\Response::redirectTo
     */
    public function testRedirectTo()
    {
        $response = new Response();
        $newResponse = $response->redirectTo('http://foo.bar/lorem', 301, ['test' => 'ing']);

        $this->assertNotEquals($response, $newResponse);
        $this->assertEquals(301, $newResponse->getStatusCode());
        $this->assertArraySubset(
            [
                'location' => ['http://foo.bar/lorem'],
                'test'     => ['ing'],
            ],
            $newResponse->getHeaders()
        );
    }

    /**
     * @covers \Engelsystem\Http\Response::with
     */
    public function testWith()
    {
        $session = new Session(new MockArraySessionStorage());
        $response = new Response('', 200, [], null, $session);

        $response->with('foo', 'bar');
        $this->assertEquals('bar', $session->get('foo'));

        $response->with('lorem', ['ipsum', 'dolor' => ['foo' => 'bar']]);
        $this->assertEquals(['ipsum', 'dolor' => ['foo' => 'bar']], $session->get('lorem'));

        $response->with('lorem', ['dolor' => ['test' => 'er']]);
        $this->assertEquals(['ipsum', 'dolor' => ['foo' => 'bar', 'test' => 'er']], $session->get('lorem'));
    }

    /**
     * @covers \Engelsystem\Http\Response::with
     */
    public function testWithNoSession()
    {
        $this->expectException(InvalidArgumentException::class);

        $response = new Response();
        $response->with('foo', 'bar');
    }

    /**
     * @covers \Engelsystem\Http\Response::withInput
     */
    public function testWithInput()
    {
        $session = new Session(new MockArraySessionStorage());
        $response = new Response('', 200, [], null, $session);

        $response->withInput(['some' => 'value']);
        $this->assertEquals(['some' => 'value'], $session->get('form-data'));

        $response->withInput(['lorem' => 'ipsum']);
        $this->assertEquals(['lorem' => 'ipsum'], $session->get('form-data'));
    }

    /**
     * @covers \Engelsystem\Http\Response::withInput
     */
    public function testWithInputNoSession()
    {
        $this->expectException(InvalidArgumentException::class);

        $response = new Response();
        $response->withInput(['some' => 'value']);
    }
}