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

tests/phpunit/Controller/InstallTest.php (4 issues)

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\Controller;
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\Controller;
8
use Redaxscript\Tests\TestCaseAbstract;
9
10
/**
11
 * InstallTest
12
 *
13
 * @since 3.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Tests
17
 * @author Henry Ruhs
18
 * @author Balázs Szilágyi
19
 *
20
 * @covers Redaxscript\Controller\ControllerAbstract
21
 * @covers Redaxscript\Controller\Install
22
 */
23
24
class InstallTest extends TestCaseAbstract
25
{
26
	/**
27
	 * setUp
28
	 *
29
	 * @since 3.0.0
30
	 */
31
32
	public function setUp()
33
	{
34
		parent::setUp();
35
		Stream::setup('root');
36
		$file = new StreamFile('config.php');
37
		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...
38
	}
39
40
	/**
41
	 * tearDown
42
	 *
43
	 * @since 3.1.0
44
	 */
45
46
	public function tearDown()
47
	{
48
		$this->dropDatabase();
49
	}
50
51
	/**
52
	 * testProcess
53
	 *
54
	 * @since 3.0.0
55
	 *
56
	 * @param array $postArray
57
	 * @param string $method
58
	 * @param string $expect
59
	 *
60
	 * @dataProvider providerAutoloader
61
	 */
62
63
	public function testProcess(array $postArray = [], string $method = null, string $expect = null)
64
	{
65
		/* setup */
66
67
		$postArray['db-type'] = $postArray['db-type'] === '%CURRENT%' ? $this->_config->get('dbType') : $postArray['db-type'];
68
		$postArray['db-host'] = $postArray['db-host'] === '%CURRENT%' ? $this->_config->get('dbHost') : $postArray['db-host'];
69
		$postArray['db-name'] = $postArray['db-name'] === '%CURRENT%' ? $this->_config->get('dbName') : $postArray['db-name'];
70
		$postArray['db-user'] = $postArray['db-user'] === '%CURRENT%' ? $this->_config->get('dbUser') : $postArray['db-user'];
71
		$postArray['db-password'] = $postArray['db-password'] === '%CURRENT%' ? $this->_config->get('dbPassword') : $postArray['db-password'];
72
		$postArray['db-prefix'] = $postArray['db-prefix'] === '%CURRENT%' ? $this->_config->get('dbPrefix') : $postArray['db-prefix'];
73
		$this->_request->set('post', $postArray);
74
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
75
		if ($method)
76
		{
77
			$installController = $this
78
				->getMockBuilder('Redaxscript\Controller\Install')
79
				->setConstructorArgs(
80
				[
81
					$this->_registry,
82
					$this->_request,
83
					$this->_language,
84
					$this->_config
85
				])
86
				->setMethods(
87
				[
88
					$method
89
				])
90
				->getMock();
91
92
			/* override */
93
94
			$installController
95
				->expects($this->any())
96
				->method($method)
97
				->will($this->returnValue(false));
98
		}
99
		else
100
		{
101
			$installController = new Controller\Install($this->_registry, $this->_request, $this->_language, $this->_config);
102
		}
103
104
		/* actual */
105
106
		$actual = $installController->process();
107
108
		/* compare */
109
110
		$this->assertEquals($expect, $actual);
111
	}
112
113
	/**
114
	 * testValidateDatabase
115
	 *
116
	 * @since 3.0.0
117
	 *
118
	 * @param array $postArray
119
	 * @param array $expectArray
120
	 *
121
	 * @dataProvider providerAutoloader
122
	 */
123
124
	public function testValidateDatabase(array $postArray = [], array $expectArray = [])
125
	{
126
		/* setup */
127
128
		$installController = new Controller\Install($this->_registry, $this->_request, $this->_language, $this->_config);
129
130
		/* actual */
131
132
		$actualArray = $this->callMethod($installController, '_validateDatabase',
0 ignored issues
show
$installController is of type object<Redaxscript\Controller\Install>, but the function expects a null|object<Redaxscript\Tests\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
133
		[
134
			$postArray
135
		]);
136
137
		/* compare */
138
139
		$this->assertEquals($expectArray, $actualArray);
140
	}
141
142
	/**
143
	 * testValidateAccount
144
	 *
145
	 * @since 3.0.0
146
	 *
147
	 * @param array $postArray
148
	 * @param array $expectArray
149
	 *
150
	 * @dataProvider providerAutoloader
151
	 */
152
153
	public function testValidateAccount(array $postArray = [], array $expectArray = [])
154
	{
155
		/* setup */
156
157
		$installController = new Controller\Install($this->_registry, $this->_request, $this->_language, $this->_config);
158
159
		/* actual */
160
161
		$actualArray = $this->callMethod($installController, '_validateAccount',
0 ignored issues
show
$installController is of type object<Redaxscript\Controller\Install>, but the function expects a null|object<Redaxscript\Tests\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
162
		[
163
			$postArray
164
		]);
165
166
		/* compare */
167
168
		$this->assertEquals($expectArray, $actualArray);
169
	}
170
171
	/**
172
	 * testInstall
173
	 *
174
	 * @since 3.0.0
175
	 *
176
	 * @param array $installArray
177
	 * @param bool $expect
178
	 *
179
	 * @dataProvider providerAutoloader
180
	 */
181
182
	public function testInstall(array $installArray = [], bool $expect = null)
183
	{
184
		/* setup */
185
186
		$installController = new Controller\Install($this->_registry, $this->_request, $this->_language, $this->_config);
187
188
		/* actual */
189
190
		$actual = $this->callMethod($installController, '_install',
0 ignored issues
show
$installController is of type object<Redaxscript\Controller\Install>, but the function expects a null|object<Redaxscript\Tests\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
191
		[
192
			$installArray
193
		]);
194
195
		/* compare */
196
197
		$this->assertEquals($expect, $actual);
198
	}
199
}
200