diff --git a/tests/DatabaseTestCase.php b/tests/DatabaseTestCase.php index c175bcc..0849293 100644 --- a/tests/DatabaseTestCase.php +++ b/tests/DatabaseTestCase.php @@ -12,6 +12,7 @@ namespace Swoole\Tests; use PHPUnit\Framework\TestCase; +use Swoole\ConnectionPool; use Swoole\Database\MysqliConfig; use Swoole\Database\MysqliPool; use Swoole\Database\PDOConfig; @@ -27,7 +28,26 @@ */ class DatabaseTestCase extends TestCase { - protected function getMysqliPool(): MysqliPool + protected static string $sqliteDatabaseFile; + + public static function setUpBeforeClass(): void + { + parent::setUpBeforeClass(); + static::$sqliteDatabaseFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('swoole_pdo_pool_sqlite_test_', true); + if (file_exists(static::$sqliteDatabaseFile)) { + unlink(static::$sqliteDatabaseFile); + } + } + + public static function tearDownAfterClass(): void + { + if (file_exists(static::$sqliteDatabaseFile)) { + unlink(static::$sqliteDatabaseFile); + } + parent::tearDownAfterClass(); + } + + protected static function getMysqliPool(int $size = ConnectionPool::DEFAULT_SIZE): MysqliPool { $config = (new MysqliConfig()) ->withHost(MYSQL_SERVER_HOST) @@ -38,10 +58,10 @@ protected function getMysqliPool(): MysqliPool ->withPassword(MYSQL_SERVER_PWD) ; - return new MysqliPool($config); + return new MysqliPool($config, $size); } - protected function getPdoPool(): PDOPool + protected static function getPdoMysqlPool(int $size = ConnectionPool::DEFAULT_SIZE): PDOPool { $config = (new PDOConfig()) ->withHost(MYSQL_SERVER_HOST) @@ -52,13 +72,49 @@ protected function getPdoPool(): PDOPool ->withPassword(MYSQL_SERVER_PWD) ; - return new PDOPool($config); + return new PDOPool($config, $size); + } + + protected static function getPdoPgsqlPool(int $size = ConnectionPool::DEFAULT_SIZE): PDOPool + { + $config = (new PDOConfig()) + ->withDriver('pgsql') + ->withHost(PGSQL_SERVER_HOST) + ->withPort(PGSQL_SERVER_PORT) + ->withDbName(PGSQL_SERVER_DB) + ->withUsername(PGSQL_SERVER_USER) + ->withPassword(PGSQL_SERVER_PWD) + ; + + return new PDOPool($config, $size); + } + + protected static function getPdoOraclePool(int $size = ConnectionPool::DEFAULT_SIZE): PDOPool + { + $config = (new PDOConfig()) + ->withDriver('oci') + ->withHost(ORACLE_SERVER_HOST) + ->withPort(ORACLE_SERVER_PORT) + ->withDbName(ORACLE_SERVER_DB) + ->withCharset('AL32UTF8') + ->withUsername(ORACLE_SERVER_USER) + ->withPassword(ORACLE_SERVER_PWD) + ; + + return new PDOPool($config, $size); + } + + protected static function getPdoSqlitePool(int $size = ConnectionPool::DEFAULT_SIZE): PDOPool + { + $config = (new PDOConfig())->withDriver('sqlite')->withDbname(static::$sqliteDatabaseFile); + + return new PDOPool($config, $size); } - protected function getRedisPool(): RedisPool + protected static function getRedisPool(int $size = ConnectionPool::DEFAULT_SIZE): RedisPool { $config = (new RedisConfig())->withHost(REDIS_SERVER_HOST)->withPort(REDIS_SERVER_PORT); - return new RedisPool($config); + return new RedisPool($config, $size); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 75ece43..462dba8 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,15 +9,16 @@ declare(strict_types=1); +use Swoole\Constant; use Swoole\Coroutine; Coroutine::set([ - 'log_level' => SWOOLE_LOG_INFO, - 'trace_flags' => 0, + Constant::OPTION_LOG_LEVEL => SWOOLE_LOG_INFO, + Constant::OPTION_TRACE_FLAGS => 0, ]); if (!defined('SWOOLE_LIBRARY')) { - require dirname(__DIR__) . '/vendor/autoload.php'; + require_once dirname(__DIR__) . '/vendor/autoload.php'; } if (!defined('MYSQL_SERVER_HOST')) { diff --git a/tests/unit/Database/PDOPoolTest.php b/tests/unit/Database/PDOPoolTest.php index 8c3daa4..c3754c0 100644 --- a/tests/unit/Database/PDOPoolTest.php +++ b/tests/unit/Database/PDOPoolTest.php @@ -11,9 +11,9 @@ namespace Swoole\Database; -use PHPUnit\Framework\TestCase; use Swoole\Coroutine; use Swoole\Coroutine\WaitGroup; +use Swoole\Tests\DatabaseTestCase; use Swoole\Tests\HookFlagsTrait; use function Swoole\Coroutine\go; @@ -25,29 +25,10 @@ * @internal * @coversNothing */ -class PDOPoolTest extends TestCase +class PDOPoolTest extends DatabaseTestCase { use HookFlagsTrait; - protected static string $sqliteDatabaseFile; - - public static function setUpBeforeClass(): void - { - parent::setUpBeforeClass(); - self::$sqliteDatabaseFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'swoole_pdo_pool_sqlite_test.db'; - if (file_exists(self::$sqliteDatabaseFile)) { - unlink(self::$sqliteDatabaseFile); - } - } - - public static function tearDownAfterClass(): void - { - if (file_exists(self::$sqliteDatabaseFile)) { - unlink(self::$sqliteDatabaseFile); - } - parent::tearDownAfterClass(); - } - public function testPutWhenErrorHappens() { self::saveHookFlags(); @@ -55,16 +36,7 @@ public function testPutWhenErrorHappens() $expect = ['0', '1', '2', '3', '4']; $actual = []; Coroutine\run(function () use (&$actual) { - $config = (new PDOConfig()) - ->withHost(MYSQL_SERVER_HOST) - ->withPort(MYSQL_SERVER_PORT) - ->withDbName(MYSQL_SERVER_DB) - ->withCharset('utf8mb4') - ->withUsername(MYSQL_SERVER_USER) - ->withPassword(MYSQL_SERVER_PWD) - ; - - $pool = new PDOPool($config, 2); + $pool = self::getPdoMysqlPool(2); for ($n = 5; $n--;) { Coroutine::create(function () use ($pool, $n, &$actual) { $pdo = $pool->get(); @@ -95,17 +67,8 @@ public function testPostgres(): void self::saveHookFlags(); self::setHookFlags(SWOOLE_HOOK_ALL); run(function () { - $config = (new PDOConfig()) - ->withDriver('pgsql') - ->withHost(PGSQL_SERVER_HOST) - ->withPort(PGSQL_SERVER_PORT) - ->withDbName(PGSQL_SERVER_DB) - ->withUsername(PGSQL_SERVER_USER) - ->withPassword(PGSQL_SERVER_PWD) - ; - $pool = new PDOPool($config, 10); - - $pdo = $pool->get(); + $pool = self::getPdoPgsqlPool(10); + $pdo = $pool->get(); $pdo->exec('CREATE TABLE IF NOT EXISTS test(id INT);'); $pool->put($pdo); @@ -137,18 +100,8 @@ public function testOracle(): void self::saveHookFlags(); self::setHookFlags(SWOOLE_HOOK_ALL); run(function () { - $config = (new PDOConfig()) - ->withDriver('oci') - ->withHost(ORACLE_SERVER_HOST) - ->withPort(ORACLE_SERVER_PORT) - ->withDbName(ORACLE_SERVER_DB) - ->withCharset('AL32UTF8') - ->withUsername(ORACLE_SERVER_USER) - ->withPassword(ORACLE_SERVER_PWD) - ; - $pool = new PDOPool($config, 10); - - $pdo = $pool->get(); + $pool = self::getPdoOraclePool(10); + $pdo = $pool->get(); try { $pdo->exec('DROP TABLE test PURGE'); } catch (\PDOException $e) { @@ -187,10 +140,8 @@ public function testSqlite(): void self::saveHookFlags(); self::setHookFlags(SWOOLE_HOOK_ALL); run(function () { - $config = (new PDOConfig())->withDriver('sqlite')->withDbname(self::$sqliteDatabaseFile); - $pool = new PDOPool($config, 10); - - $pdo = $pool->get(); + $pool = self::getPdoSqlitePool(10); + $pdo = $pool->get(); $pdo->exec('CREATE TABLE IF NOT EXISTS test(id INT);'); $pool->put($pdo); @@ -222,16 +173,7 @@ public function testTimeoutException(): void self::saveHookFlags(); self::setHookFlags(SWOOLE_HOOK_ALL); run(function () { - $config = (new PDOConfig()) - ->withHost(MYSQL_SERVER_HOST) - ->withPort(MYSQL_SERVER_PORT) - ->withDbName(MYSQL_SERVER_DB) - ->withCharset('utf8mb4') - ->withUsername(MYSQL_SERVER_USER) - ->withPassword(MYSQL_SERVER_PWD) - ; - - $pool = new PDOPool($config, 1); + $pool = self::getPdoMysqlPool(1); $waitGroup = new WaitGroup(2); // A wait group to wait for the next 2 coroutines to finish. go(function () use ($pool, $waitGroup) { diff --git a/tests/unit/Database/PDOStatementProxyTest.php b/tests/unit/Database/PDOStatementProxyTest.php index 1746ebe..4a86ac8 100644 --- a/tests/unit/Database/PDOStatementProxyTest.php +++ b/tests/unit/Database/PDOStatementProxyTest.php @@ -29,7 +29,7 @@ public function testRun() { Coroutine\run(function () { self::assertFalse( - $this->getPdoPool()->get()->query("SHOW TABLES like 'NON_EXISTING_TABLE_NAME'")->fetch(\PDO::FETCH_ASSOC), + self::getPdoMysqlPool()->get()->query("SHOW TABLES like 'NON_EXISTING_TABLE_NAME'")->fetch(\PDO::FETCH_ASSOC), 'FALSE is returned if no results found.' ); }); @@ -75,7 +75,7 @@ public static function dataSetFetchMode(): array public function testSetFetchMode(array $expected, array $args, string $message) { Coroutine\run(function () use ($expected, $args, $message) { - $stmt = $this->getPdoPool()->get()->query( + $stmt = self::getPdoMysqlPool()->get()->query( 'SELECT * FROM ( @@ -95,7 +95,7 @@ public function testSetFetchMode(array $expected, array $args, string $message) public function testBindParam() { Coroutine\run(function () { - $stmt = $this->getPdoPool()->get()->prepare('SHOW TABLES like ?'); + $stmt = self::getPdoMysqlPool()->get()->prepare('SHOW TABLES like ?'); $table = 'NON_EXISTING_TABLE_NAME'; $stmt->bindParam(1, $table, \PDO::PARAM_STR); $stmt->execute(); diff --git a/tests/unit/ObjectProxyTest.php b/tests/unit/ObjectProxyTest.php index a15e061..541bca6 100644 --- a/tests/unit/ObjectProxyTest.php +++ b/tests/unit/ObjectProxyTest.php @@ -11,6 +11,9 @@ namespace Swoole; +use Swoole\Database\MysqliProxy; +use Swoole\Database\ObjectProxy; +use Swoole\Database\PDOProxy; use Swoole\Tests\DatabaseTestCase; /** @@ -21,30 +24,39 @@ */ class ObjectProxyTest extends DatabaseTestCase { - public function tearDown(): void + /** + * @return array> + */ + public static function dateClone(): array { - parent::tearDown(); - $this->addToAssertionCount(2); // Add those in method $this->testClone(). + return [ + [[self::class, 'getMysqliPool'], MysqliProxy::class], + [[self::class, 'getPdoMysqlPool'], PDOProxy::class], + [[self::class, 'getPdoOraclePool'], PDOProxy::class], + [[self::class, 'getPdoPgsqlPool'], PDOProxy::class], + [[self::class, 'getPdoSqlitePool'], PDOProxy::class], + ]; } /** + * @param class-string $class + * @dataProvider dateClone * @covers \Swoole\Database\ObjectProxy::__clone() */ - public function testClone() + public function testClone(callable $callback, string $class): void { - Coroutine\run(function () { - $dbs = [ - $this->getPdoPool()->get(), - $this->getMysqliPool()->get(), - ]; + Coroutine\run(function () use ($callback, $class): void { + $pool = $callback(); + self::assertInstanceOf(ConnectionPool::class, $pool); + /** @var ConnectionPool $pool */ + $proxy = $pool->get(); + self::assertInstanceOf($class, $proxy); - foreach ($dbs as $db) { - try { - var_dump(clone $db); - } catch (\Error $e) { - if ($e->getMessage() != 'Trying to clone an uncloneable database proxy object') { - throw $e; - } + try { + clone $proxy; + } catch (\Error $e) { + if ($e->getMessage() != 'Trying to clone an uncloneable database proxy object') { + throw $e; } } });