Skip to content

Commit 3bbbc37

Browse files
committed
Add test-case for Type error messages
1 parent d97cf3c commit 3bbbc37

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the JsonSchema package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace JsonSchema\Tests\Constraints;
11+
12+
use JsonSchema\Constraints\Type;
13+
14+
/**
15+
* Class TypeTest
16+
*
17+
* @package JsonSchema\Tests\Constraints
18+
* @author hakre <https://github.com/hakre>
19+
*/
20+
class TypeTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @see testIndefiniteArticleForTypeInTypeCheckErrorMessage
24+
* @return array
25+
*/
26+
public function provideIndefiniteArticlesForTypes()
27+
{
28+
return array(
29+
array('integer', 'an',),
30+
array('number', 'a',),
31+
array('boolean', 'a',),
32+
array('object', 'an',),
33+
array('array', 'an',),
34+
array('string', 'a',),
35+
array('null', 'a', array(), 'array',),
36+
);
37+
}
38+
39+
/**
40+
* @dataProvider provideIndefiniteArticlesForTypes
41+
*/
42+
public function testIndefiniteArticleForTypeInTypeCheckErrorMessage($type, $wording, $value = null, $label = 'NULL')
43+
{
44+
$constraint = new Type();
45+
$constraint->check($value, (object)array('type' => $type));
46+
$this->assertTypeConstraintError("$label value found, but $wording $type is required", $constraint);
47+
}
48+
49+
/**
50+
* Helper to assert an error message
51+
*
52+
* @param string $expected
53+
* @param Type $actual
54+
*/
55+
private function assertTypeConstraintError($expected, Type $actual)
56+
{
57+
$actualErrors = $actual->getErrors();
58+
59+
$this->assertCount(1, $actualErrors, "Failed to assert that Type has exactly one error to assert the error message against.");
60+
61+
$actualError = $actualErrors[0];
62+
63+
$this->assertInternalType('array', $actualError, sprintf('Failed to assert that Type error is an array, %s given', gettype($actualError)));
64+
65+
$messageKey = 'message';
66+
$this->assertArrayHasKey(
67+
$messageKey, $actualError,
68+
sprintf('Failed to assert that Type error has a message key %s.', var_export($messageKey, true))
69+
);
70+
71+
$actualMessage = $actualError[$messageKey];
72+
73+
$this->assertEquals($expected, $actualMessage); // first equal for the diff
74+
$this->assertSame($expected, $actualMessage); // the same for the strictness
75+
}
76+
}

0 commit comments

Comments
 (0)