Completed
Push — master ( 1da492...320203 )
by Henry
07:00
created

tests/unit/Controller/InstallTest.php (7 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() : void
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() : void
47
	{
48
		$this->dropDatabase();
0 ignored issues
show
The method dropDatabase() does not seem to exist on object<Redaxscript\Tests\Controller\InstallTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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) : void
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
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/pull/3687

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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
		}
98
		else
99
		{
100
			$installController = new Controller\Install($this->_registry, $this->_request, $this->_language, $this->_config);
101
		}
102
103
		/* actual */
104
105
		$actual = $installController->process();
0 ignored issues
show
The method process does only exist in Redaxscript\Controller\Install, but not in PHPUnit\Framework\MockObject\MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
106
107
		/* compare */
108
109
		$this->assertEquals($expect, $actual);
110
	}
111
112
	/**
113
	 * testValidateDatabase
114
	 *
115
	 * @since 3.0.0
116
	 *
117
	 * @param array $postArray
118
	 * @param array $expectArray
119
	 *
120
	 * @dataProvider providerAutoloader
121
	 */
122
123
	public function testValidateDatabase(array $postArray = [], array $expectArray = []) : void
124
	{
125
		/* setup */
126
127
		$installController = new Controller\Install($this->_registry, $this->_request, $this->_language, $this->_config);
128
129
		/* actual */
130
131
		$actualArray = $this->callMethod($installController, '_validateDatabase',
0 ignored issues
show
The method callMethod() does not seem to exist on object<Redaxscript\Tests\Controller\InstallTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
		[
133
			$postArray
134
		]);
135
136
		/* compare */
137
138
		$this->assertEquals($expectArray, $actualArray);
139
	}
140
141
	/**
142
	 * testValidateAccount
143
	 *
144
	 * @since 3.0.0
145
	 *
146
	 * @param array $postArray
147
	 * @param array $expectArray
148
	 *
149
	 * @dataProvider providerAutoloader
150
	 */
151
152
	public function testValidateAccount(array $postArray = [], array $expectArray = []) : void
153
	{
154
		/* setup */
155
156
		$installController = new Controller\Install($this->_registry, $this->_request, $this->_language, $this->_config);
157
158
		/* actual */
159
160
		$actualArray = $this->callMethod($installController, '_validateAccount',
0 ignored issues
show
The method callMethod() does not seem to exist on object<Redaxscript\Tests\Controller\InstallTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
161
		[
162
			$postArray
163
		]);
164
165
		/* compare */
166
167
		$this->assertEquals($expectArray, $actualArray);
168
	}
169
170
	/**
171
	 * testInstall
172
	 *
173
	 * @since 3.0.0
174
	 *
175
	 * @param array $installArray
176
	 * @param bool $expect
177
	 *
178
	 * @dataProvider providerAutoloader
179
	 */
180
181
	public function testInstall(array $installArray = [], bool $expect = null) : void
182
	{
183
		/* setup */
184
185
		$installController = new Controller\Install($this->_registry, $this->_request, $this->_language, $this->_config);
186
187
		/* actual */
188
189
		$actual = $this->callMethod($installController, '_install',
0 ignored issues
show
The method callMethod() does not seem to exist on object<Redaxscript\Tests\Controller\InstallTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
190
		[
191
			$installArray
192
		]);
193
194
		/* compare */
195
196
		$this->assertEquals($expect, $actual);
197
	}
198
}
199