Completed
Pull Request — master (#4)
by Timothy
03:57
created

AbstractCheckCommandFactoryTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 3
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 42
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testCreateService() 0 11 1
A testNotCreateServiceDueToInvalidCommandType() 0 9 1
1
<?php
2
3
namespace Abacaphiliac\DoctrineORMDiagnosticsModuleTest;
4
5
use Abacaphiliac\DoctrineORMDiagnosticsModule\AbstractCheckCommandFactory;
6
use Abacaphiliac\DoctrineORMDiagnosticsModule\CheckCommand;
7
use Symfony\Component\Console\Application;
8
use Symfony\Component\Console\Command\Command;
9
use Zend\ServiceManager\ServiceManager;
10
11
/**
12
 * @covers \Abacaphiliac\DoctrineORMDiagnosticsModule\AbstractCheckCommandFactory
13
 */
14
class AbstractCheckCommandFactoryTest extends \PHPUnit_Framework_TestCase
15
{
16
    /** @var ServiceManager */
17
    private $serviceLocator;
18
    
19
    /** @var \PHPUnit_Framework_MockObject_MockObject|AbstractCheckCommandFactory */
20
    private $sut;
21
22
    protected function setUp()
23
    {
24
        parent::setUp();
25
26
        $this->serviceLocator = new ServiceManager();
27
        
28
        $this->sut = $this->getMockForAbstractClass(AbstractCheckCommandFactory::class);
29
    }
30
    
31
    public function testCreateService()
32
    {
33
        $this->sut->method('getCommandServiceName')->willReturn('FooBar');
34
        
35
        $this->serviceLocator->setService('doctrine.cli', new Application());
36
        $this->serviceLocator->setService('FooBar', new Command('FooBar'));
37
38
        $actual = $this->sut->createService($this->serviceLocator);
0 ignored issues
show
Bug introduced by
The method createService does only exist in Abacaphiliac\DoctrineORM...ractCheckCommandFactory, but not in PHPUnit_Framework_MockObject_MockObject.

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...
39
        
40
        self::assertInstanceOf(CheckCommand::class, $actual);
41
    }
42
43
    /**
44
     * @expectedException \UnexpectedValueException
45
     */
46
    public function testNotCreateServiceDueToInvalidCommandType()
47
    {
48
        $this->sut->method('getCommandServiceName')->willReturn('FooBar');
49
50
        $this->serviceLocator->setService('doctrine.cli', new Application());
51
        $this->serviceLocator->setService('FooBar', new \stdClass());
52
53
        $this->sut->createService($this->serviceLocator);
0 ignored issues
show
Bug introduced by
The method createService does only exist in Abacaphiliac\DoctrineORM...ractCheckCommandFactory, but not in PHPUnit_Framework_MockObject_MockObject.

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...
54
    }
55
}
56