Completed
Pull Request — develop (#167)
by Robbie
05:20
created

ChangeVersionCommandTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A testExecute() 0 49 1
1
<?php
2
3
namespace N98\Magento\Command\System\Setup;
4
5
use Symfony\Component\Console\Tester\CommandTester;
6
use N98\Magento\Command\PHPUnit\TestCase;
7
8
class ChangeVersionCommandTest extends TestCase
9
{
10
    /**
11
     * Ensure that the resource setters are called.
12
     */
13
    public function testExecute()
14
    {
15
        $command = $this->getMockBuilder('N98\Magento\Command\System\Setup\ChangeVersionCommand')
16
            ->setMethods(array('getMagentoModuleName', 'getMagentoModuleResource'))
17
            ->getMock();
18
19
        $command
20
            ->expects($this->once())
21
            ->method('getMagentoModuleName')
22
            ->with('magento_customer')
23
            ->will($this->returnValue('Magento_Customer'));
24
25
        $resourceMock = $this->getMockBuilder('Magento\Framework\Module\ModuleResource')
26
            ->setMethods(array('setDbVersion', 'setDataVersion'))
27
            ->getMock();
28
29
        $resourceMock
30
            ->expects($this->once())
31
            ->method('setDbVersion')
32
            ->with('Magento_Customer');
33
34
        $resourceMock
35
            ->expects($this->once())
36
            ->method('setDataVersion')
37
            ->with('Magento_Customer');
38
39
        $command
40
            ->expects($this->exactly(2))
41
            ->method('getMagentoModuleResource')
42
            ->will($this->returnValue($resourceMock));
43
44
        $application = $this->getApplication();
45
        $application->add($command);
0 ignored issues
show
Bug introduced by
The method add does only exist in N98\Magento\Application, 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...
46
        $command = $this->getApplication()->find('sys:setup:change-version');
0 ignored issues
show
Bug introduced by
The method find does only exist in N98\Magento\Application, 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...
47
48
        $commandTester = new CommandTester($command);
49
        $commandTester->execute(
50
            array(
51
                'command' => $command->getName(),
52
                'module'  => 'magento_customer',
53
                'version' => '1.2.3'
54
            )
55
        );
56
57
        $this->assertContains(
58
            'Successfully updated: "Magento_Customer" to version: "1.2.3"',
59
            $commandTester->getDisplay()
60
        );
61
    }
62
}
63