|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DanBettles\Defence\Tests\Handler; |
|
6
|
|
|
|
|
7
|
|
|
use DanBettles\Defence\Envelope; |
|
8
|
|
|
use DanBettles\Defence\Factory\HttpResponseFactory; |
|
9
|
|
|
use DanBettles\Defence\Handler\HandlerInterface; |
|
10
|
|
|
use DanBettles\Defence\Handler\TerminateScriptHandler; |
|
11
|
|
|
use DanBettles\Defence\Logger\NullLogger; |
|
12
|
|
|
use DanBettles\Defence\PhpFunctionsWrapper; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
use ReflectionClass; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
|
|
18
|
|
|
class TerminateScriptHandlerTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
public function testImplementsHandlerinterface(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$reflectionClass = new ReflectionClass(TerminateScriptHandler::class); |
|
23
|
|
|
|
|
24
|
|
|
$this->assertTrue($reflectionClass->implementsInterface(HandlerInterface::class)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testConstructor(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$phpFunctions = new PhpFunctionsWrapper(); |
|
30
|
|
|
$httpResponseFactory = new HttpResponseFactory(); |
|
31
|
|
|
|
|
32
|
|
|
$handler = new TerminateScriptHandler($phpFunctions, $httpResponseFactory); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertSame($phpFunctions, $handler->getPhpFunctionsWrapper()); |
|
35
|
|
|
$this->assertSame($httpResponseFactory, $handler->getHttpResponseFactory()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testInvokeTerminatesTheScript(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$request = Request::createFromGlobals(); |
|
41
|
|
|
|
|
42
|
|
|
//Create a mock response: |
|
43
|
|
|
|
|
44
|
|
|
$httpResponseMock = $this |
|
45
|
|
|
->getMockBuilder(Response::class) |
|
46
|
|
|
->onlyMethods(['prepare', 'send']) |
|
47
|
|
|
->getMock() |
|
48
|
|
|
; |
|
49
|
|
|
|
|
50
|
|
|
$httpResponseMock |
|
51
|
|
|
->expects($this->once()) |
|
52
|
|
|
->method('prepare') |
|
53
|
|
|
->with($request) |
|
54
|
|
|
->will($this->returnSelf()) |
|
55
|
|
|
; |
|
56
|
|
|
|
|
57
|
|
|
//Make sure the response will be sent. |
|
58
|
|
|
$httpResponseMock |
|
59
|
|
|
->expects($this->once()) |
|
60
|
|
|
->method('send') |
|
61
|
|
|
; |
|
62
|
|
|
|
|
63
|
|
|
//Create a mock HTTP-response factory that will return our mock response: |
|
64
|
|
|
|
|
65
|
|
|
$httpResponseFactoryMock = $this |
|
66
|
|
|
->getMockBuilder(HttpResponseFactory::class) |
|
67
|
|
|
->onlyMethods(['createForbiddenResponse']) |
|
68
|
|
|
->getMock() |
|
69
|
|
|
; |
|
70
|
|
|
|
|
71
|
|
|
$httpResponseFactoryMock |
|
72
|
|
|
->expects($this->once()) |
|
73
|
|
|
->method('createForbiddenResponse') |
|
74
|
|
|
->with("We're not going to handle your request because it looks suspicious. Please contact us if we've made a mistake.") |
|
75
|
|
|
->willReturn($httpResponseMock) |
|
76
|
|
|
; |
|
77
|
|
|
|
|
78
|
|
|
/** @var HttpResponseFactory $httpResponseFactoryMock */ |
|
79
|
|
|
|
|
80
|
|
|
//Mock the PHP-functions wrapper so we can test if the script will be terminated. |
|
81
|
|
|
|
|
82
|
|
|
$phpFunctionsMock = $this |
|
83
|
|
|
->getMockBuilder(PhpFunctionsWrapper::class) |
|
84
|
|
|
->onlyMethods(['exit']) |
|
85
|
|
|
->getMock() |
|
86
|
|
|
; |
|
87
|
|
|
|
|
88
|
|
|
$phpFunctionsMock |
|
89
|
|
|
->expects($this->once()) |
|
90
|
|
|
->method('exit') |
|
91
|
|
|
->with(0) |
|
92
|
|
|
; |
|
93
|
|
|
|
|
94
|
|
|
/** @var PhpFunctionsWrapper $phpFunctionsMock */ |
|
95
|
|
|
|
|
96
|
|
|
//Run the handler: |
|
97
|
|
|
|
|
98
|
|
|
$envelope = new Envelope($request, new NullLogger()); |
|
99
|
|
|
|
|
100
|
|
|
$handler = new TerminateScriptHandler($phpFunctionsMock, $httpResponseFactoryMock); |
|
101
|
|
|
$handler($envelope); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|