Completed
Push — develop ( a89061...54507c )
by Jaap
08:53
created

Plugin/Core/Transformer/Writer/XmlTest.php (11 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
/**
4
 * phpDocumentor
5
 *
6
 * PHP Version 5.3
7
 *
8
 * @author    Mike van Riel <[email protected]>
9
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
10
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Plugin\Core\Transformer\Writer;
15
16
use Mockery as m;
17
use org\bovigo\vfs\vfsStream;
18
19
use org\bovigo\vfs\vfsStreamDirectory;
20
use phpDocumentor\Descriptor\ProjectDescriptor;
21
use phpDocumentor\Transformer\Router\RouterAbstract;
22
use phpDocumentor\Translator\Translator;
23
24
/**
25
 * Test class for \phpDocumentor\Plugin\Core\Transformer\Writer\Xml.
26
 *
27
 * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml
28
 */
29
class XmlTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
30
{
31
    /** @var Xml $xml */
32
    protected $xml;
33
34
    /** @var m\MockInterface|RouterAbstract */
35
    protected $routerMock;
36
37
    /** @var m\MockInterface|Translator */
38
    private $translator;
39
40
    /** @var m\MockInterface|ProjectDescriptor */
41
    protected $projectDescriptor;
42
43
    /** @var vfsStreamDirectory */
44
    protected $fs;
45
46
    /**
47
     * Sets up the test suite
48
     */
49
    protected function setUp()
50
    {
51
        $this->fs = vfsStream::setup('XmlTest');
52
        $this->translator = m::mock('phpDocumentor\Translator\Translator');
53
        $this->projectDescriptor = m::mock('phpDocumentor\Descriptor\ProjectDescriptor');
54
        $this->routerMock = m::mock('phpDocumentor\Transformer\Router\RouterAbstract');
55
        $this->xml = new Xml($this->routerMock);
56
        $this->xml->setTranslator($this->translator);
57
    }
58
59
    /**
60
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml::transform
61
     */
62
    public function testTransformWithoutFiles()
63
    {
64
        $transformer = m::mock('phpDocumentor\Transformer\Transformer');
65
        $transformation = m::mock('phpDocumentor\Transformer\Transformation');
66
        $transformation->shouldReceive('getTransformer->getTarget')->andReturn(vfsStream::url('XmlTest'));
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...
67
        $transformation->shouldReceive('getArtifact')->andReturn('artifact.xml');
68
        $transformation->shouldReceive('getTransformer')->andReturn($transformer);
69
70
        $this->projectDescriptor->shouldReceive('getFiles')->andReturn([]);
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\ProjectDescriptor.

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
        $this->projectDescriptor->shouldReceive('getName')->andReturn('project');
72
        $this->projectDescriptor->shouldReceive('getPartials')->andReturn([]);
73
74
        $this->implementProtectedFinalize($this->projectDescriptor);
75
76
        // Call the actual method
77
        $this->xml->transform($this->projectDescriptor, $transformation);
78
79
        // Check file exists
80
        $this->assertTrue($this->fs->hasChild('artifact.xml'));
81
82
        // Inspect XML
83
        $xml = <<<XML
84
<?xml version="1.0" encoding="utf-8"?>
85
<project version="2.0.0b8&#10;" title="project">
86
  <partials/>
87
  <package name="global" full_name="global"/>
88
  <deprecated count="0"/>
89
</project>
90
XML;
91
        $expectedXml = new \DOMDocument();
92
        $expectedXml->loadXML($xml);
93
94
        $actualXml = new \DOMDocument();
95
        $actualXml->load(vfsStream::url('XmlTest/artifact.xml'));
96
97
        $this->assertEqualXMLStructure($expectedXml->firstChild, $actualXml->firstChild, true);
0 ignored issues
show
$expectedXml->firstChild of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
$actualXml->firstChild of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
98
    }
99
100
    /**
101
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml::transform
102
     */
103
    public function testTransformWithEmptyFileDescriptor()
104
    {
105
        $transformer = m::mock('phpDocumentor\Transformer\Transformer');
106
        $transformer->shouldReceive('getTarget')->andReturn(vfsStream::url('XmlTest'));
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...
107
108
        $transformation = m::mock('phpDocumentor\Transformer\Transformation');
109
        $transformation->shouldReceive('getArtifact')->andReturn('artifact.xml');
110
        $transformation->shouldReceive('getTransformer')->andReturn($transformer);
111
112
        $fileDescriptor = m::mock('phpDocumentor\Descriptor\FileDescriptor');
113
        $fileDescriptor->shouldReceive('getPath')->andReturn('foo.php');
114
        $fileDescriptor->shouldReceive('getInheritedElement')->andReturn(null);
115
        $transformer->shouldReceive('generateFilename')->with('foo.php')->andReturn('generated-foo.php');
116
        $fileDescriptor->shouldReceive('getHash')->andReturn('hash');
117
        $fileDescriptor->shouldReceive('getAllErrors')->andReturn([]);
118
119
        $this->projectDescriptor->shouldReceive('getFiles')->andReturn([$fileDescriptor]);
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\ProjectDescriptor.

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...
120
        $this->projectDescriptor->shouldReceive('getName')->andReturn('project');
121
        $this->projectDescriptor->shouldReceive('getPartials')->andReturn([]);
122
123
        $this->implementProtectedFinalize($this->projectDescriptor);
124
        $this->implementProtectedBuildDocBlock($fileDescriptor);
125
126
        $fileDescriptor->shouldReceive('getNamespaceAliases')->andReturn(['foo', 'bar']);
127
        $fileDescriptor->shouldReceive('getConstants')->andReturn([]);
128
        $fileDescriptor->shouldReceive('getFunctions')->andReturn([]);
129
        $fileDescriptor->shouldReceive('getInterfaces')->andReturn([]);
130
        $fileDescriptor->shouldReceive('getClasses')->andReturn([]);
131
        $fileDescriptor->shouldReceive('getTraits')->andReturn([]);
132
        $fileDescriptor->shouldReceive('getMarkers')->andReturn([]);
133
        $fileDescriptor->shouldReceive('getErrors')->andReturn([]);
134
        $fileDescriptor->shouldReceive('getPartials')->andReturn([]);
135
        $fileDescriptor->shouldReceive('getSource')->andReturn(null);
136
137
        // Call the actual method
138
        $this->xml->transform($this->projectDescriptor, $transformation);
139
140
        // Check file exists
141
        $this->assertTrue($this->fs->hasChild('artifact.xml'));
142
143
        // Inspect XML
144
        $xml = <<<XML
145
<?xml version="1.0" encoding="utf-8"?>
146
<project version="2.0.0b8" title="project">
147
  <partials/>
148
  <file path="foo.php" generated-path="generated-foo.php" hash="hash" package="myPackage">
149
    <docblock line="666">
150
      <description>my summary</description>
151
      <long-description>my description</long-description>
152
    </docblock>
153
    <namespace-alias name="0">foo</namespace-alias>
154
    <namespace-alias name="1">bar</namespace-alias>
155
  </file>
156
  <package name="global" full_name="global"/>
157
  <package name="myPackage" full_name="myPackage"/>
158
  <deprecated count="0"/>
159
</project>
160
XML;
161
        $expectedXml = new \DOMDocument();
162
        $expectedXml->loadXML($xml);
163
164
        $actualXml = new \DOMDocument();
165
        $actualXml->load(vfsStream::url('XmlTest/artifact.xml'));
166
167
        $this->assertEqualXMLStructure($expectedXml->firstChild, $actualXml->firstChild, true);
0 ignored issues
show
$expectedXml->firstChild of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
$actualXml->firstChild of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
168
    }
169
170
    /**
171
     * This implements testing of the protected finalize method.
172
     */
173
    protected function implementProtectedFinalize(ProjectDescriptor $projectDescriptor)
0 ignored issues
show
The parameter $projectDescriptor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
174
    {
175
        $this->projectDescriptor->shouldReceive('isVisibilityAllowed')
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\ProjectDescriptor.

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...
176
            ->with(ProjectDescriptor\Settings::VISIBILITY_INTERNAL)
177
            ->andReturn(true);
178
    }
179
180
    /**
181
     * This implements testing of the protected buildDocBlock method
182
     */
183
    protected function implementProtectedBuildDocBlock(m\MockInterface $descriptor)
184
    {
185
        $descriptor->shouldReceive('getLine')->andReturn(666);
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...
186
        $descriptor->shouldReceive('getPackage')->andReturn('myPackage');
187
        $descriptor->shouldReceive('getSummary')->andReturn('my summary');
188
        $descriptor->shouldReceive('getDescription')->andReturn('my description');
189
        $descriptor->shouldReceive('getTags')->andReturn([]);
190
    }
191
}
192