File: NotAllowedTest.php

package info (click to toggle)
php-slim 3.12.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,444 kB
  • sloc: php: 11,581; makefile: 14; xml: 10; sh: 3
file content (84 lines) | stat: -rw-r--r-- 2,927 bytes parent folder | download | duplicates (2)
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
<?php
/**
 * Slim Framework (https://slimframework.com)
 *
 * @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
 */

namespace Slim\Tests\Handlers;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit;
use Slim\Handlers\NotAllowed;
use Slim\Http\Request;
use Slim\Http\Response;

class NotAllowedTest extends PHPUnit\Framework\TestCase
{
    public static function invalidMethodProvider()
    {
        return [
            ['application/json', 'application/json', '{'],
            ['application/vnd.api+json', 'application/json', '{'],
            ['application/xml', 'application/xml', '<root>'],
            ['application/hal+xml', 'application/xml', '<root>'],
            ['text/xml', 'text/xml', '<root>'],
            ['text/html', 'text/html', '<html>'],
        ];
    }

    /**
     * Test invalid method returns the correct code and content type
     */
    #[DataProvider('invalidMethodProvider')]
    public function testInvalidMethod($acceptHeader, $contentType, $startOfBody)
    {
        $notAllowed = new NotAllowed();

        /** @var Response $res */
        $res = $notAllowed->__invoke($this->getRequest('GET', $acceptHeader), new Response(), ['POST', 'PUT']);

        $this->assertSame(405, $res->getStatusCode());
        $this->assertTrue($res->hasHeader('Allow'));
        $this->assertSame($contentType, $res->getHeaderLine('Content-Type'));
        $this->assertEquals('POST, PUT', $res->getHeaderLine('Allow'));
        $this->assertEquals(0, strpos((string)$res->getBody(), $startOfBody));
    }

    public function testOptions()
    {
        $notAllowed = new NotAllowed();

        /** @var Response $res */
        $res = $notAllowed->__invoke($this->getRequest('OPTIONS'), new Response(), ['POST', 'PUT']);

        $this->assertSame(200, $res->getStatusCode());
        $this->assertTrue($res->hasHeader('Allow'));
        $this->assertEquals('POST, PUT', $res->getHeaderLine('Allow'));
    }

    public function testNotFoundContentType()
    {
        $errorMock = $this->getMockBuilder(NotAllowed::class)->onlyMethods(['determineContentType'])->getMock();
        $errorMock->method('determineContentType')
            ->willReturn('unknown/type');

        $this->expectException('\UnexpectedValueException');
        $errorMock->__invoke($this->getRequest('GET', 'unknown/type'), new Response(), ['POST']);
    }

    /**
     * @param string $method
     *
     * @return PHPUnit_Framework_MockObject_MockObject|Request
     */
    protected function getRequest($method, $contentType = 'text/html')
    {
        $req = $this->getMockBuilder('Slim\Http\Request')->disableOriginalConstructor()->getMock();
        $req->expects($this->once())->method('getMethod')->willReturn($method);
        $req->expects($this->any())->method('getHeaderLine')->willReturn($contentType);

        return $req;
    }
}