Completed
Push — master ( 1dd7d1...0c5b7b )
by Tom
09:54 queued 05:02
created

TestCase::getTestMagentoRootFromEnvironment()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 5.3846
cc 8
eloc 16
nc 12
nop 2
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
use RuntimeException;
10
11
/**
12
 * Class TestCase
13
 *
14
 * @codeCoverageIgnore
15
 * @package N98\Magento\Command\PHPUnit
16
 */
17
class TestCase extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @var Application
21
     */
22
    private $application = null;
23
24
    /**
25
     * @var string|null
26
     */
27
    private $root;
28
29
    /**
30
     * @param string $varname name of the environment variable containing the test-root
31
     * @param string $basename name of the stopfile containing the test-root
32
     *
33
     * @return string|null
34
     */
35
    public static function getTestMagentoRootFromEnvironment($varname, $basename)
36
    {
37
        $root = getenv($varname);
38
        if (empty($root) && strlen($basename)) {
39
            $stopfile = getcwd() . '/' . $basename;
40
            if (is_readable($stopfile) && $buffer = rtrim(file_get_contents($stopfile))) {
41
                $root = $buffer;
42
            }
43
        }
44
        if (empty($root)) {
45
            return null;
46
        }
47
48
        # directory test
49
        if (!is_dir($root)) {
50
            throw new RuntimeException(
51
                sprintf("%s path '%s' is not a directory", $varname, $root)
52
            );
53
        }
54
55
        # resolve root to realpath to be independent to current working directory
56
        $rootRealpath = realpath($root);
57
        if (false === $rootRealpath) {
58
            throw new RuntimeException(
59
                sprintf("Failed to resolve %s path '%s' with realpath()", $varname, $root)
60
            );
61
        }
62
63
        return $rootRealpath;
64
    }
65
66
    /**
67
     * getter for the magento root directory of the test-suite
68
     *
69
     * @see ApplicationTest::testExecute
70
     *
71
     * @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...
72
     */
73
    public function getTestMagentoRoot()
74
    {
75
        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...
76
            return $this->root;
77
        }
78
79
        $varname = 'N98_MAGERUN2_TEST_MAGENTO_ROOT';
80
        $basename = '.n98-magerun2';
81
82
        $root = self::getTestMagentoRootFromEnvironment($varname, $basename);
83
84
        if (null === $root) {
85
            $this->markTestSkipped(
86
                "Please specify environment variable $varname with path to your test magento installation!"
87
            );
88
        }
89
90
        return $this->root = $root;
91
    }
92
93
    /**
94
     * @return Application|PHPUnit_Framework_MockObject_MockObject
95
     */
96
    public function getApplication()
97
    {
98
        if ($this->application === null) {
99
            $root = $this->getTestMagentoRoot();
100
101
            /** @var Application|PHPUnit_Framework_MockObject_MockObject $application */
102
            $application = $this->getMock('N98\Magento\Application', array('getMagentoRootFolder'));
103
            $loader = require __DIR__ . '/../../../../../vendor/autoload.php';
104
            $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...
105
            $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...
106
            $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...
107
            $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...
108
109
            $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...
110
        }
111
112
        return $this->application;
113
    }
114
115
    /**
116
     * @return AdapterInterface
117
     */
118
    public function getDatabaseConnection()
119
    {
120
        $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...
121
122
        return $resource->getConnection('write');
123
    }
124
}
125