TestCase   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getApplication() 0 8 2
A getConfig() 0 7 2
A getAccountId() 0 9 3
A getAccessToken() 0 9 3
1
<?php
2
/**
3
 * Magedownload CLI
4
 *
5
 * PHP version 5
6
 *
7
 * @category  MageDownload
8
 * @package   MageDownload
9
 * @author    Steve Robbins <[email protected]>
10
 * @copyright 2015 Steve Robbins
11
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
12
 * @link      https://github.com/steverobbins/magedownload-cli
13
 */
14
15
namespace MageDownload\Command\PHPUnit;
16
17
use MageDownload\Command\Setup;
18
use MageDownload\Config;
19
use PHPUnit_Framework_TestCase;
20
use Symfony\Component\Console\Application;
21
22
/**
23
 * Project test case
24
 *
25
 * @category  MageDownload
26
 * @package   MageDownload
27
 * @author    Steve Robbins <[email protected]>
28
 * @copyright 2015 Steve Robbins
29
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
30
 * @link      https://github.com/steverobbins/magedownload-cli
31
 */
32
class TestCase extends PHPUnit_Framework_TestCase
33
{
34
    /**
35
     * Cached user config
36
     *
37
     * @var Config
38
     */
39
    protected $config;
40
41
    /**
42
     * Set up the application
43
     *
44
     * @return Application
45
     */
46
    public function getApplication()
47
    {
48
        $app = new Application;
49
        foreach (Setup::getCommandClasses() as $class) {
50
            $app->add(new $class);
51
        }
52
        return $app;
53
    }
54
55
    /**
56
     * Get the user's config
57
     *
58
     * @return Config
59
     */
60
    public function getConfig()
61
    {
62
        if ($this->config === null) {
63
            $this->config = new Config;
64
        }
65
        return $this->config;
66
    }
67
68
    /**
69
     * Get Magento account id
70
     *
71
     * @return string
72
     */
73
    public function getAccountId()
0 ignored issues
show
Coding Style introduced by
getAccountId uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
74
    {
75
        if (isset($_SERVER['MAGENTO_ID'])) {
76
            return $_SERVER['MAGENTO_ID'];
77
        } elseif ($id = $this->getConfig()->getAccountId()) {
78
            return $id;
79
        }
80
        return '';
81
    }
82
83
    /**
84
     * Get Magento access token
85
     *
86
     * @return string
87
     */
88
    public function getAccessToken()
0 ignored issues
show
Coding Style introduced by
getAccessToken uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
89
    {
90
        if (isset($_SERVER['MAGENTO_TOKEN'])) {
91
            return $_SERVER['MAGENTO_TOKEN'];
92
        } elseif ($token = $this->getConfig()->getAccessToken()) {
93
            return $token;
94
        }
95
        return '';
96
    }
97
}
98