Commit 7e15665b authored by Benjamin Morel's avatar Benjamin Morel

Improved performance of BlobType & BinaryType

parent 0833d00b
......@@ -47,7 +47,10 @@ class BinaryType extends Type
}
if (is_string($value)) {
$value = fopen('data://text/plain;base64,' . base64_encode($value), 'r');
$fp = fopen('php://temp', 'rb+');
fwrite($fp, $value);
fseek($fp, 0);
$value = $fp;
}
if ( ! is_resource($value)) {
......
......@@ -46,7 +46,10 @@ class BlobType extends Type
}
if (is_string($value)) {
$value = fopen('data://text/plain;base64,' . base64_encode($value), 'r');
$fp = fopen('php://temp', 'rb+');
fwrite($fp, $value);
fseek($fp, 0);
$value = $fp;
}
if ( ! is_resource($value)) {
......
......@@ -9,18 +9,60 @@ require_once __DIR__ . '/../../TestInit.php';
class BlobTest extends \Doctrine\Tests\DbalTestCase
{
protected
$_platform,
$_type;
/**
* @var \Doctrine\Tests\DBAL\Mocks\MockPlatform
*/
protected $platform;
/**
* @var \Doctrine\DBAL\Types\BlobType
*/
protected $type;
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
$this->_type = Type::getType('blob');
$this->platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
$this->type = Type::getType('blob');
}
public function testBlobNullConvertsToPHPValue()
{
$this->assertNull($this->_type->convertToPHPValue(null, $this->_platform));
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
}
public function testBinaryStringConvertsToPHPValue()
{
$databaseValue = $this->getBinaryString();
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
$this->assertInternalType('resource', $phpValue);
$this->assertSame($databaseValue, stream_get_contents($phpValue));
}
public function testBinaryResourceConvertsToPHPValue()
{
$databaseValue = fopen('data://text/plain;base64,' . base64_encode($this->getBinaryString()), 'r');
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
$this->assertSame($databaseValue, $phpValue);
}
/**
* Creates a binary string containing all possible byte values.
*
* @return string
*/
private function getBinaryString()
{
$string = '';
for ($i = 0; $i < 256; $i++) {
$string .= chr($i);
}
return $string;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment