Completed
Push — develop ( e1ba4a...6139e1 )
by Tom
04:14
created

TestCase::runsInProductionMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Developer\Console;
4
5
use Magento\Framework\App\State;
6
use Magento\Framework\Filesystem\Directory\WriteInterface;
7
use N98\Magento\Command\TestCase as BaseTestCase;
8
use PHPUnit_Framework_MockObject_MockObject;
9
use Psy\Context;
10
use Symfony\Component\Console\Input\ArgvInput;
11
use Symfony\Component\Console\Output\ConsoleOutput;
12
use Symfony\Component\Console\Tester\CommandTester;
13
14
abstract class TestCase extends BaseTestCase
15
{
16
    protected function setUp()
17
    {
18
        if ($this->runsInProductionMode()) {
19
            $this->markTestSkipped('Developer command is not available in production mode');
20
        }
21
22
        parent::setUp();
23
    }
24
25
    private function runsInProductionMode()
26
    {
27
        $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...
28
29
        $input = new ArgvInput();
30
        $output = new ConsoleOutput();
31
32
        /* @var $mode \Magento\Deploy\Model\Mode */
33
        $mode = $di->create(
34
            'Magento\Deploy\Model\Mode',
35
            [
36
                'input'  => $input,
37
                'output' => $output,
38
            ]
39
        );
40
41
        return $mode->getMode() === State::MODE_PRODUCTION;
42
    }
43
44
    /**
45
     * @param AbstractConsoleCommand $command
46
     * @return CommandTester
47
     */
48
    public function createCommandTester(AbstractConsoleCommand $command)
49
    {
50
        $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...
51
52
        $command->setContext(new Context());
53
        $command->setScopeVariable('di', $di);
54
55
        $commandTester = new CommandTester($command);
56
57
        return $commandTester;
58
    }
59
60
    /**
61
     * @param string $reference
62
     * @return PHPUnit_Framework_MockObject_MockObject|WriteInterface
63
     */
64
    protected function mockWriterFileCWriteFileAssertion($reference)
65
    {
66
        $path = sprintf(__DIR__ . '/_files/reference/%s.php', ucfirst($reference));
67
68
        $writerMock = $this->getMock(WriteInterface::class);
69
        $writerMock
70
            ->expects($this->once())
71
            ->method('writeFile')
72
            ->with(
73
                $this->anything(), // param1
74
                $this->callback(function ($subject) use ($path) {
75
                    // apply cs-fixes as the code generator is a mess
76
                    $replacements = [
77
                        // 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...
78
                        '~\{\n\n\n\}\n\n$~' => "{\n}\n",
79
                        // fix end of file for class w content
80
                        '~    \}\n\n\n\}\n\n$~' => "    }\n}\n",
81
                        // fix beginning of class
82
                        '~^(class .*)\n{\n\n~m' => "\\1\n{\n",
83
                    ];
84
                    $buffer = preg_replace(array_keys($replacements), $replacements, $subject);
85
                    $expected = file_get_contents($path);
86
87
                    $this->assertEquals($expected, $buffer);
88
89
                    return $buffer === $expected;
90
                })
91
            );
92
93
        return $writerMock;
94
    }
95
}
96