|
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 |
|
|
|
|
|
|
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
|
|
|
'email' => [ |
|
70
|
|
|
'field' => 'email', |
|
71
|
|
|
'acceptedValue' => '[email protected]', |
|
72
|
|
|
'rejectedValue' => 'invalidemail@sss.', |
|
73
|
|
|
'errorMessage' => 'Invalid email' |
|
74
|
|
|
] |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.