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

testValidateInputThrowsExceptionIfInputDoesNotValidate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 25
rs 9.6666
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 Teapot\StatusCode;
12
use Usox\JsonSchemaApi\Exception\RequestMalformedException;
13
use Usox\JsonSchemaApi\Exception\ResponseMalformedException;
14
15
class MethodValidatorTest extends MockeryTestCase
16
{
17
    /** @var Validator|MockInterface */
18
    private MockInterface $schemaValidator;
19
    
20
    private MethodValidator $subject;
21
    
22
    public function setUp(): void
23
    {
24
        $this->schemaValidator = Mockery::mock(Validator::class);
25
        
26
        $this->subject = new MethodValidator(
27
            $this->schemaValidator
0 ignored issues
show
Bug introduced by
$this->schemaValidator 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

27
            /** @scrutinizer ignore-type */ $this->schemaValidator
Loading history...
28
        );
29
    }
30
    
31
    public function testValidateInputThrowsExceptionIfInputDoesNotValidate(): void
32
    {
33
        $parameter = ['test' => 'param'];
34
        $schemaParameter = ['schema' => 'param'];
35
        $input = (object) ['parameter' => $parameter];
36
        $content = (object) ['properties' => (object) ['parameter' => $schemaParameter]];
37
        $validationResult = 666;
38
39
        $this->expectException(RequestMalformedException::class);
40
        $this->expectExceptionMessage('Bad Request');
41
        $this->expectExceptionCode(StatusCode::BAD_REQUEST);
42
        
43
        $this->schemaValidator->shouldReceive('validate')
44
            ->with(
45
                $parameter,
46
                Mockery::on(static function($value) use ($schemaParameter): bool {
47
                    return (array) $value === $schemaParameter;
48
                })
49
            )
50
            ->once()
51
            ->andReturn($validationResult);
52
53
        $this->subject->validateInput(
54
            $content,
55
            $input
56
        );
57
    }
58
59
    public function testValidateInputValidates(): void
60
    {
61
        $parameter = ['test' => 'param'];
62
        $schemaParameter = ['schema' => 'param'];
63
        $input = (object) ['parameter' => $parameter];
64
        $content = (object) ['properties' => (object) ['parameter' => $schemaParameter]];
65
66
        $this->schemaValidator->shouldReceive('validate')
67
            ->with(
68
                $parameter,
69
                Mockery::on(static function($value) use ($schemaParameter): bool {
70
                    return (array) $value === $schemaParameter;
71
                })
72
            )
73
            ->once()
74
            ->andReturn(Validator::ERROR_NONE);
75
76
        $this->subject->validateInput(
77
            $content,
78
            $input
79
        );
80
    }
81
82
    public function testValidateOutputThrowsExceptionIfOutputDoesNotValidate(): void
83
    {
84
        $output = (object) ['test' => 'param'];
85
        $schemaParameter = ['schema' => 'param'];
86
        $content = (object) ['properties' => (object) ['response' => $schemaParameter]];
87
        $errors = ['some' => 'error'];
88
        $validationResult = 666;
89
90
        $this->expectException(ResponseMalformedException::class);
91
        $this->expectExceptionMessage('Internal Server Error');
92
        $this->expectExceptionCode(StatusCode::INTERNAL_SERVER_ERROR);
93
94
        $this->schemaValidator->shouldReceive('validate')
95
            ->with(
96
                $output,
97
                Mockery::on(static function($value) use ($schemaParameter): bool {
98
                    return (array) $value === $schemaParameter;
99
                })
100
            )
101
            ->once()
102
            ->andReturn($validationResult);
103
        $this->schemaValidator->shouldReceive('getErrors')
104
            ->withNoArgs()
105
            ->once()
106
            ->andReturn($errors);
107
108
        $this->subject->validateOutput(
109
            $content,
110
            $output
111
        );
112
    }
113
114
    public function testValidateOutputValidates(): void
115
    {
116
        $output = (object) ['test' => 'param'];
117
        $schemaParameter = ['schema' => 'param'];
118
        $content = (object) ['properties' => (object) ['response' => $schemaParameter]];
119
120
        $this->schemaValidator->shouldReceive('validate')
121
            ->with(
122
                $output,
123
                Mockery::on(static function($value) use ($schemaParameter): bool {
124
                    return (array) $value === $schemaParameter;
125
                })
126
            )
127
            ->once()
128
            ->andReturn(Validator::ERROR_NONE);
129
130
        $this->subject->validateOutput(
131
            $content,
132
            $output
133
        );
134
    }
135
}
136