Completed
Push — develop ( 3bf98d...ad187c )
by Jaap
06:26
created

FromContainerFactoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2016 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Application\ReadModel;
14
15
use Interop\Container\ContainerInterface;
16
use Mockery as m;
17
use phpDocumentor\DomainModel\ReadModel\Type;
18
19
/**
20
 * @coversDefaultClass phpDocumentor\Application\ReadModel\FromContainerFactory
21
 * @covers ::<private>
22
 */
23
final class FromContainerFactoryTest extends \PHPUnit_Framework_TestCase
24
{
25
    /** @var ContainerInterface|m\MockInterface */
26
    private $container;
27
28
    /** @var FromContainerFactory */
29
    private $factory;
30
31
    protected function setUp()
32
    {
33
        $this->container = m::mock(ContainerInterface::class);
34
        $this->factory = new FromContainerFactory($this->container, [ 'alias' => 'phpDocumentor2' ]);
35
    }
36
37
    /**
38
     * @test
39
     * @covers ::__construct
40
     * @covers ::create
41
     */
42
    public function itShouldCreateANewReadModelByRetrievingItFromTheContainer()
43
    {
44
        $expectedReadModelName = 'phpDocumentor2';
45
        $expected = 'readModel';
46
        $this->container->shouldReceive('get')->once()
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Interop\Container\ContainerInterface.

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...
47
            ->with($expectedReadModelName)->andReturn($expected);
48
49
        $this->assertSame($expected, $this->factory->create(new Type($expectedReadModelName)));
50
    }
51
52
    /**
53
     * @test
54
     * @covers ::__construct
55
     * @covers ::create
56
     */
57
    public function itShouldCreateANewReadModelByRetrievingItFromTheContainerBasedOnAnAlias()
58
    {
59
        $expectedReadModelName = 'phpDocumentor2';
60
        $expected = 'readModel';
61
        $this->container->shouldReceive('get')->once()
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Interop\Container\ContainerInterface.

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...
62
            ->with($expectedReadModelName)->andReturn($expected);
63
64
        $this->assertSame($expected, $this->factory->create(new Type('alias')));
65
    }
66
}
67