Completed
Pull Request — develop (#167)
by Robbie
04:11
created

ChangeVersionCommandTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

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
     * @runInSeparateProcess
13
     */
14
    public function testExecute()
15
    {
16
        $command = $this->getMockBuilder('\N98\Magento\Command\System\Setup\ChangeVersionCommand')
17
            ->setMethods(array('getMagentoModuleName', 'getMagentoModuleResource'))
18
            ->getMock();
19
20
        $command
21
            ->expects($this->once())
22
            ->method('getMagentoModuleName')
23
            ->with('magento_customer')
24
            ->will($this->returnValue('Magento_Customer'));
25
26
        $resourceMock = $this->getMockBuilder('\Magento\Framework\Module\ModuleResource')
27
            ->setMethods(array('setDbVersion', 'setDataVersion'))
28
            ->getMock();
29
30
        $resourceMock
31
            ->expects($this->once())
32
            ->method('setDbVersion')
33
            ->with('Magento_Customer');
34
35
        $resourceMock
36
            ->expects($this->once())
37
            ->method('setDataVersion')
38
            ->with('Magento_Customer');
39
40
        $command
41
            ->expects($this->exactly(2))
42
            ->method('getMagentoModuleResource')
43
            ->will($this->returnValue($resourceMock));
44
45
        $application = $this->getApplication();
46
        $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...
47
        $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...
48
49
        $commandTester = new CommandTester($command);
50
        $commandTester->execute(
51
            array(
52
                'command' => $command->getName(),
53
                'module'  => 'magento_customer',
54
                'version' => '1.2.3'
55
            )
56
        );
57
58
        $this->assertContains(
59
            'Successfully updated: "Magento_Customer" to version: "1.2.3"',
60
            $commandTester->getDisplay()
61
        );
62
    }
63
}
64