Completed
Push — master ( 45c36c...5fac04 )
by Henry
07:04
created

SearchTest::testGetByTable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 3
nc 3
nop 4
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
			])
41
			->save();
42
		Db::forTablePrefix('categories')
43
			->create()
44
			->set(
45
			[
46
				'title' => 'Category Two',
47
				'alias' => 'category-two'
48
			])
49
			->save();
50
		$articleOne = Db::forTablePrefix('articles')->create();
51
		$articleOne
52
			->set(
53
			[
54
				'title' => 'Article One',
55
				'alias' => 'article-one',
56
				'category' => $categoryOne->id
57
			])
58
			->save();
59
		Db::forTablePrefix('articles')
60
			->create()
61
			->set(
62
			[
63
				'title' => 'Article Two',
64
				'alias' => 'article-two',
65
				'category' => $categoryOne->id
66
			])
67
			->save();
68
		Db::forTablePrefix('comments')
69
			->create()
70
			->set(
71
			[
72
				'author' => 'Comment One',
73
				'text' => 'comment-one',
74
				'article' => $articleOne->id
75
			])
76
			->save();
77
		Db::forTablePrefix('comments')
78
			->create()
79
			->set(
80
			[
81
				'author' => 'Comment Two',
82
				'text' => 'comment-two',
83
				'article' => $articleOne->id
84
			])
85
			->save();
86
	}
87
88
	/**
89
	 * tearDown
90
	 *
91
	 * @since 4.0.0
92
	 */
93
94
	public function tearDown() : void
95
	{
96
		$this->dropDatabase();
97
	}
98
99
	/**
100
	 * testGetByTable
101
	 *
102
	 * @since 4.0.0
103
	 *
104
	 * @param string $table
105
	 * @param string $search
106
	 * @param string $language
107
	 * @param array $expectArray
108
	 *
109
	 * @dataProvider providerAutoloader
110
	 */
111
112
	public function testGetByTable(string $table = null, string $search = null, string $language = null, array $expectArray = []) : void
113
	{
114
		/* setup */
115
116
		$searchModel = new Model\Search();
117
118
		/* actual */
119
120
		$actualArray = [];
121
		$actualObject = $searchModel->getByTable($table, $search, $language);
122
123
		/* process search */
124
125
		foreach ($actualObject as $value)
0 ignored issues
show
Bug introduced by redaxmedia
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...
126
		{
127
			$actualArray[] = $value->alias ? : $value->text;
128
		}
129
130
		/* compare */
131
132
		$this->assertEquals($expectArray, $actualArray);
133
	}
134
135
	/**
136
	 * testCreateColumnArray
137
	 *
138
	 * @since 4.0.0
139
	 *
140
	 * @param string $table
141
	 * @param array $expectArray
142
	 *
143
	 * @dataProvider providerAutoloader
144
	 */
145
146
	public function testCreateColumnArray(string $table = null, array $expectArray = []) : void
147
	{
148
		/* setup */
149
150
		$searchModel = new Model\Search();
151
152
		/* actual */
153
154
		$actualArray = $this->callMethod($searchModel, '_createColumnArray',
155
		[
156
			$table
157
		]);
158
159
		/* compare */
160
161
		$this->assertEquals($expectArray, $actualArray);
162
	}
163
164
165
	/**
166
	 * testCreateLikeArray
167
	 *
168
	 * @since 4.0.0
169
	 *
170
	 * @param string $table
171
	 * @param string $search
172
	 * @param array $expectArray
173
	 *
174
	 * @dataProvider providerAutoloader
175
	 */
176
177
	public function testCreateLikeArray(string $table = null, string $search = null, array $expectArray = []) : void
178
	{
179
		/* setup */
180
181
		$searchModel = new Model\Search();
182
183
		/* actual */
184
185
		$actualArray = $this->callMethod($searchModel, '_createLikeArray',
186
		[
187
			$table,
188
			$search
189
		]);
190
191
		/* compare */
192
193
		$this->assertEquals($expectArray, $actualArray);
194
	}
195
}
196