CommentTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 210
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 72 1
A tearDown() 0 4 1
A testCountByArticleAndLanguage() 0 14 1
A testGetByArticleAndLanguageAndOrderAndStep() 0 24 2
A testGetRouteById() 0 14 1
A testCreateByArray() 0 14 1
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 = $this->getOptionArray();
32
		$installer = $this->installerFactory();
33
		$installer->init();
34
		$installer->rawCreate();
35
		$installer->insertSettings($optionArray);
36
		$categoryOne = Db::forTablePrefix('categories')->create();
37
		$categoryOne
38
			->set(
39
			[
40
				'title' => 'Category One',
41
				'alias' => 'category-one'
42
			])
43
			->save();
44
		$articleOne = Db::forTablePrefix('articles')->create();
45
		$articleOne
46
			->set(
47
			[
48
				'title' => 'Article One',
49
				'alias' => 'article-one',
50
				'category' => $categoryOne->id
51
			])
52
			->save();
53
		$articleTwo = Db::forTablePrefix('articles')->create();
54
		$articleTwo
55
			->set(
56
			[
57
				'title' => 'Article Two',
58
				'alias' => 'article-two'
59
			])
60
			->save();
61
		Db::forTablePrefix('comments')
62
			->create()
63
			->set(
64
			[
65
				'author' => 'Comment One',
66
				'text' => 'Comment One',
67
				'article' => $articleOne->id
68
			])
69
			->save();
70
		Db::forTablePrefix('comments')
71
			->create()
72
			->set(
73
			[
74
				'author' => 'Comment Two',
75
				'text' => 'Comment Two',
76
				'article' => $articleTwo->id
77
			])
78
			->save();
79
		Db::forTablePrefix('comments')
80
			->create()
81
			->set(
82
			[
83
				'author' => 'Comment Three',
84
				'text' => 'Comment Three',
85
				'language' => 'en',
86
				'article' => $articleTwo->id
87
			])
88
			->save();
89
		Db::forTablePrefix('comments')
90
			->create()
91
			->set(
92
			[
93
				'author' => 'Comment Four',
94
				'text' => 'Comment Four',
95
				'language' => 'de',
96
				'article' => $articleTwo->id
97
			])
98
			->save();
99
	}
100
101
	/**
102
	 * tearDown
103
	 *
104
	 * @since 4.0.0
105
	 */
106
107
	public function tearDown() : void
108
	{
109
		$this->dropDatabase();
110
	}
111
112
	/**
113
	 * testCountByArticleAndLanguage
114
	 *
115
	 * @since 4.0.0
116
	 *
117
	 * @param int $articleId
118
	 * @param string $language
119
	 * @param int $expect
120
	 *
121
	 * @dataProvider providerAutoloader
122
	 */
123
124
	public function testCountByArticleAndLanguage(int $articleId = null, string $language = null, int $expect = null) : void
125
	{
126
		/* setup */
127
128
		$commentModel = new Model\Comment();
129
130
		/* actual */
131
132
		$actual = $commentModel->countByArticleAndLanguage($articleId, $language);
133
134
		/* compare */
135
136
		$this->assertEquals($expect, $actual);
137
	}
138
139
	/**
140
	 * testGetByArticleAndLanguageAndOrderAndStep
141
	 *
142
	 * @since 4.0.0
143
	 *
144
	 * @param int|null $articleId
145
	 * @param string|null $language
146
	 * @param string|null $orderColumn
147
	 * @param int|null $limitStep
148
	 * @param array $expectArray
149
	 *
150
	 * @dataProvider providerAutoloader
151
	 */
152
153
	public function testGetByArticleAndLanguageAndOrderAndStep(int $articleId = null, string $language = null, string $orderColumn = null, int $limitStep = null, array $expectArray = []) : void
154
	{
155
		/* setup */
156
157
		$commentModel = new Model\Comment();
158
		$setting = $this->settingFactory();
159
		$setting->set('limit', 1);
160
161
		/* actual */
162
163
		$actualArray = [];
164
		$actualObject = $commentModel->getByArticleAndLanguageAndOrderAndStep($articleId, $language, $orderColumn, $limitStep);
165
166
		/* process comments */
167
168
		foreach ($actualObject as $value)
0 ignored issues
show
Bug introduced by
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...
169
		{
170
			$actualArray[] = $value->author;
171
		}
172
173
		/* compare */
174
175
		$this->assertEquals($expectArray, $actualArray);
176
	}
177
178
	/**
179
	 * testGetRouteById
180
	 *
181
	 * @since 4.0.0
182
	 *
183
	 * @param int $commentId
184
	 * @param string $expect
185
	 *
186
	 * @dataProvider providerAutoloader
187
	 */
188
189
	public function testGetRouteById(int $commentId = null, string $expect = null) : void
190
	{
191
		/* setup */
192
193
		$commentModel = new Model\Comment();
194
195
		/* actual */
196
197
		$actual = $commentModel->getRouteById($commentId);
198
199
		/* compare */
200
201
		$this->assertEquals($expect, $actual);
202
	}
203
204
	/**
205
	 * testCreateByArray
206
	 *
207
	 * @since 4.0.0
208
	 *
209
	 * @param array $createArray
210
	 * @param bool $expect
211
	 *
212
	 * @dataProvider providerAutoloader
213
	 */
214
215
	public function testCreateByArray(array $createArray = [], bool $expect = null) : void
216
	{
217
		/* setup */
218
219
		$commentModel = new Model\Comment();
220
221
		/* actual */
222
223
		$actual = $commentModel->createByArray($createArray);
224
225
		/* compare */
226
227
		$this->assertEquals($expect, $actual);
228
	}
229
}
230