Skip to content

Commit

Permalink
Adapter test (both driver and platform used)
Browse files Browse the repository at this point in the history
  • Loading branch information
jadrovski committed Jan 14, 2020
1 parent 28393b8 commit b4975fc
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 28 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/integration/Adapter/Driver/Mysqli/TableGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function testSelectWithEmptyCurrentWithBufferResult()
$rowset = $tableGateway->select('id = 0');

$this->assertNull($rowset->current());

$adapter->getDriver()->getConnection()->disconnect();
}

/**
Expand All @@ -46,5 +48,7 @@ public function testSelectWithEmptyCurrentWithoutBufferResult()
$rowset = $tableGateway->select('id = 0');

$this->assertNull($rowset->current());

$adapter->getDriver()->getConnection()->disconnect();
}
}
72 changes: 72 additions & 0 deletions test/integration/Adapter/Driver/Pdo/AbstractAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace LaminasIntegrationTest\Db\Adapter\Driver\Pdo;

use Laminas\Db\Adapter\Adapter;
use PHPUnit\Framework\TestCase;

/**
* @property Adapter $adapter
*/
abstract class AbstractAdapterTest extends TestCase
{
const DB_SERVER_PORT = null;

/**
* @covers \Laminas\Db\Adapter\Adapter::__construct()
*/
public function testConnection()
{
$this->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();
}
13 changes: 3 additions & 10 deletions test/integration/Adapter/Driver/Pdo/Mysql/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions test/integration/Adapter/Driver/Pdo/Mysql/AdapterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
13 changes: 13 additions & 0 deletions test/integration/Adapter/Driver/Pdo/Postgresql/AdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace LaminasIntegrationTest\Db\Adapter\Driver\Pdo\Postgresql;

use LaminasIntegrationTest\Db\Adapter\Driver\Pdo\AbstractAdapterTest;
use PHPUnit\DbUnit\TestCaseTrait;

class AdapterTest extends AbstractAdapterTest
{
use AdapterTrait;

const DB_SERVER_PORT = 5432;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ protected function setUp()
'password' => getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_PASSWORD')
]);
}

protected function getHostname()
{
return getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_PGSQL_HOSTNAME');
}
}
27 changes: 22 additions & 5 deletions test/integration/Platform/MysqlFixtureLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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;
}
}
50 changes: 37 additions & 13 deletions test/integration/Platform/PgsqlFixtureLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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(
Expand All @@ -55,6 +50,8 @@ public function createDatabase()
print_r($this->pdo->errorInfo(), true)
));
}

$this->disconnect();
}

public function dropDatabase()
Expand All @@ -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;
}
}

0 comments on commit b4975fc

Please sign in to comment.