-
Notifications
You must be signed in to change notification settings - Fork 1
/
MessageTypeConstraintTest.php
94 lines (82 loc) · 2.33 KB
/
MessageTypeConstraintTest.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
<?php
declare(strict_types=1);
namespace FR3D\Psr3MessagesAssertions\PhpUnit;
use FR3D\Psr3MessagesAssertions\Fixture\ObjectWithToString;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestFailure;
use stdClass;
class MessageTypeConstraintTest extends TestCase
{
/**
* @var Constraint
*/
protected $constraint;
protected function setUp()
{
$this->constraint = new MessageTypeConstraint();
}
public function testConstraintDefinition()
{
self::assertCount(1, $this->constraint);
self::assertEquals(
'is string or a object with a __toString() method',
$this->constraint->toString()
);
}
/**
* @dataProvider validMessageTypeProvider
*
* @param string|object $message
*
* @return void
*/
public function testValidMessageType($message)
{
self::assertTrue($this->constraint->evaluate($message, '', true));
}
/**
* @dataProvider invalidMessageTypeProvider
*
* @param mixed $message
* @param string $type String representation of $message argument type.
*
* @return void
*/
public function testInvalidMessageType($message, $type)
{
self::assertFalse($this->constraint->evaluate($message, '', true));
try {
$this->constraint->evaluate($message);
self::fail('Expected ExpectationFailedException to be thrown');
} catch (ExpectationFailedException $e) {
self::assertEquals(
<<<EOF
Failed asserting that $type is string or a object with a __toString() method.
EOF
,
TestFailure::exceptionToString($e)
);
}
}
public function validMessageTypeProvider()
{
return [
'string' => ['message'],
'object with toString' => [new ObjectWithToString()],
'integer' => [1],
'double' => [1.1],
'null' => [null],
];
}
public function invalidMessageTypeProvider()
{
return [
'array' => [[], 'array'],
'function' => [function () {
}, 'object'],
'object without toString' => [new stdClass(), 'object'],
];
}
}