Completed
Push — master ( 92bd35...fb5c6c )
by Tom
04:10
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
 * this file is part of magerun
4
 *
5
 * @author Tom Klingenberg <https://github.com/ktomk>
6
 */
7
8
namespace N98\Magento\Api;
9
10
use N98\Magento\TestApplication;
11
use PHPUnit_Framework_TestCase;
12
13
/**
14
 * Class TestCase
15
 *
16
 * @codeCoverageIgnore
17
 * @package N98\Magento\Command\PHPUnit
18
 */
19
abstract class TestCase extends PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @var TestApplication
23
     */
24
    private $testApplication;
25
26
    /**
27
     *
28
     * @param string $type
29
     * @return object of $type configured by Magento DI of the test-application
30
     */
31
    public function getObject($type)
32
    {
33
        $objectManager = $this->getTestApplication()->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...
34
        $object = $objectManager->get($type);
35
        $this->assertInstanceOf($type, $object);
36
37
        return $object;
38
    }
39
40
    /**
41
     * @return TestApplication
42
     */
43
    private function getTestApplication()
44
    {
45
        if (null === $this->testApplication) {
46
            $this->testApplication = new TestApplication();
47
        }
48
49
        return $this->testApplication;
50
    }
51
}
52