Completed
Push — master ( 16ddfb...0ea243 )
by Henry
09:18
created

tests/unit/Controller/CommentTest.php (5 issues)

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\Controller;
3
4
use Redaxscript\Controller;
5
use Redaxscript\Db;
6
use Redaxscript\Tests\TestCaseAbstract;
7
8
/**
9
 * CommentTest
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\Comment
19
 * @covers Redaxscript\Controller\ControllerAbstract
20
 */
21
22
class CommentTest 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 =
34
		[
35
			'adminName' => 'Test',
36
			'adminUser' => 'test',
37
			'adminPassword' => 'test',
38
			'adminEmail' => '[email protected]'
39
		];
40
		$installer = $this->installerFactory();
0 ignored issues
show
The method installerFactory() does not seem to exist on object<Redaxscript\Tests\Controller\CommentTest>.

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...
41
		$installer->init();
42
		$installer->rawCreate();
43
		$installer->insertSettings($optionArray);
44
		$categoryOne = Db::forTablePrefix('categories')->create();
45
		$categoryOne
46
			->set(
47
			[
48
				'title' => 'Category One',
49
				'alias' => 'category-one'
50
			])
51
			->save();
52
		Db::forTablePrefix('articles')
53
			->create()
54
			->set(
55
			[
56
				'title' => 'Article One',
57
				'alias' => 'article-one',
58
				'category' => $categoryOne->id
59
60
			])
61
			->save();
62
		$setting = $this->settingFactory();
0 ignored issues
show
The method settingFactory() does not seem to exist on object<Redaxscript\Tests\Controller\CommentTest>.

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...
63
		$setting->set('captcha', 1);
64
	}
65
66
	/**
67
	 * tearDown
68
	 *
69
	 * @since 3.1.0
70
	 */
71
72
	public function tearDown() : void
73
	{
74
		$this->dropDatabase();
0 ignored issues
show
The method dropDatabase() does not seem to exist on object<Redaxscript\Tests\Controller\CommentTest>.

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...
75
	}
76
77
	/**
78
	 * testProcess
79
	 *
80
	 * @since 3.0.0
81
	 *
82
	 * @param array $postArray
83
	 * @param array $settingArray
84
	 * @param string $method
85
	 * @param string $expect
86
	 *
87
	 * @dataProvider providerAutoloader
88
	 */
89
90
	public function testProcess(array $postArray = [], array $settingArray = [], string $method = null, string $expect = null) : void
91
	{
92
		/* setup */
93
94
		$this->_request->set('post', $postArray);
95
		$setting = $this->settingFactory();
0 ignored issues
show
The method settingFactory() does not seem to exist on object<Redaxscript\Tests\Controller\CommentTest>.

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...
96
		$setting->set('notification', $settingArray['notification']);
97
		$setting->set('moderation', $settingArray['moderation']);
98
		if ($method)
99
		{
100
			$commentController = $this
101
				->getMockBuilder('Redaxscript\Controller\Comment')
102
				->setConstructorArgs(
103
				[
104
					$this->_registry,
105
					$this->_request,
106
					$this->_language,
107
					$this->_config
108
				])
109
				->setMethods(
110
				[
111
					$method
112
				])
113
				->getMock();
114
115
			/* override */
116
117
			$commentController
118
				->expects($this->any())
119
				->method($method)
120
				->will($this->returnValue(false));
121
		}
122
		else
123
		{
124
			$commentController = new Controller\Comment($this->_registry, $this->_request, $this->_language, $this->_config);
125
		}
126
127
		/* actual */
128
129
		$actual = $commentController->process();
0 ignored issues
show
The method process does only exist in Redaxscript\Controller\Comment, 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...
130
131
		/* compare */
132
133
		$this->assertEquals($expect, $actual);
134
	}
135
}
136