Completed
Push — master ( 4c18be...d36967 )
by Mike
10:04
created

CompoundTest::testCompoundDoesNotContainType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of phpDocumentor.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Reflection\Types;
15
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * @coversDefaultClass \phpDocumentor\Reflection\Types\Compound
20
 */
21
final class CompoundTest extends TestCase
22
{
23
    /**
24
     * @covers ::__construct
25
     */
26
    public function testCompoundCannotBeConstructedFromType() : void
27
    {
28
        $this->expectException(\TypeError::class);
29
        new Compound(['foo']);
0 ignored issues
show
Documentation introduced by
array('foo') is of type array<integer,string,{"0":"string"}>, but the function expects a array<integer,object<php...entor\Reflection\Type>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30
    }
31
32
    /**
33
     * @uses \phpDocumentor\Reflection\Types\Compound::__construct
34
     * @uses \phpDocumentor\Reflection\Types\Compound::has
35
     * @uses \phpDocumentor\Reflection\Types\Integer
36
     *
37
     * @covers ::get
38
     */
39
    public function testCompoundGetType() : void
40
    {
41
        $integer = new Integer();
42
43
        $this->assertSame($integer, (new Compound([$integer]))->get(0));
44
    }
45
46
    /**
47
     * @uses \phpDocumentor\Reflection\Types\Compound::__construct
48
     * @uses \phpDocumentor\Reflection\Types\Compound::has
49
     *
50
     * @covers ::get
51
     */
52
    public function testCompoundGetNotExistingType() : void
53
    {
54
        $this->assertNull((new Compound([]))->get(0));
55
    }
56
57
    /**
58
     * @uses \phpDocumentor\Reflection\Types\Compound::__construct
59
     * @uses \phpDocumentor\Reflection\Types\Integer
60
     *
61
     * @covers ::has
62
     */
63
    public function testCompoundHasIndex() : void
64
    {
65
        $this->assertTrue((new Compound([new Integer()]))->has(0));
66
    }
67
68
    /**
69
     * @uses \phpDocumentor\Reflection\Types\Compound::__construct
70
     *
71
     * @covers ::has
72
     */
73
    public function testCompoundDoesNotHasIndex() : void
74
    {
75
        $this->assertFalse((new Compound([]))->has(0));
76
    }
77
78
    /**
79
     * @uses \phpDocumentor\Reflection\Types\Compound::__construct
80
     * @uses \phpDocumentor\Reflection\Types\Integer
81
     *
82
     * @covers ::contains
83
     */
84
    public function testCompoundContainsType() : void
85
    {
86
        $this->assertTrue((new Compound([new Integer()]))->contains(new Integer()));
87
    }
88
89
    /**
90
     * @uses \phpDocumentor\Reflection\Types\Compound::__construct
91
     * @uses \phpDocumentor\Reflection\Types\Integer
92
     * @uses \phpDocumentor\Reflection\Types\String_
93
     *
94
     * @covers ::contains
95
     */
96
    public function testCompoundDoesNotContainType() : void
97
    {
98
        $this->assertFalse((new Compound([new Integer()]))->contains(new String_()));
99
    }
100
101
    /**
102
     * @uses \phpDocumentor\Reflection\Types\Integer
103
     * @uses \phpDocumentor\Reflection\Types\Boolean
104
     *
105
     * @covers ::__construct
106
     * @covers ::__toString
107
     */
108
    public function testCompoundCanBeConstructedAndStringifiedCorrectly() : void
109
    {
110
        $this->assertSame('int|bool', (string) (new Compound([new Integer(), new Boolean()])));
111
    }
112
113
    /**
114
     * @uses \phpDocumentor\Reflection\Types\Integer
115
     * @uses \phpDocumentor\Reflection\Types\Boolean
116
     *
117
     * @covers ::__construct
118
     * @covers ::__toString
119
     */
120
    public function testCompoundDoesNotContainDuplicates() : void
121
    {
122
        $compound = new Compound([new Integer(), new Integer(), new Boolean()]);
123
124
        $this->assertCount(2, iterator_to_array($compound));
125
        $this->assertSame('int|bool', (string) $compound);
126
    }
127
128
    /**
129
     * @uses \phpDocumentor\Reflection\Types\Compound::__construct
130
     * @uses \phpDocumentor\Reflection\Types\Integer
131
     * @uses \phpDocumentor\Reflection\Types\Boolean
132
     *
133
     * @covers ::getIterator
134
     */
135
    public function testCompoundCanBeIterated() : void
136
    {
137
        $types = [new Integer(), new Boolean()];
138
139
        foreach (new Compound($types) as $index => $type) {
140
            $this->assertSame($types[$index], $type);
141
        }
142
    }
143
}
144