Completed
Push — master ( 383d0c...3523cb )
by Tom
04:14
created

ConfigTest::loadPartialConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 0
f 1
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/*
3
 * @author Tom Klingenberg <[email protected]>
4
 */
5
6
namespace N98\Magento\Application;
7
8
use Composer\Autoload\ClassLoader;
9
use ErrorException;
10
use N98\Magento\Application;
11
use N98\Magento\Command\PHPUnit\TestCase;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\ArgvInput;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputDefinition;
16
use Symfony\Component\Console\Output\BufferedOutput;
17
use Symfony\Component\Console\Output\NullOutput;
18
19
/**
20
 * Class ConfigTest
21
 *
22
 * @covers  N98\Magento\Application\Config
23
 * @package N98\Magento\Application
24
 */
25
class ConfigTest extends TestCase
26
{
27
    /**
28
     * @test
29
     */
30
    public function creation()
31
    {
32
        $config = new Config();
33
        $this->assertInstanceOf(__NAMESPACE__ . '\\Config', $config);
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function loader()
40
    {
41
        $config = new Config();
42
43
        try {
44
            $config->load();
45
            $this->fail('An expected exception was not thrown');
46
        } catch (ErrorException $e) {
47
            $this->assertEquals('Configuration not yet fully loaded', $e->getMessage());
48
        }
49
50
        $this->assertEquals(array(), $config->getConfig());
51
52
        $loader = $config->getLoader();
53
        $this->assertInstanceOf(__NAMESPACE__ . '\\ConfigurationLoader', $loader);
54
        $this->assertSame($loader, $config->getLoader());
55
56
        $loader->loadStageTwo("");
57
        $config->load();
58
59
        $this->assertInternalType('array', $config->getConfig());
60
        $this->assertGreaterThan(4, count($config->getConfig()));
61
62
        $config->setConfigurationLoader($loader);
63
    }
64
65
    /**
66
     * config array setter is used in some tests on @see \N98\Magento\Application::setConfig()
67
     *
68
     * @test
69
     */
70
    public function setConfig()
71
    {
72
        $config = new Config();
73
        $config->setConfig(array(0, 1, 2));
74
        $this->assertSame($config->getConfig()[1], 1);
75
    }
76
77
    /**
78
     * @test
79
     */
80
    public function configCommandAlias()
0 ignored issues
show
Coding Style introduced by
configCommandAlias 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...
81
    {
82
        $config = new Config();
83
        $input  = new ArgvInput();
84
        $actual = $config->checkConfigCommandAlias($input);
85
        $this->assertInstanceOf('Symfony\Component\Console\Input\InputInterface', $actual);
86
87
        $saved = $_SERVER['argv'];
88
        {
89
            $config->setConfig(array('commands' => array('aliases' => array(array('list-help' => 'list --help')))));
90
            $definition = new InputDefinition();
91
            $definition->addArgument(new InputArgument('command'));
92
93
            $argv            = array('/path/to/command', 'list-help');
94
            $_SERVER['argv'] = $argv;
95
            $input           = new ArgvInput($argv, $definition);
96
            $this->assertSame('list-help', (string) $input);
97
            $actual = $config->checkConfigCommandAlias($input);
98
            $this->assertSame('list-help', $actual->getFirstArgument());
99
            $this->assertSame('list-help --help', (string) $actual);
100
        }
101
        $_SERVER['argv'] = $saved;
102
103
        $command = new Command('list');
104
105
        $config->registerConfigCommandAlias($command);
106
107
        $this->assertSame(array('list-help'), $command->getAliases());
108
    }
109
110
    /**
111
     * @test
112
     */
113 View Code Duplication
    public function customCommands()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $array = array(
116
            'commands' => array(
117
                'customCommands' => array(
118
                    'N98\Magento\Command\Config\GetCommand',
119
                    array('name' => 'N98\Magento\Command\Config\GetCommand'),
120
                )
121
            )
122
        );
123
124
        $output = new BufferedOutput();
125
        $output->setVerbosity($output::VERBOSITY_DEBUG);
126
127
        $config = new Config(array(), false, $output);
128
        $config->setConfig($array);
129
130
        /** @var Application $application */
131
        $application = $this->getMock('N98\Magento\Application');
132
        $config->registerCustomCommands($application);
133
    }
134
135
    /**
136
     * @test
137
     */
138 View Code Duplication
    public function registerCustomAutoloaders()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $array = array(
141
            'autoloaders'      => array('$prefix' => '$path'),
142
            'autoloaders_psr4' => array('$prefix\\' => '$path'),
143
        );
144
145
        $output = new BufferedOutput();
146
147
        $config = new Config(array(), false, $output);
148
        $config->setConfig($array);
149
150
        $autloader = new ClassLoader();
151
        $config->registerCustomAutoloaders($autloader);
152
153
        $output->setVerbosity($output::VERBOSITY_DEBUG);
154
        $config->registerCustomAutoloaders($autloader);
155
    }
156
157
    /**
158
     * @test
159
     */
160
    public function loadPartialConfig()
161
    {
162
        $config = new Config();
163
        $this->assertEquals(array(), $config->getDetectSubFolders());
164
        $config->loadPartialConfig(false);
165
        $actual = $config->getDetectSubFolders();
166
        $this->assertInternalType('array', $actual);
167
        $this->assertNotEquals(array(), $actual);
168
    }
169
}
170