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

Transformer/Writer/Xml/MethodConverterTest.php (6 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\ArgumentDescriptor;
16
use phpDocumentor\Descriptor\DescriptorAbstract;
17
use phpDocumentor\Descriptor\MethodDescriptor;
18
19
/**
20
 * Test class for \phpDocumentor\Transformer\Writer\Xml\MethodConverter.
21
 *
22
 * @covers phpDocumentor\Transformer\Writer\Xml\MethodConverter
23
 */
24
class MethodConverterTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
25
{
26
    /**
27
     * Tests whether the XML Element representing a method is properly created.
28
     *
29
     * @covers phpDocumentor\Transformer\Writer\Xml\MethodConverter::convert
30
     */
31
    public function testMethodXmlElementIsCreated()
32
    {
33
        // Arrange
34
        $parent = $this->prepareParentXMLElement();
35
        $parent->setAttribute('namespace', 'phpDocumentor');
36
        $argumentDescriptor = m::mock('phpDocumentor\Descriptor\ArgumentDescriptor');
37
        $method = $this->createMethodDescriptorMock();
38
        $method->shouldReceive('getArguments')->andReturn([$argumentDescriptor]);
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...
39
        $methodConverter = $this->createFixture($method, $argumentDescriptor);
40
41
        // Act
42
        $convertedElement = $methodConverter->convert($parent, $method);
0 ignored issues
show
$method is of type object<Mockery\MockInter...tor\DescriptorAbstract>, but the function expects a object<phpDocumentor\Descriptor\MethodDescriptor>.

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...
43
44
        // Assert
45
        $this->assertSame('false', $convertedElement->getAttribute('final'));
46
        $this->assertSame('true', $convertedElement->getAttribute('abstract'));
47
        $this->assertSame('false', $convertedElement->getAttribute('static'));
48
        $this->assertSame('protected', $convertedElement->getAttribute('visibility'));
49
        $this->assertSame('100', $convertedElement->getAttribute('line'));
50
        $this->assertSame('phpDocumentor', $convertedElement->getAttribute('namespace'));
51
        $this->assertSame('Method', $convertedElement->getElementsByTagName('name')->item(0)->nodeValue);
52
        $this->assertSame(
53
            'phpDocumentor\Class::Method()',
54
            $convertedElement->getElementsByTagName('full_name')->item(0)->nodeValue
55
        );
56
    }
57
58
    /**
59
     * Tests whether the XML Element representing a method is properly created.
60
     *
61
     * @covers phpDocumentor\Transformer\Writer\Xml\MethodConverter::convert
62
     */
63
    public function testNamespaceNameIsTakenFromNamespaceDescriptorIfPresent()
64
    {
65
        // Arrange
66
        $parent = $this->prepareParentXMLElement();
67
        $namespaceDescriptor = m::mock('phpDocumentor\Descriptor\NamespaceDescriptor');
68
        $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...
69
        $method = $this->createMethodDescriptorMock();
70
        $method->shouldReceive('getArguments')->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...
71
        $method->shouldReceive('getNamespace')->andReturn($namespaceDescriptor);
72
        $methodConverter = $this->createFixture($method);
73
74
        // Act
75
        $convertedElement = $methodConverter->convert($parent, $method);
0 ignored issues
show
$method is of type object<Mockery\MockInter...tor\DescriptorAbstract>, but the function expects a object<phpDocumentor\Descriptor\MethodDescriptor>.

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...
76
77
        // Assert
78
        $this->assertSame('MySpace', $convertedElement->getAttribute('namespace'));
79
    }
80
81
    /**
82
     * Creates an XML Element that can serve as parent.
83
     *
84
     * @return \DOMElement
85
     */
86
    protected function prepareParentXMLElement()
87
    {
88
        $document = new \DOMDocument();
89
        $parent = new \DOMElement('class');
90
        $document->appendChild($parent);
91
92
        return $parent;
93
    }
94
95
    /**
96
     * Creates a mock for the MethodDescriptor class.
97
     *
98
     * @return m\MockInterface|DescriptorAbstract
99
     */
100
    protected function createMethodDescriptorMock()
101
    {
102
        $method = m::mock('phpDocumentor\\Descriptor\\MethodDescriptor');
103
        $method->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...
104
        $method->shouldReceive('isFinal')->andReturn(false);
105
        $method->shouldReceive('isAbstract')->andReturn(true);
106
        $method->shouldReceive('isStatic')->andReturn(false);
107
        $method->shouldReceive('getVisibility')->andReturn('protected');
108
        $method->shouldReceive('getName')->andReturn('Method');
109
        $method->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('phpDocumentor\Class::Method()');
110
        $method->shouldIgnoreMissing();
111
112
        return $method;
113
    }
114
115
    /**
116
     * Creates the MethodConverter fixture with a DocBlock and ArgumentConverter mock.
117
     *
118
     * @return MethodConverter
119
     */
120
    protected function createFixture(MethodDescriptor $method, ArgumentDescriptor $argumentDescriptor = null)
121
    {
122
        $docBlockConverter = m::mock('phpDocumentor\Transformer\Writer\Xml\DocBlockConverter');
123
        $docBlockConverter->shouldReceive('convert')->with(m::type('DOMElement'), $method);
124
        $argumentConverter = m::mock('phpDocumentor\Transformer\Writer\Xml\ArgumentConverter');
125
        if ($argumentDescriptor) {
126
            $argumentConverter->shouldReceive('convert')->with(m::type('DOMElement'), $argumentDescriptor);
127
        }
128
129
        return new MethodConverter($argumentConverter, $docBlockConverter);
130
    }
131
}
132