Completed
Push — develop ( c51867...7a835f )
by Jaap
15:14 queued 05:15
created

Descriptor/Builder/Reflector/FileAssemblerTest.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
 * @author    Mike van Riel <[email protected]>
8
 * @author    Sven Hagemann <[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\Descriptor\Builder\Reflector;
15
16
use Mockery as m;
17
use phpDocumentor\Descriptor\Collection;
18
use phpDocumentor\Descriptor\PackageDescriptor;
19
20
use phpDocumentor\Reflection\DocBlock;
21
use phpDocumentor\Reflection\Php\File;
22
23
/**
24
 * Test class for \phpDocumentor\Descriptor\Builder
25
 *
26
 * @covers \phpDocumentor\Descriptor\Builder\Reflector\FileAssembler
27
 */
28
class FileAssemblerTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
29
{
30
    /** @var FileAssembler $fixture */
31
    protected $fixture;
32
33
    /** @var PackageDescriptor */
34
    private $defaultPackage;
35
36
    /**
37
     * Creates a new fixture to test with.
38
     */
39
    protected function setUp()
40
    {
41
        $this->fixture = new FileAssembler();
42
        $this->fixture->setBuilder($this->getProjectDescriptorBuilderMock());
43
        $this->defaultPackage = new PackageDescriptor();
44
        $this->defaultPackage->setName('\\PhpDocumentor');
45
    }
46
47
    /**
48
     * Creates a Descriptor from a provided class.
49
     */
50
    public function testCreateFileDescriptorFromReflector()
51
    {
52
        $filename = 'file.php';
53
        $content = '<?php ... ?>';
54
        $hash = md5($content);
55
        $defaultPackageName = 'Package';
0 ignored issues
show
$defaultPackageName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
57
        $abstractDescriptor = m::mock('phpDocumentor\Descriptor\DescriptorAbstract');
58
        $abstractDescriptor->shouldReceive('getLineNumber')->andReturn(1337);
59
60
        $docBlockDescription = new DocBlock\Description(
61
<<<DOCBLOCK
62
/**
63
 * This is a example description
64
 */
65
DOCBLOCK
66
        );
67
68
        $docBlockMock = new DocBlock('This is a example description', $docBlockDescription);
69
70
        $fileReflectorMock = new File($hash, $filename, $content, $docBlockMock);
71
//        $fileReflectorMock->shouldReceive('getConstants')->once()->andReturn(
72
//            new Collection(array($abstractDescriptor))
73
//        );
74
//
75
//        $fileReflectorMock->shouldReceive('getFunctions')->once()->andReturn(
76
//            new Collection(array($abstractDescriptor))
77
//        );
78
//
79
//        $fileReflectorMock->shouldReceive('getClasses')->once()->andReturn(
80
//            new Collection(array($abstractDescriptor))
81
//        );
82
//
83
//        $fileReflectorMock->shouldReceive('getInterfaces')->once()->andReturn(
84
//            new Collection(array($abstractDescriptor))
85
//        );
86
//
87
//        $fileReflectorMock->shouldReceive('getTraits')->once()->andReturn(
88
//            new Collection(array($abstractDescriptor))
89
//        );
90
//
91
//        $fileReflectorMock->shouldReceive('getMarkers')->once()->andReturn(
92
//            array('type', 'message', 1337)
93
//        );
94
//
95
//        $fileReflectorMock->shouldReceive('getIncludes')->andReturn(new Collection);
96
//        $fileReflectorMock->shouldReceive('getNamespaceAliases')->andReturn(new Collection);
97
98
        $descriptor = $this->fixture->create($fileReflectorMock);
99
100
        $this->assertSame($filename, $descriptor->getName());
101
        $this->assertSame($hash, $descriptor->getHash());
102
        $this->assertSame($content, $descriptor->getSource());
103
        //TODO: check this when we are testing default package behavoir
104
        //$this->assertSame($this->defaultPackage, $descriptor->getPackage());
105
    }
106
107
    /**
108
     * Create a descriptor builder mock
109
     *
110
     * @return m\MockInterface
111
     */
112
    protected function getProjectDescriptorBuilderMock()
113
    {
114
        $projectDescriptorBuilderMock = m::mock('phpDocumentor\Descriptor\ProjectDescriptorBuilder');
115
        $projectDescriptorBuilderMock->shouldReceive('getDefaultPackage')->andReturn($this->defaultPackage);
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...
116
117
        $projectDescriptorBuilderMock->shouldReceive('getProjectDescriptor->getSettings->shouldIncludeSource')->andReturn(true);
118
        $projectDescriptorBuilderMock->shouldReceive('buildDescriptor')->andReturnUsing(function ($param) {
119
            $mock = m::mock('phpDocumentor\Descriptor\DescriptorAbstract');
120
            $mock->shouldReceive('setLocation')->atLeast()->once();
121
            $mock->shouldReceive('getTags')->atLeast()->once()->andReturn(new Collection());
122
            $mock->shouldReceive('getFullyQualifiedStructuralElementName')->once()->andReturn('Frank_is_een_eindbaas');
123
124
            return $mock;
125
        });
126
127
        return $projectDescriptorBuilderMock;
128
    }
129
}
130