Completed
Push — develop ( bff84d...4789a9 )
by Tom
05:15
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 N98\Magento\Application;
6
use PHPUnit_Framework_MockObject_MockObject;
7
use RuntimeException;
8
9
/**
10
 * Class TestCase
11
 *
12
 * @codeCoverageIgnore
13
 * @package N98\Magento\Command\PHPUnit
14
 */
15
class TestCase extends \PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * @var Application
19
     */
20
    private $application = null;
21
22
    /**
23
     * @var string|null
24
     */
25
    private $root;
26
27
    /**
28
     * @param string $varname name of the environment variable containing the test-root
29
     * @param string $basename name of the stopfile containing the test-root
30
     *
31
     * @return string|null
32
     */
33
    public static function getTestMagentoRootFromEnvironment($varname, $basename)
34
    {
35
        $root = getenv($varname);
36
        if (empty($root) && strlen($basename)) {
37
            $stopfile = getcwd() . '/' . $basename;
38
            if (is_readable($stopfile) && $buffer = rtrim(file_get_contents($stopfile))) {
39
                $root = $buffer;
40
            }
41
        }
42
        if (empty($root)) {
43
            return;
44
        }
45
46
        # directory test
47
        if (!is_dir($root)) {
48
            throw new RuntimeException(
49
                sprintf("%s path '%s' is not a directory", $varname, $root)
50
            );
51
        }
52
53
        # resolve root to realpath to be independent to current working directory
54
        $rootRealpath = realpath($root);
55
        if (false === $rootRealpath) {
56
            throw new RuntimeException(
57
                sprintf("Failed to resolve %s path '%s' with realpath()", $varname, $root)
58
            );
59
        }
60
61
        return $rootRealpath;
62
    }
63
64
    /**
65
     * getter for the magento root directory of the test-suite
66
     *
67
     * @see ApplicationTest::testExecute
68
     *
69
     * @return string
70
     */
71
    public function getTestMagentoRoot()
72
    {
73
        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...
74
            return $this->root;
75
        }
76
77
        $varname = 'N98_MAGERUN_TEST_MAGENTO_ROOT';
78
        $basename = '.n98-magerun';
79
80
        $root = self::getTestMagentoRootFromEnvironment($varname, $basename);
81
82
        if (null === $root) {
83
            $this->markTestSkipped(
84
                "Please specify environment variable $varname with path to your test magento installation!"
85
            );
86
        }
87
88
        return $this->root = $root;
89
    }
90
91
    /**
92
     * @return Application|PHPUnit_Framework_MockObject_MockObject
93
     */
94
    public function getApplication()
95
    {
96
        if ($this->application === null) {
97
            $root = $this->getTestMagentoRoot();
98
99
            $this->application = $this->getMock(
100
                'N98\Magento\Application',
101
                array('getMagentoRootFolder')
102
            );
103
104
            // Get the composer bootstrap
105
            if (defined('PHPUNIT_COMPOSER_INSTALL')) {
106
                $loader = require PHPUNIT_COMPOSER_INSTALL;
107
            } elseif (file_exists(__DIR__ . '/../../../../../../../autoload.php')) {
108
                // Installed via composer, already in vendor
109
                $loader = require __DIR__ . '/../../../../../../../autoload.php';
110
            } else {
111
                // Check if testing root package without PHPUnit
112
                $loader = require __DIR__ . '/../../../../../vendor/autoload.php';
113
            }
114
115
            $this->application->setAutoloader($loader);
116
            $this->application->expects($this->any())->method('getMagentoRootFolder')->will($this->returnValue($root));
117
118
            spl_autoload_unregister(array(\Varien_Autoload::instance(), 'autoload'));
119
120
            $this->application->init();
121
            $this->application->initMagento();
122
            if ($this->application->getMagentoMajorVersion() == Application::MAGENTO_MAJOR_VERSION_1) {
123
                spl_autoload_unregister(array(\Varien_Autoload::instance(), 'autoload'));
124
            }
125
        }
126
127
        return $this->application;
128
    }
129
130
    /**
131
     * @return \Varien_Db_Adapter_Pdo_Mysql
132
     */
133
    public function getDatabaseConnection()
134
    {
135
        $resource = \Mage::getSingleton('core/resource');
136
137
        return $resource->getConnection('write');
138
    }
139
}
140