Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter test (both driver and platform used) #44

Merged
merged 2 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 28 additions & 20 deletions src/Adapter/Platform/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class Mysql extends AbstractPlatform
protected $quoteIdentifierTo = '``';

/**
* @var \mysqli|\PDO
* @var \mysqli|\PDO|Pdo\Pdo|Mysqli\Mysqli
*/
protected $resource = null;
protected $driver = null;

/**
* NOTE: Include dashes for MySQL only, need tests for others platforms
Expand Down Expand Up @@ -60,7 +60,7 @@ public function setDriver($driver)
|| ($driver instanceof \mysqli)
|| ($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'mysql')
) {
$this->resource = $driver;
$this->driver = $driver;
return $this;
}

Expand Down Expand Up @@ -90,32 +90,40 @@ public function quoteIdentifierChain($identifierChain)
*/
public function quoteValue($value)
{
if ($this->resource instanceof DriverInterface) {
$this->resource = $this->resource->getConnection()->getResource();
}
if ($this->resource instanceof \mysqli) {
return '\'' . $this->resource->real_escape_string($value) . '\'';
}
if ($this->resource instanceof \PDO) {
return $this->resource->quote($value);
}
return parent::quoteValue($value);
$quotedViaDriverValue = $this->quoteViaDriver($value);

return $quotedViaDriverValue !== null ? $quotedViaDriverValue : parent::quoteValue($value);
}

/**
* {@inheritDoc}
*/
public function quoteTrustedValue($value)
{
if ($this->resource instanceof DriverInterface) {
$this->resource = $this->resource->getConnection()->getResource();
$quotedViaDriverValue = $this->quoteViaDriver($value);

return $quotedViaDriverValue !== null ? $quotedViaDriverValue : parent::quoteTrustedValue($value);
}

/**
* @param string $value
* @return string|null
*/
protected function quoteViaDriver($value)
{
if ($this->driver instanceof DriverInterface) {
$resource = $this->driver->getConnection()->getResource();
} else {
$resource = $this->driver;
}
if ($this->resource instanceof \mysqli) {
return '\'' . $this->resource->real_escape_string($value) . '\'';

if ($resource instanceof \mysqli) {
return '\'' . $resource->real_escape_string($value) . '\'';
}
if ($this->resource instanceof \PDO) {
return $this->resource->quote($value);
if ($resource instanceof \PDO) {
return $resource->quote($value);
}
return parent::quoteTrustedValue($value);

return null;
}
}
48 changes: 28 additions & 20 deletions src/Adapter/Platform/Postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Postgresql extends AbstractPlatform
protected $quoteIdentifierTo = '""';

/**
* @var resource|\PDO
* @var resource|\PDO|Pdo\Pdo|Pgsql\Pgsql
*/
protected $resource = null;
protected $driver = null;

/**
* @param null|\Laminas\Db\Adapter\Driver\Pgsql\Pgsql|\Laminas\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver
Expand All @@ -49,7 +49,7 @@ public function setDriver($driver)
|| (is_resource($driver) && (in_array(get_resource_type($driver), ['pgsql link', 'pgsql link persistent'])))
|| ($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'pgsql')
) {
$this->resource = $driver;
$this->driver = $driver;
return $this;
}

Expand Down Expand Up @@ -80,32 +80,40 @@ public function quoteIdentifierChain($identifierChain)
*/
public function quoteValue($value)
{
if ($this->resource instanceof DriverInterface) {
$this->resource = $this->resource->getConnection()->getResource();
}
if (is_resource($this->resource)) {
return '\'' . pg_escape_string($this->resource, $value) . '\'';
}
if ($this->resource instanceof \PDO) {
return $this->resource->quote($value);
}
return 'E' . parent::quoteValue($value);
$quotedViaDriverValue = $this->quoteViaDriver($value);

return $quotedViaDriverValue !== null ? $quotedViaDriverValue : ('E' . parent::quoteValue($value));
}

/**
* {@inheritDoc}
*/
public function quoteTrustedValue($value)
{
if ($this->resource instanceof DriverInterface) {
$this->resource = $this->resource->getConnection()->getResource();
$quotedViaDriverValue = $this->quoteViaDriver($value);

return $quotedViaDriverValue !== null ? $quotedViaDriverValue : ('E' . parent::quoteTrustedValue($value));
}

/**
* @param string $value
* @return string|null
*/
protected function quoteViaDriver($value)
{
if ($this->driver instanceof DriverInterface) {
$resource = $this->driver->getConnection()->getResource();
} else {
$resource = $this->driver;
}
if (is_resource($this->resource)) {
return '\'' . pg_escape_string($this->resource, $value) . '\'';

if (is_resource($resource)) {
return '\'' . pg_escape_string($resource, $value) . '\'';
}
if ($this->resource instanceof \PDO) {
return $this->resource->quote($value);
if ($resource instanceof \PDO) {
return $resource->quote($value);
}
return 'E' . parent::quoteTrustedValue($value);

return null;
}
}
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;
}
}
Loading