Completed
Push — develop ( e58221...7a35e1 )
by Jaap
08:17 queued 01:05
created

Core/Transformer/Writer/Xml/TagConverterTest.php (1 issue)

Labels
Severity

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
 * @copyright 2010-2013 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Plugin\Core\Transformer\Writer\Xml;
13
14
use Mockery as m;
15
use phpDocumentor\Descriptor\TagDescriptor;
16
use phpDocumentor\Reflection\Fqsen;
17
use phpDocumentor\Reflection\Types\Compound;
18
use phpDocumentor\Reflection\Types\Integer;
19
use phpDocumentor\Reflection\Types\Object_;
20
use phpDocumentor\Reflection\Types\String_;
21
22
/**
23
 * Test class for \phpDocumentor\Plugin\Core\Transformer\Writer\Xml\TagConverter.
24
 *
25
 * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml\TagConverter
26
 */
27
class TagConverterTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
28
{
29
    const TEST_LINENUMBER = 100;
30
31
    /**
32
     * Tests whether the information common to all tags is stored on an XML element.
33
     *
34
     * @param string $name              Name of the tag as provided by the Descriptor.
35
     * @param string $description       Description for the tag as provided by the Descriptor.
36
     * @param string $resultName        Expected resulting name in the XML Element.
37
     * @param string $resultDescription Expected resulting description in the XML Element.
38
     *
39
     * @dataProvider provideTestGenericTag
40
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml\TagConverter::convert
41
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml\TagConverter::getDescription
42
     *
43
     * @return void
44
     */
45
    public function testConvertGenericTag($name, $description, $resultName, $resultDescription)
46
    {
47
        // Arrange
48
        $tagConverter = new TagConverter();
49
        $parent       = $this->prepareDocBlockXMLElement();
50
        $tag          = $this->createTagDescriptorMock($name, $description);
51
52
        // Act
53
        $convertedElement = $tagConverter->convert($parent, $tag);
54
55
        // Assert
56
        $this->assertSame($resultName, $convertedElement->getAttribute('name'));
57
        $this->assertSame($resultDescription, $convertedElement->getAttribute('description'));
58
        $this->assertSame((string) self::TEST_LINENUMBER, $convertedElement->getAttribute('line'));
59
    }
60
61
    /**
62
     * Tests whether type information is stored when a tag is processed with type information.
63
     *
64
     * @covers \phpDocumentor\Plugin\Core\Transformer\Writer\Xml\TagConverter::convert
65
     *
66
     * @return void
67
     */
68
    public function testWhetherTypesAreAddedWhenPresent()
69
    {
70
        // Arrange
71
        $tagConverter = new TagConverter();
72
        $parent       = $this->prepareDocBlockXMLElement();
73
        $tag          = $this->createTagDescriptorMock('name', 'description', 'Tag\VarDescriptor');
74
        $tag->shouldReceive('getTypes')->andReturn(
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\TagDescriptor.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
75
            new Compound(array(new String_(), new Integer(), new Object_(new Fqsen('\DateTime'))))
76
        );
77
78
        // Act
79
        $convertedElement = $tagConverter->convert($parent, $tag);
80
81
        // Assert
82
        $types = $convertedElement->getElementsByTagName('type');
83
        $this->assertSame(3, $types->length);
84
        $this->assertSame('string', $types->item(0)->nodeValue);
85
        $this->assertSame('int', $types->item(1)->nodeValue);
86
        $this->assertSame('\DateTime', $types->item(2)->nodeValue);
87
        $this->assertSame('string|int|\DateTime', $convertedElement->getAttribute('type'));
88
    }
89
90
    /**
91
     * Tests whether the variable name is stored for tags containing variable names.
92
     *
93
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml\TagConverter::convert
94
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml\TagConverter::addTypes
95
     *
96
     * @return void
97
     */
98
    public function testWhetherVariableNamesAreAddedWhenPresent()
99
    {
100
        // Arrange
101
        $tagConverter = new TagConverter();
102
        $parent       = $this->prepareDocBlockXMLElement();
103
        $tag          = $this->createTagDescriptorMock('name', 'description', 'Tag\VarDescriptor');
104
        $tag->shouldReceive('getTypes')->andReturn(null);
105
        $tag->shouldReceive('getVariableName')->andReturn('varName');
106
107
        // Act
108
        $convertedElement = $tagConverter->convert($parent, $tag);
109
110
        // Assert
111
        $this->assertSame('varName', $convertedElement->getAttribute('variable'));
112
    }
113
114
    /**
115
     * Tests whether the version number is prepended to the description when version information is available.
116
     *
117
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml\TagConverter::convert
118
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml\TagConverter::getDescription
119
     *
120
     * @todo this should be dealt with in a template and not in the code! This activity should be removed and the
121
     * templates updated.
122
     *
123
     * @return void
124
     */
125
    public function testWhetherTheVersionIsPrependedToTheDescription()
126
    {
127
        // Arrange
128
        $tagConverter = new TagConverter();
129
        $parent       = $this->prepareDocBlockXMLElement();
130
        $tag          = $this->createTagDescriptorMock('name', 'description', 'Tag\VersionDescriptor');
131
        $tag->shouldReceive('getVersion')->andReturn('1.0');
132
133
        // Act
134
        $convertedElement = $tagConverter->convert($parent, $tag);
135
136
        // Assert
137
        $this->assertSame('1.0 description', $convertedElement->getAttribute('description'));
138
    }
139
140
    /**
141
     * Tests whether a reference to another element is stored with the tag when such is present.
142
     *
143
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml\TagConverter::convert
144
     *
145
     * @return void
146
     */
147
    public function testWhetherReferencesAreAddedWhenPresent()
148
    {
149
        // Arrange
150
        $reference = '\DateTime::add()';
151
        $tagConverter = new TagConverter();
152
        $parent       = $this->prepareDocBlockXMLElement();
153
        $tag          = $this->createTagDescriptorMock('name', 'description', 'Tag\UsesDescriptor');
154
        $tag->shouldReceive('getReference')->andReturn($reference);
155
156
        // Act
157
        $convertedElement = $tagConverter->convert($parent, $tag);
158
159
        // Assert
160
        $this->assertSame($reference, $convertedElement->getAttribute('link'));
161
    }
162
163
    /**
164
     * Tests whether a link to a URL is stored with the tag when such is present.
165
     *
166
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml\TagConverter::convert
167
     *
168
     * @return void
169
     */
170
    public function testWhetherLinksAreAddedWhenPresent()
171
    {
172
        // Arrange
173
        $link         = 'http://www.phpdoc.org';
174
        $tagConverter = new TagConverter();
175
        $parent       = $this->prepareDocBlockXMLElement();
176
        $tag          = $this->createTagDescriptorMock('name', 'description', 'Tag\LinkDescriptor');
177
        $tag->shouldReceive('getLink')->andReturn($link);
178
179
        // Act
180
        $convertedElement = $tagConverter->convert($parent, $tag);
181
182
        // Assert
183
        $this->assertSame($link, $convertedElement->getAttribute('link'));
184
    }
185
186
    /**
187
     * Tests whether a method name to another element is stored with the tag when such is present.
188
     *
189
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml\TagConverter::convert
190
     *
191
     * @return void
192
     */
193
    public function testWhetherMethodNamesAreAddedWhenPresent()
194
    {
195
        // Arrange
196
        $methodName   = 'getMethod';
197
        $tagConverter = new TagConverter();
198
        $parent       = $this->prepareDocBlockXMLElement();
199
        $tag          = $this->createTagDescriptorMock('name', 'description', 'Tag\MethodDescriptor');
200
        $tag->shouldReceive('getMethodName')->andReturn($methodName);
201
202
        // Act
203
        $convertedElement = $tagConverter->convert($parent, $tag);
204
205
        // Assert
206
        $this->assertSame($methodName, $convertedElement->getAttribute('method_name'));
207
    }
208
209
    /**
210
     * Provides a test name and description for the generic test.
211
     *
212
     * @see testConvertGenericTag
213
     *
214
     * @return string[][]
215
     */
216
    public function provideTestGenericTag()
217
    {
218
        return array(
219
            array('name', 'description', 'name', 'description'),
220
            array('name&test', 'description&test', 'name&amp;test', 'description&amp;test')
221
        );
222
    }
223
224
    /**
225
     * Creates an XML Element that can serve as parent.
226
     *
227
     * @return \DOMElement
228
     */
229
    protected function prepareDocBlockXMLElement()
230
    {
231
        $document = new \DOMDocument();
232
        $parent   = new \DOMElement('parent');
233
        $document->appendChild($parent);
234
        $parent->setAttribute('line', self::TEST_LINENUMBER);
235
236
        return $parent;
237
    }
238
239
    /**
240
     * Creates a mock for the TagDescriptor class.
241
     *
242
     * @param string $name        The name of the tag.
243
     * @param string $description The description that is present in the tag.
244
     * @param string $class       The descriptor class that is to be mocked
245
     *
246
     * @return m\MockInterface|TagDescriptor
247
     */
248
    protected function createTagDescriptorMock($name, $description, $class = 'TagDescriptor')
249
    {
250
        $tag = m::mock('phpDocumentor\\Descriptor\\' . $class);
251
        $tag->shouldReceive('getName')->andReturn($name);
252
        $tag->shouldReceive('getDescription')->andReturn($description);
253
        $tag->shouldIgnoreMissing();
254
255
        return $tag;
256
    }
257
}
258