Completed
Push — master ( 7de195...5f1ca1 )
by Henry
08:39
created

tests/unit/Controller/LoginTest.php (2 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 Redaxscript\Controller;
5
use Redaxscript\Db;
6
use Redaxscript\Tests\TestCaseAbstract;
7
8
/**
9
 * UserTest
10
 *
11
 * @since 3.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Tests
15
 * @author Henry Ruhs
16
 * @author Balázs Szilágyi
17
 *
18
 * @covers Redaxscript\Controller\ControllerAbstract
19
 * @covers Redaxscript\Controller\Login
20
 */
21
22
class LoginTest extends TestCaseAbstract
23
{
24
	/**
25
	 * setUp
26
	 *
27
	 * @since 3.1.0
28
	 */
29
30
	public function setUp() : void
31
	{
32
		parent::setUp();
33
		$optionArray = $this->getOptionArray();
34
		$installer = $this->installerFactory();
35
		$installer->init();
36
		$installer->rawCreate();
37
		$installer->insertSettings($optionArray);
38
		$installer->insertUsers($optionArray);
39
		$setting = $this->settingFactory();
40
		$setting->set('captcha', 1);
41
	}
42
43
	/**
44
	 * tearDown
45
	 *
46
	 * @since 3.1.0
47
	 */
48
49
	public function tearDown() : void
50
	{
51
		$this->dropDatabase();
52
	}
53
54
	/**
55
	 * testProcess
56
	 *
57
	 * @since 3.0.0
58
	 *
59
	 * @param array $postArray
60
	 * @param array $userArray
61
	 * @param string $method
62
	 * @param string $expect
63
	 *
64
	 * @dataProvider providerAutoloader
65
	 */
66
67
	public function testProcess(array $postArray = [], array $userArray = [], string $method = null, string $expect = null) : void
68
	{
69
		/* setup */
70
71
		Db::forTablePrefix('users')
72
			->whereIdIs(1)
73
			->findOne()
74
			->set('status', $userArray['status'])
75
			->save();
76
		$this->_request->set('post', $postArray);
77
		if ($method)
0 ignored issues
show
Bug Best Practice introduced by
The expression $method of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
78
		{
79
			$loginController = $this
80
				->getMockBuilder('Redaxscript\Controller\Login')
81
				->setConstructorArgs(
82
				[
83
					$this->_registry,
84
					$this->_request,
85
					$this->_language,
86
					$this->_config
87
				])
88
				->setMethods(
89
				[
90
					$method
91
				])
92
				->getMock();
93
94
			/* override */
95
96
			$loginController
97
				->expects($this->any())
98
				->method($method);
99
		}
100
		else
101
		{
102
			$loginController = new Controller\Login($this->_registry, $this->_request, $this->_language, $this->_config);
103
		}
104
105
		/* actual */
106
107
		$actual = $loginController->process();
0 ignored issues
show
The method process does only exist in Redaxscript\Controller\Login, 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...
108
109
		/* compare */
110
111
		$this->assertEquals($expect, $actual);
112
	}
113
}
114