Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
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-2012 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 phpDocumentor\Descriptor\ProjectDescriptor;
17
use phpDocumentor\Descriptor\DescriptorAbstract;
18
19
use Mockery as m;
20
use org\bovigo\vfs\vfsStream;
21
use phpDocumentor\Transformer\Router\RouterAbstract;
22
use phpDocumentor\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 \PHPUnit_Framework_TestCase
30
{
31
    /** @var Xml $xml */
32
    protected $xml;
33
34
    /** @var m\MockInterface|RouterAbstract */
35
    protected $routerMock;
36
37
    /** @var m\MockInterface|Translator */
38
    protected $translator;
39
40
    /** @var m\MockInterface|ProjectDescriptor */
41
    protected $projectDescriptor;
42
43
    /** @var vfsStream */
44
    protected $fs;
45
46
    /**
47
     * Sets up the test suite
48
     *
49
     * @return void
50
     */
51
    public function setUp()
52
    {
53
        $this->fs                = vfsStream::setup('XmlTest');
54
        $this->translator        = m::mock('phpDocumentor\Translator\Translator');
55
        $this->projectDescriptor = m::mock('phpDocumentor\Descriptor\ProjectDescriptor');
56
        $this->routerMock        = m::mock('phpDocumentor\Transformer\Router\RouterAbstract');
57
        $this->xml               = new Xml($this->routerMock);
58
        $this->xml->setTranslator($this->translator);
59
    }
60
61
    /**
62
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml::transform
63
     */
64
    public function testTransformWithoutFiles()
65
    {
66
        $transformer = m::mock('phpDocumentor\Transformer\Transformer');
67
        $transformation = m::mock('phpDocumentor\Transformer\Transformation');
68
        $transformation->shouldReceive('getTransformer->getTarget')->andReturn(vfsStream::url('XmlTest'));
69
        $transformation->shouldReceive('getArtifact')->andReturn('artifact.xml');
70
        $transformation->shouldReceive('getTransformer')->andReturn($transformer);
71
72
        $this->projectDescriptor->shouldReceive('getFiles')->andReturn(array());
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...
73
        $this->projectDescriptor->shouldReceive('getName')->andReturn('project');
74
        $this->projectDescriptor->shouldReceive('getPartials')->andReturn(array());
75
76
        $this->implementProtectedFinalize($this->projectDescriptor);
77
78
        // Call the actual method
79
        $this->xml->transform($this->projectDescriptor, $transformation);
80
81
        // Check file exists
82
        $this->assertTrue($this->fs->hasChild('artifact.xml'));
0 ignored issues
show
The method hasChild() does not seem to exist on object<org\bovigo\vfs\vfsStream>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
84
        // Inspect XML
85
        $xml = <<<XML
86
<?xml version="1.0" encoding="utf-8"?>
87
<project version="2.0.0b8&#10;" title="project">
88
  <partials/>
89
  <package name="global" full_name="global"/>
90
  <deprecated count="0"/>
91
</project>
92
XML;
93
        $expectedXml = new \DOMDocument;
94
        $expectedXml->loadXML($xml);
95
96
        $actualXml = new \DOMDocument;
97
        $actualXml->load(vfsStream::url('XmlTest/artifact.xml'));
98
99
        $this->assertEqualXMLStructure($expectedXml->firstChild, $actualXml->firstChild, true);
100
    }
101
102
    /**
103
     * @covers phpDocumentor\Plugin\Core\Transformer\Writer\Xml::transform
104
     */
105
    public function testTransformWithEmptyFileDescriptor()
106
    {
107
        $transformer = m::mock('phpDocumentor\Transformer\Transformer');
108
        $transformer->shouldReceive('getTarget')->andReturn(vfsStream::url('XmlTest'));
109
110
        $transformation = m::mock('phpDocumentor\Transformer\Transformation');
111
        $transformation->shouldReceive('getArtifact')->andReturn('artifact.xml');
112
        $transformation->shouldReceive('getTransformer')->andReturn($transformer);
113
114
        $fileDescriptor = m::mock('phpDocumentor\Descriptor\FileDescriptor');
115
        $fileDescriptor->shouldReceive('getPath')->andReturn('foo.php');
116
        $fileDescriptor->shouldReceive('getInheritedElement')->andReturn(null);
117
        $transformer->shouldReceive('generateFilename')->with('foo.php')->andReturn('generated-foo.php');
118
        $fileDescriptor->shouldReceive('getHash')->andReturn('hash');
119
        $fileDescriptor->shouldReceive('getAllErrors')->andReturn(array());
120
121
        $this->projectDescriptor->shouldReceive('getFiles')->andReturn(array($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...
122
        $this->projectDescriptor->shouldReceive('getName')->andReturn('project');
123
        $this->projectDescriptor->shouldReceive('getPartials')->andReturn(array());
124
125
        $this->implementProtectedFinalize($this->projectDescriptor);
126
        $this->implementProtectedBuildDocBlock($fileDescriptor);
127
128
        $fileDescriptor->shouldReceive('getNamespaceAliases')->andReturn(array('foo', 'bar'));
129
        $fileDescriptor->shouldReceive('getConstants')->andReturn(array());
130
        $fileDescriptor->shouldReceive('getFunctions')->andReturn(array());
131
        $fileDescriptor->shouldReceive('getInterfaces')->andReturn(array());
132
        $fileDescriptor->shouldReceive('getClasses')->andReturn(array());
133
        $fileDescriptor->shouldReceive('getTraits')->andReturn(array());
134
        $fileDescriptor->shouldReceive('getMarkers')->andReturn(array());
135
        $fileDescriptor->shouldReceive('getErrors')->andReturn(array());
136
        $fileDescriptor->shouldReceive('getPartials')->andReturn(array());
137
        $fileDescriptor->shouldReceive('getSource')->andReturn(null);
138
139
        // Call the actual method
140
        $this->xml->transform($this->projectDescriptor, $transformation);
141
142
        // Check file exists
143
        $this->assertTrue($this->fs->hasChild('artifact.xml'));
0 ignored issues
show
The method hasChild() does not seem to exist on object<org\bovigo\vfs\vfsStream>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
145
        // Inspect XML
146
        $xml = <<<XML
147
<?xml version="1.0" encoding="utf-8"?>
148
<project version="2.0.0b8" title="project">
149
  <partials/>
150
  <file path="foo.php" generated-path="generated-foo.php" hash="hash" package="myPackage">
151
    <docblock line="666">
152
      <description>my summary</description>
153
      <long-description>my description</long-description>
154
    </docblock>
155
    <namespace-alias name="0">foo</namespace-alias>
156
    <namespace-alias name="1">bar</namespace-alias>
157
  </file>
158
  <package name="global" full_name="global"/>
159
  <package name="myPackage" full_name="myPackage"/>
160
  <deprecated count="0"/>
161
</project>
162
XML;
163
        $expectedXml = new \DOMDocument;
164
        $expectedXml->loadXML($xml);
165
166
        $actualXml = new \DOMDocument;
167
        $actualXml->load(vfsStream::url('XmlTest/artifact.xml'));
168
169
        $this->assertEqualXMLStructure($expectedXml->firstChild, $actualXml->firstChild, true);
170
    }
171
172
    /**
173
     * This implements testing of the protected finalize method.
174
     *
175
     * @param ProjectDescriptor $projectDescriptor
176
     * @return void
177
     */
178
    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...
179
    {
180
        $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...
181
            ->with(ProjectDescriptor\Settings::VISIBILITY_INTERNAL)
182
            ->andReturn(true);
183
    }
184
185
    /**
186
     * This implements testing of the protected buildDocBlock method
187
     *
188
     * @param DescriptorAbstract $descriptor
189
     * @return void
190
     */
191
    protected function implementProtectedBuildDocBlock(DescriptorAbstract $descriptor)
192
    {
193
        $descriptor->shouldReceive('getLine')->andReturn(666);
0 ignored issues
show
Documentation Bug introduced by
The method shouldReceive does not exist on object<phpDocumentor\Des...tor\DescriptorAbstract>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
194
        $descriptor->shouldReceive('getPackage')->andReturn('myPackage');
0 ignored issues
show
Documentation Bug introduced by
The method shouldReceive does not exist on object<phpDocumentor\Des...tor\DescriptorAbstract>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
195
        $descriptor->shouldReceive('getSummary')->andReturn('my summary');
0 ignored issues
show
Documentation Bug introduced by
The method shouldReceive does not exist on object<phpDocumentor\Des...tor\DescriptorAbstract>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
196
        $descriptor->shouldReceive('getDescription')->andReturn('my description');
0 ignored issues
show
Documentation Bug introduced by
The method shouldReceive does not exist on object<phpDocumentor\Des...tor\DescriptorAbstract>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
197
        $descriptor->shouldReceive('getTags')->andReturn(array());
0 ignored issues
show
Documentation Bug introduced by
The method shouldReceive does not exist on object<phpDocumentor\Des...tor\DescriptorAbstract>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
198
    }
199
}
200