Completed
Push — master ( cbd317...231b94 )
by Henry
09:43
created

tests/phpunit/Console/Command/ConfigTest.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Tests\Console\Command;
3
4
use org\bovigo\vfs\vfsStream as Stream;
5
use org\bovigo\vfs\vfsStreamFile as StreamFile;
6
use org\bovigo\vfs\vfsStreamWrapper as StreamWrapper;
7
use Redaxscript\Console\Command;
8
use Redaxscript\Tests\TestCaseAbstract;
9
10
/**
11
 * ConfigTest
12
 *
13
 * @since 3.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Tests
17
 * @author Henry Ruhs
18
 *
19
 * @covers Redaxscript\Console\Command\Config
20
 * @covers Redaxscript\Console\Command\CommandAbstract
21
 */
22
23
class ConfigTest extends TestCaseAbstract
24
{
25
	/**
26
	 * array to restore config
27
	 *
28
	 * @var array
29
	 */
30
31
	protected $_configArray = [];
32
33
	/**
34
	 * setUp
35
	 *
36
	 * @since 3.0.0
37
	 */
38
39
	public function setUp()
40
	{
41
		parent::setUp();
42
		Stream::setup('root');
43
		$file = new StreamFile('config.php');
44
		StreamWrapper::getRoot()->addChild($file);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface org\bovigo\vfs\vfsStreamContent as the method addChild() does only exist in the following implementations of said interface: org\bovigo\vfs\DotDirectory, org\bovigo\vfs\vfsStreamDirectory.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
45
		$this->_configArray = $this->_config->get();
46
	}
47
48
	/**
49
	 * tearDown
50
	 *
51
	 * @since 3.1.0
52
	 */
53
54
	public function tearDown()
55
	{
56
		$this->_config->set('dbType', $this->_configArray['dbType']);
57
		$this->_config->set('dbHost', $this->_configArray['dbHost']);
58
		$this->_config->set('dbPrefix', $this->_configArray['dbPrefix']);
59
		$this->_config->set('dbName', $this->_configArray['dbName']);
60
		$this->_config->set('dbUser', $this->_configArray['dbUser']);
61
		$this->_config->set('dbPassword', $this->_configArray['dbPassword']);
62
		$this->_config->set('env', $this->_configArray['env']);
63
		$this->_request->setServer('argv', null);
64
	}
65
66
	/**
67
	 * testNoArgument
68
	 *
69
	 * @since 3.0.0
70
	 */
71
72
	public function testNoArgument()
73
	{
74
		/* setup */
75
76
		$configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config);
77
78
		/* expect and actual */
79
80
		$expect = $configCommand->getHelp();
81
		$actual = $configCommand->run('cli');
82
83
		/* compare */
84
85
		$this->assertEquals($expect, $actual);
86
	}
87
88
	/**
89
	 * testList
90
	 *
91
	 * @since 3.0.0
92
	 */
93
94
	public function testList()
95
	{
96
		/* setup */
97
98
		$this->_config->set('dbPassword', 'test');
99
		$this->_request->setServer('argv',
100
		[
101
			'console.php',
102
			'config',
103
			'list'
104
		]);
105
		$configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config);
106
107
		/* actual */
108
109
		$actual = $configCommand->run('cli');
110
111
		/* compare */
112
113
		$this->assertString($actual);
114
	}
115
116
	/**
117
	 * testSet
118
	 *
119
	 * @since 3.0.0
120
	 */
121
122
	public function testSet()
123
	{
124
		/* setup */
125
126
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
127
		$this->_request->setServer('argv',
128
		[
129
			'console.php',
130
			'config',
131
			'set',
132
			'--db-type',
133
			'sqlite',
134
			'--db-host',
135
			'127.0.0.1'
136
		]);
137
		$configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config);
138
139
		/* expect and actual */
140
141
		$expect = $configCommand->success();
142
		$actual = $configCommand->run('cli');
143
144
		/* compare */
145
146
		$this->assertEquals($expect, $actual);
147
	}
148
149
	/**
150
	 * testSetInvalid
151
	 *
152
	 * @since 3.0.0
153
	 */
154
155
	public function testSetInvalid()
156
	{
157
		/* setup */
158
159
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
160
		$this->_request->setServer('argv',
161
		[
162
			'console.php',
163
			'config',
164
			'set',
165
			'--no-interaction'
166
		]);
167
		$configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config);
168
169
		/* expect and actual */
170
171
		$expect = $configCommand->error();
172
		$actual = $configCommand->run('cli');
173
174
		/* compare */
175
176
		$this->assertEquals($expect, $actual);
177
	}
178
179
	/**
180
	 * testParse
181
	 *
182
	 * @since 3.0.0
183
	 */
184
185
	public function testParse()
186
	{
187
		/* setup */
188
189
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
190
		$this->_request->setServer('argv',
191
		[
192
			'console.php',
193
			'config',
194
			'parse',
195
			'--db-url',
196
			'$DB_URL'
197
		]);
198
		$configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config);
199
200
		/* expect and actual */
201
202
		$expect = $configCommand->success();
203
		$actual = $configCommand->run('cli');
204
205
		/* compare */
206
207
		$this->assertEquals($expect, $actual);
208
	}
209
210
	/**
211
	 * testParseInvalid
212
	 *
213
	 * @since 3.0.0
214
	 */
215
216
	public function testParseInvalid()
217
	{
218
		/* setup */
219
220
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
221
		$this->_request->setServer('argv',
222
		[
223
			'console.php',
224
			'config',
225
			'parse',
226
			'--no-interaction'
227
		]);
228
		$configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config);
229
230
		/* expect and actual */
231
232
		$expect = $configCommand->error();
233
		$actual = $configCommand->run('cli');
234
235
		/* compare */
236
237
		$this->assertEquals($expect, $actual);
238
	}
239
240
	/**
241
	 * testLock
242
	 *
243
	 * @since 3.0.0
244
	 */
245
246
	public function testLock()
247
	{
248
		/* setup */
249
250
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
251
		$this->_request->setServer('argv',
252
		[
253
			'console.php',
254
			'config',
255
			'lock'
256
		]);
257
		$configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config);
258
259
		/* expect and actual */
260
261
		$expect = $configCommand->success();
262
		$actual = $configCommand->run('cli');
263
264
		/* compare */
265
266
		$this->assertEquals($expect, $actual);
267
	}
268
}
269