Completed
Push — master ( dd69bf...68ed13 )
by Richard
10s
created

testLazyAssertionExceptionExtendsInvalidArgumentException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 9.6666
1
<?php
2
3
namespace Assert\Tests;
4
5
use Assert\Assert;
6
use Assert\LazyAssertion;
7
use Assert\LazyAssertionException;
8
9
class LazyAssertionTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @test
13
     */
14
    public function it_collects_errors_until_assertall()
0 ignored issues
show
Coding Style introduced by
Method name "LazyAssertionTest::it_collects_errors_until_assertall" is not in camel caps format
Loading history...
15
    {
16
        $this->setExpectedException('Assert\LazyAssertionException', <<<EXC
17
The following 3 assertions failed:
18
1) foo: Value "10" expected to be string, type integer given.
19
2) bar: Value "<NULL>" is empty, but non empty value was expected.
20
3) baz: Value "string" is not an array.
21
22
EXC
23
        );
24
25
        Assert::lazy()
26
            ->that(10, 'foo')->string()
27
            ->that(null, 'bar')->notEmpty()
28
            ->that('string', 'baz')->isArray()
29
            ->verifyNow();
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function it_skips_assertions_of_current_chain_after_failure()
0 ignored issues
show
Coding Style introduced by
Method name "LazyAssertionTest::it_skips_assertions_of_current_chain_after_failure" is not in camel caps format
Loading history...
36
    {
37
        $this->setExpectedException('Assert\LazyAssertionException', <<<EXC
38
The following 1 assertions failed:
39
1) foo: Value "<NULL>" is empty, but non empty value was expected.
40
41
EXC
42
        );
43
44
        Assert::lazy()
45
            ->that(null, 'foo')->notEmpty()->string()
46
            ->verifyNow();
47
    }
48
49
    public function testLazyAssertionExceptionCanReturnAllErrors()
50
    {
51
        try {
52
            Assert::lazy()
53
                ->that(10, 'foo')->string()
54
                ->that(null, 'bar')->notEmpty()
55
                ->that('string', 'baz')->isArray()
56
                ->verifyNow();
57
        } catch (LazyAssertionException $ex) {
58
            self::assertEquals(array(
59
                'Value "10" expected to be string, type integer given.',
60
                'Value "<NULL>" is empty, but non empty value was expected.',
61
                'Value "string" is not an array.',
62
            ), array_map(function (\Exception $ex) {
63
                return $ex->getMessage();
64
            }, $ex->getErrorExceptions()));
65
        }
66
    }
67
68
    public function testVerifyNowReturnsTrueIfAssertionsPass()
69
    {
70
        $this->assertTrue(
71
            Assert::lazy()
72
                ->that(2, 'Two')->eq(2)
73
                ->verifyNow()
74
        );
75
    }
76
77
    public function testRestOfChainNotSkippedWhenTryAllUsed()
78
    {
79
        try {
80
            Assert::lazy()
81
                ->that(9.9, 'foo')->tryAll()->integer('must be int')->between(10, 20, 'must be between')
82
                ->verifyNow();
83
        } catch (LazyAssertionException $ex) {
84
            $this->assertEquals(array(
85
                'must be int',
86
                'must be between'
87
            ), array_map(function (\Exception $ex) {
88
                return $ex->getMessage();
89
            }, $ex->getErrorExceptions()));
90
        }
91
    }
92
93
    public function testCallsToThatFollowingTryAllSkipAssertionsAfterFailure()
94
    {
95
        $this->setExpectedException('Assert\LazyAssertionException', <<<EXC
96
The following 1 assertions failed:
97
1) foo: Value "<NULL>" is empty, but non empty value was expected.
98
99
EXC
100
        );
101
102
        Assert::lazy()
103
            ->that(10, 'foo')->tryAll()->integer()
104
            ->that(null, 'foo')->notEmpty()->string()
105
            ->verifyNow();
106
    }
107
108
    public function testCallsToThatWithTryAllWithMultipleAssertionsAllGetReported()
109
    {
110
        $this->setExpectedException('\Assert\LazyAssertionException', <<<EXC
111
The following 4 assertions failed:
112
1) foo: Value "10" is not a float.
113
2) foo: Provided "10" is not greater than "100".
114
3) foo: Value "<NULL>" is empty, but non empty value was expected.
115
4) foo: Value "<NULL>" expected to be string, type NULL given.
116
117
EXC
118
);
119
        Assert::lazy()
120
            ->that(10, 'foo')->tryAll()->float()->greaterThan(100)
121
            ->that(null, 'foo')->tryAll()->notEmpty()->string()
122
            ->verifyNow();
123
    }
124
125
    public function testCallsToTryAllOnLazyAlwaysReportAllGetReported()
126
    {
127
        $this->setExpectedException('\Assert\LazyAssertionException', <<<EXC
128
The following 4 assertions failed:
129
1) foo: Value "10" is not a float.
130
2) foo: Provided "10" is not greater than "100".
131
3) foo: Value "<NULL>" is empty, but non empty value was expected.
132
4) foo: Value "<NULL>" expected to be string, type NULL given.
133
134
EXC
135
);
136
        Assert::lazy()->tryAll()
137
            ->that(10, 'foo')->float()->greaterThan(100)
138
            ->that(null, 'foo')->notEmpty()->string()
139
            ->verifyNow();
140
    }
141
142
    public function testThatLazyAssertionThrowsCustomExceptionWhenSet()
143
    {
144
        $lazyAssertion = new LazyAssertion();
145
        $lazyAssertion->setExceptionClass('Assert\Tests\CustomLazyAssertionException');
146
147
        $this->setExpectedException('Assert\Tests\CustomLazyAssertionException');
148
        $lazyAssertion
149
            ->that('foo', 'property')->integer()
150
            ->verifyNow()
151
        ;
152
    }
153
154
    /**
155
     * @dataProvider provideDataToTestThatSetExceptionClassWillNotAcceptInvalidExceptionClasses
156
     * @param mixed $exceptionClass
157
     */
158
    public function testThatSetExceptionClassWillNotAcceptInvalidExceptionClasses($exceptionClass)
159
    {
160
        $lazyAssertion = new LazyAssertion();
161
162
        $this->setExpectedException('LogicException');
163
        $lazyAssertion->setExceptionClass($exceptionClass);
164
    }
165
166
    /**
167
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
168
     */
169
    public function provideDataToTestThatSetExceptionClassWillNotAcceptInvalidExceptionClasses()
170
    {
171
        return array(
172
            'null' => array(null),
173
            'string' => array('foo'),
174
            'array' => array(array()),
175
            'object' => array(new \stdClass()),
176
            'other class' => array(__CLASS__),
177
            'other exception' => array('Exception'),
178
        );
179
    }
180
181
    public function testLazyAssertionExceptionExtendsInvalidArgumentException()
182
    {
183
        $this->setExpectedException('\Assert\InvalidArgumentException');
184
185
        Assert::lazy()->tryAll()
186
            ->that(10, 'foo')->float()->greaterThan(100)
187
            ->that(null, 'foo')->notEmpty()->string()
188
            ->verifyNow();
189
    }
190
}
191