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

UrlGenerator/Standard/NamespaceDescriptorTest.php (2 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\Router\UrlGenerator\Standard;
13
14
use Mockery as m;
15
16
/**
17
 * Test for the NamespaceDescriptor URL Generator with the Standard Router
18
 */
19
class NamespaceDescriptorTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
20
{
21
    /**
22
     * @covers phpDocumentor\Transformer\Router\UrlGenerator\Standard\NamespaceDescriptor::__invoke
23
     * @covers phpDocumentor\Transformer\Router\UrlGenerator\Standard\QualifiedNameToUrlConverter::fromNamespace
24
     */
25
    public function testGenerateUrlForNamespaceDescriptor()
26
    {
27
        // Arrange
28
        $fixture = new NamespaceDescriptor();
29
        $NamespaceDescriptorMock = m::mock('phpDocumentor\Descriptor\NamespaceDescriptor');
30
        $NamespaceDescriptorMock->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('My\\Space');
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...
31
32
        // Act
33
        $result = $fixture($NamespaceDescriptorMock);
34
35
        // Assert
36
        $this->assertSame('/namespaces/My.Space.html', $result);
37
    }
38
39
    /**
40
     * @covers phpDocumentor\Transformer\Router\UrlGenerator\Standard\NamespaceDescriptor::__invoke
41
     * @covers phpDocumentor\Transformer\Router\UrlGenerator\Standard\QualifiedNameToUrlConverter::fromNamespace
42
     */
43
    public function testGenerateUrlForNamespaceDescriptorWithGlobalNamespace()
44
    {
45
        // Arrange
46
        $fixture = new NamespaceDescriptor();
47
        $NamespaceDescriptorMock = m::mock('phpDocumentor\Descriptor\NamespaceDescriptor');
48
        $NamespaceDescriptorMock->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('\\');
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...
49
50
        // Act
51
        $result = $fixture($NamespaceDescriptorMock);
52
53
        // Assert
54
        $this->assertSame('/namespaces/default.html', $result);
55
    }
56
}
57