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

Transformer/Writer/Xml/InterfaceConverterTest.php (4 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\ConstantDescriptor;
16
use phpDocumentor\Descriptor\DescriptorAbstract;
17
use phpDocumentor\Descriptor\InterfaceDescriptor;
18
use phpDocumentor\Descriptor\MethodDescriptor;
19
20
/**
21
 * Test class for \phpDocumentor\Transformer\Writer\Xml\InterfaceConverter.
22
 *
23
 * @covers phpDocumentor\Transformer\Writer\Xml\InterfaceConverter
24
 */
25
class InterfaceConverterTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
26
{
27
    /**
28
     * Tests whether the XML Element representing a interface is properly created.
29
     *
30
     * @covers phpDocumentor\Transformer\Writer\Xml\InterfaceConverter::convert
31
     */
32
    public function testInterfaceXmlElementIsCreated()
33
    {
34
        // Arrange
35
        $methodDescriptor = m::mock('phpDocumentor\Descriptor\MethodDescriptor');
36
        $constantDescriptor = m::mock('phpDocumentor\Descriptor\ConstantDescriptor');
37
        $namespaceDescriptor = m::mock('phpDocumentor\Descriptor\NamespaceDescriptor');
38
        $namespaceDescriptor->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('phpDocumentor');
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...
39
        $interface = $this->createInterfaceDescriptorMock();
40
        $interface->shouldReceive('getParent')->andReturn([]);
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...
41
        $interface->shouldReceive('getMethods')->andReturn([$methodDescriptor]);
42
        $interface->shouldReceive('getConstants')->andReturn([$constantDescriptor]);
43
        $interface->shouldReceive('getNamespace')->andReturn($namespaceDescriptor);
44
        $parent = $this->prepareParentXMLElement();
45
        $interfaceConverter = $this->createFixture($interface, $methodDescriptor, $constantDescriptor);
46
47
        // Act
48
        $convertedElement = $interfaceConverter->convert($parent, $interface);
0 ignored issues
show
$interface is of type object<Mockery\MockInter...tor\DescriptorAbstract>, but the function expects a object<phpDocumentor\Des...or\InterfaceDescriptor>.

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...
49
50
        // Assert
51
        $this->assertSame('100', $convertedElement->getAttribute('line'));
52
        $this->assertSame('phpDocumentor', $convertedElement->getAttribute('namespace'));
53
        $this->assertSame('Interface', $convertedElement->getElementsByTagName('name')->item(0)->nodeValue);
54
        $this->assertSame(
55
            'phpDocumentor\Interface',
56
            $convertedElement->getElementsByTagName('full_name')->item(0)->nodeValue
57
        );
58
    }
59
60
    /**
61
     * Creates an XML Element that can serve as parent.
62
     *
63
     * @return \DOMElement
64
     */
65
    protected function prepareParentXMLElement()
66
    {
67
        $document = new \DOMDocument();
68
        $parent = new \DOMElement('file');
69
        $document->appendChild($parent);
70
71
        return $parent;
72
    }
73
74
    /**
75
     * Creates a mock for the InterfaceDescriptor class.
76
     *
77
     * @return m\MockInterface|DescriptorAbstract
78
     */
79
    protected function createInterfaceDescriptorMock()
80
    {
81
        $interface = m::mock('phpDocumentor\\Descriptor\\InterfaceDescriptor');
82
        $interface->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...
83
        $interface->shouldReceive('getName')->andReturn('Interface');
84
        $interface->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('phpDocumentor\Interface');
85
        $interface->shouldIgnoreMissing();
86
87
        return $interface;
88
    }
89
90
    /**
91
     * Creates the InterfaceConverter fixture with a DocBlock mock.
92
     *
93
     * @param MethodDescriptor $method
94
     * @param ConstantDescriptor $constant
95
     * @return InterfaceConverter
96
     */
97
    protected function createFixture(InterfaceDescriptor $interface, $method, $constant)
98
    {
99
        $docBlockConverter = m::mock('phpDocumentor\Transformer\Writer\Xml\DocBlockConverter');
100
        $docBlockConverter->shouldReceive('convert')->with(m::type('DOMElement'), $interface);
101
102
        $methodConverter = m::mock('phpDocumentor\Transformer\Writer\Xml\MethodConverter');
103
        $methodConverter->shouldReceive('convert')->with(m::type('DOMElement'), $method);
104
105
        $constantConverter = m::mock('phpDocumentor\Transformer\Writer\Xml\ConstantConverter');
106
        $constantConverter->shouldReceive('convert')->with(m::type('DOMElement'), $constant);
107
108
        return new InterfaceConverter($docBlockConverter, $methodConverter, $constantConverter);
109
    }
110
}
111