Completed
Push — master ( f93ca8...fd9012 )
by Henry
11:16 queued 01:19
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 Redaxscript\Console\Command;
5
use Redaxscript\Tests\TestCaseAbstract;
6
use org\bovigo\vfs\vfsStream as Stream;
7
use org\bovigo\vfs\vfsStreamFile as StreamFile;
8
use org\bovigo\vfs\vfsStreamWrapper as StreamWrapper;
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
		/* actual */
140
141
		$actual = $configCommand->run('cli');
142
143
		/* compare */
144
145
		$this->assertTrue($actual);
146
	}
147
148
	/**
149
	 * testSetInvalid
150
	 *
151
	 * @since 3.0.0
152
	 */
153
154
	public function testSetInvalid()
155
	{
156
		/* setup */
157
158
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
159
		$this->_request->setServer('argv',
160
		[
161
			'console.php',
162
			'config',
163
			'set',
164
			'--no-interaction'
165
		]);
166
		$configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config);
167
168
		/* actual */
169
170
		$actual = $configCommand->run('cli');
171
172
		/* compare */
173
174
		$this->assertFalse($actual);
175
	}
176
177
	/**
178
	 * testParse
179
	 *
180
	 * @since 3.0.0
181
	 */
182
183
	public function testParse()
184
	{
185
		/* setup */
186
187
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
188
		$this->_request->setServer('argv',
189
		[
190
			'console.php',
191
			'config',
192
			'parse',
193
			'--db-url',
194
			'mysql://root:[email protected]/test'
195
		]);
196
		$configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config);
197
198
		/* actual */
199
200
		$actual = $configCommand->run('cli');
201
202
		/* compare */
203
204
		$this->assertTrue($actual);
205
	}
206
207
	/**
208
	 * testParseEnv
209
	 *
210
	 * @since 3.0.0
211
	 */
212
213
	public function testParseEnv()
214
	{
215
		/* setup */
216
217
		$dbUrl = getenv('DB_URL');
218
		putenv('DB_URL=mysql://root:[email protected]/test');
219
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
220
		$this->_request->setServer('argv',
221
		[
222
			'console.php',
223
			'config',
224
			'parse',
225
			'--db-url',
226
			'DB_URL',
227
			'--db-env'
228
		]);
229
		$configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config);
230
231
		/* restore */
232
233
		if ($dbUrl)
234
		{
235
			putenv('DB_URL=' . $dbUrl);
236
		}
237
238
		/* actual */
239
240
		$actual = $configCommand->run('cli');
241
242
		/* compare */
243
244
		$this->assertTrue($actual);
245
	}
246
247
	/**
248
	 * testParseInvalid
249
	 *
250
	 * @since 3.0.0
251
	 */
252
253
	public function testParseInvalid()
254
	{
255
		/* setup */
256
257
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
258
		$this->_request->setServer('argv',
259
		[
260
			'console.php',
261
			'config',
262
			'parse',
263
			'--no-interaction'
264
		]);
265
		$configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config);
266
267
		/* actual */
268
269
		$actual = $configCommand->run('cli');
270
271
		/* compare */
272
273
		$this->assertFalse($actual);
274
	}
275
276
	/**
277
	 * testLock
278
	 *
279
	 * @since 3.0.0
280
	 */
281
282
	public function testLock()
283
	{
284
		/* setup */
285
286
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
287
		$this->_request->setServer('argv',
288
		[
289
			'console.php',
290
			'config',
291
			'lock'
292
		]);
293
		$configCommand = new Command\Config($this->_registry, $this->_request, $this->_language, $this->_config);
294
295
		/* actual */
296
297
		$actual = $configCommand->run('cli');
298
299
		/* compare */
300
301
		$this->assertTrue($actual);
302
	}
303
}
304