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
|
<?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\NotFound;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Uri;
class NotFoundTest extends PHPUnit\Framework\TestCase
{
public static function notFoundProvider()
{
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('notFoundProvider')]
public function testNotFound($acceptHeader, $contentType, $startOfBody)
{
$notAllowed = new NotFound();
/** @var Response $res */
$res = $notAllowed->__invoke($this->getRequest('GET', $acceptHeader), new Response(), ['POST', 'PUT']);
$this->assertSame(404, $res->getStatusCode());
$this->assertSame($contentType, $res->getHeaderLine('Content-Type'));
$this->assertEquals(0, strpos((string)$res->getBody(), $startOfBody));
}
public function testNotFoundContentType()
{
$errorMock = $this->getMockBuilder(NotFound::class)->onlyMethods(['determineContentType'])->getMock();
$errorMock->method('determineContentType')
->willReturn('unknown/type');
$req = $this->getMockBuilder('Slim\Http\Request')->disableOriginalConstructor()->getMock();
$this->expectException('\UnexpectedValueException');
$errorMock->__invoke($req, new Response(), ['POST']);
}
/**
* @param string $method
*
* @return PHPUnit_Framework_MockObject_MockObject|Request
*/
protected function getRequest($method, $contentType = 'text/html')
{
$uri = new Uri('http', 'example.com', 80, '/notfound');
$req = $this->getMockBuilder('Slim\Http\Request')->disableOriginalConstructor()->getMock();
$req->expects($this->once())->method('getHeaderLine')->willReturn($contentType);
$req->expects($this->any())->method('getUri')->willReturn($uri);
return $req;
}
}
|