QueryTest.php 1.4 KB
Newer Older
Paul Klimov committed
1 2
<?php

3
namespace yiiunit\extensions\mongodb\file;
Paul Klimov committed
4

5 6
use yii\mongodb\file\Query;
use yiiunit\extensions\mongodb\MongoDbTestCase;
Paul Klimov committed
7 8

/**
9
 * @group mongodb
Paul Klimov committed
10
 */
11
class QueryTest extends MongoDbTestCase
Paul Klimov committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
{
	protected function setUp()
	{
		parent::setUp();
		$this->setUpTestRows();
	}

	protected function tearDown()
	{
		$this->dropFileCollection();
		parent::tearDown();
	}

	/**
	 * Sets up test rows.
	 */
	protected function setUpTestRows()
	{
		$collection = $this->getConnection()->getFileCollection();
		for ($i = 1; $i <= 10; $i++) {
32
			$collection->insertFileContent('content' . $i, [
33 34 35
				'filename' => 'name' . $i,
				'file_index' => $i,
			]);
Paul Klimov committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
		}
	}

	// Tests :

	public function testAll()
	{
		$connection = $this->getConnection();
		$query = new Query;
		$rows = $query->from('fs')->all($connection);
		$this->assertEquals(10, count($rows));
	}

	public function testOne()
	{
		$connection = $this->getConnection();
		$query = new Query;
		$row = $query->from('fs')->one($connection);
54 55
		$this->assertTrue(is_array($row));
		$this->assertTrue($row['file'] instanceof \MongoGridFSFile);
Paul Klimov committed
56
	}
57 58 59 60 61 62 63 64 65 66 67

	public function testDirectMatch()
	{
		$connection = $this->getConnection();
		$query = new Query;
		$rows = $query->from('fs')
			->where(['file_index' => 5])
			->all($connection);
		$this->assertEquals(1, count($rows));
		/** @var $file \MongoGridFSFile */
		$file = $rows[0];
68
		$this->assertEquals('name5', $file['filename']);
69
	}
Paul Klimov committed
70
}