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

Transformer/Writer/Xml/PropertyConverterTest.php (2 issues)

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-2018 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\Transformer\Writer\Xml;
13
14
use Mockery as m;
15
use phpDocumentor\Descriptor\DescriptorAbstract;
16
use phpDocumentor\Descriptor\PropertyDescriptor;
17
18
/**
19
 * Test class for \phpDocumentor\Transformer\Writer\Xml\PropertyConverter.
20
 *
21
 * @covers phpDocumentor\Transformer\Writer\Xml\PropertyConverter
22
 */
23
class PropertyConverterTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
24
{
25
    /**
26
     * Tests whether the XML Element representing a property is properly created.
27
     *
28
     * @covers phpDocumentor\Transformer\Writer\Xml\PropertyConverter::convert
29
     */
30
    public function testPropertyXmlElementIsCreated()
31
    {
32
        // Arrange
33
        $property = $this->createPropertyDescriptorMock();
34
        $propertyConverter = $this->createFixture($property);
35
        $parent = $this->prepareParentXMLElement();
36
        $parent->setAttribute('namespace', 'phpDocumentor');
37
38
        // Act
39
        $convertedElement = $propertyConverter->convert($parent, $property);
0 ignored issues
show
$property is of type object<Mockery\MockInter...tor\DescriptorAbstract>, but the function expects a object<phpDocumentor\Des...tor\PropertyDescriptor>.

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...
40
41
        // Assert
42
        $this->assertSame('false', $convertedElement->getAttribute('static'));
43
        $this->assertSame('protected', $convertedElement->getAttribute('visibility'));
44
        $this->assertSame('100', $convertedElement->getAttribute('line'));
45
        $this->assertSame('phpDocumentor', $convertedElement->getAttribute('namespace'));
46
        $this->assertSame('$Property', $convertedElement->getElementsByTagName('name')->item(0)->nodeValue);
47
        $this->assertSame(
48
            'phpDocumentor\Class::$Property',
49
            $convertedElement->getElementsByTagName('full_name')->item(0)->nodeValue
50
        );
51
        $this->assertSame('defaultString', $convertedElement->getElementsByTagName('default')->item(0)->nodeValue);
52
    }
53
54
    /**
55
     * Tests whether the XML Element representing a property is properly created.
56
     *
57
     * @covers phpDocumentor\Transformer\Writer\Xml\PropertyConverter::convert
58
     */
59
    public function testNamespaceNameIsTakenFromNamespaceDescriptorIfPresent()
60
    {
61
        // Arrange
62
        $parent = $this->prepareParentXMLElement();
63
        $namespaceDescriptor = m::mock('phpDocumentor\Descriptor\NamespaceDescriptor');
64
        $namespaceDescriptor->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('MySpace');
65
        $property = $this->createPropertyDescriptorMock();
66
        $property->shouldReceive('getNamespace')->andReturn($namespaceDescriptor);
67
        $propertyConverter = $this->createFixture($property);
68
69
        // Act
70
        $convertedElement = $propertyConverter->convert($parent, $property);
0 ignored issues
show
$property is of type object<Mockery\MockInter...tor\DescriptorAbstract>, but the function expects a object<phpDocumentor\Des...tor\PropertyDescriptor>.

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...
71
72
        // Assert
73
        $this->assertSame('MySpace', $convertedElement->getAttribute('namespace'));
74
    }
75
76
    /**
77
     * Creates an XML Element that can serve as parent.
78
     *
79
     * @return \DOMElement
80
     */
81
    protected function prepareParentXMLElement()
82
    {
83
        $document = new \DOMDocument();
84
        $parent = new \DOMElement('class');
85
        $document->appendChild($parent);
86
87
        return $parent;
88
    }
89
90
    /**
91
     * Creates a mock for the PropertyDescriptor class.
92
     *
93
     * @return m\MockInterface|DescriptorAbstract
94
     */
95
    protected function createPropertyDescriptorMock()
96
    {
97
        $property = m::mock('phpDocumentor\\Descriptor\\PropertyDescriptor');
98
        $property->shouldReceive('getLine')->andReturn(100);
99
        $property->shouldReceive('isStatic')->andReturn(false);
100
        $property->shouldReceive('getVisibility')->andReturn('protected');
101
        $property->shouldReceive('getName')->andReturn('Property');
102
        $property->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('phpDocumentor\Class::$Property');
103
        $property->shouldReceive('getDefault')->andReturn('defaultString');
104
        $property->shouldIgnoreMissing();
105
106
        return $property;
107
    }
108
109
    /**
110
     * Creates the PropertyConverter fixture with a DocBlock mock.
111
     *
112
     * @return PropertyConverter
113
     */
114
    protected function createFixture(PropertyDescriptor $property)
115
    {
116
        $docBlockConverter = m::mock('phpDocumentor\Transformer\Writer\Xml\DocBlockConverter');
117
        $docBlockConverter->shouldReceive('convert')->with(m::type('DOMElement'), $property);
118
119
        return new PropertyConverter($docBlockConverter);
120
    }
121
}
122