From b4975fcd04d243c007737bd8dbdde0fd2d0e19b3 Mon Sep 17 00:00:00 2001 From: Roman Jadrovski Date: Mon, 13 Jan 2020 11:54:00 +0300 Subject: [PATCH] Adapter test (both driver and platform used) --- .travis.yml | 15 ++++ .../Driver/Mysqli/TableGatewayTest.php | 4 ++ .../Driver/Pdo/AbstractAdapterTest.php | 72 +++++++++++++++++++ .../Adapter/Driver/Pdo/Mysql/AdapterTest.php | 13 +--- .../Adapter/Driver/Pdo/Mysql/AdapterTrait.php | 5 ++ .../Driver/Pdo/Postgresql/AdapterTest.php | 13 ++++ .../Driver/Pdo/Postgresql/AdapterTrait.php | 5 ++ .../Platform/MysqlFixtureLoader.php | 27 +++++-- .../Platform/PgsqlFixtureLoader.php | 50 +++++++++---- 9 files changed, 176 insertions(+), 28 deletions(-) create mode 100644 test/integration/Adapter/Driver/Pdo/AbstractAdapterTest.php create mode 100644 test/integration/Adapter/Driver/Pdo/Postgresql/AdapterTest.php diff --git a/.travis.yml b/.travis.yml index deb7d2c2a..9171d4c0a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -67,8 +67,23 @@ matrix: env: - DEPS=lowest - php: 7.3 + services: + - mysql env: - DEPS=latest + - TEST_INTEGRATION=true + - TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL=true + - TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL_HOSTNAME=127.0.0.1 + - php: 7.3 + services: + - postgresql + addons: + postgresql: "9.6" + env: + - DEPS=latest + - TEST_INTEGRATION=true + - TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL=true + - TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_HOSTNAME=127.0.0.1 before_install: - if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi diff --git a/test/integration/Adapter/Driver/Mysqli/TableGatewayTest.php b/test/integration/Adapter/Driver/Mysqli/TableGatewayTest.php index 9c241022d..7bcf296fc 100644 --- a/test/integration/Adapter/Driver/Mysqli/TableGatewayTest.php +++ b/test/integration/Adapter/Driver/Mysqli/TableGatewayTest.php @@ -27,6 +27,8 @@ public function testSelectWithEmptyCurrentWithBufferResult() $rowset = $tableGateway->select('id = 0'); $this->assertNull($rowset->current()); + + $adapter->getDriver()->getConnection()->disconnect(); } /** @@ -46,5 +48,7 @@ public function testSelectWithEmptyCurrentWithoutBufferResult() $rowset = $tableGateway->select('id = 0'); $this->assertNull($rowset->current()); + + $adapter->getDriver()->getConnection()->disconnect(); } } diff --git a/test/integration/Adapter/Driver/Pdo/AbstractAdapterTest.php b/test/integration/Adapter/Driver/Pdo/AbstractAdapterTest.php new file mode 100644 index 000000000..cfca98b33 --- /dev/null +++ b/test/integration/Adapter/Driver/Pdo/AbstractAdapterTest.php @@ -0,0 +1,72 @@ +assertInstanceOf(Adapter::class, $this->adapter); + } + + public function testDriverDisconnectAfterQuoteWithPlatform() + { + $isTcpConnection = $this->isTcpConnection(); + + $this->adapter->getDriver()->getConnection()->connect(); + + self::assertTrue($this->adapter->getDriver()->getConnection()->isConnected()); + if ($isTcpConnection) { + self::assertTrue($this->isConnectedTcp()); + } + + $this->adapter->getDriver()->getConnection()->disconnect(); + + self::assertFalse($this->adapter->getDriver()->getConnection()->isConnected()); + if ($isTcpConnection) { + self::assertFalse($this->isConnectedTcp()); + } + + $this->adapter->getDriver()->getConnection()->connect(); + + self::assertTrue($this->adapter->getDriver()->getConnection()->isConnected()); + if ($isTcpConnection) { + self::assertTrue($this->isConnectedTcp()); + } + + $this->adapter->getPlatform()->quoteValue('test'); + + $this->adapter->getDriver()->getConnection()->disconnect(); + + self::assertFalse($this->adapter->getDriver()->getConnection()->isConnected()); + if ($isTcpConnection) { + self::assertFalse($this->isConnectedTcp()); + } + } + + protected function isConnectedTcp() + { + $mypid = getmypid(); + $dbPort = static::DB_SERVER_PORT; + $lsof = shell_exec("lsof -i -P -n | grep $dbPort | grep $mypid"); + return $lsof !== null; + } + + protected function isTcpConnection() + { + return $this->getHostname() !== 'localhost'; + } + + abstract protected function getHostname(); +} diff --git a/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTest.php b/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTest.php index 335ca11cb..97fc67268 100644 --- a/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTest.php +++ b/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTest.php @@ -2,19 +2,12 @@ namespace LaminasIntegrationTest\Db\Adapter\Driver\Pdo\Mysql; -use Laminas\Db\Adapter\Adapter; +use LaminasIntegrationTest\Db\Adapter\Driver\Pdo\AbstractAdapterTest; use PHPUnit\DbUnit\TestCaseTrait; -use PHPUnit\Framework\TestCase; -class AdapterTest extends TestCase +class AdapterTest extends AbstractAdapterTest { use AdapterTrait; - /** - * @covers \Laminas\Db\Adapter\Adapter::__construct() - */ - public function testConnection() - { - $this->assertInstanceOf(Adapter::class, $this->adapter); - } + const DB_SERVER_PORT = 3306; } diff --git a/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTrait.php b/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTrait.php index c7001b293..3782f3151 100644 --- a/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTrait.php +++ b/test/integration/Adapter/Driver/Pdo/Mysql/AdapterTrait.php @@ -20,4 +20,9 @@ protected function setUp() 'password' => getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL_PASSWORD') ]); } + + protected function getHostname() + { + return getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL_HOSTNAME'); + } } diff --git a/test/integration/Adapter/Driver/Pdo/Postgresql/AdapterTest.php b/test/integration/Adapter/Driver/Pdo/Postgresql/AdapterTest.php new file mode 100644 index 000000000..6b8d00d62 --- /dev/null +++ b/test/integration/Adapter/Driver/Pdo/Postgresql/AdapterTest.php @@ -0,0 +1,13 @@ + getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_PASSWORD') ]); } + + protected function getHostname() + { + return getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_HOSTNAME'); + } } diff --git a/test/integration/Platform/MysqlFixtureLoader.php b/test/integration/Platform/MysqlFixtureLoader.php index 9a6daf151..b5509cdc4 100644 --- a/test/integration/Platform/MysqlFixtureLoader.php +++ b/test/integration/Platform/MysqlFixtureLoader.php @@ -19,11 +19,8 @@ class MysqlFixtureLoader implements FixtureLoader public function createDatabase() { - $this->pdo = new \PDO( - 'mysql:host=' . getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL_HOSTNAME'), - getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL_USERNAME'), - getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL_PASSWORD') - ); + $this->connect(); + if (false === $this->pdo->exec(sprintf( "CREATE DATABASE IF NOT EXISTS %s", getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL_DATABASE') @@ -45,13 +42,33 @@ public function createDatabase() print_r($this->pdo->errorInfo(), true) )); } + + $this->disconnect(); } public function dropDatabase() { + $this->connect(); + $this->pdo->exec(sprintf( "DROP DATABASE IF EXISTS %s", getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL_DATABASE') )); + + $this->disconnect(); + } + + protected function connect() + { + $this->pdo = new \PDO( + 'mysql:host=' . getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL_HOSTNAME'), + getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL_USERNAME'), + getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_MYSQL_PASSWORD') + ); + } + + protected function disconnect() + { + $this->pdo = null; } } diff --git a/test/integration/Platform/PgsqlFixtureLoader.php b/test/integration/Platform/PgsqlFixtureLoader.php index cedf74022..bbe4b8b77 100644 --- a/test/integration/Platform/PgsqlFixtureLoader.php +++ b/test/integration/Platform/PgsqlFixtureLoader.php @@ -20,13 +20,12 @@ class PgsqlFixtureLoader implements FixtureLoader public function createDatabase() { - $this->pdo = new \PDO( - 'pgsql:host=' . getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_HOSTNAME'), - getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_USERNAME'), - getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_PASSWORD') - ); + $this->connect(); + + $this->dropDatabase(); // closes connection + + $this->connect(); - $this->dropDatabase(); if (false === $this->pdo->exec(sprintf( "CREATE DATABASE %s", getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_DATABASE') @@ -39,13 +38,9 @@ public function createDatabase() } // PostgreSQL cannot switch database on same connection. - unset($this->pdo); - $this->pdo = new \PDO( - 'pgsql:host=' . getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_HOSTNAME') . ';' . - 'dbname=' . getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_DATABASE'), - getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_USERNAME'), - getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_PASSWORD') - ); + $this->disconnect(); + + $this->connect(true); if (false === $this->pdo->exec(file_get_contents($this->fixtureFile))) { throw new \Exception(sprintf( @@ -55,6 +50,8 @@ public function createDatabase() print_r($this->pdo->errorInfo(), true) )); } + + $this->disconnect(); } public function dropDatabase() @@ -67,9 +64,36 @@ public function dropDatabase() } $this->initialRun = false; + $this->connect(); + $this->pdo->exec(sprintf( "DROP DATABASE IF EXISTS %s", getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_DATABASE') )); + + $this->disconnect(); + } + + /** + * @param bool $useDb add dbname using in dsn + */ + protected function connect($useDb = false) + { + $dsn = 'pgsql:host=' . getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_HOSTNAME'); + + if ($useDb) { + $dsn .= ';dbname=' . getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_DATABASE'); + } + + $this->pdo = new \PDO( + $dsn, + getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_USERNAME'), + getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_PASSWORD') + ); + } + + protected function disconnect() + { + $this->pdo = null; } }