Passed
Push — master ( 1ccf8a...1e427b )
by Daniel
02:19
created

RequestValidatorTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
c 0
b 0
f 0
dl 0
loc 123
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidateThrowsExceptionIfInputIsInvalid() 0 22 1
A setUp() 0 8 1
A testValidateReturnsValidatedInput() 0 37 1
A testValidateThrowsExceptionIfInputDoesNotValidate() 0 39 1
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,
0 ignored issues
show
Bug introduced by
$this->schemaLoader of type Mockery\MockInterface is incompatible with the type Usox\JsonSchemaApi\Dispatch\SchemaLoaderInterface expected by parameter $schemaLoader of Usox\JsonSchemaApi\Dispa...alidator::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
            /** @scrutinizer ignore-type */ $this->schemaLoader,
Loading history...
34
            $this->validator
0 ignored issues
show
Bug introduced by
$this->validator of type Mockery\MockInterface is incompatible with the type JsonSchema\Validator expected by parameter $schemaValidator of Usox\JsonSchemaApi\Dispa...alidator::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
            /** @scrutinizer ignore-type */ $this->validator
Loading history...
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);
0 ignored issues
show
Bug introduced by
$request of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Psr\Http\Message\ServerRequestInterface expected by parameter $request of Usox\JsonSchemaApi\Dispa...stValidator::validate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        $this->subject->validate(/** @scrutinizer ignore-type */ $request);
Loading history...
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);
0 ignored issues
show
Bug introduced by
$request of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Psr\Http\Message\ServerRequestInterface expected by parameter $request of Usox\JsonSchemaApi\Dispa...stValidator::validate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
        $this->subject->validate(/** @scrutinizer ignore-type */ $request);
Loading history...
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)
0 ignored issues
show
Bug introduced by
$request of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Psr\Http\Message\ServerRequestInterface expected by parameter $request of Usox\JsonSchemaApi\Dispa...stValidator::validate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
            $this->subject->validate(/** @scrutinizer ignore-type */ $request)
Loading history...
140
        );
141
    }
142
}
143