Passed
Push — master ( 1e427b...ae9ba8 )
by Daniel
01:54
created

RequestValidatorTest::testValidateReturnsValidatedInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 26
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 37
rs 9.504
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Usox\JsonSchemaApi\Dispatch;
6
7
use JsonSchema\Validator;
8
use Mockery;
9
use Mockery\Adapter\Phpunit\MockeryTestCase;
10
use Mockery\MockInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Message\StreamInterface;
13
use Teapot\StatusCode;
14
use Usox\JsonSchemaApi\Dispatch\Exception\JsonInvalidException;
15
use Usox\JsonSchemaApi\Exception\RequestMalformedException;
16
17
class RequestValidatorTest extends MockeryTestCase
18
{
19
    /** @var MockInterface|SchemaLoaderInterface */
20
    private MockInterface $schemaLoader;
21
    
22
    /** @var MockInterface|Validator */
23
    private MockInterface $validator;
24
    
25
    private RequestValidator $subject;
26
    
27
    public function setUp(): void
28
    {
29
        $this->schemaLoader = Mockery::mock(SchemaLoaderInterface::class);
30
        $this->validator = Mockery::mock(Validator::class);
31
        
32
        $this->subject = new RequestValidator(
33
            $this->schemaLoader,
34
            $this->validator
35
        );
36
    }
37
    
38
    public function testValidateThrowsExceptionIfInputIsInvalid(): void
39
    {
40
        $this->expectException(JsonInvalidException::class);
41
        $this->expectExceptionMessage('Input is no valid json (Syntax error)');
42
        $this->expectExceptionCode(StatusCode::BAD_REQUEST);
43
        
44
        $stream = Mockery::mock(StreamInterface::class);
45
        $request = Mockery::mock(ServerRequestInterface::class);
46
        
47
        $input = 'some-input' . PHP_EOL . 'errors';
48
        
49
        $request->shouldReceive('getBody')
50
            ->withNoArgs()
51
            ->once()
52
            ->andReturn($stream);
53
        
54
        $stream->shouldReceive('getContents')
55
            ->withNoArgs()
56
            ->once()
57
            ->andReturn($input);
58
        
59
        $this->subject->validate($request);
60
    }
61
62
    public function testValidateThrowsExceptionIfInputDoesNotValidate(): void
63
    {
64
        $this->expectException(RequestMalformedException::class);
65
        $this->expectExceptionMessage('Request is invalid');
66
        $this->expectExceptionCode(StatusCode::BAD_REQUEST);
67
68
        $stream = Mockery::mock(StreamInterface::class);
69
        $request = Mockery::mock(ServerRequestInterface::class);
70
71
        $input = ['some' => 'input'];
72
        $schemaContent = (object) ['some' => 'schema-content'];
73
74
        $this->schemaLoader->shouldReceive('load')
75
            ->once()
76
            ->andReturn($schemaContent);
77
        
78
        $this->validator->shouldReceive('validate')
79
            ->with(
80
                Mockery::on(static function ($value) use ($input): bool {
81
                    return (array) $value === $input;
82
                }),
83
                Mockery::on(static function ($value) use ($schemaContent): bool {
84
                    return $value == $schemaContent;
85
                })
86
            )
87
            ->once()
88
            ->andReturn(666);
89
90
        $request->shouldReceive('getBody')
91
            ->withNoArgs()
92
            ->once()
93
            ->andReturn($stream);
94
95
        $stream->shouldReceive('getContents')
96
            ->withNoArgs()
97
            ->once()
98
            ->andReturn(json_encode($input));
99
100
        $this->subject->validate($request);
101
    }
102
103
    public function testValidateReturnsValidatedInput(): void
104
    {
105
        $stream = Mockery::mock(StreamInterface::class);
106
        $request = Mockery::mock(ServerRequestInterface::class);
107
108
        $input = ['some' => 'input'];
109
        $schemaContent = (object) ['some' => 'schema-content'];
110
111
        $this->schemaLoader->shouldReceive('load')
112
            ->once()
113
            ->andReturn($schemaContent);
114
115
        $this->validator->shouldReceive('validate')
116
            ->with(
117
                Mockery::on(static function ($value) use ($input): bool {
118
                    return (array) $value === $input;
119
                }),
120
                Mockery::on(static function ($value) use ($schemaContent): bool {
121
                    return $value == $schemaContent;
122
                })
123
            )
124
            ->once()
125
            ->andReturn(Validator::ERROR_NONE);
126
127
        $request->shouldReceive('getBody')
128
            ->withNoArgs()
129
            ->once()
130
            ->andReturn($stream);
131
132
        $stream->shouldReceive('getContents')
133
            ->withNoArgs()
134
            ->once()
135
            ->andReturn(json_encode($input));
136
137
        static::assertEquals(
138
            (object) $input,
139
            $this->subject->validate($request)
140
        );
141
    }
142
}
143