CollectionTest.php 2.33 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\mongodb\file;
4

5
use yiiunit\extensions\mongodb\MongoDbTestCase;
6

Paul Klimov committed
7
/**
8
 * @group mongodb
Paul Klimov committed
9
 */
10
class CollectionTest extends MongoDbTestCase
11 12 13 14 15 16 17 18 19
{
	protected function tearDown()
	{
		$this->dropFileCollection('fs');
		parent::tearDown();
	}

	// Tests :

20 21 22 23
	public function testGetChunkCollection()
	{
		$collection = $this->getConnection()->getFileCollection();
		$chunkCollection = $collection->getChunkCollection();
24
		$this->assertTrue($chunkCollection instanceof \yii\mongodb\Collection);
25 26 27
		$this->assertTrue($chunkCollection->mongoCollection instanceof \MongoCollection);
	}

28 29 30 31 32 33 34
	public function testFind()
	{
		$collection = $this->getConnection()->getFileCollection();
		$cursor = $collection->find();
		$this->assertTrue($cursor instanceof \MongoGridFSCursor);
	}

35
	public function testInsertFile()
36 37 38 39
	{
		$collection = $this->getConnection()->getFileCollection();

		$filename = __FILE__;
40
		$id = $collection->insertFile($filename);
41 42 43 44 45 46 47 48 49 50 51
		$this->assertTrue($id instanceof \MongoId);

		$files = $this->findAll($collection);
		$this->assertEquals(1, count($files));

		/** @var $file \MongoGridFSFile */
		$file = $files[0];
		$this->assertEquals($filename, $file->getFilename());
		$this->assertEquals(file_get_contents($filename), $file->getBytes());
	}

52
	public function testInsertFileContent()
53 54 55 56
	{
		$collection = $this->getConnection()->getFileCollection();

		$bytes = 'Test file content';
57
		$id = $collection->insertFileContent($bytes);
58 59 60 61 62 63 64 65 66 67 68
		$this->assertTrue($id instanceof \MongoId);

		$files = $this->findAll($collection);
		$this->assertEquals(1, count($files));

		/** @var $file \MongoGridFSFile */
		$file = $files[0];
		$this->assertEquals($bytes, $file->getBytes());
	}

	/**
69
	 * @depends testInsertFileContent
70 71 72 73 74 75
	 */
	public function testGet()
	{
		$collection = $this->getConnection()->getFileCollection();

		$bytes = 'Test file content';
76
		$id = $collection->insertFileContent($bytes);
77 78 79 80 81 82 83 84 85 86 87 88 89 90

		$file = $collection->get($id);
		$this->assertTrue($file instanceof \MongoGridFSFile);
		$this->assertEquals($bytes, $file->getBytes());
	}

	/**
	 * @depends testGet
	 */
	public function testDelete()
	{
		$collection = $this->getConnection()->getFileCollection();

		$bytes = 'Test file content';
91
		$id = $collection->insertFileContent($bytes);
92 93 94 95 96 97 98

		$this->assertTrue($collection->delete($id));

		$file = $collection->get($id);
		$this->assertNull($file);
	}
}