Completed
Pull Request — develop (#243)
by Tom
20:40 queued 07:41
created

TestCase::getTestApplication()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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\TestApplication;
9
use PHPUnit_Framework_MockObject_MockObject;
10
11
/**
12
 * Class TestCase
13
 *
14
 * @codeCoverageIgnore
15
 * @package N98\Magento\Command\PHPUnit
16
 */
17
abstract class TestCase extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @var TestApplication
21
     */
22
    private $testApplication;
23
24
    /**
25
     * getter for the magento root directory of the test-suite
26
     *
27
     * @see ApplicationTest::testExecute
28
     *
29
     * @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...
30
     */
31
    public function getTestMagentoRoot()
32
    {
33
        return $this->getTestApplication()->getTestMagentoRoot();
34
    }
35
36
    /**
37
     * @return Application|PHPUnit_Framework_MockObject_MockObject
38
     */
39
    public function getApplication()
40
    {
41
        return $this->getTestApplication()->getApplication();
42
    }
43
44
    /**
45
     * @return AdapterInterface
46
     */
47
    public function getDatabaseConnection()
48
    {
49
        $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...
50
51
        return $resource->getConnection('write');
52
    }
53
54
    /**
55
     * @return TestApplication
56
     */
57
    private function getTestApplication()
58
    {
59
        if (null === $this->testApplication) {
60
            $this->testApplication = new TestApplication();
61
        }
62
63
        return $this->testApplication;
64
    }
65
}
66