Skip to content

Commit

Permalink
Merge pull request #555 from BenMorel/blob
Browse files Browse the repository at this point in the history
Performance improvement on BlobType
  • Loading branch information
deeky666 committed Apr 24, 2014
2 parents d6b9140 + 7e15665 commit a077707
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/Doctrine/DBAL/Types/BinaryType.php
Expand Up @@ -47,7 +47,10 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
}

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)) {
Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/DBAL/Types/BlobType.php
Expand Up @@ -46,7 +46,10 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
}

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)) {
Expand Down
54 changes: 48 additions & 6 deletions tests/Doctrine/Tests/DBAL/Types/BlobTest.php
Expand Up @@ -9,18 +9,60 @@

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;
}
}

0 comments on commit a077707

Please sign in to comment.