Completed
Push — develop ( 2fa385...aae295 )
by Mike
06:11
created

Transformer/Writer/CollectionTest.php (1 issue)

Labels
Severity

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\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());
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
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