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

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

a method exists on all of the called types.

Bug Major

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);
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');
0 ignored issues
show
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
65
        $property = $this->createPropertyDescriptorMock();
66
        $property->shouldReceive('getNamespace')->andReturn($namespaceDescriptor);
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\DescriptorAbstract.

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...
67
        $propertyConverter = $this->createFixture($property);
68
69
        // Act
70
        $convertedElement = $propertyConverter->convert($parent, $property);
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);
0 ignored issues
show
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
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