diff --git a/tests/functional/DBConnection.php b/phpunit/functional/DBConnectionTest.php similarity index 91% rename from tests/functional/DBConnection.php rename to phpunit/functional/DBConnectionTest.php index 3f97c5b01ea..098a6114a76 100644 --- a/tests/functional/DBConnection.php +++ b/phpunit/functional/DBConnectionTest.php @@ -39,9 +39,9 @@ /* Test for inc/dbconnection.class.php */ -class DBConnection extends \GLPITestCase +class DBConnectionTest extends \GLPITestCase { - protected function setConnectionCharsetProvider() + public static function setConnectionCharsetProvider() { return [ [ @@ -62,21 +62,24 @@ protected function setConnectionCharsetProvider() */ public function testSetConnectionCharset(bool $utf8mb4, string $expected_charset, string $expected_query) { - $this->mockGenerator->orphanize('__construct'); - $dbh = new \mock\mysqli(); + $dbh = $this->createMock(\mysqli::class); + $dbh->method('set_charset')->willReturn(true); $queries = []; - $this->calling($dbh)->set_charset = true; - $this->calling($dbh)->query = function ($query) use (&$queries) { - $queries[] = $query; - return true; - }; + $dbh->expects($this->once()) + ->method('query') + ->willReturnCallback( + function ($query) use (&$queries) { + $queries[] = $query; + return true; + } + ); \DBConnection::setConnectionCharset($dbh, $utf8mb4); - $this->mock($dbh)->call('set_charset')->withArguments($expected_charset)->once(); - $this->array($queries)->isIdenticalTo([$expected_query]); + + $this->assertSame([$expected_query], $queries); } - protected function mainConfigPropertiesProvider() + public static function mainConfigPropertiesProvider() { return [ [ @@ -185,14 +188,14 @@ public function testCreateMainConfig( $allow_signed_keys, vfsStream::url('config-dir') ); - $this->boolean($result)->isTrue(); + $this->assertTrue($result); $path = vfsStream::url('config-dir/config_db.php'); - $this->boolean(file_exists($path))->isTrue(); - $this->string(file_get_contents($path))->isEqualTo($expected); + $this->assertTrue(file_exists($path)); + $this->assertEquals($expected, file_get_contents($path)); } - protected function slaveConfigPropertiesProvider() + public static function slaveConfigPropertiesProvider() { return [ [ @@ -308,14 +311,14 @@ public function testCreateSlaveConnectionFile( $allow_signed_keys, vfsStream::url('config-dir') ); - $this->boolean($result)->isTrue(); + $this->assertTrue($result); $path = vfsStream::url('config-dir/config_db_slave.php'); - $this->boolean(file_exists($path))->isTrue(); - $this->string(file_get_contents($path))->isEqualTo($expected, file_get_contents($path)); + $this->assertTrue(file_exists($path)); + $this->assertEquals($expected, file_get_contents($path), file_get_contents($path)); } - protected function updatePropertiesProvider() + public static function updatePropertiesProvider() { return [ [ @@ -500,13 +503,13 @@ public function testUpdateConfigProperty( foreach ($properties as $name => $new_value) { $result = \DBConnection::updateConfigProperty($name, $new_value, $update_slave, vfsStream::url('config-dir')); - $this->boolean($result)->isEqualTo($expected_result); + $this->assertEquals($expected_result, $result); } foreach ($expected_config_files as $filename => $contents) { $path = vfsStream::url('config-dir/' . $filename); - $this->boolean(file_exists($path))->isTrue(); - $this->string(file_get_contents($path))->isEqualTo($contents); + $this->assertTrue(file_exists($path)); + $this->assertEquals($contents, file_get_contents($path)); } } @@ -523,12 +526,12 @@ public function testUpdateConfigProperties( vfsStream::setup('config-dir', null, $init_config_files); $result = \DBConnection::updateConfigProperties($properties, $update_slave, vfsStream::url('config-dir')); - $this->boolean($result)->isEqualTo($expected_result); + $this->assertEquals($expected_result, $result); foreach ($expected_config_files as $filename => $contents) { $path = vfsStream::url('config-dir/' . $filename); - $this->boolean(file_exists($path))->isTrue(); - $this->string(file_get_contents($path))->isEqualTo($contents); + $this->assertTrue(file_exists($path)); + $this->assertEquals($contents, file_get_contents($path)); } } } diff --git a/tests/functional/DB.php b/phpunit/functional/DBTest.php similarity index 75% rename from tests/functional/DB.php rename to phpunit/functional/DBTest.php index de6181a8dca..30d2780733e 100644 --- a/tests/functional/DB.php +++ b/phpunit/functional/DBTest.php @@ -35,46 +35,49 @@ namespace tests\units; +use Monolog\Logger; use Psr\Log\LogLevel; /* Test for inc/dbmysql.class.php */ -class DB extends \GLPITestCase +class DBTest extends \GLPITestCase { public function testTableExist() { - $this - ->if($this->newTestedInstance) - ->then - ->boolean($this->testedInstance->tableExists('glpi_configs'))->isTrue() - ->boolean($this->testedInstance->tableExists('fakeTable'))->isFalse(); + $instance = new \DB(); + $this->assertTrue($instance->tableExists('glpi_configs')); + $this->assertFalse($instance->tableExists('fakeTable')); } public function testFieldExists() { - $this - ->if($this->newTestedInstance) - ->then - ->boolean($this->testedInstance->fieldExists('glpi_configs', 'id'))->isTrue() - ->boolean($this->testedInstance->fieldExists('glpi_configs', 'ID'))->isFalse() - ->boolean($this->testedInstance->fieldExists('glpi_configs', 'fakeField'))->isFalse() - ->when( - function () { - $this->boolean($this->testedInstance->fieldExists('fakeTable', 'id'))->isFalse(); - } - )->error - ->withType(E_USER_WARNING) - ->exists() - ->when( - function () { - $this->boolean($this->testedInstance->fieldExists('fakeTable', 'fakeField'))->isFalse(); - } - )->error - ->withType(E_USER_WARNING) - ->exists(); + $instance = new \DB(); + $this->assertTrue($instance->fieldExists('glpi_configs', 'id')); + $this->assertFalse($instance->fieldExists('glpi_configs', 'ID')); + $this->assertFalse($instance->fieldExists('glpi_configs', 'fakeField')); } - protected function dataName() + public function testFieldExistsNoTable() + { + $instance = new \DB(); + $this->assertFalse($instance->fieldExists('fakeTable', 'id')); + $this->hasPhpLogRecordThatContains( + 'Table fakeTable does not exist', + Logger::WARNING + ); + } + + public function testFieldExistsNoTableNoField() + { + $instance = new \DB(); + $this->assertFalse($instance->fieldExists('fakeTable', 'fakeField')); + $this->hasPhpLogRecordThatContains( + 'Table fakeTable does not exist', + Logger::WARNING + ); + } + + public static function nameProvider() { return [ ['field', '`field`'], @@ -89,14 +92,14 @@ protected function dataName() } /** - * @dataProvider dataName + * @dataProvider nameProvider */ public function testQuoteName($raw, $quoted) { - $this->string(\DBmysql::quoteName($raw))->isIdenticalTo($quoted); + $this->assertSame($quoted, \DBmysql::quoteName($raw)); } - protected function dataValue() + public static function dataValue() { return [ ['foo', "'foo'"], @@ -119,11 +122,11 @@ protected function dataValue() */ public function testQuoteValue($raw, $expected) { - $this->string(\DBmysql::quoteValue($raw))->isIdenticalTo($expected); + $this->assertSame($expected, \DBmysql::quoteValue($raw)); } - protected function dataInsert() + public static function dataInsert() { return [ [ @@ -166,13 +169,11 @@ protected function dataInsert() */ public function testBuildInsert($table, $values, $expected) { - $this - ->if($this->newTestedInstance) - ->then - ->string($this->testedInstance->buildInsert($table, $values))->isIdenticalTo($expected); + $instance = new \DB(); + $this->assertSame($expected, $instance->buildInsert($table, $values)); } - protected function dataUpdate() + public static function dataUpdate() { return [ [ @@ -267,25 +268,18 @@ protected function dataUpdate() */ public function testBuildUpdate($table, $values, $where, array $joins, $expected) { - $this - ->if($this->newTestedInstance) - ->then - ->string($this->testedInstance->buildUpdate($table, $values, $where, $joins))->isIdenticalTo($expected); + $instance = new \DB(); + $this->assertSame($expected, $instance->buildUpdate($table, $values, $where, $joins)); } public function testBuildUpdateWException() { - $this->exception( - function () { - $this - ->if($this->newTestedInstance) - ->then - ->string($this->testedInstance->buildUpdate('table', ['a' => 'b'], []))->isIdenticalTo(''); - } - )->hasMessage('Cannot run an UPDATE query without WHERE clause!'); + $instance = new \DB(); + $this->expectExceptionMessage('Cannot run an UPDATE query without WHERE clause!'); + $instance->buildUpdate('table', ['a' => 'b'], []); } - protected function dataDelete() + public static function dataDelete() { return [ [ @@ -351,80 +345,64 @@ protected function dataDelete() */ public function testBuildDelete($table, $where, array $joins, $expected) { - $this - ->if($this->newTestedInstance) - ->then - ->string($this->testedInstance->buildDelete($table, $where, $joins))->isIdenticalTo($expected); + $instance = new \DB(); + $this->assertSame($expected, $instance->buildDelete($table, $where, $joins)); } public function testBuildDeleteWException() { - $this->exception( - function () { - $this - ->if($this->newTestedInstance) - ->then - ->string($this->testedInstance->buildDelete('table', []))->isIdenticalTo(''); - } - )->hasMessage('Cannot run an DELETE query without WHERE clause!'); + $instance = new \DB(); + $this->expectExceptionMessage('Cannot run an DELETE query without WHERE clause!'); + $instance->buildDelete('table', []); } public function testListTables() { - $this - ->if($this->newTestedInstance) - ->then - ->given($tables = $this->testedInstance->listTables()) - ->object($tables) - ->isInstanceOf(\DBmysqlIterator::class) - ->integer(count($tables)) - ->isGreaterThan(100) - ->given($tables = $this->testedInstance->listTables('glpi_configs')) - ->object($tables) - ->isInstanceOf(\DBmysqlIterator::class) - ->hasSize(1); + $instance = new \DB(); + $tables = $instance->listTables(); + $this->assertInstanceOf(\DBmysqlIterator::class, $tables); + $this->assertGreaterThan(100, count($tables)); + $tables = $instance->listTables('glpi_configs'); + $this->assertInstanceOf(\DBmysqlIterator::class, $tables); + $this->assertCount(1, $tables); } public function testTablesHasItemtype() { $dbu = new \DbUtils(); - $this->newTestedInstance(); - $list = $this->testedInstance->listTables(); - $this->object($list)->isInstanceOf(\DBmysqlIterator::class); - $this->integer(count($list))->isGreaterThan(200); + $instance = new \DB(); + $list = $instance->listTables(); + $this->assertInstanceOf(\DBmysqlIterator::class, $list); + $this->assertGreaterThan(200, count($list)); - //check if each table has a corresponding itemtype + //check if each table has a corresponding itemtype foreach ($list as $line) { - $this->array($line) - ->hasSize(1); + $this->assertCount(1, $line); $table = $line['TABLE_NAME']; if ($table == 'glpi_appliancerelations') { //FIXME temporary hack for unit tests continue; } $type = $dbu->getItemTypeForTable($table); - $this->string($type)->isNotEqualTo('UNKNOWN', "$table does not have corresponding item"); - - $this->string($type)->isNotEqualTo('UNKNOWN', 'Cannot find type for table ' . $table); - $this->object($item = $dbu->getItemForItemtype($type))->isInstanceOf('CommonDBTM', $table); - $this->string(get_class($item))->isIdenticalTo($type); - $this->string($dbu->getTableForItemType($type))->isIdenticalTo($table); + $this->assertNotEquals('UNKNOWN', $type, 'Cannot find type for table ' . $table); + $item = $dbu->getItemForItemtype($type); + $this->assertInstanceOf(\CommonDBTM::class, $item, get_class($item)); + $this->assertEquals($type, get_class($item)); + $this->assertEquals($table, $dbu->getTableForItemType($type)); } } public function testEscape() { - $this - ->if($this->newTestedInstance) - ->then - ->string($this->testedInstance->escape('nothing to do'))->isIdenticalTo('nothing to do') - ->string($this->testedInstance->escape("shoul'be escaped"))->isIdenticalTo("shoul\\'be escaped") - ->string($this->testedInstance->escape("First\nSecond"))->isIdenticalTo("First\\nSecond") - ->string($this->testedInstance->escape("First\rSecond"))->isIdenticalTo("First\\rSecond") - ->string($this->testedInstance->escape('Hi, "you"'))->isIdenticalTo('Hi, \\"you\\"'); + $instance = new \DB(); + $this->assertSame('nothing to do', $instance->escape('nothing to do')); + $this->assertSame("shoul\\'be escaped", $instance->escape("shoul'be escaped")); + $this->assertSame("First\\nSecond", $instance->escape("First\nSecond")); + $this->assertSame("First\\rSecond", $instance->escape("First\rSecond")); + $this->assertSame('Hi, \\"you\\"', $instance->escape('Hi, "you"')); } - protected function commentsProvider() + public static function commentsProvider() { return [ [ @@ -444,23 +422,21 @@ protected function commentsProvider() */ public function testRemoveSqlComments($sql, $expected) { - $this - ->if($this->newTestedInstance) - ->then - ->string($this->testedInstance->removeSqlComments($sql))->isIdenticalTo($expected); + $instance = new \DB(); + $this->assertSame($expected, $instance->removeSqlComments($sql)); } /** * Sql expressions provider */ - protected function sqlProvider() + public static function sqlProvider() { return array_merge([ [ 'sql' => "SQL;\n-- comment;\n\nSQL2;", 'expected' => "SQL;\n\nSQL2;" ] - ], $this->commentsProvider()); + ], self::commentsProvider()); } /** @@ -468,13 +444,11 @@ protected function sqlProvider() */ public function testRemoveSqlRemarks($sql, $expected) { - $this - ->if($this->newTestedInstance) - ->then - ->string($this->testedInstance->removeSqlRemarks($sql))->isIdenticalTo($expected); + $instance = new \DB(); + $this->assertSame($expected, $instance->removeSqlRemarks($sql)); } - protected function tableOptionProvider(): iterable + public static function tableOptionProvider(): iterable { yield [ 'sql' => <<when( - function () use ($db, $create_query_template, $drop_query_template, $table) { - $db->doQuery(sprintf($create_query_template, $table)); - $db->doQuery(sprintf($drop_query_template, $table)); - } - )->error() - ->withType(E_USER_WARNING) - ->withMessage(str_replace(['{$table}'], [$table], $warning ?? '')) - ->$asserter(); + $db->doQuery(sprintf($create_query_template, $table)); + $db->doQuery(sprintf($drop_query_template, $table)); + + if ($warning !== null) { + $this->hasPhpLogRecordThatContains( + str_replace(['{$table}'], [$table], $warning), + LogLevel::WARNING + ); + } } public function testSavepoints() @@ -718,44 +692,45 @@ public function testSavepoints() 'name' => 'computer0', 'entities_id' => 0 ]); - $this->integer($computers_id_0)->isGreaterThan(0); + $this->assertGreaterThan(0, $computers_id_0); $DB->setSavepoint('save1', false); $computers_id_1 = $computer->add([ 'name' => 'computer1', 'entities_id' => 0 ]); - $this->integer($computers_id_1)->isGreaterThan(0); - $this->boolean($computer->getFromDB($computers_id_1))->isTrue(); + $this->assertGreaterThan(0, $computers_id_1); + $this->assertTrue($computer->getFromDB($computers_id_1)); $DB->rollBack('save1'); - $this->boolean($computer->getFromDB($computers_id_1))->isFalse(); - $this->boolean($computer->getFromDB($computers_id_0))->isTrue(); + $this->assertFalse($computer->getFromDB($computers_id_1)); + $this->assertTrue($computer->getFromDB($computers_id_0)); $DB->rollBack('save0'); - $this->boolean($computer->getFromDB($computers_id_1))->isFalse(); - $this->boolean($computer->getFromDB($computers_id_0))->isFalse(); + $this->assertFalse($computer->getFromDB($computers_id_1)); + $this->assertFalse($computer->getFromDB($computers_id_0)); $DB->rollBack(); } public function testGetLastQueryWarnings() { - $db = new \mock\DB(); + $db = new \DB(); $db->doQuery('SELECT 1/0'); - $this->array($db->getLastQueryWarnings())->isEqualTo( + $this->assertEquals( [ [ 'Level' => 'Warning', 'Code' => 1365, 'Message' => 'Division by 0', ] - ] + ], + $db->getLastQueryWarnings() ); $this->hasSqlLogRecordThatContains('1365: Division by 0', LogLevel::WARNING); $db->doQuery('SELECT CAST("1a" AS SIGNED), CAST("123b" AS SIGNED)'); - $this->array($db->getLastQueryWarnings())->isEqualTo( + $this->assertEquals( [ [ 'Level' => 'Warning', @@ -767,7 +742,8 @@ public function testGetLastQueryWarnings() 'Code' => 1292, 'Message' => 'Truncated incorrect INTEGER value: \'123b\'', ] - ] + ], + $db->getLastQueryWarnings() ); $this->hasSqlLogRecordThatContains( '1292: Truncated incorrect INTEGER value: \'1a\'' . "\n" . '1292: Truncated incorrect INTEGER value: \'123b\'', @@ -775,7 +751,7 @@ public function testGetLastQueryWarnings() ); } - protected function dataDrop() + public static function dataDrop() { return [ [ @@ -807,22 +783,15 @@ protected function dataDrop() */ public function testBuildDrop($name, $type, $exists, $expected) { - $this - ->if($this->newTestedInstance) - ->then - ->string($this->testedInstance->buildDrop($name, $type, $exists))->isIdenticalTo($expected); + $instance = new \DB(); + $this->assertSame($expected, $instance->buildDrop($name, $type, $exists)); } public function testBuildDropWException() { - $this->exception( - function () { - $this - ->if($this->newTestedInstance) - ->then - ->string($this->testedInstance->buildDrop('aname', 'UNKNOWN'))->isIdenticalTo(''); - } - )->hasMessage('Unknown type to drop: UNKNOWN'); + $instance = new \DB(); + $this->expectExceptionMessage('Unknown type to drop: UNKNOWN'); + $this->assertSame('', $instance->buildDrop('aname', 'UNKNOWN')); } /** @@ -832,10 +801,10 @@ function () { public function testDeprecatedDirectSql() { $db = new \DB(); - $this->when($db->query('SELECT 1')) - ->error() - ->withType(E_USER_DEPRECATED) - ->withMessage('Direct query usage is strongly discouraged! Use DB::request() instead.') - ->exists(); + $db->query('SELECT 1'); + $this->hasPhpLogRecordThatContains( + 'Direct query usage is strongly discouraged! Use DB::request() instead.', + Logger::NOTICE + ); } } diff --git a/src/DBmysql.php b/src/DBmysql.php index a0ed1caeec1..b8a78fbefa8 100644 --- a/src/DBmysql.php +++ b/src/DBmysql.php @@ -1439,7 +1439,7 @@ public function buildUpdate($table, $params, $clauses, array $joins = []) str_replace( '%clause', $key, - 'Trying to use an unknonw clause (%clause) building update query!' + 'Trying to use an unknown clause (%clause) building update query!' ) ); }