Skip to content

Commit

Permalink
Changes for old PostgreSQL driver and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Narloch committed Dec 22, 2018
1 parent d9cb8b4 commit 432d139
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 5 deletions.
167 changes: 162 additions & 5 deletions Tests/DriverPostgresqlTest.php
Expand Up @@ -99,6 +99,22 @@ public function dataTestReplacePrefix()
array('SELECT * FROM "#!-_table"', '#!-_', 'SELECT * FROM "jos_table"'));
}

/**
* Data for the testQuoteBinary test.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public function dataTestQuoteBinary()
{
return array(
array('DATA', "decode('" . bin2hex('DATA') . "', 'hex')"),
array("\x00\x01\x02\xff", "decode('000102ff', 'hex')"),
array("\x01\x01\x02\xff", "decode('010102ff', 'hex')"),
);
}

/**
* Data for testQuoteName test.
*
Expand Down Expand Up @@ -193,6 +209,25 @@ public function testEscape($text, $extra, $result)
);
}

/**
* Test the quoteBinary method.
*
* @param string $data The binary quoted input string.
*
* @return void
*
* @dataProvider dataTestQuoteBinary
* @since __DEPLOY_VERSION__
*/
public function testQuoteBinary($data, $expected)
{
$this->assertThat(
self::$driver->quoteBinary($data),
$this->equalTo($expected),
'The binary data was not quoted properly'
);
}

/**
* Test getAffectedRows method.
*
Expand Down Expand Up @@ -269,7 +304,13 @@ public function testGetTableCreate()
*/
public function testGetTableColumns()
{
$tableCol = array('id' => 'integer', 'title' => 'character varying', 'start_date' => 'timestamp without time zone', 'description' => 'text');
$tableCol = array(
'id' => 'integer',
'title' => 'character varying',
'start_date' => 'timestamp without time zone',
'description' => 'text',
'data' => 'bytea',
);

$this->assertThat(self::$driver->getTableColumns('jos_dbtest'), $this->equalTo($tableCol), __LINE__);

Expand Down Expand Up @@ -314,9 +355,27 @@ public function testGetTableColumns()
$description->Default = null;
$description->comments = '';

$data = new \stdClass;
$data->column_name = 'data';
$data->Field = 'data';
$data->type = 'bytea';
$data->Type = 'bytea';
$data->null = 'YES';
$data->Null = 'YES';
$data->Default = null;
$data->comments = '';

$this->assertThat(
self::$driver->getTableColumns('jos_dbtest', false),
$this->equalTo(array('id' => $id, 'title' => $title, 'start_date' => $start_date, 'description' => $description)),
$this->equalTo(
array(
'id' => $id,
'title' => $title,
'start_date' => $start_date,
'description' => $description,
'data' => $data,
)
),
__LINE__
);
}
Expand Down Expand Up @@ -655,6 +714,7 @@ public function testLoadObject()
$objCompare->title = 'Testing3';
$objCompare->start_date = '1980-04-18 00:00:00';
$objCompare->description = 'three';
$objCompare->data = null;

$this->assertThat($result, $this->equalTo($objCompare), __LINE__);
}
Expand Down Expand Up @@ -682,6 +742,7 @@ public function testLoadObjectList()
$objCompare->title = 'Testing';
$objCompare->start_date = '1980-04-18 00:00:00';
$objCompare->description = 'one';
$objCompare->data = null;

$expected[] = clone $objCompare;

Expand All @@ -690,6 +751,7 @@ public function testLoadObjectList()
$objCompare->title = 'Testing2';
$objCompare->start_date = '1980-04-18 00:00:00';
$objCompare->description = 'one';
$objCompare->data = null;

$expected[] = clone $objCompare;

Expand All @@ -698,6 +760,7 @@ public function testLoadObjectList()
$objCompare->title = 'Testing3';
$objCompare->start_date = '1980-04-18 00:00:00';
$objCompare->description = 'three';
$objCompare->data = null;

$expected[] = clone $objCompare;

Expand All @@ -706,6 +769,7 @@ public function testLoadObjectList()
$objCompare->title = 'Testing4';
$objCompare->start_date = '1980-04-18 00:00:00';
$objCompare->description = 'four';
$objCompare->data = null;

$expected[] = clone $objCompare;

Expand Down Expand Up @@ -748,7 +812,7 @@ public function testLoadRow()
self::$driver->setQuery($query);
$result = self::$driver->loadRow();

$expected = array(3, 'Testing3', '1980-04-18 00:00:00', 'three');
$expected = array(3, 'Testing3', '1980-04-18 00:00:00', 'three', null);

$this->assertThat($result, $this->equalTo($expected), __LINE__);
}
Expand All @@ -769,8 +833,101 @@ public function testLoadRowList()
self::$driver->setQuery($query);
$result = self::$driver->loadRowList();

$expected = array(array(1, 'Testing', '1980-04-18 00:00:00', 'one'), array(2, 'Testing2', '1980-04-18 00:00:00', 'one'));
$expected = array(
array(1, 'Testing', '1980-04-18 00:00:00', 'one', null),
array(2, 'Testing2', '1980-04-18 00:00:00', 'one', null)
);

$this->assertThat($result, $this->equalTo($expected), __LINE__);
}

/**
* Test quoteBinary and decodeBinary methods
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testLoadBinary()
{
// Add binary data with null byte
$query = self::$driver->getQuery(true)
->update('jos_dbtest')
->set('data = ' . self::$driver->quoteBinary("\x00\x01\x02\xff"))
->where('id = 3');

self::$driver->setQuery($query)->execute();

// Add binary data with invalid UTF-8
$query = self::$driver->getQuery(true)
->update('jos_dbtest')
->set('data = ' . self::$driver->quoteBinary("\x01\x01\x02\xff"))
->where('id = 4');

self::$driver->setQuery($query)->execute();

$selectRow3 = self::$driver->getQuery(true)
->select('id')
->from('jos_dbtest')
->where('data = ' . self::$driver->quoteBinary("\x00\x01\x02\xff"));

$selectRow4 = self::$driver->getQuery(true)
->select('id')
->from('jos_dbtest')
->where('data = '. self::$driver->quoteBinary("\x01\x01\x02\xff"));

$result = self::$driver->setQuery($selectRow3)->loadResult();
$this->assertThat($result, $this->equalTo(3), __LINE__);

$result = self::$driver->setQuery($selectRow4)->loadResult();
$this->assertThat($result, $this->equalTo(4), __LINE__);

$selectRows = self::$driver->getQuery(true)
->select('data')
->from('jos_dbtest')
->order('id');

// Test loadColumn
$result = self::$driver->setQuery($selectRows)->loadColumn();

foreach ($result as $i => $v)
{
$result[$i] = self::$driver->decodeBinary($v);
}

$expected = array(null, null, "\x00\x01\x02\xff", "\x01\x01\x02\xff");
$this->assertThat($result, $this->equalTo($expected), __LINE__);

// Test loadAssocList
$result = self::$driver->setQuery($selectRows)->loadAssocList();

foreach ($result as $i => $v)
{
$result[$i]['data'] = self::$driver->decodeBinary($v['data']);
}

$expected = array(
array('data' => null),
array('data' => null),
array('data' => "\x00\x01\x02\xff"),
array('data' => "\x01\x01\x02\xff"),
);
$this->assertThat($result, $this->equalTo($expected), __LINE__);

// Test loadObjectList
$result = self::$driver->setQuery($selectRows)->loadObjectList();

foreach ($result as $i => $v)
{
$result[$i]->data = self::$driver->decodeBinary($v->data);
}

$expected = array(
(object) array('data' => null),
(object) array('data' => null),
(object) array('data' => "\x00\x01\x02\xff"),
(object) array('data' => "\x01\x01\x02\xff"),
);
$this->assertThat($result, $this->equalTo($expected), __LINE__);
}

Expand Down Expand Up @@ -953,7 +1110,7 @@ public function testTransactionCommit()
self::$driver->setQuery($queryCheck);
$result = self::$driver->loadRow();

$expected = array(6, 'testTitle', '1970-01-01 00:00:00', 'testDescription');
$expected = array(6, 'testTitle', '1970-01-01 00:00:00', 'testDescription', null);

$this->assertThat($result, $this->equalTo($expected), __LINE__);
}
Expand Down
33 changes: 33 additions & 0 deletions src/Postgresql/PostgresqlDriver.php
Expand Up @@ -1603,4 +1603,37 @@ public function updateObject($table, &$object, $key, $nulls = false)

return $this->execute();
}

/**
* Quotes a binary string to database requirements for use in database queries.
*
* @param string $data A binary string to quote.
*
* @return string The binary quoted input string.
*
* @since __DEPLOY_VERSION__
*/
public function quoteBinary($data)
{
return "decode('" . bin2hex($data) . "', 'hex')";
}

/**
* Replace special placeholder representing binary field with the original string.
*
* @param string|resource $data Encoded string or resource.
*
* @return string The original string.
*
* @since __DEPLOY_VERSION__
*/
public function decodeBinary($data)
{
if (is_string($data))
{
return pg_unescape_bytea($data);
}

return $data;
}
}

0 comments on commit 432d139

Please sign in to comment.