summaryrefslogtreecommitdiffstats
path: root/tests/Controller/CheckRequestTest.php
blob: 8ba2e23194e76f5b918fc20cdf442bd4623936ed (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
<?php

namespace Jasny\Controller;

use Psr\Http\Message\ServerRequestInterface;
use Jasny\Controller\TestHelper;

/**
 * @covers Jasny\Controller\CheckRequest
 */
class CheckRequestTest extends \PHPUnit_Framework_TestCase
{
    use TestHelper;

    /**
     * Provide data for testing functions that determine request method
     *
     * @return array
     */
    public function requestMethodProvider()
    {
        return [
            ['GET'], ['POST'], ['PUT'], ['DELETE'], ['HEAD']
        ];
    }

    /**
     * Test functions that check request method
     *
     * @dataProvider requestMethodProvider
     * @param string $method
     */
    public function testRequestMethod($method)
    {
        $request = $this->createMock(ServerRequestInterface::class);
        $request->method('getMethod')->will($this->returnValue($method));

        $controller = $this->getController(['getRequest']);
        $controller->method('getRequest')->will($this->returnValue($request));

        $this->assertEquals($method === 'GET', $controller->isGetRequest());
        $this->assertEquals($method === 'POST', $controller->isPostRequest());
        $this->assertEquals($method === 'PUT', $controller->isPutRequest());
        $this->assertEquals($method === 'DELETE', $controller->isDeleteRequest());
        $this->assertEquals($method === 'HEAD', $controller->isHeadRequest());
    }

    /**
     * Provide data fot testing 'getLocalReferer' function
     *
     * @return array
     */
    public function localRefererProvider()
    {
        return [
            ['http://google.com/path', 'example.com', null],
            ['http://example.com/', 'example.com', '/'],
            ['http://www.example.com/path', 'example.com', null],
        ];
    }

    /**
     * Test 'getLocalReferer' funtion
     *
     * @dataProvider localRefererProvider
     * @param string $referer
     * @param string $host
     * @param boolean $local
     */
    public function testLocalReferer($referer, $host, $local)
    {
        $request = $this->createMock(ServerRequestInterface::class);
        $request->expects($this->exactly(2))->method('getHeaderLine')->withConsecutive(
            [$this->equalTo('HTTP_REFERER')],
            [$this->equalTo('HTTP_HOST')]
        )->willReturnOnConsecutiveCalls($referer, $host);

        $controller = $this->getController(['getRequest']);
        $controller->method('getRequest')->will($this->returnValue($request));

        $result = $controller->getLocalReferer();

        $local ?
            $this->assertEquals($referer, $result, "Local referer should be returned") :
            $this->assertEquals('', $result, "Local referer should not be returned");
    }
}