-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathAssertionChainTest.php
More file actions
127 lines (108 loc) · 3.69 KB
/
AssertionChainTest.php
File metadata and controls
127 lines (108 loc) · 3.69 KB
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
<?php
/**
* Assert
*
* LICENSE
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
*/
namespace Assert\Tests;
use Assert\Assert;
use Assert\AssertionChain;
use Assert\Tests\Fixtures\CustomAssertion;
use PHPUnit\Framework\TestCase;
class AssertionChainTest extends TestCase
{
public function testThatAssertionChainReturnAnAssertionChain()
{
$this->assertInstanceOf(AssertionChain::class, Assert::that(10)->notEmpty()->integer());
}
public function testThatAssertionChainShiftsArgumentsBy1()
{
$this->assertInstanceOf(AssertionChain::class, Assert::that(10)->eq(10));
}
/**
* @expectedException \Assert\InvalidArgumentException
* @expectedExceptionMessage Not Null and such
*/
public function testThatAssertionChainKnowsDefaultErrorMessage()
{
Assert::that(null, 'Not Null and such')->notEmpty();
}
public function testThatAssertionChainSkipAssertionsOnValidNull()
{
$this->assertInstanceOf(AssertionChain::class, Assert::that(null)->nullOr()->integer()->eq(10));
}
public function testThatAssertionChainDoesANegativeAssert()
{
$this->assertInstanceOf(AssertionChain::class, Assert::that(1)->not()->eq(10));
}
public function testThatAssertionChainValidatesAllInputs()
{
$this->assertInstanceOf(AssertionChain::class, Assert::that([1, 2, 3])->all()->integer());
}
public function testAssertionChainThatAllShortcut()
{
$this->assertInstanceOf(AssertionChain::class, Assert::thatAll([1, 2, 3])->integer());
}
public function testAssertionChainNullOrShortcut()
{
$this->assertInstanceOf(AssertionChain::class, Assert::thatNullOr(null)->integer()->eq(10));
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Assertion 'unknownAssertion' does not exist.
*/
public function testThatAssertionChainThrowsExceptionForUnknownAssertion()
{
Assert::that(null)->unknownAssertion();
}
public function testAssertionChainSatisfyShortcut()
{
$this->assertInstanceOf(
AssertionChain::class,
Assert::that(null)->satisfy(
function ($value) {
return \is_null($value);
}
)
);
}
public function testThatCustomAssertionClassIsUsedWhenSet()
{
$assertionChain = new AssertionChain('foo');
$assertionChain->setAssertionClassName(CustomAssertion::class);
CustomAssertion::clearCalls();
$message = \uniqid();
$assertionChain->string($message);
$this->assertSame([['string', 'foo']], CustomAssertion::getCalls());
}
/**
* @dataProvider provideDataToTestThatSetAssertionClassNameWillNotAcceptInvalidAssertionClasses
* @expectedException \LogicException
*
* @param mixed $assertionClassName
*/
public function testThatSetAssertionClassNameWillNotAcceptInvalidAssertionClasses($assertionClassName)
{
$lazyAssertion = new AssertionChain('foo');
$lazyAssertion->setAssertionClassName($assertionClassName);
}
/**
* @return array
*/
public function provideDataToTestThatSetAssertionClassNameWillNotAcceptInvalidAssertionClasses()
{
return [
'null' => [null],
'string' => ['foo'],
'array' => [[]],
'object' => [new \stdClass()],
'other class' => [__CLASS__],
];
}
}