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

tests/unit/Model/ArticleTest.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
 * ArticleTest
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Tests
15
 * @author Henry Ruhs
16
 *
17
 * @covers Redaxscript\Model\Article
18
 */
19
20
class ArticleTest 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
		$categoryTwo = Db::forTablePrefix('categories')->create();
51
		$categoryTwo
52
			->set(
53
			[
54
				'title' => 'Category Two',
55
				'alias' => 'category-two',
56
				'parent' => $categoryOne->id
57
			])
58
			->save();
59
		$categoryTwoSibling = Db::forTablePrefix('categories')->create();
60
		$categoryTwoSibling
61
			->set(
62
			[
63
				'title' => 'Category Two Sibling',
64
				'alias' => 'category-two-sibling',
65
				'sibling' => $categoryTwo->id
66
			])
67
			->save();
68
		Db::forTablePrefix('articles')
69
			->create()
70
			->set(
71
			[
72
				'title' => 'Article One',
73
				'alias' => 'article-one',
74
				'category' => $categoryOne->id
75
			])
76
			->save();
77
		Db::forTablePrefix('articles')
78
			->create()
79
			->set(
80
			[
81
				'title' => 'Article Two',
82
				'alias' => 'article-two',
83
				'category' => $categoryTwo->id
84
			])
85
			->save();
86
		$articleThree = Db::forTablePrefix('articles')
87
			->create()
88
			->set(
89
			[
90
				'title' => 'Article Three',
91
				'alias' => 'article-three',
92
				'language' => 'en',
93
				'category' => $categoryTwo->id
94
			])
95
			->save();
96
		Db::forTablePrefix('articles')
97
			->create()
98
			->set(
99
				[
100
					'title' => 'Article Three Sibling',
101
					'alias' => 'article-three-sibling',
102
					'language' => 'de',
103
					'sibling' => $articleThree->id,
104
					'category' => $categoryTwoSibling->id
105
				])
106
			->save();
107
		Db::forTablePrefix('articles')
108
			->create()
109
			->set(
110
			[
111
				'title' => 'Article Four',
112
				'alias' => 'article-four'
113
			])
114
			->save();
115
	}
116
117
	/**
118
	 * tearDown
119
	 *
120
	 * @since 4.0.0
121
	 */
122
123
	public function tearDown() : void
124
	{
125
		$this->dropDatabase();
126
	}
127
128
	/**
129
	 * testGetByAlias
130
	 *
131
	 * @since 4.0.0
132
	 *
133
	 * @param string $articleAlias
134
	 * @param int $expect
135
	 *
136
	 * @dataProvider providerAutoloader
137
	 */
138
139
	public function testGetByAlias(string $articleAlias = null, int $expect = null) : void
140
	{
141
		/* setup */
142
143
		$articleModel = new Model\Article();
144
145
		/* actual */
146
147
		$actual = $articleModel->getByAlias($articleAlias)->id;
148
149
		/* compare */
150
151
		$this->assertEquals($expect, $actual);
152
	}
153
154
	/**
155
	 *
156
	 * testCountByCategoryAndLanguage
157
	 *
158
	 * @since 4.0.0
159
	 *
160
	 * @param int $categoryId
161
	 * @param string $language
162
	 * @param int $expect
163
	 *
164
	 * @dataProvider providerAutoloader
165
	 */
166
167
	public function testCountByCategoryAndLanguage(int $categoryId = null, string $language = null, int $expect = null) : void
168
	{
169
		/* setup */
170
171
		$articleModel = new Model\Article();
172
173
		/* actual */
174
175
		$actual = $articleModel->countByCategoryAndLanguage($categoryId, $language);
176
177
		/* compare */
178
179
		$this->assertEquals($expect, $actual);
180
	}
181
182
	/**
183
	 * testGetSiblingByCategoryAndLanguageAndOrderAndStep
184
	 *
185
	 * @since 4.0.0
186
	 *
187
	 * @param int $categoryId
188
	 * @param string $language
189
	 * @param string $orderColumn
190
	 * @param int $limitStep
191
	 * @param array $expectArray
192
	 *
193
	 * @dataProvider providerAutoloader
194
	 */
195
196
	public function testGetSiblingByCategoryAndLanguageAndOrderAndStep(int $categoryId = null, string $language = null, string $orderColumn = null, int $limitStep = null, array $expectArray = []) : void
197
	{
198
		/* setup */
199
200
		$articleModel = new Model\Article();
201
		$setting = $this->settingFactory();
202
		$setting->set('limit', 1);
203
204
		/* actual */
205
206
		$actualArray = [];
207
		$actualObject = $articleModel->getSiblingByCategoryAndLanguageAndOrderAndStep($categoryId, $language, $orderColumn, $limitStep);
208
209
		/* process articles */
210
211
		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...
212
		{
213
			$actualArray[] = $value->alias;
214
		}
215
216
		/* compare */
217
218
		$this->assertEquals($expectArray, $actualArray);
219
	}
220
221
	/**
222
	 * testGetRouteById
223
	 *
224
	 * @since 4.0.0
225
	 *
226
	 * @param int $articleId
227
	 * @param string $expect
228
	 *
229
	 * @dataProvider providerAutoloader
230
	 */
231
232
	public function testGetRouteById(int $articleId = null, string $expect = null) : void
233
	{
234
		/* setup */
235
236
		$articleModel = new Model\Article();
237
238
		/* actual */
239
240
		$actual = $articleModel->getRouteById($articleId);
241
242
		/* compare */
243
244
		$this->assertEquals($expect, $actual);
245
	}
246
}
247