Skip to content

Commit

Permalink
Use mock'ed db
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed May 27, 2019
1 parent 889897b commit a159c67
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions tests/units/DBmysqlIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@
class DBmysqlIterator extends DbTestCase {

private $it;
private $db;

public function beforeTestMethod($method) {
parent::beforeTestMethod($method);

$db_config = Yaml::parseFile(GLPI_CONFIG_DIR . '/db.yaml');
$dbclass = '\mock\\' . \Glpi\DatabaseFactory::getDbClass($db_config['driver']);
$db = new $dbclass($db_config);
$this->calling($db)->rawQuery->doesNothing;
$this->it = new \DBmysqlIterator($db);
$this->db = new $dbclass($db_config);
$this->calling($this->db)->rawQuery->doesNothing;
$this->it = new \DBmysqlIterator($this->db);
}

public function testQuery() {
Expand All @@ -63,11 +64,9 @@ public function testQuery() {
}

public function testSqlError() {
global $DB;

$this->exception(
function () use ($DB) {
$DB->request('fakeTable');
function () {
$this->db->request('fakeTable');
}
)
->isInstanceOf('GlpitestSQLerror')
Expand Down Expand Up @@ -719,37 +718,33 @@ public function testModern() {


public function testRows() {
global $DB;

$it = $this->it->execute('foo');
$this->integer($it->numrows())->isIdenticalTo(0);
$this->integer(count($it))->isIdenticalTo(0);
$this->boolean($it->next())->isFalse();

$it = $DB->request('glpi_configs', ['context' => 'core', 'name' => 'version']);
$it = $this->db->request('glpi_configs', ['context' => 'core', 'name' => 'version']);
$this->integer($it->numrows())->isIdenticalTo(1);
$this->integer(count($it))->isIdenticalTo(1);
$row = $it->next();
$key = $it->key();
$this->string($row['id'])->isIdenticalTo($key);

$it = $DB->request('glpi_configs', ['context' => 'core']);
$it = $this->db->request('glpi_configs', ['context' => 'core']);
$this->integer($it->numrows())->isGreaterThan(100);
$this->integer(count($it))->isGreaterThan(100);
$this->boolean($it->numrows() == count($it))->isTrue();
}

public function testKey() {
global $DB;

// test keys with absence of 'id' in select
// we should use a incremented position in the first case
// see https://github.com/glpi-project/glpi/pull/3401
// previously, the first query returned only one result
$users_list = iterator_to_array($DB->request([
$users_list = iterator_to_array($this->db->request([
'SELECT' => 'name',
'FROM' => 'glpi_users']));
$users_list2 = iterator_to_array($DB->request([
$users_list2 = iterator_to_array($this->db->request([
'SELECT' => ['id', 'name'],
'FROM' => 'glpi_users']));
$nb = count($users_list);
Expand Down Expand Up @@ -953,7 +948,7 @@ public function testComplexUnionQuery() {
}

public function testComplexUnionQueryAgain() {
global $CFG_GLPI, $DB;
global $CFG_GLPI;

//Old build way
$queries = [];
Expand Down Expand Up @@ -1105,7 +1100,7 @@ public function testComplexUnionQueryAgain() {
'NAME.id AS name_id',
'PORT.id AS port_id',
'ITEM.id AS item_id',
new \QueryExpression("'$itemtype' AS " . $DB->quoteName('item_type'))
new \QueryExpression("'$itemtype' AS " . $this->db->quoteName('item_type'))
]);
$criteria['INNER JOIN'] = $criteria['INNER JOIN'] + [
'glpi_networknames AS NAME' => [
Expand Down Expand Up @@ -1142,8 +1137,8 @@ public function testComplexUnionQueryAgain() {
$criteria['SELECT'] = array_merge($criteria['SELECT'], [
'NAME.id AS name_id',
'PORT.id AS port_id',
new \QueryExpression('NULL AS ' . $DB->quoteName('item_id')),
new \QueryExpression("NULL AS " . $DB->quoteName('item_type')),
new \QueryExpression('NULL AS ' . $this->db->quoteName('item_id')),
new \QueryExpression("NULL AS " . $this->db->quoteName('item_type')),
]);
$criteria['INNER JOIN'] = $criteria['INNER JOIN'] + [
'glpi_networknames AS NAME' => [
Expand Down Expand Up @@ -1174,9 +1169,9 @@ public function testComplexUnionQueryAgain() {
$criteria = $main_criteria;
$criteria['SELECT'] = array_merge($criteria['SELECT'], [
'NAME.id AS name_id',
new \QueryExpression("NULL AS " . $DB->quoteName('port_id')),
new \QueryExpression('NULL AS ' . $DB->quoteName('item_id')),
new \QueryExpression("NULL AS " . $DB->quoteName('item_type'))
new \QueryExpression("NULL AS " . $this->db->quoteName('port_id')),
new \QueryExpression('NULL AS ' . $this->db->quoteName('item_id')),
new \QueryExpression("NULL AS " . $this->db->quoteName('item_type'))
]);
$criteria['INNER JOIN'] = $criteria['INNER JOIN'] + [
'glpi_networknames AS NAME' => [
Expand All @@ -1194,10 +1189,10 @@ public function testComplexUnionQueryAgain() {

$criteria = $main_criteria;
$criteria['SELECT'] = array_merge($criteria['SELECT'], [
new \QueryExpression("NULL AS " . $DB->quoteName('name_id')),
new \QueryExpression("NULL AS " . $DB->quoteName('port_id')),
new \QueryExpression('NULL AS ' . $DB->quoteName('item_id')),
new \QueryExpression("NULL AS " . $DB->quoteName('item_type'))
new \QueryExpression("NULL AS " . $this->db->quoteName('name_id')),
new \QueryExpression("NULL AS " . $this->db->quoteName('port_id')),
new \QueryExpression('NULL AS ' . $this->db->quoteName('item_id')),
new \QueryExpression("NULL AS " . $this->db->quoteName('item_type'))
]);
$criteria['INNER JOIN']['glpi_ipaddresses AS ADDR']['ON'][0]['AND']['ADDR.itemtype'] = ['!=', 'NetworkName'];
$queries[] = $criteria;
Expand Down Expand Up @@ -1267,12 +1262,10 @@ public function testComplexUnionQueryAgain() {
}

public function testAnalyseCrit() {
global $DB;

$crit = [new \QuerySubQuery([
'SELECT' => ['COUNT' => ['users_id']],
'FROM' => 'glpi_groups_users',
'WHERE' => ['groups_id' => new \QueryExpression($DB->quoteName('glpi_groups.id'))]
'WHERE' => ['groups_id' => new \QueryExpression($this->db->quoteName('glpi_groups.id'))]
])];
$this->string($this->it->analyseCrit($crit))->isIdenticalTo("(SELECT COUNT(`users_id`) FROM `glpi_groups_users` WHERE `groups_id` = `glpi_groups`.`id`)");
}
Expand Down

0 comments on commit a159c67

Please sign in to comment.