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

phpDocumentor/Transformer/TransformationTest.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 phpDocumentor\Transformer\Template\Parameter;
17
18
/**
19
 * Test class for phpDocumentor\Transformer\Transformation
20
 */
21
class TransformationTest extends m\Adapter\Phpunit\MockeryTestCase
22
{
23
    /** @var Transformation $fixture */
24
    protected $fixture;
25
26
    /** @var string */
27
    protected $query = 'queryString';
28
29
    /** @var string */
30
    protected $writer = 'writerString';
31
32
    /** @var string */
33
    protected $source = 'sourceString';
34
35
    /** @var string */
36
    protected $artifact = 'artifactString';
37
38
    /**
39
     * Initializes the fixture and dependencies for this testcase.
40
     */
41
    protected function setUp()
42
    {
43
        $this->fixture = new Transformation($this->query, $this->writer, $this->source, $this->artifact);
44
    }
45
46
    /**
47
     * @covers \phpDocumentor\Transformer\Transformation::__construct
48
     * @covers \phpDocumentor\Transformer\Transformation::setQuery
49
     * @covers \phpDocumentor\Transformer\Transformation::setWriter
50
     * @covers \phpDocumentor\Transformer\Transformation::setSource
51
     * @covers \phpDocumentor\Transformer\Transformation::setArtifact
52
     */
53
    public function testIfDependenciesAreCorrectlyRegisteredOnInitialization()
54
    {
55
        $this->assertAttributeSame($this->query, 'query', $this->fixture);
56
        $this->assertAttributeSame($this->writer, 'writer', $this->fixture);
57
        $this->assertAttributeSame($this->source, 'source', $this->fixture);
58
        $this->assertAttributeSame($this->artifact, 'artifact', $this->fixture);
59
    }
60
61
    /**
62
     * @covers \phpDocumentor\Transformer\Transformation::getQuery
63
     */
64
    public function testGetQuery()
65
    {
66
        $this->assertSame($this->query, $this->fixture->getQuery());
67
    }
68
69
    /**
70
     * @covers \phpDocumentor\Transformer\Transformation::getWriter
71
     */
72
    public function testGetWriter()
73
    {
74
        $this->assertSame($this->writer, $this->fixture->getWriter());
75
    }
76
77
    /**
78
     * @covers \phpDocumentor\Transformer\Transformation::getSource
79
     */
80
    public function testGetSource()
81
    {
82
        $this->assertSame($this->source, $this->fixture->getSource());
83
    }
84
85
    /**
86
     * @covers \phpDocumentor\Transformer\Transformation::getArtifact
87
     */
88
    public function testGetArtifact()
89
    {
90
        $this->assertSame($this->artifact, $this->fixture->getArtifact());
91
    }
92
93
    /**
94
     * @covers \phpDocumentor\Transformer\Transformation::getParameters
95
     * @covers \phpDocumentor\Transformer\Transformation::setParameters
96
     */
97
    public function testSetAndGetParameters()
98
    {
99
        $this->assertSame([], $this->fixture->getParameters());
100
101
        $parameters = $this->givenAParameter();
102
103
        $this->assertSame($parameters, $this->fixture->getParameters());
104
    }
105
106
    /**
107
     * @covers \phpDocumentor\Transformer\Transformation::getParameter
108
     */
109
    public function testGetParameterWithExistingName()
110
    {
111
        $parameters = $this->givenAParameter();
112
        $this->assertSame($parameters['firstKey'], $this->fixture->getParameter('name'));
113
    }
114
115
    /**
116
     * @covers \phpDocumentor\Transformer\Transformation::getParameter
117
     */
118
    public function testGetParameterWithNonExistingName()
119
    {
120
        $this->assertNull($this->fixture->getParameter('somethingElse'));
121
    }
122
123
    /**
124
     * @covers \phpDocumentor\Transformer\Transformation::getParametersWithKey
125
     */
126
    public function testGetParametersWithKeyWithExistingName()
127
    {
128
        $parameters = $this->givenAParameter();
129
        $this->assertSame([$parameters['firstKey']], $this->fixture->getParametersWithKey('name'));
130
    }
131
132
    /**
133
     * @covers \phpDocumentor\Transformer\Transformation::getParametersWithKey
134
     */
135
    public function testGetParametersWithKeyWithNonExistingName()
136
    {
137
        $this->assertSame([], $this->fixture->getParametersWithKey('somethingElse'));
138
    }
139
140
    /**
141
     * @covers \phpDocumentor\Transformer\Transformation::getTransformer
142
     * @covers \phpDocumentor\Transformer\Transformation::setTransformer
143
     */
144
    public function testSetAndGetTransformer()
145
    {
146
        $transformer = m::mock('phpDocumentor\Transformer\Transformer');
147
148
        $this->assertNull($this->fixture->getTransformer());
149
150
        $this->fixture->setTransformer($transformer);
151
152
        $this->assertSame($transformer, $this->fixture->getTransformer());
153
    }
154
155
    /**
156
     * Sets a parameter in the fixture for tests that need to get parameters and
157
     * returns the parameter array used to set this parameter for comparison
158
     */
159
    private function givenAParameter()
160
    {
161
        $parameterMock = m::mock('phpDocumentor\Transformer\Template\Parameter');
162
        $parameterMock->shouldReceive('getKey')->andReturn('name');
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...
163
        $parameters = ['firstKey' => $parameterMock];
164
        $this->fixture->setParameters($parameters);
0 ignored issues
show
$parameters is of type array<string,object<Mock...kery\\MockInterface>"}>, but the function expects a array<integer,object<php...er\Template\Parameter>>.

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...
165
166
        return $parameters;
167
    }
168
}
169