Completed
Push — develop ( 0e706a...50003e )
by Mike
08:24
created

testIfVariadicArgumentsAreDetected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @author    Mike van Riel <[email protected]>
8
 * @author    Sven Hagemann <[email protected]>
9
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
10
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Descriptor\Builder\Reflector;
15
16
use Mockery as m;
17
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
18
use phpDocumentor\Reflection\Php\Argument;
19
use phpDocumentor\Reflection\Type;
20
use phpDocumentor\Reflection\Types\Boolean;
21
22
/**
23
 * Test class for phpDocumentor\Descriptor\Builder\Reflector\ArgumentAssembler
24
 */
25
class ArgumentAssemblerTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
26
{
27
    /** @var ArgumentAssembler $fixture */
28
    protected $fixture;
29
30
    /** @var ProjectDescriptorBuilder|m\MockInterface */
31
    protected $builderMock;
32
33
    /**
34
     * Creates a new fixture to test with.
35
     */
36
    protected function setUp()
37
    {
38
        $this->builderMock = m::mock('phpDocumentor\Descriptor\ProjectDescriptorBuilder');
39
        $this->fixture = new ArgumentAssembler();
40
        $this->fixture->setBuilder($this->builderMock);
41
    }
42
43
    /**
44
     * @covers \phpDocumentor\Descriptor\Builder\Reflector\ArgumentAssembler::create
45
     */
46
    public function testCreateArgumentDescriptorFromReflector()
47
    {
48
        // Arrange
49
        $name = 'goodArgument';
50
        $type = new Boolean();
51
52
        $argumentReflectorMock = $this->givenAnArgumentReflectorWithNameAndType($name, $type);
53
54
        // Act
55
        $descriptor = $this->fixture->create($argumentReflectorMock);
56
57
        // Assert
58
        $this->assertSame($name, $descriptor->getName());
59
        $this->assertSame($type, $descriptor->getType());
60
        $this->assertNull($descriptor->getDefault());
61
        $this->assertFalse($descriptor->isByReference());
62
    }
63
64
    /**
65
     * @covers \phpDocumentor\Descriptor\Builder\Reflector\ArgumentAssembler::create
66
     * @covers \phpDocumentor\Descriptor\Builder\Reflector\ArgumentAssembler::overwriteTypeAndDescriptionFromParamTag
67
     */
68
    public function testIfTypeAndDescriptionAreSetFromParamDescriptor()
69
    {
70
        // Arrange
71
        $name = 'goodArgument';
72
        $type = new Boolean();
73
74
        $argumentReflectorMock = $this->givenAnArgumentReflectorWithNameAndType($name, $type);
75
76
        // Mock a paramDescriptor
77
        $paramDescriptorTagMock = m::mock('phpDocumentor\Descriptor\Tag\ParamDescriptor');
78
        $paramDescriptorTagMock->shouldReceive('getVariableName')->once()->andReturn($name);
79
        $paramDescriptorTagMock->shouldReceive('getDescription')->once()->andReturn('Is this a good argument, or nah?');
80
        $paramDescriptorTagMock->shouldReceive('getType')->once()->andReturn($type);
81
82
        // Act
83
        $descriptor = $this->fixture->create($argumentReflectorMock, [$paramDescriptorTagMock]);
0 ignored issues
show
Documentation introduced by Sven Hagemann
array($paramDescriptorTagMock) is of type array<integer,object<Moc...kery\\MockInterface>"}>, but the function expects a array<integer,object<php...r\Tag\ParamDescriptor>>.

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...
84
85
        // Assert
86
        $this->assertSame($name, $descriptor->getName());
87
        $this->assertSame($type, $descriptor->getType());
88
        $this->assertNull($descriptor->getDefault());
89
        $this->assertFalse($descriptor->isByReference());
90
        $this->assertFalse($descriptor->isVariadic());
91
    }
92
93
    /**
94
     * @covers \phpDocumentor\Descriptor\Builder\Reflector\ArgumentAssembler::create
95
     */
96
    public function testIfVariadicArgumentsAreDetected()
97
    {
98
        // Arrange
99
        $name = 'goodArgument';
100
        $type = new Boolean();
101
102
        $argumentReflectorMock = $this->givenAnArgumentReflectorWithNameAndType($name, $type, true);
103
104
        // Mock a paramDescriptor
105
        $paramDescriptorTagMock = m::mock('phpDocumentor\Descriptor\Tag\ParamDescriptor');
106
        $paramDescriptorTagMock->shouldReceive('getVariableName')->once()->andReturn($name);
107
        $paramDescriptorTagMock->shouldReceive('getDescription')->once()->andReturn('Is this a good argument, or nah?');
108
        $paramDescriptorTagMock->shouldReceive('getType')->once()->andReturn($type);
109
110
        // Act
111
        $descriptor = $this->fixture->create($argumentReflectorMock, [$paramDescriptorTagMock]);
0 ignored issues
show
Documentation introduced by Mike van Riel
array($paramDescriptorTagMock) is of type array<integer,object<Moc...kery\\MockInterface>"}>, but the function expects a array<integer,object<php...r\Tag\ParamDescriptor>>.

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...
112
113
        // Assert
114
        $this->assertSame($name, $descriptor->getName());
115
        $this->assertSame($type, $descriptor->getType());
116
        $this->assertNull($descriptor->getDefault());
117
        $this->assertFalse($descriptor->isByReference());
118
        $this->assertTrue($descriptor->isVariadic());
119
    }
120
121
    protected function givenAnArgumentReflectorWithNameAndType(
122
        string $name,
123
        Type $type,
124
        bool $isVariadic = false
125
    ): Argument {
126
        return new Argument($name, $type, null, false, $isVariadic);
127
    }
128
}
129