Completed
Push — develop ( f63da3...8c2b94 )
by Tom
04:32
created

TestCase::getTestMagentoRoot()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 8.6737
cc 5
eloc 13
nc 7
nop 0
1
<?php
2
3
namespace N98\Magento\Command\PHPUnit;
4
5
use Magento\Framework\App\ResourceConnection;
6
use Magento\Framework\DB\Adapter\AdapterInterface;
7
use N98\Magento\Application;
8
use PHPUnit_Framework_MockObject_MockObject;
9
10
/**
11
 * Class TestCase
12
 *
13
 * @codeCoverageIgnore
14
 * @package N98\Magento\Command\PHPUnit
15
 */
16
class TestCase extends \PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * @var Application
20
     */
21
    private $application = null;
22
23
    /**
24
     * @var string|null
25
     */
26
    private $root;
27
28
    /**
29
     * getter for the magento root directory of the test-suite
30
     *
31
     * @see ApplicationTest::testExecute
32
     *
33
     * @return string
34
     */
35
    public function getTestMagentoRoot()
36
    {
37
        if ($this->root) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->root of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
38
            return $this->root;
39
        }
40
41
        $root = getenv('N98_MAGERUN2_TEST_MAGENTO_ROOT');
42
        if (empty($root)) {
43
            if ($buffer = rtrim(file_get_contents(getcwd() . '/.n98-magerun2'))) {
44
                $root = $buffer;
45
            }
46
        }
47
        if (empty($root)) {
48
            $this->markTestSkipped(
49
                'Please specify environment variable N98_MAGERUN2_TEST_MAGENTO_ROOT with path to your test ' .
50
                'magento installation!'
51
            );
52
        }
53
54
        $this->root = realpath($root);
55
        return $this->root;
56
    }
57
58
    /**
59
     * @return Application|PHPUnit_Framework_MockObject_MockObject
60
     */
61
    public function getApplication()
62
    {
63
        if ($this->application === null) {
64
            $root = $this->getTestMagentoRoot();
65
66
            /** @var Application|PHPUnit_Framework_MockObject_MockObject $application */
67
            $application = $this->getMock('N98\Magento\Application', array('getMagentoRootFolder'));
68
            $loader = require __DIR__ . '/../../../../../vendor/autoload.php';
69
            $application->setAutoloader($loader);
0 ignored issues
show
Bug introduced by
The method setAutoloader 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...
70
            $application->expects($this->any())->method('getMagentoRootFolder')->will($this->returnValue($root));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in N98\Magento\Application.

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...
71
            $application->init();
0 ignored issues
show
Bug introduced by
The method init 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...
72
            $application->initMagento();
0 ignored issues
show
Bug introduced by
The method initMagento 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...
73
74
            $this->application = $application;
0 ignored issues
show
Documentation Bug introduced by
It seems like $application can also be of type object<PHPUnit_Framework_MockObject_MockObject>. However, the property $application is declared as type object<N98\Magento\Application>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
75
        }
76
77
        return $this->application;
78
    }
79
80
    /**
81
     * @return AdapterInterface
82
     */
83
    public function getDatabaseConnection()
84
    {
85
        $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...
86
87
        return $resource->getConnection('write');
88
    }
89
}
90