Skip to content

Commit

Permalink
Enhance CommonDBTM::getFromDBByCrit() warning trace (#9538)
Browse files Browse the repository at this point in the history
* Enhance CommonDBTM::getFromDBByCrit() warning trace

* Fix test
  • Loading branch information
cedric-anne committed Sep 14, 2021
1 parent 7c4fe46 commit 03a3687
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 6 additions & 4 deletions inc/commondbtm.class.php
Expand Up @@ -345,11 +345,13 @@ public function getFromDBByCrit(array $crit) {
$row = $iter->next();
return $this->getFromDB($row['id']);
} else if (count($iter) > 1) {
Toolbox::logWarning(
trigger_error(
sprintf(
'getFromDBByCrit expects to get one result, %1$s found!',
count($iter)
)
'getFromDBByCrit expects to get one result, %1$s found in query "%2$s".',
count($iter),
$iter->getSql()
),
E_USER_WARNING
);
}
return false;
Expand Down
7 changes: 5 additions & 2 deletions tests/functionnal/Computer.php
Expand Up @@ -350,11 +350,14 @@ public function testGetFromDbByCrit() {
$this->boolean($comp->getFromDBByCrit(['name' => '_test_pc01']))->isTrue();
$this->string($comp->getField('name'))->isIdenticalTo('_test_pc01');

$this->exception(
$this->when(
function () use ($comp) {
$this->boolean($comp->getFromDBByCrit(['name' => ['LIKE', '_test%']]))->isFalse();
}
)->message->contains('getFromDBByCrit expects to get one result, 8 found!');
)->error()
->withType(E_USER_WARNING)
->withMessage('getFromDBByCrit expects to get one result, 8 found in query "SELECT `id` FROM `glpi_computers` WHERE `name` LIKE \'_test%\'".')
->exists();
}

public function testClone() {
Expand Down

0 comments on commit 03a3687

Please sign in to comment.