|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Koded\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Koded\Http\Interfaces\HttpInputValidator; |
|
6
|
|
|
use Koded\Http\Interfaces\HttpStatus; |
|
7
|
|
|
use Koded\Http\JsonResponse; |
|
8
|
|
|
use Koded\Http\ServerRequest; |
|
9
|
|
|
use Koded\Stdlib\Data; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
|
|
12
|
|
|
class HttpInputValidatorTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
public function test_success_validate_with_empty_body() |
|
15
|
|
|
{ |
|
16
|
|
|
$request = new ServerRequest; |
|
17
|
|
|
$response = $request->validate(new TestSuccessValidator, $input); |
|
18
|
|
|
|
|
19
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
|
20
|
|
|
$this->assertSame(HttpStatus::BAD_REQUEST, $response->getStatusCode()); |
|
21
|
|
|
$this->assertSame('{"validate":"Nothing to validate","code":400}', (string)$response->getBody()); |
|
22
|
|
|
$this->assertSame('application/problem+json', $response->getHeaderLine('Content-Type')); |
|
23
|
|
|
|
|
24
|
|
|
$this->assertInstanceOf(Data::class, $input); |
|
25
|
|
|
$this->assertCount(0, $input); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function test_success_validate_with_body() |
|
29
|
|
|
{ |
|
30
|
|
|
$_POST = ['key' => 'value']; |
|
31
|
|
|
|
|
32
|
|
|
$request = new ServerRequest; |
|
33
|
|
|
$response = $request->validate(new TestSuccessValidator, $input); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertNull($response); |
|
36
|
|
|
$this->assertEquals($_POST, $input->toArray()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function test_failure_validate() |
|
40
|
|
|
{ |
|
41
|
|
|
$_POST = ['key' => 'value']; |
|
42
|
|
|
|
|
43
|
|
|
$request = new ServerRequest; |
|
44
|
|
|
$response = $request->validate(new TestFailureValidator); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
|
47
|
|
|
$this->assertSame(HttpStatus::BAD_REQUEST, $response->getStatusCode()); |
|
48
|
|
|
$this->assertSame('{"message":"This is the error message","status":400}', (string)$response->getBody()); |
|
49
|
|
|
$this->assertSame('application/problem+json', $response->getHeaderLine('Content-Type')); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function test_failure_validate_response_code() |
|
53
|
|
|
{ |
|
54
|
|
|
$_POST = ['key' => 'value']; |
|
55
|
|
|
|
|
56
|
|
|
$request = new ServerRequest; |
|
57
|
|
|
$response = $request->validate(new TestFailureValidatorWithStatusCode); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
|
60
|
|
|
$this->assertSame(HttpStatus::UNPROCESSABLE_ENTITY, $response->getStatusCode()); |
|
61
|
|
|
$this->assertSame('{"text":"Cannot proceed","status":422}', (string)$response->getBody()); |
|
62
|
|
|
$this->assertSame('application/problem+json', $response->getHeaderLine('Content-Type')); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function tearDown(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$_POST = []; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
class TestSuccessValidator implements HttpInputValidator { |
|
72
|
|
|
|
|
73
|
|
|
public function validate(Data $input): array |
|
74
|
|
|
{ |
|
75
|
|
|
return []; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
class TestFailureValidator implements HttpInputValidator { |
|
80
|
|
|
|
|
81
|
|
|
public function validate(Data $input): array |
|
82
|
|
|
{ |
|
83
|
|
|
return [ |
|
84
|
|
|
'message' => 'This is the error message', |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
class TestFailureValidatorWithStatusCode implements HttpInputValidator { |
|
90
|
|
|
|
|
91
|
|
|
public function validate(Data $input): array |
|
92
|
|
|
{ |
|
93
|
|
|
return [ |
|
94
|
|
|
'text' => 'Cannot proceed', |
|
95
|
|
|
'status' => HttpStatus::UNPROCESSABLE_ENTITY, |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
} |