summaryrefslogtreecommitdiff
path: root/tests/Unit/Http/RedirectorTest.php
blob: 300b01801b6da8f56871afcc3724511eeefc6ae6 (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
<?php

namespace Engelsystem\Test\Unit\Http;

use Engelsystem\Http\Redirector;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use PHPUnit\Framework\TestCase;

class RedirectorTest extends TestCase
{
    /**
     * @covers \Engelsystem\Http\Redirector::__construct
     * @covers \Engelsystem\Http\Redirector::to
     */
    public function testTo()
    {
        $request = new Request();
        $response = new Response();
        $redirector = new Redirector($request, $response);

        $return = $redirector->to('/test');
        $this->assertEquals(['/test'], $return->getHeader('location'));
        $this->assertEquals(302, $return->getStatusCode());

        $return = $redirector->to('/foo', 303, ['test' => 'data']);
        $this->assertEquals(['/foo'], $return->getHeader('location'));
        $this->assertEquals(303, $return->getStatusCode());
        $this->assertEquals(['data'], $return->getHeader('test'));
    }

    /**
     * @covers \Engelsystem\Http\Redirector::back
     * @covers \Engelsystem\Http\Redirector::getPreviousUrl
     */
    public function testBack()
    {
        $request = new Request();
        $response = new Response();
        $redirector = new Redirector($request, $response);

        $return = $redirector->back();
        $this->assertEquals(['/'], $return->getHeader('location'));
        $this->assertEquals(302, $return->getStatusCode());

        $request = $request->withHeader('referer', '/old-page');
        $redirector = new Redirector($request, $response);
        $return = $redirector->back(303, ['foo' => 'bar']);
        $this->assertEquals(303, $return->getStatusCode());
        $this->assertEquals(['/old-page'], $return->getHeader('location'));
        $this->assertEquals(['bar'], $return->getHeader('foo'));
    }
}