Completed
Push — develop ( 7623eb...611a18 )
by Tom
03:37
created

TestCase::getMagerunTester()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 10
nc 4
nop 1
1
<?php
2
3
namespace N98\Magento\Command;
4
5
use Magento\Framework\App\ResourceConnection;
6
use Magento\Framework\DB\Adapter\AdapterInterface;
7
use N98\Magento\Application;
8
use N98\Magento\MagerunCommandTester;
9
use N98\Magento\TestApplication;
10
use PHPUnit_Framework_MockObject_MockObject;
11
12
/**
13
 * Class TestCase
14
 *
15
 * @codeCoverageIgnore
16
 * @package N98\Magento\Command\PHPUnit
17
 */
18
abstract class TestCase extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var TestApplication
22
     */
23
    private $testApplication;
24
25
    /**
26
     * getter for the magento root directory of the test-suite
27
     *
28
     * @see ApplicationTest::testExecute
29
     *
30
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
31
     */
32
    public function getTestMagentoRoot()
33
    {
34
        return $this->getTestApplication()->getTestMagentoRoot();
35
    }
36
37
    /**
38
     * @return Application|PHPUnit_Framework_MockObject_MockObject
39
     */
40
    public function getApplication()
41
    {
42
        return $this->getTestApplication()->getApplication();
43
    }
44
45
    /**
46
     * @return AdapterInterface
47
     */
48
    public function getDatabaseConnection()
49
    {
50
        $resource = $this->getApplication()->getObjectManager()->get(ResourceConnection::class);
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
        return $resource->getConnection('write');
53
    }
54
55
    /**
56
     * @return TestApplication
57
     */
58
    private function getTestApplication()
59
    {
60
        if (null === $this->testApplication) {
61
            $this->testApplication = new TestApplication();
62
        }
63
64
        return $this->testApplication;
65
    }
66
67
    /**
68
     * @var array
69
     */
70
    private $testers = array();
71
72
    /**
73
     * @param string|array $command name or input
74
     * @return MagerunCommandTester
75
     */
76
    private function getMagerunTester($command)
77
    {
78
        if (is_string($command)) {
79
            $input = array(
80
                'command' => $command,
81
            );
82
        } else {
83
            $input = $command;
84
        }
85
86
        $hash = md5(json_encode($input));
87
        if (!isset($this->testers[$hash])) {
88
            $this->testers[$hash] = new MagerunCommandTester($this, $input);
89
        }
90
91
        return $this->testers[$hash];
92
    }
93
94
    /**
95
     * @param string|array $command actual command to execute and obtain the display (output) from
96
     * @param string $needle string within the display
97
     * @param string $message [optional]
98
     */
99
    protected function assertDisplayContains($command, $needle, $message = "")
100
    {
101
        $display = $this->getMagerunTester($command)->getDisplay();
102
103
        $this->assertContains($needle, $display, $message);
104
    }
105
106
    /**
107
     * @param string|array $command actual command to execute and obtain the display (output) from
108
     * @param string $needle string within the display
109
     * @param string $message [optional]
110
     */
111
    protected function assertDisplayNotContains($command, $needle, $message = "")
112
    {
113
        $display = $this->getMagerunTester($command)->getDisplay();
114
115
        $this->assertNotContains($needle, $display, $message);
116
    }
117
118
    /**
119
     * @param string|array $command
120
     * @param string $pattern
121
     * @param string $message [optional]
122
     */
123
    protected function assertDisplayRegExp($command, $pattern, $message = "")
124
    {
125
        $display = $this->getMagerunTester($command)->getDisplay();
126
127
        $this->assertRegExp($pattern, $display, $message);
128
    }
129
130
    /**
131
     * Command executes with a status code of zero
132
     *
133
     * @param string|array $command
134
     * @param string $message
135
     * @return MagerunCommandTester
136
     */
137
    protected function assertExecute($command, $message = "")
138
    {
139
        $tester = $this->getMagerunTester($command);
140
        $status = $tester->getStatus();
141
142
        if (strlen($message)) {
143
            $message .= "\n";
144
        }
145
146
        $message .= "Command executes with a status code of zero";
147
148
        $this->assertSame(0, $status, $message);
149
150
        return $tester;
151
    }
152
}
153