Completed
Pull Request — master (#4)
by Timothy
08:48 queued 02:25
created

AbstractCheckCommandFactoryTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testCreateService() 0 10 1
A testNotCreateServiceDueToInvalidCommandType() 0 8 1
1
<?php
2
3
namespace Abacaphiliac\DoctrineORMDiagnosticsModuleTest;
4
5
use Abacaphiliac\DoctrineORMDiagnosticsModule\AbstractCheckCommandFactory;
6
use Abacaphiliac\DoctrineORMDiagnosticsModule\CheckCommand;
7
use Symfony\Component\Console\Command\Command;
8
use Zend\ServiceManager\ServiceManager;
9
10
/**
11
 * @covers \Abacaphiliac\DoctrineORMDiagnosticsModule\AbstractCheckCommandFactory
12
 */
13
class AbstractCheckCommandFactoryTest extends \PHPUnit_Framework_TestCase
14
{
15
    /** @var ServiceManager */
16
    private $serviceLocator;
17
    
18
    /** @var \PHPUnit_Framework_MockObject_MockObject|AbstractCheckCommandFactory */
19
    private $sut;
20
21
    protected function setUp()
22
    {
23
        parent::setUp();
24
25
        $this->serviceLocator = new ServiceManager();
26
        
27
        $this->sut = $this->getMockForAbstractClass(AbstractCheckCommandFactory::class);
28
    }
29
    
30
    public function testCreateService()
31
    {
32
        $this->sut->method('getCommandServiceName')->willReturn('FooBar');
33
        
34
        $this->serviceLocator->setService('FooBar', new Command('FooBar'));
35
36
        $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...
37
        
38
        self::assertInstanceOf(CheckCommand::class, $actual);
39
    }
40
41
    /**
42
     * @expectedException \UnexpectedValueException
43
     */
44
    public function testNotCreateServiceDueToInvalidCommandType()
45
    {
46
        $this->sut->method('getCommandServiceName')->willReturn('FooBar');
47
        
48
        $this->serviceLocator->setService('FooBar', new \stdClass());
49
50
        $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...
51
    }
52
}
53