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

Transformer/Writer/Xml/TraitConverterTest.php (3 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\MethodDescriptor;
17
use phpDocumentor\Descriptor\PropertyDescriptor;
18
use phpDocumentor\Descriptor\TraitDescriptor;
19
20
/**
21
 * Test class for \phpDocumentor\Transformer\Writer\Xml\TraitConverter.
22
 *
23
 * @covers \phpDocumentor\Transformer\Writer\Xml\TraitConverter
24
 */
25
class TraitConverterTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
26
{
27
    /**
28
     * Tests whether the XML Element representing a trait is properly created.
29
     *
30
     * @covers \phpDocumentor\Transformer\Writer\Xml\TraitConverter::convert
31
     */
32
    public function testTraitXmlElementIsCreated()
33
    {
34
        // Arrange
35
        $methodDescriptor = m::mock('phpDocumentor\Descriptor\MethodDescriptor');
36
        $propertyDescriptor = m::mock('phpDocumentor\Descriptor\PropertyDescriptor');
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
        $trait = $this->createTraitDescriptorMock();
40
        $trait->shouldReceive('getMethods')->andReturn([$methodDescriptor]);
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
        $trait->shouldReceive('getProperties')->andReturn([$propertyDescriptor]);
42
        $trait->shouldReceive('getNamespace')->andReturn($namespaceDescriptor);
43
        $parent = $this->prepareParentXMLElement();
44
        $traitConverter = $this->createFixture($trait, $methodDescriptor, $propertyDescriptor);
45
46
        // Act
47
        $convertedElement = $traitConverter->convert($parent, $trait);
48
49
        // Assert
50
        $this->assertSame('100', $convertedElement->getAttribute('line'));
51
        $this->assertSame('phpDocumentor', $convertedElement->getAttribute('namespace'));
52
        $this->assertSame('Trait', $convertedElement->getElementsByTagName('name')->item(0)->nodeValue);
53
        $this->assertSame(
54
            'phpDocumentor\Trait',
55
            $convertedElement->getElementsByTagName('full_name')->item(0)->nodeValue
56
        );
57
    }
58
59
    /**
60
     * Creates an XML Element that can serve as parent.
61
     *
62
     * @return \DOMElement
63
     */
64
    protected function prepareParentXMLElement()
65
    {
66
        $document = new \DOMDocument();
67
        $parent = new \DOMElement('file');
68
        $document->appendChild($parent);
69
70
        return $parent;
71
    }
72
73
    /**
74
     * Creates a mock for the TraitDescriptor class.
75
     *
76
     * @return m\MockInterface|DescriptorAbstract
77
     */
78
    protected function createTraitDescriptorMock()
79
    {
80
        $trait = m::mock('phpDocumentor\\Descriptor\\TraitDescriptor');
81
        $trait->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...
82
        $trait->shouldReceive('getName')->andReturn('Trait');
83
        $trait->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('phpDocumentor\Trait');
84
        $trait->shouldIgnoreMissing();
85
86
        return $trait;
87
    }
88
89
    /**
90
     * Creates the TraitConverter fixture with a DocBlock mock.
91
     *
92
     * @param MethodDescriptor $method
93
     * @param PropertyDescriptor $property
94
     * @return TraitConverter
95
     */
96
    protected function createFixture(TraitDescriptor $trait, $method, $property)
97
    {
98
        $docBlockConverter = m::mock('phpDocumentor\Transformer\Writer\Xml\DocBlockConverter');
99
        $docBlockConverter->shouldReceive('convert')->with(m::type('DOMElement'), $trait);
100
101
        $methodConverter = m::mock('phpDocumentor\Transformer\Writer\Xml\MethodConverter');
102
        $methodConverter->shouldReceive('convert')->with(m::type('DOMElement'), $method);
103
104
        $propertyConverter = m::mock('phpDocumentor\Transformer\Writer\Xml\PropertyConverter');
105
        $propertyConverter->shouldReceive('convert')->with(m::type('DOMElement'), $property);
106
107
        return new TraitConverter($docBlockConverter, $methodConverter, $propertyConverter);
108
    }
109
}
110