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

tests/unit/Model/SearchTest.php (3 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\Model;
3
4
use Redaxscript\Db;
5
use Redaxscript\Model;
6
use Redaxscript\Tests\TestCaseAbstract;
7
8
/**
9
 * SearchTest
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Tests
15
 * @author Henry Ruhs
16
 *
17
 * @covers Redaxscript\Model\Search
18
 */
19
20
class SearchTest extends TestCaseAbstract
21
{
22
	/**
23
	 * setUp
24
	 *
25
	 * @since 4.0.0
26
	 */
27
28
	public function setUp() : void
29
	{
30
		parent::setUp();
31
		$installer = $this->installerFactory();
32
		$installer->init();
33
		$installer->rawCreate();
34
		$categoryOne = Db::forTablePrefix('categories')->create();
35
		$categoryOne
36
			->set(
37
			[
38
				'title' => 'Category One',
39
				'alias' => 'category-one',
40
				'keywords' => 'category, one'
41
			])
42
			->save();
43
		Db::forTablePrefix('categories')
44
			->create()
45
			->set(
46
			[
47
				'title' => 'Category Two',
48
				'alias' => 'category-two',
49
				'keywords' => 'category, two'
50
			])
51
			->save();
52
		$articleOne = Db::forTablePrefix('articles')->create();
53
		$articleOne
54
			->set(
55
			[
56
				'title' => 'Article One',
57
				'alias' => 'article-one',
58
				'keywords' => 'article, one',
59
				'category' => $categoryOne->id
60
			])
61
			->save();
62
		Db::forTablePrefix('articles')
63
			->create()
64
			->set(
65
			[
66
				'title' => 'Article Two',
67
				'alias' => 'article-two',
68
				'keywords' => 'article, one',
69
				'category' => $categoryOne->id
70
			])
71
			->save();
72
		Db::forTablePrefix('comments')
73
			->create()
74
			->set(
75
			[
76
				'author' => 'Comment One',
77
				'text' => 'Comment One',
78
				'article' => $articleOne->id
79
			])
80
			->save();
81
		Db::forTablePrefix('comments')
82
			->create()
83
			->set(
84
			[
85
				'author' => 'Comment Two',
86
				'text' => 'Comment Two',
87
				'article' => $articleOne->id
88
			])
89
			->save();
90
	}
91
92
	/**
93
	 * tearDown
94
	 *
95
	 * @since 4.0.0
96
	 */
97
98
	public function tearDown() : void
99
	{
100
		$this->dropDatabase();
101
	}
102
103
	/**
104
	 * testGetByTable
105
	 *
106
	 * @since 4.0.0
107
	 *
108
	 * @param string $table
109
	 * @param string $search
110
	 * @param string $language
111
	 * @param array $expectArray
112
	 *
113
	 * @dataProvider providerAutoloader
114
	 */
115
116
	public function testGetByTable(string $table = null, string $search = null, string $language = null, array $expectArray = []) : void
117
	{
118
		/* setup */
119
120
		$searchModel = new Model\Search();
121
122
		/* actual */
123
124
		$actualArray = [];
125
		$actualObject = $searchModel->getByTable($table, $search, $language);
126
127
		/* process search */
128
129
		foreach ($actualObject as $value)
0 ignored issues
show
The expression $actualObject of type object|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
130
		{
131
			$actualArray[] = $value->title ? : $value->author;
132
		}
133
134
		/* compare */
135
136
		$this->assertEquals($expectArray, $actualArray);
137
	}
138
139
	/**
140
	 * testCreateColumnArray
141
	 *
142
	 * @since 4.0.0
143
	 *
144
	 * @param string $table
145
	 * @param array $expectArray
146
	 *
147
	 * @dataProvider providerAutoloader
148
	 */
149
150
	public function testCreateColumnArray(string $table = null, array $expectArray = []) : void
151
	{
152
		/* setup */
153
154
		$searchModel = new Model\Search();
155
156
		/* actual */
157
158
		$actualArray = $this->callMethod($searchModel, '_createColumnArray',
0 ignored issues
show
$searchModel is of type object<Redaxscript\Model\Search>, 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...
159
		[
160
			$table
161
		]);
162
163
		/* compare */
164
165
		$this->assertEquals($expectArray, $actualArray);
166
	}
167
168
169
	/**
170
	 * testCreateLikeArray
171
	 *
172
	 * @since 4.0.0
173
	 *
174
	 * @param string $table
175
	 * @param string $search
176
	 * @param array $expectArray
177
	 *
178
	 * @dataProvider providerAutoloader
179
	 */
180
181
	public function testCreateLikeArray(string $table = null, string $search = null, array $expectArray = []) : void
182
	{
183
		/* setup */
184
185
		$searchModel = new Model\Search();
186
187
		/* actual */
188
189
		$actualArray = $this->callMethod($searchModel, '_createLikeArray',
0 ignored issues
show
$searchModel is of type object<Redaxscript\Model\Search>, 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...
190
		[
191
			$table,
192
			$search
193
		]);
194
195
		/* compare */
196
197
		$this->assertEquals($expectArray, $actualArray);
198
	}
199
}
200