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

PackageDescriptorTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testGenerateUrlForPackageDescriptor() 0 13 1
A testGenerateUrlForPackageDescriptorWithGlobalNamespace() 0 13 1
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 PackageDescriptor URL Generator with the Standard Router
18
 */
19
class PackageDescriptorTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
20
{
21
    /**
22
     * @covers phpDocumentor\Transformer\Router\UrlGenerator\Standard\PackageDescriptor::__invoke
23
     * @covers phpDocumentor\Transformer\Router\UrlGenerator\Standard\QualifiedNameToUrlConverter::fromPackage
24
     */
25
    public function testGenerateUrlForPackageDescriptor()
26
    {
27
        // Arrange
28
        $fixture = new PackageDescriptor();
29
        $PackageDescriptorMock = m::mock('phpDocumentor\Descriptor\PackageDescriptor');
30
        $PackageDescriptorMock->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('My\\Space_Package');
0 ignored issues
show
Bug introduced by
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($PackageDescriptorMock);
34
35
        // Assert
36
        $this->assertSame('/packages/My.Space.Package.html', $result);
37
    }
38
39
    /**
40
     * @covers phpDocumentor\Transformer\Router\UrlGenerator\Standard\PackageDescriptor::__invoke
41
     * @covers phpDocumentor\Transformer\Router\UrlGenerator\Standard\QualifiedNameToUrlConverter::fromPackage
42
     */
43
    public function testGenerateUrlForPackageDescriptorWithGlobalNamespace()
44
    {
45
        // Arrange
46
        $fixture = new PackageDescriptor();
47
        $PackageDescriptorMock = m::mock('phpDocumentor\Descriptor\PackageDescriptor');
48
        $PackageDescriptorMock->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('\\');
0 ignored issues
show
Bug introduced by
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($PackageDescriptorMock);
52
53
        // Assert
54
        $this->assertSame('/packages/default.html', $result);
55
    }
56
}
57