From 929202873c95e3f43068c56822da144fd72b65a6 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 17 Mar 2013 17:05:05 -0400 Subject: [PATCH] Add Binary type. Binary types will return PHP file handles for all binary data types. Strings will also be converted into file handles. Saving binary data can either be done with filehandles or strings. --- .../Datasource/Database/Type/BinaryType.php | 76 ++++++++++++++++ .../Database/Type/BinaryTypeTest.php | 91 +++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 lib/Cake/Model/Datasource/Database/Type/BinaryType.php create mode 100644 lib/Cake/Test/TestCase/Model/Datasource/Database/Type/BinaryTypeTest.php diff --git a/lib/Cake/Model/Datasource/Database/Type/BinaryType.php b/lib/Cake/Model/Datasource/Database/Type/BinaryType.php new file mode 100644 index 00000000000..170991d8a95 --- /dev/null +++ b/lib/Cake/Model/Datasource/Database/Type/BinaryType.php @@ -0,0 +1,76 @@ +type = Type::build('binary'); + $this->driver = $this->getMock('Cake\Model\Datasource\Database\Driver'); + } + +/** + * Test toPHP + * + * @return void + */ + public function testToPHP() { + $this->assertNull($this->type->toPHP(null, $this->driver)); + + $result = $this->type->toPHP('some data', $this->driver); + $this->assertInternalType('resource', $result); + + $fh = fopen(__FILE__, 'r'); + $result = $this->type->toPHP($fh, $this->driver); + $this->assertSame($fh, $result); + fclose($fh); + } + +/** + * Test exceptions on invalid data. + * + * @expectedException \Cake\Error\Exception + * @expectedExceptionMessage Unable to convert array into binary. + */ + public function testToPHPFailure() { + $this->type->toPHP([], $this->driver); + } + +/** + * Test converting to database format + * + * @return void + */ + public function testToDatabase() { + $value = 'some data'; + $result = $this->type->toDatabase($value, $this->driver); + $this->assertEquals($value, $result); + + $fh = fopen(__FILE__, 'r'); + $result = $this->type->toDatabase($fh, $this->driver); + $this->assertSame($fh, $result); + } + +/** + * Test that the PDO binding type is correct. + * + * @return void + */ + public function testToStatement() { + $this->assertEquals(PDO::PARAM_LOB, $this->type->toStatement('', $this->driver)); + } + +}