1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpDocumentor. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2010-2017 Mike van Riel<[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @link http://phpdoc.org |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace phpDocumentor\Descriptor\Builder\Reflector; |
14
|
|
|
|
15
|
|
|
use phpDocumentor\Descriptor\ProjectDescriptorBuilder; |
16
|
|
|
use phpDocumentor\Reflection\DocBlock; |
17
|
|
|
use phpDocumentor\Reflection\ClassReflector\PropertyReflector; |
18
|
|
|
use Mockery as m; |
19
|
|
|
|
20
|
|
|
class PropertyAssemblerTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** @var PropertyAssembler $fixture */ |
23
|
|
|
protected $fixture; |
24
|
|
|
|
25
|
|
|
/** @var ProjectDescriptorBuilder|m\MockInterface */ |
26
|
|
|
protected $builderMock; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Creates a new fixture to test with. |
30
|
|
|
*/ |
31
|
|
|
protected function setUp() |
32
|
|
|
{ |
33
|
|
|
$this->builderMock = m::mock('phpDocumentor\Descriptor\ProjectDescriptorBuilder'); |
34
|
|
|
$this->builderMock->shouldReceive('buildDescriptor')->andReturn(null); |
35
|
|
|
|
36
|
|
|
$this->fixture = new PropertyAssembler(); |
37
|
|
|
$this->fixture->setBuilder($this->builderMock); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @covers phpDocumentor\Descriptor\Builder\Reflector\PropertyAssembler::create |
42
|
|
|
*/ |
43
|
|
|
public function testCreatePropertyDescriptorFromReflector() |
44
|
|
|
{ |
45
|
|
|
// Arrange |
46
|
|
|
$namespace = 'Namespace'; |
47
|
|
|
$propertyName = 'property'; |
48
|
|
|
|
49
|
|
|
$propertyReflectorMock = $this->givenAPropertyReflector( |
50
|
|
|
$namespace, |
51
|
|
|
$propertyName, |
52
|
|
|
$this->givenADocBlockObject(true) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
// Act |
56
|
|
|
$descriptor = $this->fixture->create($propertyReflectorMock); |
|
|
|
|
57
|
|
|
|
58
|
|
|
// Assert |
59
|
|
|
$expectedFqsen = $namespace . '\\$' . $propertyName; |
60
|
|
|
$this->assertSame($expectedFqsen, $descriptor->getFullyQualifiedStructuralElementName()); |
61
|
|
|
$this->assertSame($propertyName, $descriptor->getName()); |
62
|
|
|
$this->assertSame('\\' . $namespace, $descriptor->getNamespace()); |
63
|
|
|
$this->assertSame('protected', $descriptor->getVisibility()); |
64
|
|
|
$this->assertSame(false, $descriptor->isStatic()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Creates a sample property reflector for the tests with the given data. |
69
|
|
|
* |
70
|
|
|
* @param string $namespace |
71
|
|
|
* @param string $propertyName |
72
|
|
|
* @param DocBlock|m\MockInterface $docBlockMock |
73
|
|
|
* |
74
|
|
|
* @return PropertyReflector|m\MockInterface |
75
|
|
|
*/ |
76
|
|
|
protected function givenAPropertyReflector($namespace, $propertyName, $docBlockMock = null) |
77
|
|
|
{ |
78
|
|
|
$propertyReflectorMock = m::mock('phpDocumentor\Reflection\PropertyReflector'); |
79
|
|
|
$propertyReflectorMock->shouldReceive('getName')->andReturn($namespace . '\\$' . $propertyName); |
80
|
|
|
$propertyReflectorMock->shouldReceive('getShortName')->andReturn($propertyName); |
81
|
|
|
$propertyReflectorMock->shouldReceive('getNamespace')->andReturn($namespace); |
82
|
|
|
$propertyReflectorMock->shouldReceive('getDocBlock')->andReturn($docBlockMock); |
83
|
|
|
$propertyReflectorMock->shouldReceive('getLinenumber')->andReturn(128); |
84
|
|
|
$propertyReflectorMock->shouldReceive('getVisibility')->andReturn('protected'); |
85
|
|
|
$propertyReflectorMock->shouldReceive('getDefault')->andReturn(null); |
86
|
|
|
$propertyReflectorMock->shouldReceive('isStatic')->andReturn(false); |
87
|
|
|
|
88
|
|
|
return $propertyReflectorMock; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Generates a DocBlock object with applicable defaults for these tests. |
93
|
|
|
* |
94
|
|
|
* @return DocBlock|m\MockInterface |
95
|
|
|
*/ |
96
|
|
|
protected function givenADocBlockObject($withTags) |
97
|
|
|
{ |
98
|
|
|
$docBlockDescription = new DocBlock\Description('This is an example description'); |
99
|
|
|
|
100
|
|
|
$docBlockMock = m::mock('phpDocumentor\Reflection\DocBlock'); |
101
|
|
|
$docBlockMock->shouldReceive('getTags')->andReturn(array()); |
102
|
|
|
$docBlockMock->shouldReceive('getShortDescription')->andReturn('This is a example description'); |
103
|
|
|
$docBlockMock->shouldReceive('getLongDescription')->andReturn($docBlockDescription); |
104
|
|
|
|
105
|
|
|
if ($withTags) { |
106
|
|
|
$docBlockMock->shouldReceive('getTagsByName')->andReturnUsing(function ($param) { |
|
|
|
|
107
|
|
|
$tag = m::mock('phpDocumentor\Reflection\DocBlock\Tag'); |
108
|
|
|
|
109
|
|
|
$tag->shouldReceive('isVariadic')->once()->andReturn(true); |
110
|
|
|
$tag->shouldReceive('getVariableName')->andReturn('variableName'); |
111
|
|
|
$tag->shouldReceive('getTypes')->andReturn(array()); |
112
|
|
|
$tag->shouldReceive('getDescription'); |
113
|
|
|
|
114
|
|
|
return array($tag); |
115
|
|
|
}); |
116
|
|
|
} else { |
117
|
|
|
$docBlockMock->shouldReceive('getTagsByName')->andReturn(array()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $docBlockMock; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.