Completed
Pull Request — develop (#430)
by Narcotic
130:30 queued 65:31
created

SchemaConstraintsTest::testSchemaConstraint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
rs 9.2
cc 1
eloc 15
nc 1
nop 4
1
<?php
2
/**
3
 * integration tests for our supported constraints
4
 */
5
6
namespace Graviton\SchemaBundle\Tests\Controller;
7
8
use Graviton\TestBundle\Test\RestTestCase;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class SchemaConstraintsTest extends RestTestCase
17
{
18
    /**
19
     * tests schema based validation constraints
20
     *
21
     * @param string $field         field
22
     * @param string $acceptedValue accepted value
23
     * @param string $rejectedValue rejected value
24
     * @param string $errorMessage  expected error message
25
     *
26
     * @dataProvider schemaConstraintDataProvider
27
     *
28
     * @return void
29
     */
30
    public function testSchemaConstraint($field, $acceptedValue, $rejectedValue, $errorMessage)
31
    {
32
        // test accepted value
33
        $object = new \stdClass();
34
        $object->{$field} = $acceptedValue;
35
36
        $client = static::createRestClient();
37
        $client->post('/testcase/schema-constraints/', $object);
38
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
39
        $this->assertNull($client->getResults());
40
41
        // test rejected value
42
        $object = new \stdClass();
43
        $object->{$field} = $rejectedValue;
44
45
        $client = static::createRestClient();
46
        $client->post('/testcase/schema-constraints/', $object);
47
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
48
        $results = $client->getResults();
49
        $this->assertEquals($field, $results[0]->propertyPath);
50
        $this->assertEquals($errorMessage, $results[0]->message);
51
    }
52
53
    /**
54
     * Data provider for constraint test
55
     *
56
     * @return array data
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
57
     */
58
    public function schemaConstraintDataProvider()
59
    {
60
        return [
61
62
            // Choice
63
64
            'choice-string' => [
65
                'field' => 'choiceString',
66
                'acceptedValue' => 'a lo mejor',
67
                'rejectedValue' => 'no puedo',
68
                'errorMessage' => 'Does not have a value in the enumeration ["si","no","a lo mejor","mas"]'
69
            ],
70
            'choice-integer' => [
71
                'field' => 'choiceInteger',
72
                'acceptedValue' => 0,
73
                'rejectedValue' => 5,
74
                'errorMessage' => 'Does not have a value in the enumeration [0,1,2]'
75
            ],
76
77
            // Email
78
79
            'email' => [
80
                'field' => 'email',
81
                'acceptedValue' => '[email protected]',
82
                'rejectedValue' => 'invalidemail@sss.',
83
                'errorMessage' => 'Invalid email'
84
            ],
85
86
            // Url
87
88
            'url' => [
89
                'field' => 'url',
90
                'acceptedValue' => 'https://github.com/libgraviton/graviton',
91
                'rejectedValue' => 'jjj--no-url',
92
                'errorMessage' => 'Invalid URL format'
93
            ],
94
            
95
            // Range
96
            
97
            'range-integer-lower-bound' => [
98
                'field' => 'rangeInteger',
99
                'acceptedValue' => 5,
100
                'rejectedValue' => 4,
101
                'errorMessage' => 'Must have a minimum value of 5'
102
            ],
103
            'range-integer-upper-bound' => [
104
                'field' => 'rangeInteger',
105
                'acceptedValue' => 9,
106
                'rejectedValue' => 10,
107
                'errorMessage' => 'Must have a maximum value of 9'
108
            ],
109
            'range-double-lower-bound' => [
110
                'field' => 'rangeDouble',
111
                'acceptedValue' => 0.0,
112
                'rejectedValue' => -0.0001,
113
                'errorMessage' => 'Must have a minimum value of 0'
114
            ],
115
            'range-double-upper-bound' => [
116
                'field' => 'rangeDouble',
117
                'acceptedValue' => 1.0,
118
                'rejectedValue' => 1.0000001,
119
                'errorMessage' => 'Must have a maximum value of 1'
120
            ],
121
            'range-integer-only-min' => [
122
                'field' => 'rangeIntegerOnlyMin',
123
                'acceptedValue' => 5,
124
                'rejectedValue' => 4,
125
                'errorMessage' => 'Must have a minimum value of 5'
126
            ],
127
            'range-integer-only-max' => [
128
                'field' => 'rangeIntegerOnlyMax',
129
                'acceptedValue' => 5,
130
                'rejectedValue' => 6,
131
                'errorMessage' => 'Must have a maximum value of 5'
132
            ],
133
            
134
            // GreatherThanOrEqual
135
136
            'greaterthan-integer' => [
137
                'field' => 'greaterThanOrEqualInt',
138
                'acceptedValue' => 0,
139
                'rejectedValue' => -1,
140
                'errorMessage' => 'Must have a minimum value of 0'
141
            ],
142
            'greaterthan-double' => [
143
                'field' => 'greaterThanOrEqualDouble',
144
                'acceptedValue' => 0.1,
145
                'rejectedValue' => 0,
146
                'errorMessage' => 'Must have a minimum value of 0.1'
147
            ],
148
149
            // LessThanOrEqual
150
151
            'lessthan-integer' => [
152
                'field' => 'lessThanOrEqualInt',
153
                'acceptedValue' => 0,
154
                'rejectedValue' => 1,
155
                'errorMessage' => 'Must have a maximum value of 0'
156
            ],
157
            'lessthan-double' => [
158
                'field' => 'lessThanOrEqualDouble',
159
                'acceptedValue' => 0.1,
160
                'rejectedValue' => 0.1000001,
161
                'errorMessage' => 'Must have a maximum value of 0.1'
162
            ],
163
164
            // Decimal (a decimal formatted string field)
165
166
            'decimal-string' => [
167
                'field' => 'decimalField',
168
                'acceptedValue' => '1000000000.5555',
169
                'rejectedValue' => '1.55555', // too much precision
170
                'errorMessage' => 'Does not match the regex pattern ^[+\-]?\d+(\.\d{0,4})?$'
171
            ],
172
            'decimal-string-notation' => [
173
                'field' => 'decimalField',
174
                'acceptedValue' => '1000000000',
175
                'rejectedValue' => '1,0', // wrong separator
176
                'errorMessage' => 'Does not match the regex pattern ^[+\-]?\d+(\.\d{0,4})?$'
177
            ],
178
            'decimal-string-string' => [
179
                'field' => 'decimalField',
180
                'acceptedValue' => '0',
181
                'rejectedValue' => 'somestring', // string
182
                'errorMessage' => 'Does not match the regex pattern ^[+\-]?\d+(\.\d{0,4})?$'
183
            ],
184
185
            // Count (number of array elements)
186
187
            'count-array-lower' => [
188
                'field' => 'arrayCount',
189
                'acceptedValue' => ['a'],
190
                'rejectedValue' => [],
191
                'errorMessage' => 'There must be a minimum of 1 items in the array'
192
            ],
193
            'count-array-lower' => [
194
                'field' => 'arrayCount',
195
                'acceptedValue' => ['a', 'b', 'c'],
196
                'rejectedValue' => ['a', 'b', 'c', 'd'],
197
                'errorMessage' => 'There must be a maximum of 3 items in the array'
198
            ]
199
200
201
        ];
202
    }
203
}
204