Added test cases.

This commit is contained in:
李铭昕 2020-09-18 17:31:56 +08:00
parent 0eaa14c13f
commit 7aea1ffbe6
3 changed files with 31 additions and 1 deletions

View File

@ -106,6 +106,29 @@ class ConnectionTest extends TestCase
$this->assertSame('mysql:host=192.168.1.1;dbname=hyperf', $pdo->dsn);
}
public function testPdoDontDestruct()
{
$callables = [function ($connection) {
$connection->selectOne('SELECT 1;');
}, function ($connection) {
$connection->table('user')->leftJoin('user_ext', 'user.id', '=', 'user_ext.id')->get();
}];
foreach ($callables as $callable) {
PDOStub::$destruct = 0;
$container = ContainerStub::mockContainer();
$pool = $container->get(PoolFactory::class)->getPool('default');
$config = $container->get(ConfigInterface::class)->get('databases.default');
$connection = new ConnectionStub($container, $pool, $config);
$connection->setPdo(new PDOStub('', '', '', []));
$callable($connection);
$this->assertSame(0, PDOStub::$destruct);
$connection->close();
$this->assertSame(1, PDOStub::$destruct);
}
}
public function testConnectionSticky()
{
$container = ContainerStub::mockReadWriteContainer();

View File

@ -58,7 +58,7 @@ class ContainerStub
'databases' => [
'default' => [
'driver' => 'mysql',
'host' => 'localhost',
'host' => '127.0.0.1',
'database' => 'hyperf',
'username' => 'root',
'password' => '',

View File

@ -21,6 +21,8 @@ class PDOStub extends \PDO
public $options;
public static $destruct = 0;
public function __construct(string $dsn, string $username, string $passwd, array $options)
{
$this->dsn = $dsn;
@ -37,4 +39,9 @@ class PDOStub extends \PDO
public function exec($statement)
{
}
public function __destruct()
{
self::$destruct++;
}
}