Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

Builder/Reflector/MethodAssemblerTest.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-2012 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
namespace phpDocumentor\Descriptor\Builder\Reflector;
14
15
use phpDocumentor\Descriptor\ArgumentDescriptor;
16
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
17
use phpDocumentor\Reflection\DocBlock;
18
use phpDocumentor\Reflection\ClassReflector\MethodReflector;
19
use Mockery as m;
20
21
class MethodAssemblerTest extends \PHPUnit_Framework_TestCase
22
{
23
    /** @var MethodAssembler $fixture */
24
    protected $fixture;
25
26
    /** @var ArgumentAssembler|m\MockInterface */
27
    protected $argumentAssemblerMock;
28
29
    /** @var ProjectDescriptorBuilder|m\MockInterface */
30
    protected $builderMock;
31
32
    /**
33
     * Creates a new fixture to test with.
34
     */
35
    protected function setUp()
36
    {
37
        $this->builderMock = m::mock('phpDocumentor\Descriptor\ProjectDescriptorBuilder');
38
        $this->builderMock->shouldReceive('buildDescriptor')->andReturn(null);
39
40
        $this->argumentAssemblerMock = m::mock('phpDocumentor\Descriptor\Builder\Reflector\ArgumentAssembler');
41
        $this->argumentAssemblerMock->shouldReceive('getBuilder')->andReturn($this->builderMock);
42
43
        $this->fixture = new MethodAssembler($this->argumentAssemblerMock);
44
        $this->fixture->setBuilder($this->builderMock);
45
    }
46
47
    /**
48
     * @covers phpDocumentor\Descriptor\Builder\Reflector\MethodAssembler::__construct
49
     * @covers phpDocumentor\Descriptor\Builder\Reflector\MethodAssembler::create
50
     * @covers phpDocumentor\Descriptor\Builder\Reflector\MethodAssembler::mapReflectorToDescriptor
51
     * @covers phpDocumentor\Descriptor\Builder\Reflector\MethodAssembler::addArguments
52
     * @covers phpDocumentor\Descriptor\Builder\Reflector\MethodAssembler::addArgument
53
     * @covers phpDocumentor\Descriptor\Builder\Reflector\MethodAssembler::addVariadicArgument
54
     */
55
    public function testCreateMethodDescriptorFromReflector()
56
    {
57
        // Arrange
58
        $namespace    = 'Namespace';
59
        $methodName   = 'goodbyeWorld';
60
        $argumentName = 'waveHand';
61
62
        $argumentDescriptorMock = $this->givenAnArgumentWithName($argumentName);
63
        $methodReflectorMock = $this->givenAMethodReflector(
64
            $namespace,
65
            $methodName,
66
            $argumentDescriptorMock,
67
            $this->givenADocBlockObject()
68
        );
69
70
        // Act
71
        $descriptor = $this->fixture->create($methodReflectorMock);
72
73
        // Assert
74
        $expectedFqsen = $namespace . '\\' . $methodName . '()';
75
        $this->assertSame($expectedFqsen, $descriptor->getFullyQualifiedStructuralElementName());
76
        $this->assertSame($methodName, $descriptor->getName());
77
        $this->assertSame('protected', $descriptor->getVisibility());
78
        $this->assertSame(false, $descriptor->isFinal());
79
        $this->assertSame(false, $descriptor->isAbstract());
80
        $this->assertSame(false, $descriptor->isStatic());
81
82
        $argument = $descriptor->getArguments()->get($argumentName);
83
        $this->assertSame($argument, $argumentDescriptorMock);
84
    }
85
86
    /**
87
     * Creates a sample method reflector for the tests with the given data.
88
     *
89
     * @param string                             $namespace
90
     * @param string                             $methodName
91
     * @param ArgumentDescriptor|m\MockInterface $argumentMock
92
     * @param DocBlock|m\MockInterface           $docBlockMock
93
     *
94
     * @return MethodReflector|m\MockInterface
95
     */
96
    protected function givenAMethodReflector($namespace, $methodName, $argumentMock, $docBlockMock)
97
    {
98
        $methodReflectorMock = m::mock('phpDocumentor\Reflection\MethodReflector');
99
        $methodReflectorMock->shouldReceive('getName')->andReturn($namespace . '\\' . $methodName);
100
        $methodReflectorMock->shouldReceive('getShortName')->andReturn($methodName);
101
        $methodReflectorMock->shouldReceive('getNamespace')->andReturn($namespace);
102
        $methodReflectorMock->shouldReceive('getDocBlock')->andReturn($docBlockMock);
103
        $methodReflectorMock->shouldReceive('getLinenumber')->andReturn(128);
104
        $methodReflectorMock->shouldReceive('getArguments')->andReturn(array($argumentMock));
105
        $methodReflectorMock->shouldReceive('getVisibility')->andReturn('protected');
106
        $methodReflectorMock->shouldReceive('isFinal')->andReturn(false);
107
        $methodReflectorMock->shouldReceive('isAbstract')->andReturn(false);
108
        $methodReflectorMock->shouldReceive('isStatic')->andReturn(false);
109
110
        return $methodReflectorMock;
111
    }
112
113
    /**
114
     * Generates a DocBlock object with applicable defaults for these tests.
115
     *
116
     * @return DocBlock|m\MockInterface
117
     */
118
    protected function givenADocBlockObject()
119
    {
120
        $docBlockDescription = new DocBlock\Description('This is an example description');
121
122
        $docBlockMock = m::mock('phpDocumentor\Reflection\DocBlock');
123
        $docBlockMock->shouldReceive('getTags')->andReturn(array());
124
        $docBlockMock->shouldReceive('getShortDescription')->andReturn('This is a example description');
125
        $docBlockMock->shouldReceive('getLongDescription')->andReturn($docBlockDescription);
126
127
        $docBlockMock->shouldReceive('getTagsByName')->andReturnUsing(function ($param) {
0 ignored issues
show
The parameter $param is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
            $tag = m::mock('phpDocumentor\Reflection\DocBlock\Tag');
129
130
            $tag->shouldReceive('isVariadic')->once()->andReturn(true);
131
            $tag->shouldReceive('getVariableName')->andReturn('variableName');
132
133
            return array($tag);
134
        });
135
136
        return $docBlockMock;
137
    }
138
139
    /**
140
     * Prepares a mock Argument with the given name.
141
     *
142
     * @param string $argumentName
143
     *
144
     * @return ArgumentDescriptor|m\MockInterface
145
     */
146
    protected function givenAnArgumentWithName($argumentName)
147
    {
148
        $argumentMock = m::mock('phpDocumentor\Descriptor\ArgumentDescriptor');
149
        $argumentMock->shouldReceive('getName')->andReturn($argumentName);
150
        $argumentMock->shouldReceive('setMethod');
151
        $this->argumentAssemblerMock->shouldReceive('create')->andReturn($argumentMock);
152
153
        return $argumentMock;
154
    }
155
}
156