Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

Builder/Reflector/ArgumentAssemblerTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
    }
91
92
    /**
93
     * @param string $name
94
     * @return Argument
95
     */
96
    protected function givenAnArgumentReflectorWithNameAndType(string $name, Type $type)
97
    {
98
        $argument = new Argument($name, $type);
99
100
        return $argument;
101
    }
102
}
103