Browse files

Add more GridFSFile tests and split GridFS tests

  • Loading branch information...
1 parent b4bef51 commit 196895af2a6b69effe0f9d2b0dfa9765c41d3c9f @jmikola jmikola committed Nov 8, 2012
Showing with 253 additions and 173 deletions.
  1. +97 −173 tests/Doctrine/MongoDB/Tests/GridFSFileTest.php
  2. +156 −0 tests/Doctrine/MongoDB/Tests/GridFSTest.php
View
270 tests/Doctrine/MongoDB/Tests/GridFSFileTest.php
@@ -2,244 +2,168 @@
namespace Doctrine\MongoDB\Tests;
-use Doctrine\MongoDB\Configuration;
-use Doctrine\MongoDB\Connection;
use Doctrine\MongoDB\GridFSFile;
class GridFSFileTest extends BaseTest
{
- public function testSetAndGetMongoGridFSFile()
- {
- $path = __DIR__.'/GridFSFileTest.php';
- $file = $this->getTestGridFSFile($path);
- $mockPHPGridFSFile = $this->getMockPHPGridFSFile();
- $file->setMongoGridFSFile($mockPHPGridFSFile);
- $this->assertEquals($mockPHPGridFSFile, $file->getMongoGridFSFile());
- }
-
public function testIsDirty()
{
- $file = $this->getTestGridFSFile();
+ $file = new GridFSFile();
$this->assertFalse($file->isDirty());
$file->isDirty(true);
$this->assertTrue($file->isDirty());
$file->isDirty(false);
$this->assertFalse($file->isDirty());
}
- public function testSetAndGetFilename()
+ public function testSetAndGetBytes()
{
- $path = __DIR__.'/GridFSFileTest.php';
- $file = $this->getTestGridFSFile();
- $this->assertFalse($file->isDirty());
- $file->setFilename($path);
- $this->assertTrue($file->isDirty());
- $this->assertFalse($file->hasUnpersistedBytes());
- $this->assertTrue($file->hasUnpersistedFile());
- $this->assertEquals($path, $file->getFilename());
- }
-
- public function testSetBytes()
- {
- $file = $this->getTestGridFSFile();
+ $file = new GridFSFile();
$file->setBytes('bytes');
$this->assertTrue($file->isDirty());
$this->assertTrue($file->hasUnpersistedBytes());
$this->assertFalse($file->hasUnpersistedFile());
$this->assertEquals('bytes', $file->getBytes());
}
- public function testWriteWithSetBytes()
+ public function testSetAndGetFilename()
{
- $file = $this->getTestGridFSFile();
- $file->setBytes('bytes');
- $path = realpath(sys_get_temp_dir()).'/doctrine_write_test';
- $file->write($path);
- $this->assertTrue(file_exists($path));
- $this->assertEquals('bytes', file_get_contents($path));
- unlink($path);
+ $file = new GridFSFile();
+ $this->assertFalse($file->isDirty());
+ $file->setFilename(__FILE__);
+ $this->assertTrue($file->isDirty());
+ $this->assertFalse($file->hasUnpersistedBytes());
+ $this->assertTrue($file->hasUnpersistedFile());
+ $this->assertEquals(__FILE__, $file->getFilename());
}
- public function testWriteWithSetFilename()
+ public function testSetAndGetMongoGridFSFile()
{
- $origPath = __DIR__.'/GridFSFileTest.php';
- $file = $this->getTestGridFSFile();
- $file->setFilename($origPath);
- $path = realpath(sys_get_temp_dir()).'/doctrine_write_test';
- $file->write($path);
- $this->assertTrue(file_exists($path));
- $this->assertEquals(file_get_contents($origPath), file_get_contents($path));
- unlink($path);
+ $mongoGridFSFile = $this->getMockMongoGridFSFile();
+
+ $file = new GridFSFile();
+ $file->setMongoGridFSFile($mongoGridFSFile);
+ $this->assertSame($mongoGridFSFile, $file->getMongoGridFSFile());
}
- public function testGetSizeWithSetBytes()
+ public function testGetBytesWithSetBytes()
{
- $file = $this->getTestGridFSFile();
+ $file = new GridFSFile();
$file->setBytes('bytes');
- $this->assertEquals(5, $file->getSize());
+ $this->assertEquals('bytes', $file->getBytes());
}
- public function testGetSizeWithSetFilename()
+ public function testGetBytesWithSetFilename()
{
- $file = $this->getTestGridFSFile();
- $file->setFilename(__DIR__.'/file.txt');
- $this->assertEquals(22, $file->getSize());
+ $file = new GridFSFile();
+ $file->setFilename(__FILE__);
+ $this->assertStringEqualsFile(__FILE__, $file->getBytes());
}
- public function testFunctional()
+ public function testGetBytesWithSetMongoGridFSFile()
{
- $db = $this->conn->selectDatabase(self::$dbName);
-
- $path = __DIR__.'/file.txt';
- $gridFS = $db->getGridFS();
- $file = new GridFSFile($path);
- $document = array(
- 'title' => 'Test Title',
- 'file' => $file
- );
- $gridFS->insert($document);
- $id = $document['_id'];
+ $mongoGridFSFile = $this->getMockMongoGridFSFile();
+ $mongoGridFSFile->expects($this->once())
+ ->method('getBytes')
+ ->will($this->returnValue('bytes'));
- $document = $gridFS->findOne(array('_id' => $id));
- $file = $document['file'];
-
- $this->assertFalse($file->isDirty());
- $this->assertEquals($path, $file->getFilename());
- $this->assertEquals(file_get_contents($path), $file->getBytes());
- $this->assertEquals(22, $file->getSize());
-
- $tmpPath = realpath(sys_get_temp_dir()).'/doctrine_write_test';
- $file->write($tmpPath);
- $this->assertTrue(file_exists($path));
- $this->assertEquals(file_get_contents($path), file_get_contents($tmpPath));
- unlink($tmpPath);
+ $file = new GridFSFile();
+ $file->setMongoGridFSFile($mongoGridFSFile);
+ $this->assertEquals('bytes', $file->getBytes());
}
- public function testStoreFile()
+ public function testGetBytesWithEmptyState()
{
- $db = $this->conn->selectDatabase(self::$dbName);
- $gridFS = $db->getGridFS();
-
- $metadata = array(
- 'test' => 'file'
- );
- $file = $gridFS->storeFile(__DIR__.'/file.txt', $metadata);
- $this->assertInstanceOf('Doctrine\MongoDB\GridFSFile', $file);
- $this->assertTrue(isset($metadata['_id']));
+ $file = new GridFSFile();
+ $this->assertNull($file->getBytes());
}
- public function testUpdate()
+ public function testWriteWithSetBytes()
{
- $db = $this->conn->selectDatabase(self::$dbName);
+ $path = tempnam(sys_get_temp_dir(), 'doctrine_write_test');
+ $this->assertNotEquals(false, $path);
- $path = __DIR__.'/file.txt';
- $gridFS = $db->getGridFS();
- $file = new GridFSFile($path);
- $document = array(
- 'title' => 'Test Title',
- 'file' => $file
- );
- $gridFS->insert($document);
- $id = $document['_id'];
-
- $document = $gridFS->findOne(array('_id' => $id));
- $file = $document['file'];
-
- $gridFS->update(array('_id' => $id), array('$pushAll' => array('test' => array(1, 2, 3))));
- $check = $gridFS->findOne(array('_id' => $id));
- $this->assertTrue(isset($check['test']));
- $this->assertEquals(3, count($check['test']));
- $this->assertEquals(array(1, 2, 3), $check['test']);
-
- $gridFS->update(array('_id' => $id), array('_id' => $id));
- $gridFS->update(array('_id' => $id), array('_id' => $id, 'boom' => true));
- $check = $gridFS->findOne(array('_id' => $id));
- $this->assertFalse(array_key_exists('test', $check));
- $this->assertTrue($check['boom']);
+ $file = new GridFSFile();
+ $file->setBytes('bytes');
+ $file->write($path);
+ $this->assertStringEqualsFile($path, 'bytes');
+ unlink($path);
}
- public function testUpsertDocumentWithoutFile()
+ public function testWriteWithSetFilename()
{
- $gridFS = $this->conn->selectDatabase(self::$dbName)->getGridFS();
-
- $gridFS->update(
- array('id' => 123),
- array('x' => 1),
- array('upsert' => true, 'multiple' => false)
- );
+ $path = tempnam(sys_get_temp_dir(), 'doctrine_write_test');
+ $this->assertNotEquals(false, $path);
- $document = $gridFS->findOne();
-
- $this->assertNotNull($document);
- $this->assertNotEquals(123, $document['_id']);
- $this->assertEquals(1, $document['x']);
+ $file = new GridFSFile();
+ $file->setFilename(__FILE__);
+ $file->write($path);
+ $this->assertFileEquals(__FILE__, $path);
+ unlink($path);
}
- public function testUpsertDocumentWithoutFileWithId()
+ public function testWriteWithSetMongoGridFSFile()
{
- $gridFS = $this->conn->selectDatabase(self::$dbName)->getGridFS();
-
- $gridFS->update(
- array('x' => 1),
- array('_id' => 123),
- array('upsert' => true, 'multiple' => false)
- );
+ $mongoGridFSFile = $this->getMockMongoGridFSFile();
+ $mongoGridFSFile->expects($this->once())
+ ->method('write')
+ ->with('filename');
- $document = $gridFS->findOne(array('_id' => 123));
-
- $this->assertNotNull($document);
- $this->assertFalse(array_key_exists('x', $document));
+ $file = new GridFSFile();
+ $file->setMongoGridFSFile($mongoGridFSFile);
+ $file->write('filename');
}
- public function testUpsertModifierWithoutFile()
+ /**
+ * @expectedException BadMethodCallException
+ */
+ public function testWriteWithEmptyState()
{
- $gridFS = $this->conn->selectDatabase(self::$dbName)->getGridFS();
-
- $gridFS->update(
- array('_id' => 123),
- array('$set' => array('x' => 1)),
- array('upsert' => true, 'multiple' => false)
- );
-
- $document = $gridFS->findOne(array('_id' => 123));
-
- $this->assertNotNull($document);
- $this->assertEquals(1, $document['x']);
+ $file = new GridFSFile();
+ $file->write('filename');
}
- public function testUpsert()
+ public function testGetSizeWithSetBytes()
{
- $db = $this->conn->selectDatabase(self::$dbName);
-
- $path = __DIR__.'/file.txt';
- $gridFS = $db->getGridFS();
- $file = new GridFSFile($path);
+ $file = new GridFSFile();
+ $file->setBytes('bytes');
+ $this->assertEquals(5, $file->getSize());
+ }
- $newObj = array(
- '$set' => array(
- 'title' => 'Test Title',
- 'file' => $file,
- ),
- );
- $gridFS->update(array('_id' => 123), $newObj, array('upsert' => true, 'multiple' => false));
+ public function testGetSizeWithSetFilename()
+ {
+ $file = new GridFSFile();
+ $file->setFilename(__FILE__);
+ $this->assertEquals(filesize(__FILE__), $file->getSize());
+ }
- $document = $gridFS->findOne(array('_id' => 123));
+ public function testGetSizeWithSetMongoGridFSFile()
+ {
+ $mongoGridFSFile = $this->getMockMongoGridFSFile();
+ $mongoGridFSFile->expects($this->once())
+ ->method('getSize')
+ ->will($this->returnValue(200));
- $file = $document['file'];
+ $file = new GridFSFile();
+ $file->setMongoGridFSFile($mongoGridFSFile);
+ $this->assertEquals(200, $file->getSize());
+ }
- $this->assertFalse($file->isDirty());
- $this->assertEquals($path, $file->getFilename());
- $this->assertEquals(file_get_contents($path), $file->getBytes());
- $this->assertEquals(22, $file->getSize());
+ public function testGetSizeWithEmptyState()
+ {
+ $file = new GridFSFile();
+ $this->assertEquals(0, $file->getSize());
}
- private function getMockPHPGridFSFile()
+ private function getMockMongoGridFSFile()
{
- return $this->getMock('MongoGridFSFile', array(), array(), '', false, false);
+ return $this->getMockBuilder('MongoGridFSFile')
+ ->disableOriginalConstructor()
+ ->getMock();
}
- private function getTestGridFSFile($file = null)
+ private function getGridFS()
{
- return new GridFSFile($file);
+ return $this->conn->selectDatabase(self::$dbName)->getGridFS();
}
-}
+}
View
156 tests/Doctrine/MongoDB/Tests/GridFSTest.php
@@ -0,0 +1,156 @@
+<?php
+
+namespace Doctrine\MongoDB\Tests;
+
+use Doctrine\MongoDB\GridFSFile;
+
+class GridFSTest extends BaseTest
+{
+ public function testFunctional()
+ {
+ $gridFS = $this->getGridFS();
+
+ $path = __DIR__.'/file.txt';
+ $file = new GridFSFile($path);
+ $document = array(
+ 'title' => 'Test Title',
+ 'file' => $file
+ );
+ $gridFS->insert($document);
+ $id = $document['_id'];
+
+ $document = $gridFS->findOne(array('_id' => $id));
+ $file = $document['file'];
+
+ $this->assertFalse($file->isDirty());
+ $this->assertEquals($path, $file->getFilename());
+ $this->assertEquals(file_get_contents($path), $file->getBytes());
+ $this->assertEquals(22, $file->getSize());
+
+ $tmpPath = realpath(sys_get_temp_dir()).'/doctrine_write_test';
+ $file->write($tmpPath);
+ $this->assertTrue(file_exists($path));
+ $this->assertEquals(file_get_contents($path), file_get_contents($tmpPath));
+ unlink($tmpPath);
+ }
+
+ public function testStoreFile()
+ {
+ $gridFS = $this->getGridFS();
+
+ $metadata = array(
+ 'test' => 'file'
+ );
+ $file = $gridFS->storeFile(__DIR__.'/file.txt', $metadata);
+ $this->assertInstanceOf('Doctrine\MongoDB\GridFSFile', $file);
+ $this->assertTrue(isset($metadata['_id']));
+ }
+
+ public function testUpdate()
+ {
+ $gridFS = $this->getGridFS();
+
+ $path = __DIR__.'/file.txt';
+ $file = new GridFSFile($path);
+ $document = array(
+ 'title' => 'Test Title',
+ 'file' => $file
+ );
+ $gridFS->insert($document);
+ $id = $document['_id'];
+
+ $document = $gridFS->findOne(array('_id' => $id));
+ $file = $document['file'];
+
+ $gridFS->update(array('_id' => $id), array('$pushAll' => array('test' => array(1, 2, 3))));
+ $check = $gridFS->findOne(array('_id' => $id));
+ $this->assertTrue(isset($check['test']));
+ $this->assertEquals(3, count($check['test']));
+ $this->assertEquals(array(1, 2, 3), $check['test']);
+
+ $gridFS->update(array('_id' => $id), array('_id' => $id));
+ $gridFS->update(array('_id' => $id), array('_id' => $id, 'boom' => true));
+ $check = $gridFS->findOne(array('_id' => $id));
+ $this->assertFalse(array_key_exists('test', $check));
+ $this->assertTrue($check['boom']);
+ }
+
+ public function testUpsertDocumentWithoutFile()
+ {
+ $gridFS = $this->getGridFS();
+
+ $gridFS->update(
+ array('id' => 123),
+ array('x' => 1),
+ array('upsert' => true, 'multiple' => false)
+ );
+
+ $document = $gridFS->findOne();
+
+ $this->assertNotNull($document);
+ $this->assertNotEquals(123, $document['_id']);
+ $this->assertEquals(1, $document['x']);
+ }
+
+ public function testUpsertDocumentWithoutFileWithId()
+ {
+ $gridFS = $this->getGridFS();
+
+ $gridFS->update(
+ array('x' => 1),
+ array('_id' => 123),
+ array('upsert' => true, 'multiple' => false)
+ );
+
+ $document = $gridFS->findOne(array('_id' => 123));
+
+ $this->assertNotNull($document);
+ $this->assertFalse(array_key_exists('x', $document));
+ }
+
+ public function testUpsertModifierWithoutFile()
+ {
+ $gridFS = $this->getGridFS();
+
+ $gridFS->update(
+ array('_id' => 123),
+ array('$set' => array('x' => 1)),
+ array('upsert' => true, 'multiple' => false)
+ );
+
+ $document = $gridFS->findOne(array('_id' => 123));
+
+ $this->assertNotNull($document);
+ $this->assertEquals(1, $document['x']);
+ }
+
+ public function testUpsert()
+ {
+ $gridFS = $this->getGridFS();
+
+ $path = __DIR__.'/file.txt';
+ $file = new GridFSFile($path);
+
+ $newObj = array(
+ '$set' => array(
+ 'title' => 'Test Title',
+ 'file' => $file,
+ ),
+ );
+ $gridFS->update(array('_id' => 123), $newObj, array('upsert' => true, 'multiple' => false));
+
+ $document = $gridFS->findOne(array('_id' => 123));
+
+ $file = $document['file'];
+
+ $this->assertFalse($file->isDirty());
+ $this->assertEquals($path, $file->getFilename());
+ $this->assertEquals(file_get_contents($path), $file->getBytes());
+ $this->assertEquals(22, $file->getSize());
+ }
+
+ private function getGridFS()
+ {
+ return $this->conn->selectDatabase(self::$dbName)->getGridFS();
+ }
+}

0 comments on commit 196895a

Please sign in to comment.