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 177 178 179 180 181 182 183 184
|
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
*/
namespace Slim\Tests\Handlers;
use Exception;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit;
use Slim\Handlers\PhpError;
use Slim\Http\Request;
use Slim\Http\Response;
use Throwable;
use UnexpectedValueException;
class PhpErrorTest extends PHPUnit\Framework\TestCase
{
public static function phpErrorProvider()
{
return [
['application/json', 'application/json', '{'],
['application/vnd.api+json', 'application/json', '{'],
['application/xml', 'application/xml', '<error>'],
['application/hal+xml', 'application/xml', '<error>'],
['text/xml', 'text/xml', '<error>'],
['text/html', 'text/html', '<html>'],
];
}
/**
* Test invalid method returns the correct code and content type
*
* @requires PHP 7.0
*/
#[DataProvider('phpErrorProvider')]
#[\PHPUnit\Framework\Attributes\RequiresPhp('7.0')]
public function testPhpError($acceptHeader, $contentType, $startOfBody)
{
$error = new PhpError();
/** @var Response $res */
$res = $error->__invoke($this->getRequest('GET', $acceptHeader), new Response(), new Exception());
$this->assertSame(500, $res->getStatusCode());
$this->assertSame($contentType, $res->getHeaderLine('Content-Type'));
$this->assertEquals(0, strpos((string)$res->getBody(), $startOfBody));
}
/**
* Test invalid method returns the correct code and content type
*
* @requires PHP 7.0
*/
#[DataProvider('phpErrorProvider')]
#[\PHPUnit\Framework\Attributes\RequiresPhp('7.0')]
public function testPhpErrorDisplayDetails($acceptHeader, $contentType, $startOfBody)
{
$error = new PhpError(true);
$exception = new Exception('Oops', 1, new Exception('Opps before'));
/** @var Response $res */
$res = $error->__invoke($this->getRequest('GET', $acceptHeader), new Response(), $exception);
$this->assertSame(500, $res->getStatusCode());
$this->assertSame($contentType, $res->getHeaderLine('Content-Type'));
$this->assertEquals(0, strpos((string)$res->getBody(), $startOfBody));
}
/**
* @requires PHP 7.0
*/
#[\PHPUnit\Framework\Attributes\RequiresPhp('7.0')]
public function testNotFoundContentType()
{
$errorMock = $this->getMockBuilder(PhpError::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(), new Exception());
}
/**
* Test invalid method returns the correct code and content type
*
* @requires PHP 5.0
*/
#[DataProvider('phpErrorProvider')]
#[\PHPUnit\Framework\Attributes\RequiresPhp('5.0')]
public function testPhpError5($acceptHeader, $contentType, $startOfBody)
{
$this->skipIfPhp70();
$error = new PhpError();
/** @var Throwable $throwable */
$throwable = $this->getMock(
'\Throwable',
['getCode', 'getMessage', 'getFile', 'getLine', 'getTraceAsString', 'getPrevious']
);
$res = $error->__invoke($this->getRequest('GET', $acceptHeader), new Response(), $throwable);
$this->assertSame(500, $res->getStatusCode());
$this->assertSame($contentType, $res->getHeaderLine('Content-Type'));
$this->assertEquals(0, strpos((string)$res->getBody(), $startOfBody));
}
/**
* Test invalid method returns the correct code and content type
*/
#[DataProvider('phpErrorProvider')]
public function testPhpErrorDisplayDetails5($acceptHeader, $contentType, $startOfBody)
{
$this->skipIfPhp70();
$error = new PhpError(true);
/** @var Throwable $throwable */
$throwable = $this->getMock(
'\Throwable',
['getCode', 'getMessage', 'getFile', 'getLine', 'getTraceAsString', 'getPrevious']
);
$throwablePrev = clone $throwable;
$throwable->method('getCode')->willReturn(1);
$throwable->method('getMessage')->willReturn('Oops');
$throwable->method('getFile')->willReturn('test.php');
$throwable->method('getLine')->willReturn('1');
$throwable->method('getTraceAsString')->willReturn('This is error');
$throwable->method('getPrevious')->willReturn($throwablePrev);
$res = $error->__invoke($this->getRequest('GET', $acceptHeader), new Response(), $throwable);
$this->assertSame(500, $res->getStatusCode());
$this->assertSame($contentType, $res->getHeaderLine('Content-Type'));
$this->assertEquals(0, strpos((string)$res->getBody(), $startOfBody));
}
#[\PHPUnit\Framework\Attributes\RequiresPhp('5.0')]
public function testNotFoundContentType5()
{
$this->expectException('UnexpectedValueException');
$this->skipIfPhp70();
$errorMock = $this->getMock(PhpError::class, ['determineContentType']);
$errorMock->method('determineContentType')
->willReturn('unknown/type');
$throwable = $this->getMockBuilder('Throwable')->getMock();
$req = $this->getMockBuilder('Slim\Http\Request')->disableOriginalConstructor()->getMock();
$errorMock->__invoke($req, new Response(), $throwable);
}
/**
* @param string $method
*
* @return PHPUnit_Framework_MockObject_MockObject|Request
*/
protected function getRequest($method, $acceptHeader)
{
$req = $this->getMockBuilder('Slim\Http\Request')->disableOriginalConstructor()->getMock();
$req->expects($this->once())->method('getHeaderLine')->willReturn($acceptHeader);
return $req;
}
/**
* @return mixed
*/
protected function skipIfPhp70()
{
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$this->markTestSkipped();
}
}
}
|