Completed
Push — master ( d0c173...e44a53 )
by Henry
09:36
created

tests/unit/Model/CommentTest.php (1 issue)

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\Model;
3
4
use Redaxscript\Db;
5
use Redaxscript\Model;
6
use Redaxscript\Tests\TestCaseAbstract;
7
8
/**
9
 * CommentTest
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Tests
15
 * @author Henry Ruhs
16
 *
17
 * @covers Redaxscript\Model\Comment
18
 */
19
20
class CommentTest extends TestCaseAbstract
21
{
22
	/**
23
	 * setUp
24
	 *
25
	 * @since 4.0.0
26
	 */
27
28
	public function setUp() : void
29
	{
30
		parent::setUp();
31
		$optionArray =
32
		[
33
			'adminName' => 'Test',
34
			'adminUser' => 'test',
35
			'adminPassword' => 'test',
36
			'adminEmail' => '[email protected]'
37
		];
38
		$installer = $this->installerFactory();
39
		$installer->init();
40
		$installer->rawCreate();
41
		$installer->insertSettings($optionArray);
42
		$categoryOne = Db::forTablePrefix('categories')->create();
43
		$categoryOne
44
			->set(
45
			[
46
				'title' => 'Category One',
47
				'alias' => 'category-one'
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
		$articleTwo = Db::forTablePrefix('articles')->create();
60
		$articleTwo
61
			->set(
62
			[
63
				'title' => 'Article Two',
64
				'alias' => 'article-two'
65
			])
66
			->save();
67
		Db::forTablePrefix('comments')
68
			->create()
69
			->set(
70
			[
71
				'author' => 'Comment One',
72
				'text' => 'Comment One',
73
				'article' => $articleOne->id
74
			])
75
			->save();
76
		Db::forTablePrefix('comments')
77
			->create()
78
			->set(
79
			[
80
				'author' => 'Comment Two',
81
				'text' => 'Comment Two',
82
				'article' => $articleTwo->id
83
			])
84
			->save();
85
		Db::forTablePrefix('comments')
86
			->create()
87
			->set(
88
			[
89
				'author' => 'Comment Three',
90
				'text' => 'Comment Three',
91
				'language' => 'en',
92
				'article' => $articleTwo->id
93
			])
94
			->save();
95
		Db::forTablePrefix('comments')
96
			->create()
97
			->set(
98
			[
99
				'author' => 'Comment Four',
100
				'text' => 'Comment Four',
101
				'language' => 'de',
102
				'article' => $articleTwo->id
103
			])
104
			->save();
105
	}
106
107
	/**
108
	 * tearDown
109
	 *
110
	 * @since 4.0.0
111
	 */
112
113
	public function tearDown() : void
114
	{
115
		$this->dropDatabase();
116
	}
117
118
	/**
119
	 * testCountByArticleAndLanguage
120
	 *
121
	 * @since 4.0.0
122
	 *
123
	 * @param int $articleId
124
	 * @param string $language
125
	 * @param int $expect
126
	 *
127
	 * @dataProvider providerAutoloader
128
	 */
129
130
	public function testCountByArticleAndLanguage(int $articleId = null, string $language = null, int $expect = null) : void
131
	{
132
		/* setup */
133
134
		$commentModel = new Model\Comment();
135
136
		/* actual */
137
138
		$actual = $commentModel->countByArticleAndLanguage($articleId, $language);
139
140
		/* compare */
141
142
		$this->assertEquals($expect, $actual);
143
	}
144
145
	/**
146
	 * testGetByArticleAndLanguageAndOrderAndStep
147
	 *
148
	 * @since 4.0.0
149
	 *
150
	 * @param int|null $articleId
151
	 * @param string|null $language
152
	 * @param string|null $orderColumn
153
	 * @param int|null $limitStep
154
	 * @param array $expectArray
155
	 *
156
	 * @dataProvider providerAutoloader
157
	 */
158
159
	public function testGetByArticleAndLanguageAndOrderAndStep(int $articleId = null, string $language = null, string $orderColumn = null, int $limitStep = null, array $expectArray = []) : void
160
	{
161
		/* setup */
162
163
		$commentModel = new Model\Comment();
164
		$setting = $this->settingFactory();
165
		$setting->set('limit', 1);
166
167
		/* actual */
168
169
		$actualArray = [];
170
		$actualObject = $commentModel->getByArticleAndLanguageAndOrderAndStep($articleId, $language, $orderColumn, $limitStep);
171
172
		/* process comments */
173
174
		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...
175
		{
176
			$actualArray[] = $value->author;
177
		}
178
179
		/* compare */
180
181
		$this->assertEquals($expectArray, $actualArray);
182
	}
183
184
	/**
185
	 * testGetRouteById
186
	 *
187
	 * @since 4.0.0
188
	 *
189
	 * @param int $commentId
190
	 * @param string $expect
191
	 *
192
	 * @dataProvider providerAutoloader
193
	 */
194
195
	public function testGetRouteById(int $commentId = null, string $expect = null) : void
196
	{
197
		/* setup */
198
199
		$commentModel = new Model\Comment();
200
201
		/* actual */
202
203
		$actual = $commentModel->getRouteById($commentId);
204
205
		/* compare */
206
207
		$this->assertEquals($expect, $actual);
208
	}
209
210
	/**
211
	 * testCreateByArray
212
	 *
213
	 * @since 4.0.0
214
	 *
215
	 * @param array $createArray
216
	 * @param bool $expect
217
	 *
218
	 * @dataProvider providerAutoloader
219
	 */
220
221
	public function testCreateByArray(array $createArray = [], bool $expect = null) : void
222
	{
223
		/* setup */
224
225
		$commentModel = new Model\Comment();
226
227
		/* actual */
228
229
		$actual = $commentModel->createByArray($createArray);
230
231
		/* compare */
232
233
		$this->assertEquals($expect, $actual);
234
	}
235
}
236