WritableStreamFunctionalTest.php 3.75 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\GridFS;

5
use MongoDB\Exception\InvalidArgumentException;
6 7
use MongoDB\GridFS\CollectionWrapper;
use MongoDB\GridFS\WritableStream;
8
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
9
use function str_repeat;
10 11 12 13 14 15

/**
 * Functional tests for the internal WritableStream class.
 */
class WritableStreamFunctionalTest extends FunctionalTestCase
{
16 17
    use SetUpTearDownTrait;

18
    /** @var CollectionWrapper */
19 20
    private $collectionWrapper;

21
    private function doSetUp()
22 23 24 25 26 27
    {
        parent::setUp();

        $this->collectionWrapper = new CollectionWrapper($this->manager, $this->getDatabaseName(), 'fs');
    }

28 29 30
    /**
     * @doesNotPerformAssertions
     */
31 32 33 34 35 36 37 38 39 40 41 42 43 44
    public function testValidConstructorOptions()
    {
        new WritableStream($this->collectionWrapper, 'filename', [
            '_id' => 'custom-id',
            'chunkSizeBytes' => 2,
            'metadata' => ['foo' => 'bar'],
        ]);
    }

    /**
     * @dataProvider provideInvalidConstructorOptions
     */
    public function testConstructorOptionTypeChecks(array $options)
    {
45
        $this->expectException(InvalidArgumentException::class);
46 47 48 49 50 51 52
        new WritableStream($this->collectionWrapper, 'filename', $options);
    }

    public function provideInvalidConstructorOptions()
    {
        $options = [];

53
        foreach ($this->getInvalidIntegerValues(true) as $value) {
54 55 56
            $options[][] = ['chunkSizeBytes' => $value];
        }

57
        foreach ($this->getInvalidBooleanValues(true) as $value) {
58 59 60
            $options[][] = ['disableMD5' => $value];
        }

61 62 63 64 65 66 67
        foreach ($this->getInvalidDocumentValues() as $value) {
            $options[][] = ['metadata' => $value];
        }

        return $options;
    }

68 69
    public function testConstructorShouldRequireChunkSizeBytesOptionToBePositive()
    {
70 71
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Expected "chunkSizeBytes" option to be >= 1, 0 given');
72 73 74
        new WritableStream($this->collectionWrapper, 'filename', ['chunkSizeBytes' => 0]);
    }

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    public function testWriteBytesAlwaysUpdatesFileSize()
    {
        $stream = new WritableStream($this->collectionWrapper, 'filename', ['chunkSizeBytes' => 1024]);

        $this->assertSame(0, $stream->getSize());
        $this->assertSame(512, $stream->writeBytes(str_repeat('a', 512)));
        $this->assertSame(512, $stream->getSize());
        $this->assertSame(512, $stream->writeBytes(str_repeat('a', 512)));
        $this->assertSame(1024, $stream->getSize());
        $this->assertSame(512, $stream->writeBytes(str_repeat('a', 512)));
        $this->assertSame(1536, $stream->getSize());

        $stream->close();
        $this->assertSame(1536, $stream->getSize());
    }

91 92 93
    /**
     * @dataProvider provideInputDataAndExpectedMD5
     */
94
    public function testWriteBytesCalculatesMD5($input, $expectedMD5)
95 96
    {
        $stream = new WritableStream($this->collectionWrapper, 'filename');
97
        $stream->writeBytes($input);
98 99 100
        $stream->close();

        $fileDocument = $this->filesCollection->findOne(
101
            ['_id' => $stream->getFile()->_id],
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
            ['projection' => ['md5' => 1, '_id' => 0]]
        );

        $this->assertSameDocument(['md5' => $expectedMD5], $fileDocument);
    }

    public function provideInputDataAndExpectedMD5()
    {
        return [
            ['', 'd41d8cd98f00b204e9800998ecf8427e'],
            ['foobar', '3858f62230ac3c915f300c664312c63f'],
            [str_repeat('foobar', 43520), '88ff0e5fcb0acb27947d736b5d69cb73'],
            [str_repeat('foobar', 43521), '8ff86511c95a06a611842ceb555d8454'],
            [str_repeat('foobar', 87040), '45bfa1a9ec36728ee7338d15c5a30c13'],
            [str_repeat('foobar', 87041), '95e78f624f8e745bcfd2d11691fa601e'],
        ];
    }
}