Alcaeus\MongoDbAdapter\Tests\Mongo\MongoGridFSTest::testStoreFileResource PHP Method

testStoreFileResource() public method

    public function testStoreFileResource()
    {
        $collection = $this->getGridFS();
        $id = $collection->storeFile(fopen(__FILE__, 'r'), ['chunkSize' => 100, 'foo' => 'bar', 'filename' => 'test.php']);
        $newCollection = $this->getCheckDatabase()->selectCollection('fs.files');
        $newChunksCollection = $this->getCheckDatabase()->selectCollection('fs.chunks');
        $this->assertSame(1, $newCollection->count());
        $md5 = md5_file(__FILE__);
        $size = filesize(__FILE__);
        $filename = basename(__FILE__);
        $record = $newCollection->findOne();
        $this->assertNotNull($record);
        $this->assertAttributeInstanceOf('MongoDB\\BSON\\ObjectID', '_id', $record);
        $this->assertSame((string) $id, (string) $record->_id);
        $this->assertObjectHasAttribute('foo', $record);
        $this->assertAttributeSame('bar', 'foo', $record);
        $this->assertObjectHasAttribute('length', $record);
        $this->assertAttributeSame($size, 'length', $record);
        $this->assertObjectHasAttribute('chunkSize', $record);
        $this->assertAttributeSame(100, 'chunkSize', $record);
        $this->assertObjectHasAttribute('md5', $record);
        $this->assertAttributeSame($md5, 'md5', $record);
        $this->assertObjectHasAttribute('filename', $record);
        $this->assertAttributeSame('test.php', 'filename', $record);
        $numberOfChunks = (int) ceil($size / 100);
        $this->assertSame($numberOfChunks, $newChunksCollection->count());
        $expectedContent = substr(file_get_contents(__FILE__), 0, 100);
        $firstChunk = $newChunksCollection->findOne([], ['sort' => ['n' => 1]]);
        $this->assertNotNull($firstChunk);
        $this->assertAttributeInstanceOf('MongoDB\\BSON\\ObjectID', 'files_id', $firstChunk);
        $this->assertSame((string) $id, (string) $firstChunk->files_id);
        $this->assertAttributeSame(0, 'n', $firstChunk);
        $this->assertAttributeInstanceOf('MongoDB\\BSON\\Binary', 'data', $firstChunk);
        $this->assertSame($expectedContent, (string) $firstChunk->data->getData());
    }