Completed
Push — feature/other-validation ( ca1c27...da0ad1 )
by Narcotic
141:14 queued 76:14
created

schemaConstraintDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 3
Metric Value
c 4
b 0
f 3
dl 0
loc 53
rs 9.5797
cc 1
eloc 42
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
            'range-integer-lower-bound' => [
88
                'field' => 'rangeInteger',
89
                'acceptedValue' => 5,
90
                'rejectedValue' => 4,
91
                'errorMessage' => 'Must have a minimum value of 5'
92
            ],
93
            'range-integer-upper-bound' => [
94
                'field' => 'rangeInteger',
95
                'acceptedValue' => 9,
96
                'rejectedValue' => 10,
97
                'errorMessage' => 'Must have a maximum value of 9'
98
            ],
99
            'range-double-lower-bound' => [
100
                'field' => 'rangeDouble',
101
                'acceptedValue' => 0.0,
102
                'rejectedValue' => -0.1,
103
                'errorMessage' => 'Must have a minimum value of 0'
104
            ],
105
            'range-double-upper-bound' => [
106
                'field' => 'rangeDouble',
107
                'acceptedValue' => 0.99,
108
                'rejectedValue' => 1.11,
109
                'errorMessage' => 'Must have a maximum value of 1'
110
            ]
111
        ];
112
    }
113
}
114