forked from jsonrainbow/json-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeTest.php
143 lines (121 loc) · 4.59 KB
/
TypeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/*
* This file is part of the JsonSchema package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JsonSchema\Tests\Constraints;
use JsonSchema\Constraints\TypeCheck\LooseTypeCheck;
use JsonSchema\Constraints\TypeConstraint;
use PHPUnit\Framework\TestCase;
/**
* Class TypeTest
*
* @package JsonSchema\Tests\Constraints
*
* @author hakre <https://github.com/hakre>
*/
class TypeTest extends TestCase
{
/**
* @see testIndefiniteArticleForTypeInTypeCheckErrorMessage
*
* @return array
*/
public function provideIndefiniteArticlesForTypes()
{
return array(
array('integer', 'an integer'),
array('number', 'a number'),
array('boolean', 'a boolean'),
array('object', 'an object'),
array('array', 'an array'),
array('string', 'a string'),
array('null', 'a null', array(), 'array'),
array(array('string', 'boolean', 'integer'), 'a string, a boolean or an integer'),
array(array('string', 'boolean'), 'a string or a boolean'),
array(array('string'), 'a string'),
);
}
/**
* @dataProvider provideIndefiniteArticlesForTypes
*/
public function testIndefiniteArticleForTypeInTypeCheckErrorMessage($type, $wording, $value = null, $label = 'NULL')
{
$constraint = new TypeConstraint();
$constraint->check($value, (object) array('type' => $type));
$this->assertTypeConstraintError(ucwords($label) . " value found, but $wording is required", $constraint);
}
/**
* Test uncovered areas of the loose type checker
*/
public function testLooseTypeChecking()
{
$v = new \stdClass();
$v->property = 'dataOne';
LooseTypeCheck::propertySet($v, 'property', 'dataTwo');
$this->assertEquals('dataTwo', $v->property);
$this->assertEquals('dataTwo', LooseTypeCheck::propertyGet($v, 'property'));
$this->assertEquals(1, LooseTypeCheck::propertyCount($v));
}
/**
* Helper to assert an error message
*
* @param string $expected
* @param TypeConstraint $actual
*/
private function assertTypeConstraintError($expected, TypeConstraint $actual)
{
$actualErrors = $actual->getErrors();
$this->assertCount(1, $actualErrors, 'Failed to assert that Type has exactly one error to assert the error message against.');
$actualError = $actualErrors[0];
$this->assertInternalType('array', $actualError, sprintf('Failed to assert that Type error is an array, %s given', gettype($actualError)));
$messageKey = 'message';
$this->assertArrayHasKey(
$messageKey, $actualError,
sprintf('Failed to assert that Type error has a message key %s.', var_export($messageKey, true))
);
$actualMessage = $actualError[$messageKey];
$this->assertEquals($expected, $actualMessage); // first equal for the diff
$this->assertSame($expected, $actualMessage); // the same for the strictness
}
public function validNameWordingDataProvider()
{
$wordings = array();
foreach (array_keys(TypeConstraint::$wording) as $value) {
$wordings[] = array($value);
}
return $wordings;
}
/**
* @dataProvider validNameWordingDataProvider
*/
public function testValidateTypeNameWording($nameWording)
{
$t = new TypeConstraint();
$r = new \ReflectionObject($t);
$m = $r->getMethod('validateTypeNameWording');
$m->setAccessible(true);
$m->invoke($t, $nameWording);
}
public function testInvalidateTypeNameWording()
{
$t = new TypeConstraint();
$r = new \ReflectionObject($t);
$m = $r->getMethod('validateTypeNameWording');
$m->setAccessible(true);
$this->expectException('\UnexpectedValueException');
$this->expectExceptionMessage("No wording for 'notAValidTypeName' available, expected wordings are: [an integer, a number, a boolean, an object, an array, a string, a null]");
$m->invoke($t, 'notAValidTypeName');
}
public function testValidateTypeException()
{
$t = new TypeConstraint();
$data = new \stdClass();
$schema = json_decode('{"type": "notAValidTypeName"}');
$this->expectException('JsonSchema\Exception\InvalidArgumentException');
$this->expectExceptionMessage('object is an invalid type for notAValidTypeName');
$t->check($data, $schema);
}
}