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

unit/phpDocumentor/Transformer/TransformerTest.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
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Transformer;
14
15
use Mockery as m;
16
use Psr\Log\NullLogger;
17
18
/**
19
 * Test class for \phpDocumentor\Transformer\Transformer.
20
 *
21
 * @covers phpDocumentor\Transformer\Transformer
22
 */
23
class TransformerTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
24
{
25
    /** @var int Max length of description printed. */
26
    protected static $MAX_DESCRIPTION_LENGTH = 68;
27
28
    /** @var Transformer $fixture */
29
    protected $fixture = null;
30
31
    /**
32
     * Instantiates a new \phpDocumentor\Transformer for use as fixture.
33
     */
34
    protected function setUp()
35
    {
36
        $templateCollectionMock = m::mock('phpDocumentor\Transformer\Template\Collection');
37
        $templateCollectionMock->shouldIgnoreMissing();
38
        $writerCollectionMock = m::mock('phpDocumentor\Transformer\Writer\Collection');
39
        $writerCollectionMock->shouldIgnoreMissing();
40
41
        $this->fixture = new Transformer($templateCollectionMock, $writerCollectionMock, new NullLogger());
42
    }
43
44
    /**
45
     * @covers phpDocumentor\Transformer\Transformer::__construct
46
     */
47
    public function testInitialization()
48
    {
49
        $templateCollectionMock = m::mock('phpDocumentor\Transformer\Template\Collection');
50
        $templateCollectionMock->shouldIgnoreMissing();
51
        $writerCollectionMock = m::mock('phpDocumentor\Transformer\Writer\Collection');
52
        $writerCollectionMock->shouldIgnoreMissing();
53
        $this->fixture = new Transformer($templateCollectionMock, $writerCollectionMock, new NullLogger());
54
55
        $this->assertAttributeEquals($templateCollectionMock, 'templates', $this->fixture);
56
        $this->assertAttributeEquals($writerCollectionMock, 'writers', $this->fixture);
57
    }
58
59
    /**
60
     * @covers phpDocumentor\Transformer\Transformer::getTarget
61
     * @covers phpDocumentor\Transformer\Transformer::setTarget
62
     */
63
    public function testSettingAndGettingATarget()
64
    {
65
        $this->assertEquals('', $this->fixture->getTarget());
66
67
        $this->fixture->setTarget(__DIR__);
68
69
        $this->assertEquals(__DIR__, $this->fixture->getTarget());
70
    }
71
72
    /**
73
     * @covers phpDocumentor\Transformer\Transformer::setTarget
74
     * @expectedException \InvalidArgumentException
75
     */
76
    public function testExceptionWhenSettingFileAsTarget()
77
    {
78
        $this->fixture->setTarget(__FILE__);
79
    }
80
81
    /**
82
     * @covers phpDocumentor\Transformer\Transformer::setTarget
83
     * @expectedException \InvalidArgumentException
84
     * @expectedExceptionMessage Target directory (vfs://myroot) does not exist and could not be created
85
     */
86
    public function testExceptionWhenSettingExistingDirAsTarget()
87
    {
88
        $fileSystem = \org\bovigo\vfs\vfsStream::setup('myroot');
0 ignored issues
show
$fileSystem 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...
89
        $this->fixture->setTarget(\org\bovigo\vfs\vfsStream::url('myroot'));
90
    }
91
92
    /**
93
     * @covers phpDocumentor\Transformer\Transformer::getTemplates
94
     */
95
    public function testRetrieveTemplateCollection()
96
    {
97
        $templateCollectionMock = m::mock('phpDocumentor\Transformer\Template\Collection');
98
        $templateCollectionMock->shouldIgnoreMissing();
99
        $writerCollectionMock = m::mock('phpDocumentor\Transformer\Writer\Collection');
100
        $writerCollectionMock->shouldIgnoreMissing();
101
102
        $fixture = new Transformer($templateCollectionMock, $writerCollectionMock, new NullLogger());
103
104
        $this->assertEquals($templateCollectionMock, $fixture->getTemplates());
105
    }
106
107
    /**
108
     * @covers phpDocumentor\Transformer\Transformer::execute
109
     */
110
    public function testExecute()
111
    {
112
        $myTestWritter = 'myTestWriter';
113
114
        $templateCollection = m::mock('phpDocumentor\Transformer\Template\Collection');
115
116
        $project = m::mock('phpDocumentor\Descriptor\ProjectDescriptor');
117
118
        $myTestWritterMock = m::mock('phpDocumentor\Transformer\Writer\WriterAbstract')
0 ignored issues
show
The method getMock 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...
119
            ->shouldReceive('transform')->getMock();
120
121
        $writerCollectionMock = m::mock('phpDocumentor\Transformer\Writer\Collection')
122
            ->shouldReceive('offsetGet')->with($myTestWritter)->andReturn($myTestWritterMock)
123
            ->getMock();
124
125
        $fixture = new Transformer($templateCollection, $writerCollectionMock, new NullLogger());
126
127
        $transformation = m::mock('phpDocumentor\Transformer\Transformation')
128
            ->shouldReceive('execute')->with($project)
129
            ->shouldReceive('getQuery')->andReturn('')
130
            ->shouldReceive('getWriter')->andReturn($myTestWritter)
131
            ->shouldReceive('getArtifact')->andReturn('')
132
            ->shouldReceive('setTransformer')->with($fixture)
133
            ->getMock();
134
135
        $templateCollection->shouldReceive('getTransformations')->andReturn(
136
            [$transformation]
137
        );
138
139
        $fixture->execute($project);
140
    }
141
142
    /**
143
     * Tests whether the generateFilename method returns a file according to
144
     * the right format.
145
     *
146
     * @covers phpDocumentor\Transformer\Transformer::generateFilename
147
     */
148
    public function testGenerateFilename()
149
    {
150
        // separate the directories with the DIRECTORY_SEPARATOR constant to prevent failing tests on windows
151
        $filename = 'directory' . DIRECTORY_SEPARATOR . 'directory2' . DIRECTORY_SEPARATOR . 'file.php';
152
        $this->assertEquals('directory.directory2.file.html', $this->fixture->generateFilename($filename));
153
    }
154
155
    /**
156
     * @covers phpDocumentor\Transformer\Transformer::getDescription
157
     */
158
    public function testGetDescription()
159
    {
160
        $description = $this->fixture->getDescription();
161
        $this->assertNotNull($description);
162
        $this->assertLessThanOrEqual(static::$MAX_DESCRIPTION_LENGTH, strlen($description));
163
    }
164
}
165