Completed
Push — develop ( 9affa3...a2b494 )
by Tom
13:26
created

TestCase   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 54
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createCommandTester() 0 11 1
B mockWriterFileCWriteFileAssertion() 0 31 1
1
<?php
2
3
namespace N98\Magento\Command\Developer\Console;
4
5
use Magento\Framework\Filesystem\Directory\WriteInterface;
6
use N98\Magento\Command\TestCase as BaseTestCase;
7
use PHPUnit_Framework_MockObject_MockObject;
8
use Psy\Context;
9
use Symfony\Component\Console\Tester\CommandTester;
10
11
abstract class TestCase extends BaseTestCase
12
{
13
    /**
14
     * @param AbstractConsoleCommand $command
15
     * @return CommandTester
16
     */
17
    public function createCommandTester(AbstractConsoleCommand $command)
18
    {
19
        $di = $this->getApplication()->getObjectManager();
0 ignored issues
show
Bug introduced by
The method getObjectManager 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...
20
21
        $command->setContext(new Context());
22
        $command->setScopeVariable('di', $di);
23
24
        $commandTester = new CommandTester($command);
25
26
        return $commandTester;
27
    }
28
29
    /**
30
     * @param string $reference
31
     * @return PHPUnit_Framework_MockObject_MockObject|WriteInterface
32
     */
33
    protected function mockWriterFileCWriteFileAssertion($reference)
34
    {
35
        $path = sprintf(__DIR__ . '/_files/reference/%s.php', ucfirst($reference));
36
37
        $writerMock = $this->getMock(WriteInterface::class);
38
        $writerMock
39
            ->expects($this->once())
40
            ->method('writeFile')
41
            ->with(
42
                $this->anything(), // param1
43
                $this->callback(function ($subject) use ($path) {
44
                    // apply cs-fixes as the code generator is a mess
45
                    $replacements = [
46
                        // empty class/interface
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
                        '~\{\n\n\n\}\n\n$~' => "{\n}\n",
48
                        // fix end of file for class w content
49
                        '~    \}\n\n\n\}\n\n$~' => "    }\n}\n",
50
                        // fix beginning of class
51
                        '~^(class .*)\n{\n\n~m' => "\\1\n{\n",
52
                    ];
53
                    $buffer = preg_replace(array_keys($replacements), $replacements, $subject);
54
                    $expected = file_get_contents($path);
55
56
                    $this->assertEquals($expected, $buffer);
57
58
                    return $buffer === $expected;
59
                })
60
            );
61
62
        return $writerMock;
63
    }
64
}
65