Completed
Push — master ( 217dea...1fea95 )
by Henry
125:33 queued 85:40
created

tests/phpunit/DbTest.php (1 issue)

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;
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
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
	 * testCountTablePrefix
139
	 *
140
	 * @since 2.4.0
141
	 */
142
143
	public function testCountTablePrefix()
144
	{
145
		/* actual */
146
147
		$actual = Db::countTablePrefix();
148
149
		/* compare */
150
151
		$this->assertEquals(8, $actual);
152
	}
153
154
	/**
155
	 * testForTablePrefix
156
	 *
157
	 * @since 2.2.0
158
	 */
159
160
	public function testForTablePrefix()
161
	{
162
		/* actual */
163
164
		$actual = Db::forTablePrefix('categories')->where('alias', 'category-one')->findOne()->alias;
165
166
		/* compare */
167
168
		$this->assertEquals('category-one', $actual);
169
	}
170
171
	/**
172
	 * testLeftJoinPrefix
173
	 *
174
	 * @since 2.2.0
175
	 */
176
177
	public function testLeftJoinPrefix()
178
	{
179
		/* expect and actual */
180
181
		$expectArray =
182
		[
183
			'category_alias' => 'category-one',
184
			'article_alias' => 'article-one'
185
		];
186
		$actualArray = Db::forTablePrefix('articles')
187
			->tableAlias('a')
188
			->leftJoinPrefix('categories', 'a.category = c.id', 'c')
189
			->select('c.alias', 'category_alias')
190
			->select('a.alias', 'article_alias')
191
			->where('a.alias', 'article-one')
192
			->findArray();
193
194
		/* compare */
195
196
		$this->assertEquals($expectArray, $actualArray[0]);
197
	}
198
199
	/**
200
	 * testWhereLikeMany
201
	 *
202
	 * @since 2.3.0
203
	 */
204
205
	public function testWhereLikeMany()
206
	{
207
		/* actual */
208
209
		$actual = Db::forTablePrefix('articles')->whereLikeMany(
210
		[
211
			'alias'
212
		],
213
		[
214
			'%article-one%'
215
		])
216
		->findOne()->alias;
217
218
		/* compare */
219
220
		$this->assertEquals('article-one', $actual);
221
	}
222
223
	/**
224
	 * testWhereLanguageIs
225
	 *
226
	 * @since 3.0.0
227
	 *
228
	 * @param string $language
229
	 * @param string $expect
230
	 *
231
	 * @dataProvider providerAutoloader
232
	 */
233
234
	public function testWhereLanguageIs(string $language = null, string $expect = null)
235
	{
236
		/* actual */
237
238
		$actual = Db::forTablePrefix('articles')->whereLanguageIs($language)->findOne()->alias;
239
240
		/* compare */
241
242
		$this->assertEquals($expect, $actual);
243
	}
244
245
	/**
246
	 * testOrderBySetting
247
	 *
248
	 * @since 4.0.0
249
	 */
250
251
	public function testOrderBySetting()
252
	{
253
		/* actual */
254
255
		$actual = Db::forTablePrefix('categories')->orderBySetting('rank')->findOne()->alias;
256
257
		/* compare */
258
259
		$this->assertEquals('category-one', $actual);
260
	}
261
262
	/**
263
	 * testLimitBySetting
264
	 *
265
	 * @since 4.0.0
266
	 */
267
268
	public function testLimitBySetting()
269
	{
270
		/* actual */
271
272
		$actual = Db::forTablePrefix('categories')->limitBySetting()->findOne()->alias;
273
274
		/* compare */
275
276
		$this->assertEquals('category-one', $actual);
277
	}
278
}
279