Completed
Push — master ( a015de...da4521 )
by Mike
04:17
created

MethodDescriptorTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 1
lcom 0
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A testGenerateUrlForMethodDescriptor() 0 21 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;
13
14
use Mockery as m;
15
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, phpDocumentor\Transforme...r\UrlGeneratorInterface.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
17
/**
18
 * Test for the MethodDescriptor URL Generator with the Standard Router
19
 */
20
class MethodDescriptorTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
21
{
22
    /**
23
     * @covers \phpDocumentor\Transformer\Router\UrlGenerator\MethodDescriptor::__invoke
24
     * @covers \phpDocumentor\Transformer\Router\UrlGenerator\QualifiedNameToUrlConverter::fromClass
25
     */
26
    public function testGenerateUrlForMethodDescriptor() : void
27
    {
28
        $expected = '/classes/My.Space.Class.html#method_myMethod';
29
        $urlGenerator = m::mock(UrlGeneratorInterface::class);
30
        $urlGenerator->shouldReceive('generate')->andReturn($expected);
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
        $converter = new QualifiedNameToUrlConverter();
32
33
        // Arrange
34
        $fixture = new MethodDescriptor($urlGenerator, $converter);
0 ignored issues
show
Documentation introduced by
$urlGenerator is of type object<Mockery\LegacyMockInterface>, but the function expects a object<Symfony\Component...\UrlGeneratorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
        $methodDescriptorMock = m::mock('phpDocumentor\Descriptor\MethodDescriptor');
36
        $methodDescriptorMock
37
            ->shouldReceive('getParent->getFullyQualifiedStructuralElementName')
38
            ->andReturn('My\\Space\\Class');
39
        $methodDescriptorMock->shouldReceive('getName')->andReturn('myMethod');
40
41
        // Act
42
        $result = $fixture($methodDescriptorMock);
43
44
        // Assert
45
        $this->assertSame($expected, $result);
46
    }
47
}
48