testIfDependenciesAreCorrectlyRegisteredOnInitialization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Writer;
14
15
use Mockery as m;
16
use Mockery\Adapter\Phpunit\MockeryTestCase;
17
use Mockery\MockInterface;
18
use phpDocumentor\Transformer\Router\Queue;
19
20
/**
21
 * Test class for phpDocumentor\Transformer\Writer\Collection
22
 */
23
class CollectionTest extends MockeryTestCase
24
{
25
    /** @var MockInterface|Queue */
26
    protected $routers;
27
28
    /** @var MockInterface|WriterAbstract */
29
    protected $writer;
30
31
    /** @var Collection */
32
    protected $fixture;
33
34
    /**
35
     * Initializes the fixture and dependencies for this testcase.
36
     */
37
    protected function setUp()
38
    {
39
        $this->routers = m::mock(Queue::class);
40
        $this->writer = m::mock(WriterAbstract::class);
41
        $this->fixture = new Collection($this->routers);
42
    }
43
44
    /**
45
     * @covers phpDocumentor\Transformer\Writer\Collection::__construct
46
     */
47
    public function testIfDependenciesAreCorrectlyRegisteredOnInitialization()
48
    {
49
        $this->assertAttributeSame($this->routers, 'routers', $this->fixture);
50
    }
51
52
    /**
53
     * @expectedException \InvalidArgumentException
54
     * @covers phpDocumentor\Transformer\Writer\Collection::offsetSet
55
     */
56
    public function testOffsetSetWithWriterNotDescendingFromWriterAbstract()
57
    {
58
        $this->fixture->offsetSet('index', new \stdClass());
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a object<phpDocumentor\Tra...\Writer\WriterAbstract>.

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...
59
    }
60
61
    /**
62
     * @expectedException \InvalidArgumentException
63
     * @covers phpDocumentor\Transformer\Writer\Collection::offsetSet
64
     */
65
    public function testOffsetSetWithInvalidIndexName()
66
    {
67
        $this->fixture->offsetSet('i', $this->writer);
68
    }
69
70
    /**
71
     * @expectedException \InvalidArgumentException
72
     * @covers phpDocumentor\Transformer\Writer\Collection::offsetGet
73
     */
74
    public function testOffsetGetWithNonExistingIndex()
75
    {
76
        $this->fixture->offsetGet('nonExistingIndex');
77
    }
78
79
    /**
80
     * @covers phpDocumentor\Transformer\Writer\Collection::offsetGet
81
     */
82
    public function testOffsetGetWithExistingIndex()
83
    {
84
        $this->registerWriter();
85
86
        $this->assertSame($this->writer, $this->fixture->offsetGet('index'));
87
    }
88
89
    /**
90
     * @covers phpDocumentor\Transformer\Writer\Collection::checkRequirements
91
     */
92
    public function testCheckRequirements()
93
    {
94
        $this->registerWriter();
95
96
        $this->writer->shouldReceive('checkRequirements')->once();
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Transformer\Writer\WriterAbstract.

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...
97
        $this->fixture->checkRequirements();
98
99
        $this->assertTrue(true);
100
    }
101
102
    /**
103
     * Registers a writer for tests that need a collection item
104
     */
105
    private function registerWriter()
106
    {
107
        $this->fixture->offsetSet('index', $this->writer);
108
    }
109
}
110