QueryTest.php 1.66 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
    protected function setUp()
    {
        parent::setUp();
        $this->setUpTestRows();
    }
Paul Klimov committed
18

19 20 21 22 23
    protected function tearDown()
    {
        $this->dropFileCollection();
        parent::tearDown();
    }
Paul Klimov committed
24

25 26 27 28 29 30 31 32 33 34 35 36 37
    /**
     * Sets up test rows.
     */
    protected function setUpTestRows()
    {
        $collection = $this->getConnection()->getFileCollection();
        for ($i = 1; $i <= 10; $i++) {
            $collection->insertFileContent('content' . $i, [
                'filename' => 'name' . $i,
                'file_index' => $i,
            ]);
        }
    }
Paul Klimov committed
38

39
    // Tests :
Paul Klimov committed
40

41 42 43 44 45 46 47
    public function testAll()
    {
        $connection = $this->getConnection();
        $query = new Query;
        $rows = $query->from('fs')->all($connection);
        $this->assertEquals(10, count($rows));
    }
Paul Klimov committed
48

49 50 51 52 53 54 55 56
    public function testOne()
    {
        $connection = $this->getConnection();
        $query = new Query;
        $row = $query->from('fs')->one($connection);
        $this->assertTrue(is_array($row));
        $this->assertTrue($row['file'] instanceof \MongoGridFSFile);
    }
57

58 59 60 61 62 63 64 65 66 67 68 69
    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];
        $this->assertEquals('name5', $file['filename']);
    }
AlexGx committed
70
}