Completed
Push — feature/other-validation ( da0ad1...221a48 )
by Narcotic
229:38 queued 164:45
created

schemaConstraintDataProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 87
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 87
rs 8.6296
cc 1
eloc 62
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * Functional test for /hans/showcase
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class SchemaConstraintsTest extends RestTestCase
19
{
20
    /**
21
     * tests schema based validation constraints
22
     *
23
     * @param string $field         field
24
     * @param string $acceptedValue accepted value
25
     * @param string $rejectedValue rejected value
26
     * @param string $errorMessage  expected error message
27
     *
28
     * @dataProvider schemaConstraintDataProvider
29
     *
30
     * @return void
31
     */
32
    public function testSchemaConstraint($field, $acceptedValue, $rejectedValue, $errorMessage)
33
    {
34
        // test accepted value
35
        $object = new \stdClass();
36
        $object->{$field} = $acceptedValue;
37
38
        $client = static::createRestClient();
39
        $client->post('/testcase/schema-constraints/', $object);
40
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
41
        $this->assertNull($client->getResults());
42
43
        // test rejected value
44
        $object = new \stdClass();
45
        $object->{$field} = $rejectedValue;
46
47
        $client = static::createRestClient();
48
        $client->post('/testcase/schema-constraints/', $object);
49
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
50
        $results = $client->getResults();
51
        $this->assertEquals($field, $results[0]->propertyPath);
52
        $this->assertEquals($errorMessage, $results[0]->message);
53
    }
54
55
    /**
56
     * Data provider for constraint test
57
     *
58
     * @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...
59
     */
60
    public function schemaConstraintDataProvider()
61
    {
62
        return [
63
            'choice-string' => [
64
                'field' => 'choiceString',
65
                'acceptedValue' => 'a lo mejor',
66
                'rejectedValue' => 'no puedo',
67
                'errorMessage' => 'Does not have a value in the enumeration ["si","no","a lo mejor","mas"]'
68
            ],
69
            'choice-integer' => [
70
                'field' => 'choiceInteger',
71
                'acceptedValue' => 0,
72
                'rejectedValue' => 5,
73
                'errorMessage' => 'Does not have a value in the enumeration [0,1,2]'
74
            ],
75
            'email' => [
76
                'field' => 'email',
77
                'acceptedValue' => '[email protected]',
78
                'rejectedValue' => 'invalidemail@sss.',
79
                'errorMessage' => 'Invalid email'
80
            ],
81
            'url' => [
82
                'field' => 'url',
83
                'acceptedValue' => 'https://github.com/libgraviton/graviton',
84
                'rejectedValue' => 'jjj--no-url',
85
                'errorMessage' => 'Invalid URL format'
86
            ],
87
            
88
            // Range
89
            
90
            'range-integer-lower-bound' => [
91
                'field' => 'rangeInteger',
92
                'acceptedValue' => 5,
93
                'rejectedValue' => 4,
94
                'errorMessage' => 'Must have a minimum value of 5'
95
            ],
96
            'range-integer-upper-bound' => [
97
                'field' => 'rangeInteger',
98
                'acceptedValue' => 9,
99
                'rejectedValue' => 10,
100
                'errorMessage' => 'Must have a maximum value of 9'
101
            ],
102
            'range-double-lower-bound' => [
103
                'field' => 'rangeDouble',
104
                'acceptedValue' => 0.0,
105
                'rejectedValue' => -0.0001,
106
                'errorMessage' => 'Must have a minimum value of 0'
107
            ],
108
            'range-double-upper-bound' => [
109
                'field' => 'rangeDouble',
110
                'acceptedValue' => 1.0,
111
                'rejectedValue' => 1.0000001,
112
                'errorMessage' => 'Must have a maximum value of 1'
113
            ],
114
            
115
            // GreatherThanOrEqual
116
117
            'greaterthan-integer' => [
118
                'field' => 'greaterThanOrEqualInt',
119
                'acceptedValue' => 0,
120
                'rejectedValue' => -1,
121
                'errorMessage' => 'Must have a minimum value of 0'
122
            ],
123
            'greaterthan-double' => [
124
                'field' => 'greaterThanOrEqualDouble',
125
                'acceptedValue' => 0.1,
126
                'rejectedValue' => 0,
127
                'errorMessage' => 'Must have a minimum value of 0.1'
128
            ],
129
130
            // LessThanOrEqual
131
132
            'lessthan-integer' => [
133
                'field' => 'lessThanOrEqualInt',
134
                'acceptedValue' => 0,
135
                'rejectedValue' => 1,
136
                'errorMessage' => 'Must have a maximum value of 0'
137
            ],
138
            'lessthan-double' => [
139
                'field' => 'lessThanOrEqualDouble',
140
                'acceptedValue' => 0.1,
141
                'rejectedValue' => 0.1000001,
142
                'errorMessage' => 'Must have a maximum value of 0.1'
143
            ]
144
            
145
        ];
146
    }
147
}
148