Completed
Pull Request — develop (#169)
by Robbie
07:51
created

ViewCommandTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Eav\Attribute;
4
5
use Symfony\Component\Console\Tester\CommandTester;
6
use N98\Magento\Command\PHPUnit\TestCase;
7
8
class ViewCommandTest extends TestCase
9
{
10
    /**
11
     * The test subject
12
     * @var ViewCommand
13
     */
14
    private $command;
15
16
    /**
17
     * @var CommandTester
18
     */
19
    private $commandTester;
20
21
    /**
22
     * Initialize the command and the tester
23
     */
24 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
        $this->getApplication()->add(new ViewCommand());
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...
27
        $this->command = $this->getApplication()->find('eav:attribute:view');
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...
28
29
        $this->commandTester = new CommandTester($this->command);
30
    }
31
32
    /**
33
     * Ensure that the ViewCommand returns information about the supplied attribute
34
     * @test
35
     */
36
    public function execute()
37
    {
38
        $this->commandTester->execute(
39
            array(
40
                'command'       => $this->command->getName(),
41
                'entityType'    => 'catalog_product',
42
                'attributeCode' => 'sku',
43
            )
44
        );
45
46
        $result = $this->commandTester->getDisplay();
47
48
        $this->assertContains('sku', $result);
49
        $this->assertContains('catalog_product_entity', $result);
50
        $this->assertContains('Backend-Type', $result);
51
        $this->assertContains('static', $result);
52
    }
53
54
    /**
55
     * When the attribute doesn't exist, an exception should be thrown
56
     * @expectedException InvalidArgumentException
57
     * @test
58
     */
59
    public function executeWithException()
60
    {
61
        $this->commandTester->execute(
62
            array(
63
                'command'       => $this->command->getName(),
64
                'entityType'    => 'catalog_product',
65
                'attributeCode' => 'foo_bar_attribute_that_should_never_ever_ever_exist'
66
67
            )
68
        );
69
    }
70
71
    /**
72
     * Should return extra fields when it's a frontend attribute
73
     * @test
74
     */
75
    public function getTableInput()
76
    {
77
        $withoutFrontend = $this->command->getTableInput(false);
78
        $this->assertArrayHasKey('Name', $withoutFrontend);
79
80
        $withFrontend = $this->command->getTableInput(true);
81
        $this->assertArrayHasKey('Frontend/Label', $withFrontend);
82
    }
83
84
    /**
85
     * Should return an attribute model (from the abstract class)
86
     * @test
87
     */
88
    public function getAttribute()
89
    {
90
        $result = $this->command->getAttribute('catalog_product', 'sku');
91
        $this->assertInstanceOf('Magento\Eav\Model\Entity\Attribute\AbstractAttribute', $result);
92
    }
93
}
94