<commit_before><?php

/**
 * This file is part of CodeIgniter 4 framework.
 *
 * (c) CodeIgniter Foundation <<EMAIL>>
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace CodeIgniter\Database\Live;

use CodeIgniter\Database\Database;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;

/**
 * @group DatabaseLive
 *
 * @internal
 */
final class DbUtilsTest extends CIUnitTestCase
{
    use DatabaseTestTrait;

    protected $refresh = true;
    protected $seed    = 'Tests\Support\Database\Seeds\CITestSeeder';
    protected static $origDebug;

    /**
     * This test must run first to store the inital debug value before we tinker with it below
     */
    public function testFirst()
    {
        $this::$origDebug = $this->getPrivateProperty($this->db, 'DBDebug');

        $this->assertIsBool($this::$origDebug);
    }

    public function testUtilsBackup()
    {
        $util = (new Database())->loadUtils($this->db);

        $this->expectException(DatabaseException::class);
        $this->expectExceptionMessage('Unsupported feature of the database platform you are using.');

        $util->backup();
    }

    public function testUtilsBackupWithParamsArray()
    {
        $util = (new Database())->loadUtils($this->db);

        $params = [
            'format' => 'json',
        ];
        $this->expectException(DatabaseException::class);
        $this->expectExceptionMessage('Unsupported feature of the database platform you are using.');

        $util->backup($params);
    }

    public function testUtilsBackupWithParamsString()
    {
        $util = (new Database())->loadUtils($this->db);

        $this->expectException(DatabaseException::class);
        $this->expectExceptionMessage('Unsupported feature of the database platform you are using.');

        $util->backup('db_jobs');
    }

    public function testUtilsListDatabases()
    {
        $util = (new Database())->loadUtils($this->db);

        if (in_array($this->db->DBDriver, ['MySQLi', 'Postgre', 'SQLSRV'], true)) {
            $databases = $util->listDatabases();

            $this->assertContains($this->db->getDatabase(), $databases);
        } elseif ($this->db->DBDriver === 'SQLite3') {
            $this->expectException(DatabaseException::class);
            $this->expectExceptionMessage('Unsupported feature of the database platform you are using.');

            $util->listDatabases();
        }
    }

    public function testUtilsDatabaseExist()
    {
        $util = (new Database())->loadUtils($this->db);

        if (in_array($this->db->DBDriver, ['MySQLi', 'Postgre', 'SQLSRV'], true)) {
            $exist = $util->databaseExists($this->db->getDatabase());

            $this->assertTrue($exist);
        } elseif ($this->db->DBDriver === 'SQLite3') {
            $this->expectException(DatabaseException::class);
            $this->expectExceptionMessage('Unsupported feature of the database platform you are using.');

            $util->databaseExists($this->db->getDatabase());
        }
    }

    public function testUtilsOptimizeDatabase()
    {
        $util = (new Database())->loadUtils($this->db);<commit_msg>test: add test for oci listDatabase method.<commit_after><?php

/**
 * This file is part of CodeIgniter 4 framework.
 *
 * (c) CodeIgniter Foundation <<EMAIL>>
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace CodeIgniter\Database\Live;

use CodeIgniter\Database\Database;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;

/**
 * @group DatabaseLive
 *
 * @internal
 */
final class DbUtilsTest extends CIUnitTestCase
{
    use DatabaseTestTrait;

    protected $refresh = true;
    protected $seed    = 'Tests\Support\Database\Seeds\CITestSeeder';
    protected static $origDebug;

    /**
     * This test must run first to store the inital debug value before we tinker with it below
     */
    public function testFirst()
    {
        $this::$origDebug = $this->getPrivateProperty($this->db, 'DBDebug');

        $this->assertIsBool($this::$origDebug);
    }

    public function testUtilsBackup()
    {
        $util = (new Database())->loadUtils($this->db);

        $this->expectException(DatabaseException::class);
        $this->expectExceptionMessage('Unsupported feature of the database platform you are using.');

        $util->backup();
    }

    public function testUtilsBackupWithParamsArray()
    {
        $util = (new Database())->loadUtils($this->db);

        $params = [
            'format' => 'json',
        ];
        $this->expectException(DatabaseException::class);
        $this->expectExceptionMessage('Unsupported feature of the database platform you are using.');

        $util->backup($params);
    }

    public function testUtilsBackupWithParamsString()
    {
        $util = (new Database())->loadUtils($this->db);

        $this->expectException(DatabaseException::class);
        $this->expectExceptionMessage('Unsupported feature of the database platform you are using.');

        $util->backup('db_jobs');
    }

    public function testUtilsListDatabases()
    {
        $util = (new Database())->loadUtils($this->db);

        if (in_array($this->db->DBDriver, ['MySQLi', 'Postgre', 'SQLSRV', 'OCI8'], true)) {
            $databases = $util->listDatabases();

            $this->assertContains($this->db->getDatabase(), $databases);
        } elseif ($this->db->DBDriver === 'SQLite3') {
            $this->expectException(DatabaseException::class);
            $this->expectExceptionMessage('Unsupported feature of the database platform you are using.');

            $util->listDatabases();
        }
    }

    public function testUtilsDatabaseExist()
    {
        $util = (new Database())->loadUtils($this->db);

        if (in_array($this->db->DBDriver, ['MySQLi', 'Postgre', 'SQLSRV'], true)) {
            $exist = $util->databaseExists($this->db->getDatabase());

            $this->assertTrue($exist);
        } elseif ($this->db->DBDriver === 'SQLite3') {
            $this->expectException(DatabaseException::class);
            $this->expectExceptionMessage('Unsupported feature of the database platform you are using.');

            $util->databaseExists($this->db->getDatabase());
        }
    }

    public function testUtilsOptimizeDatabase()
    {
        $util = (new Database())->loadUtils($this->db);
{'fraction_non_alphanumeric': 0.14107572115384615, 'fraction_numerical': 0.0010516826923076923, 'mean_word_length': 3.0616229408175717, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7648553', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1818, 'n_words': 414}