Completed
Branch master (48c558)
by
unknown
05:41
created

InstallTest::testProcessFailure()   B

↳ Parent: InstallTest

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 42
Code Lines 25

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
nc 1
nop 3
1
<?php
2
namespace Redaxscript\Tests\Controller;
3
4
use Redaxscript\Config;
5
use Redaxscript\Controller;
6
use Redaxscript\Installer;
7
use Redaxscript\Language;
8
use Redaxscript\Registry;
9
use Redaxscript\Request;
10
use Redaxscript\Tests\TestCaseAbstract;
11
use org\bovigo\vfs\vfsStream as Stream;
12
use org\bovigo\vfs\vfsStreamFile as StreamFile;
13
use org\bovigo\vfs\vfsStreamWrapper as StreamWrapper;
14
15
/**
16
 * InstallTest
17
 *
18
 * @since 3.0.0
19
 *
20
 * @package Redaxscript
21
 * @category Tests
22
 *
23
 * @author Balázs Szilágyi
24
 */
25
26
class InstallTest extends TestCaseAbstract
27
{
28
	/**
29
	 * instance of the registry class
30
	 *
31
	 * @var object
32
	 */
33
34
	protected $_registry;
35
36
	/**
37
	 * instance of the request class
38
	 *
39
	 * @var object
40
	 */
41
42
	protected $_request;
43
44
	/**
45
	 * instance of the language class
46
	 *
47
	 * @var object
48
	 */
49
50
	protected $_language;
51
52
	/**
53
	 * instance of the config class
54
	 *
55
	 * @var object
56
	 */
57
58
	protected $_config;
59
60
	/**
61
	 * array to restore config
62
	 *
63
	 * @var array
64
	 */
65
66
	protected $_configArray = [];
67
68
	/**
69
	 * setUp
70
	 *
71
	 * @since 3.0.0
72
	 */
73
74
	public function setUp()
75
	{
76
		$this->_registry = Registry::getInstance();
77
		$this->_request = Request::getInstance();
78
		$this->_language = Language::getInstance();
79
		$this->_config = Config::getInstance();
80
		$this->_configArray = $this->_config->get();
81
		$this->_config->set('dbPrefix', 'controller_');
82
	}
83
84
	/**
85
	 * tearDown
86
	 *
87
	 * @since 3.0.0
88
	 */
89
90
	public function tearDown()
91
	{
92
		$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
93
		$installer->init();
94
		$installer->rawDrop();
95
		$this->_config->set('dbPrefix', $this->_configArray['dbPrefix']);
96
	}
97
98
	/**
99
	 * setUpBeforeClass
100
	 *
101
	 * @since 3.0.0
102
	 */
103
104
	public static function setUpBeforeClass()
105
	{
106
		Stream::setup('root');
107
		$file = new StreamFile('config.php');
108
		StreamWrapper::getRoot()->addChild($file);
0 ignored issues
show
Bug introduced by redaxmedia
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...
109
	}
110
111
	/**
112
	 * providerProcess
113
	 *
114
	 * @since 3.0.0
115
	 *
116
	 * @return array
117
	 */
118
119
	public function providerProcess()
120
	{
121
		return $this->getProvider('tests/provider/Controller/install_process.json');
122
	}
123
124
	/**
125
	 * providerProcessFailure
126
	 *
127
	 * @since 3.0.0
128
	 *
129
	 * @return array
130
	 */
131
132
	public function providerProcessFailure()
133
	{
134
		return $this->getProvider('tests/provider/Controller/install_process_failure.json');
135
	}
136
137
	/**
138
	 * providerValidateDatabase
139
	 *
140
	 * @since 3.0.0
141
	 *
142
	 * @return array
143
	 */
144
145
	public function providerValidateDatabase()
146
	{
147
		return $this->getProvider('tests/provider/Controller/install_validate_database.json');
148
	}
149
150
	/**
151
	 * providerValidateAccount
152
	 *
153
	 * @since 3.0.0
154
	 *
155
	 * @return array
156
	 */
157
158
	public function providerValidateAccount()
159
	{
160
		return $this->getProvider('tests/provider/Controller/install_validate_account.json');
161
	}
162
163
	/**
164
	 * providerInstall
165
	 *
166
	 * @since 3.0.0
167
	 *
168
	 * @return array
169
	 */
170
171
	public function providerInstall()
172
	{
173
		return $this->getProvider('tests/provider/Controller/install_install.json');
174
	}
175
176
	/**
177
	 * testProcess
178
	 *
179
	 * @since 3.0.0
180
	 *
181
	 * @param array $postArray
182
	 * @param string $expect
183
	 *
184
	 * @dataProvider providerProcess
185
	 */
186
187
	public function testProcess($postArray = [], $expect = null)
188
	{
189
		/* setup */
190
191
		$postArray['db-type'] = $postArray['db-type'] === '%CURRENT%' ? $this->_config->get('dbType') :  $postArray['db-type'];
192
		$postArray['db-host'] = $postArray['db-host'] === '%CURRENT%' ? $this->_config->get('dbHost') : $postArray['db-host'];
193
		$postArray['db-name'] = $postArray['db-name'] === '%CURRENT%' ? $this->_config->get('dbName') : $postArray['db-name'];
194
		$postArray['db-user'] = $postArray['db-user'] === '%CURRENT%' ? $this->_config->get('dbUser') : $postArray['db-user'];
195
		$postArray['db-password'] = $postArray['db-password'] === '%CURRENT%' ? $this->_config->get('dbPassword') : $postArray['db-password'];
196
		$postArray['db-prefix'] = $postArray['db-prefix'] === '%CURRENT%' ? $this->_config->get('dbPrefix') : $postArray['db-prefix'];
197
		$this->_request->set('post', $postArray);
198
		$this->_config->init(Stream::url('root/config.php'));
199
		$controllerInstall = new Controller\Install($this->_registry, $this->_language, $this->_request, $this->_config);
200
201
		/* actual */
202
203
		$actual = $controllerInstall->process();
204
205
		/* compare */
206
207
		$this->assertEquals($expect, $actual);
208
	}
209
210
	/**
211
	 * testProcessFailure
212
	 *
213
	 * @since 3.0.0
214
	 *
215
	 * @param array $postArray
216
	 * @param string $method
217
	 * @param string $expect
218
	 *
219
	 * @dataProvider providerProcessFailure
220
	 */
221
222
	public function testProcessFailure($postArray = [], $method = null, $expect = null)
223
	{
224
		/* setup */
225
226
		$postArray['db-type'] = $this->_config->get('dbType');
227
		$postArray['db-host'] = $this->_config->get('dbHost');
228
		$postArray['db-name'] = $this->_config->get('dbName');
229
		$postArray['db-user'] = $this->_config->get('dbUser');
230
		$postArray['db-password'] = $this->_config->get('dbPassword');
231
		$postArray['db-prefix'] = $this->_config->get('dbPrefix');
232
		$this->_request->set('post', $postArray);
233
		$this->_config->init(Stream::url('root/config.php'));
234
		$stub = $this
235
			->getMockBuilder('Redaxscript\Controller\Install')
236
			->setConstructorArgs(
237
			[
238
				$this->_registry,
239
				$this->_language,
240
				$this->_request,
241
				$this->_config
242
			])
243
			->setMethods(
244
			[
245
				$method
246
			])
247
			->getMock();
248
249
		/* override */
250
251
		$stub
252
			->expects($this->any())
253
			->method($method)
254
			->will($this->returnValue(false));
255
256
		/* actual */
257
258
		$actual = $stub->process();
259
260
		/* compare */
261
262
		$this->assertEquals($expect, $actual);
263
	}
264
265
	/**
266
	 * testValidateDatabase
267
	 *
268
	 * @since 3.0.0
269
	 *
270
	 * @param array $postArray
271
	 * @param string $expect
272
	 *
273
	 * @dataProvider providerValidateDatabase
274
	 */
275
276
	public function testValidateDatabase($postArray = [], $expect = null)
277
	{
278
		/* setup */
279
280
		$controllerInstall = new Controller\Install($this->_registry, $this->_language, $this->_request, $this->_config);
281
282
		/* actual */
283
284
		$actual = $this->callMethod($controllerInstall, '_validateDatabase',
285
		[
286
			$postArray
287
		]);
288
289
		/* compare */
290
291
		$this->assertEquals($expect, $actual);
292
	}
293
294
	/**
295
	 * testValidateAccount
296
	 *
297
	 * @since 3.0.0
298
	 *
299
	 * @param array $postArray
300
	 * @param string $expect
301
	 *
302
	 * @dataProvider providerValidateAccount
303
	 */
304
305
	public function testValidateAccount($postArray = [], $expect = null)
306
	{
307
		/* setup */
308
309
		$controllerInstall = new Controller\Install($this->_registry, $this->_language, $this->_request, $this->_config);
310
311
		/* actual */
312
313
		$actual = $this->callMethod($controllerInstall, '_validateAccount',
314
		[
315
			$postArray
316
		]);
317
318
		/* compare */
319
320
		$this->assertEquals($expect, $actual);
321
	}
322
323
	/**
324
	 * testInstall
325
	 *
326
	 * @since 3.0.0
327
	 *
328
	 * @param array $installArray
329
	 * @param string $expect
330
	 *
331
	 * @dataProvider providerInstall
332
	 */
333
334
	public function testInstall($installArray = [], $expect = null)
335
	{
336
		/* setup */
337
338
		$controllerInstall = new Controller\Install($this->_registry, $this->_language, $this->_request, $this->_config);
339
340
		/* actual */
341
342
		$actual = $this->callMethod($controllerInstall, '_install',
343
		[
344
			$installArray
345
		]);
346
347
		/* compare */
348
349
		$this->assertEquals($expect, $actual);
350
	}
351
}
352