PropertyConverterTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testPropertyXmlElementIsCreated() 0 23 1
A testNamespaceNameIsTakenFromNamespaceDescriptorIfPresent() 0 16 1
A prepareParentXMLElement() 0 8 1
A createPropertyDescriptorMock() 0 13 1
A createFixture() 0 7 1
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
Documentation introduced by
$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');
0 ignored issues
show
Bug introduced by
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
Bug introduced by
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);
0 ignored issues
show
Documentation introduced by
$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);
0 ignored issues
show
Bug introduced by
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