Completed
Push — master ( f256f0...ce65c0 )
by Jaap
10:05
created

testCreationWithErrorContainingResource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace phpDocumentor\Reflection\DocBlock\Tags;
6
7
use Exception;
8
use InvalidArgumentException;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\InvalidTag
13
 * @covers ::<private>
14
 * @covers ::getName
15
 * @covers ::render
16
 * @covers ::getException
17
 * @covers ::create
18
 */
19
final class InvalidTagTest extends TestCase
20
{
21
    public function testCreationWithoutError() : void
22
    {
23
        $tag = InvalidTag::create('Body', 'name');
24
25
        self::assertSame('name', $tag->getName());
26
        self::assertSame('@name Body', $tag->render());
27
        self::assertNull($tag->getException());
28
    }
29
30
    /**
31
     * @covers ::withError
32
     * @covers ::__toString
33
     */
34
    public function testCreationWithError() : void
35
    {
36
        $exception = new Exception();
37
        $tag       = InvalidTag::create('Body', 'name')->withError($exception);
38
39
        self::assertSame('name', $tag->getName());
40
        self::assertSame('@name Body', $tag->render());
41
        self::assertSame('Body', (string)$tag);
42
        self::assertSame($exception, $tag->getException());
43
    }
44
45
    public function testCreationWithErrorContainingClosure() : void
46
    {
47
        try {
48
            $this->throwExceptionFromClosureWithClosureArgument();
49
        } catch (Exception $e) {
50
            $parentException = new Exception('test', 0, $e);
51
            $tag       = InvalidTag::create('Body', 'name')->withError($parentException);
52
            self::assertSame('name', $tag->getName());
53
            self::assertSame('@name Body', $tag->render());
54
            self::assertSame($parentException, $tag->getException());
55
            self::assertStringStartsWith('(Closure at', $tag->getException()->getPrevious()->getTrace()[0]['args'][0]);
56
            self::assertStringContainsString(__FILE__, $tag->getException()->getPrevious()->getTrace()[0]['args'][0]);
57
            self::assertEquals($parentException, unserialize(serialize($parentException)));
58
        }
59
    }
60
61
    private function throwExceptionFromClosureWithClosureArgument()
62
    {
63
        $function = function() {
64
            throw new InvalidArgumentException();
65
        };
66
67
        $function($function);
68
    }
69
70
    public function testCreationWithErrorContainingResource()
71
    {
72
        try {
73
            $this->throwExceptionWithResourceArgument();
74
        } catch (Exception $e) {
75
            $parentException = new Exception('test', 0, $e);
76
            $tag       = InvalidTag::create('Body', 'name')->withError($parentException);
77
            self::assertSame('name', $tag->getName());
78
            self::assertSame('@name Body', $tag->render());
79
            self::assertSame($parentException, $tag->getException());
80
            self::assertStringStartsWith(
81
                'resource(stream)',
82
                $tag->getException()->getPrevious()->getTrace()[0]['args'][0])
83
            ;
84
            self::assertEquals($parentException, unserialize(serialize($parentException)));
85
        }
86
    }
87
88
    private function throwExceptionWithResourceArgument()
89
    {
90
        $function = function() {
91
            throw new InvalidArgumentException();
92
        };
93
94
        $function(fopen(__FILE__, 'r'));
95
    }
96
}
97