Completed
Push — master ( 7523f4...6813fb )
by Henry
91:21 queued 62:37
created

DbTest::testSetAutoIncrement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
namespace Redaxscript\Tests;
3
4
use Redaxscript\Db;
5
6
/**
7
 * DbTest
8
 *
9
 * @since 2.2.0
10
 *
11
 * @package Redaxscript
12
 * @category Tests
13
 * @author Henry Ruhs
14
 *
15
 * @covers Redaxscript\Db
16
 */
17
18
class DbTest extends TestCaseAbstract
19
{
20
	/**
21
	 * array to restore config
22
	 *
23
	 * @var array
24
	 */
25
26
	protected $_configArray = [];
27
28
	/**
29
	 * setUp
30
	 *
31
	 * @since 3.0.0
32
	 */
33
34
	public function setUp()
35
	{
36
		parent::setUp();
37
		$optionArray =
38
		[
39
			'adminName' => 'Test',
40
			'adminUser' => 'test',
41
			'adminPassword' => 'test',
42
			'adminEmail' => '[email protected]'
43
		];
44
		$this->_configArray = $this->_config->get();
0 ignored issues
show
Documentation Bug introduced by redaxmedia
It seems like $this->_config->get() can also be of type string or null. However, the property $_configArray is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
45
		$installer = $this->installerFactory();
46
		$installer->init();
47
		$installer->rawCreate();
48
		$installer->insertSettings($optionArray);
49
		$categoryOne = Db::forTablePrefix('categories')->create();
50
		$categoryOne
51
			->set(
52
			[
53
				'title' => 'Category One',
54
				'alias' => 'category-one'
55
			])
56
			->save();
57
		Db::forTablePrefix('articles')
58
			->create()
59
			->set(
60
			[
61
				'title' => 'Article One',
62
				'alias' => 'article-one',
63
				'language' => 'en',
64
				'category' => $categoryOne->id
65
			])
66
			->save();
67
		Db::forTablePrefix('articles')
68
			->create()
69
			->set(
70
			[
71
				'title' => 'Article Two',
72
				'alias' => 'article-two',
73
				'language' => 'de',
74
				'category' => $categoryOne->id
75
			])
76
			->save();
77
	}
78
79
	/**
80
	 * tearDown
81
	 *
82
	 * @since 3.0.0
83
	 */
84
85
	public function tearDown()
86
	{
87
		$this->dropDatabase();
88
		$this->_config->set('dbType', $this->_configArray['dbType']);
89
		$this->_config->set('dbPassword', $this->_configArray['dbPassword']);
90
	}
91
92
	/**
93
	 * testInit
94
	 *
95
	 * @since 3.0.0
96
	 *
97
	 * @param array $configArray
98
	 *
99
	 * @dataProvider providerAutoloader
100
	 */
101
102
	public function testInit(array $configArray = [])
103
	{
104
		/* setup */
105
106
		$this->_config->set('dbType', $configArray['dbType']);
107
		$this->_config->set('dbPassword', $configArray['dbPassword']);
108
		Db::construct($this->_config);
109
		Db::init();
110
111
		/* actual */
112
113
		$actual = Db::getDb();
114
115
		/* compare */
116
117
		$this->assertInstanceOf('PDO', $actual);
118
	}
119
120
	/**
121
	 * testGetStatus
122
	 *
123
	 * @since 2.4.0
124
	 */
125
126
	public function testGetStatus()
127
	{
128
		/* actual */
129
130
		$actual = Db::getStatus();
131
132
		/* compare */
133
134
		$this->assertEquals(2, $actual);
135
	}
136
137
	/**
138
	 * testSetAutoIncrement
139
	 *
140
	 * @since 4.0.0
141
	 */
142
143
	public function testSetAutoIncrement()
144
	{
145
		/* actual */
146
147
		$actual = Db::setAutoIncrement('categories', 0);
148
149
		/* compare */
150
151
		$this->_config->get('dbType') === 'mysql' ? $this->assertTrue($actual) : $this->markTestSkipped();
152
	}
153
154
	/**
155
	 * testCountTablePrefix
156
	 *
157
	 * @since 2.4.0
158
	 */
159
160
	public function testCountTablePrefix()
161
	{
162
		/* actual */
163
164
		$actual = Db::countTablePrefix();
165
166
		/* compare */
167
168
		$this->assertEquals(8, $actual);
169
	}
170
171
	/**
172
	 * testForTablePrefix
173
	 *
174
	 * @since 2.2.0
175
	 */
176
177
	public function testForTablePrefix()
178
	{
179
		/* actual */
180
181
		$actual = Db::forTablePrefix('categories')->where('alias', 'category-one')->findOne()->alias;
182
183
		/* compare */
184
185
		$this->assertEquals('category-one', $actual);
186
	}
187
188
	/**
189
	 * testLeftJoinPrefix
190
	 *
191
	 * @since 2.2.0
192
	 */
193
194
	public function testLeftJoinPrefix()
195
	{
196
		/* expect and actual */
197
198
		$expectArray =
199
		[
200
			'category_alias' => 'category-one',
201
			'article_alias' => 'article-one'
202
		];
203
		$actualArray = Db::forTablePrefix('articles')
204
			->tableAlias('a')
205
			->leftJoinPrefix('categories', 'a.category = c.id', 'c')
206
			->select('c.alias', 'category_alias')
207
			->select('a.alias', 'article_alias')
208
			->where('a.alias', 'article-one')
209
			->findArray();
210
211
		/* compare */
212
213
		$this->assertEquals($expectArray, $actualArray[0]);
214
	}
215
216
	/**
217
	 * testWhereLikeMany
218
	 *
219
	 * @since 2.3.0
220
	 */
221
222
	public function testWhereLikeMany()
223
	{
224
		/* actual */
225
226
		$actual = Db::forTablePrefix('articles')->whereLikeMany(
227
		[
228
			'alias'
229
		],
230
		[
231
			'%article-one%'
232
		])
233
		->findOne()->alias;
234
235
		/* compare */
236
237
		$this->assertEquals('article-one', $actual);
238
	}
239
240
	/**
241
	 * testWhereLanguageIs
242
	 *
243
	 * @since 3.0.0
244
	 *
245
	 * @param string $language
246
	 * @param string $expect
247
	 *
248
	 * @dataProvider providerAutoloader
249
	 */
250
251
	public function testWhereLanguageIs(string $language = null, string $expect = null)
252
	{
253
		/* actual */
254
255
		$actual = Db::forTablePrefix('articles')->whereLanguageIs($language)->findOne()->alias;
256
257
		/* compare */
258
259
		$this->assertEquals($expect, $actual);
260
	}
261
262
	/**
263
	 * testOrderBySetting
264
	 *
265
	 * @since 4.0.0
266
	 */
267
268
	public function testOrderBySetting()
269
	{
270
		/* actual */
271
272
		$actual = Db::forTablePrefix('categories')->orderBySetting('rank')->findOne()->alias;
273
274
		/* compare */
275
276
		$this->assertEquals('category-one', $actual);
277
	}
278
279
	/**
280
	 * testLimitBySetting
281
	 *
282
	 * @since 4.0.0
283
	 */
284
285
	public function testLimitBySetting()
286
	{
287
		/* actual */
288
289
		$actual = Db::forTablePrefix('categories')->limitBySetting()->findOne()->alias;
290
291
		/* compare */
292
293
		$this->assertEquals('category-one', $actual);
294
	}
295
}
296