Completed
Push — master ( 52d5f7...56991b )
by John
02:46
created

canUseAdditionalJsonSchemaConstraints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Tests\Request;
10
11
use KleijnWeb\SwaggerBundle\Document\OperationObject;
12
use KleijnWeb\SwaggerBundle\Request\RequestValidator;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * @author John Kleijn <[email protected]>
17
 */
18
class RequestValidatorTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @test
22
     */
23
    public function canValidateDate()
24
    {
25
        $dateTime = new \DateTime();
26
        $this->runStringTest($dateTime, '2015-12-12', 'date');
27
    }
28
29
    /**
30
     * @test
31
     * @expectedException \KleijnWeb\SwaggerBundle\Exception\InvalidParametersException
32
     */
33
    public function canInvalidateDate()
34
    {
35
        $this->runStringTest('2016-01-01T00:00:00Z', '2016-01-01T00:00:00Z', 'date');
36
        $dateTime = new \DateTime();
37
        $this->runStringTest($dateTime, '2016-01-01T00:00:00Z', 'date');
38
    }
39
40
    /**
41
     * @test
42
     */
43
    public function canValidateDateTime()
44
    {
45
        $dateTime = new \DateTime();
46
        $this->runStringTest($dateTime, $dateTime->format(\DateTime::W3C), 'date-time');
47
        $this->runStringTest('2016-01-01T00:00:00Z', '2016-01-01T00:00:00Z', 'date-time');
48
    }
49
50
    /**
51
     * @test
52
     * @expectedException \KleijnWeb\SwaggerBundle\Exception\InvalidParametersException
53
     */
54
    public function canInvalidateDateTime()
55
    {
56
        $this->runStringTest(new \DateTime(), 'm-d-Y', 'date-time');
57
        $this->runStringTest(new \DateTime(), '01-01-2014T00:00:00Z', 'date-time');
58
    }
59
60
    /**
61
     * @test
62
     * @expectedException \KleijnWeb\SwaggerBundle\Exception\InvalidParametersException
63
     */
64
    public function canUseAdditionalJsonSchemaConstraints()
65
    {
66
        $value = str_repeat('f', 100);
67
68
        $this->runStringTest(null, $value, $value, ['maxLength' => 99]);
69
    }
70
71
72
    /**
73
     * @test
74
     */
75 View Code Duplication
    public function canOmitParameterWhenNotExplicitlyMarkedAsRequired()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $operationDefinition = (object)[
78
            'parameters' => [
79
                (object)[
80
                    'name'   => 'foo',
81
                    'in'     => 'body',
82
                    'schema' => (object)[
83
                        'type' => 'integer'
84
                    ]
85
                ]
86
            ]
87
        ];
88
89
        $validator = new RequestValidator(OperationObject::createFromOperationDefinition($operationDefinition));
90
        $request   = new Request();
91
        $validator->validateRequest($request);
92
    }
93
94
    /**
95
     * @test
96
     * @expectedException \KleijnWeb\SwaggerBundle\Exception\InvalidParametersException
97
     */
98 View Code Duplication
    public function cannotOmitParameterWhenExplicitlyMarkedAsRequired()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        $request = new Request();
101
102
        $operationDefinition = (object)[
103
            'parameters' => [
104
                (object)[
105
                    'name'     => 'foo',
106
                    'required' => true,
107
                    'in'       => 'query',
108
                    'type'     => 'int'
109
                ]
110
            ]
111
        ];
112
113
        $validator = new RequestValidator(OperationObject::createFromOperationDefinition($operationDefinition));
114
        $validator->validateRequest($request);
115
    }
116
117
    /**
118
     * @test
119
     * @expectedException \KleijnWeb\SwaggerBundle\Exception\InvalidParametersException
120
     */
121 View Code Duplication
    public function cannotOmitBodyWhenExplicitlyMarkedAsRequired()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
    {
123
        $request = new Request();
124
125
        $operationDefinition = (object)[
126
            'parameters' => [
127
                (object)[
128
                    'name'     => 'foo',
129
                    'required' => true,
130
                    'in'       => 'query',
131
                    'type'     => 'int'
132
                ]
133
            ]
134
        ];
135
136
        $validator = new RequestValidator(OperationObject::createFromOperationDefinition($operationDefinition));
137
138
        $validator->validateRequest($request);
139
    }
140
141
    /**
142
     * @param mixed  $value
143
     * @param string $rawValue
144
     * @param string $format
145
     *
146
     * @param array  $additionalParameterProperties
147
     *
148
     * @throws \KleijnWeb\SwaggerBundle\Exception\InvalidParametersException
149
     */
150
    private function runStringTest($value, $rawValue, $format, array $additionalParameterProperties = [])
151
    {
152
        $paramBagMapping = [
153
            'query'  => 'query',
154
            'path'   => 'attributes',
155
            'header' => 'headers'
156
        ];
157
158
        $parameterName = 'test';
159
160
        foreach (['query', 'path', 'header'] as $source) {
161
            $parameterDefinition = array_merge(
162
                $additionalParameterProperties,
163
                [
164
                    'name'   => $parameterName,
165
                    'in'     => $source,
166
                    'type'   => 'string',
167
                    'format' => $format
168
                ]
169
            );
170
171
            $operationDefinition = (object)[
172
                'parameters' => [
173
                    (object)$parameterDefinition
174
                ]
175
            ];
176
177
            $validator = new RequestValidator(OperationObject::createFromOperationDefinition($operationDefinition));
178
            $request   = new Request();
179
            $bagName   = $paramBagMapping[$source];
180
181
            $request->$bagName->set($parameterName, $rawValue);
182
            $request->attributes->set($parameterName, $value);
183
            $validator->validateRequest($request);
184
        }
185
    }
186
}
187