Skip to content

Commit ec1db18

Browse files
committed
Remove stream entities and update GridFS operations
1 parent 194ba47 commit ec1db18

File tree

6 files changed

+48
-81
lines changed

6 files changed

+48
-81
lines changed

tests/UnifiedSpecTests/Constraint/IsStream.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/UnifiedSpecTests/Constraint/IsStreamTest.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/UnifiedSpecTests/Constraint/Matches.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
use function logicalOr;
3737
use function range;
3838
use function sprintf;
39-
use function stream_get_contents;
4039
use function strpos;
4140
use const PHP_INT_SIZE;
4241

tests/UnifiedSpecTests/Context.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@
1616
use function assertInternalType;
1717
use function assertNotEmpty;
1818
use function assertNotFalse;
19-
use function assertRegExp;
2019
use function assertStringStartsWith;
2120
use function count;
2221
use function current;
2322
use function explode;
24-
use function fopen;
25-
use function fwrite;
26-
use function hex2bin;
2723
use function implode;
2824
use function key;
2925
use function parse_url;
30-
use function rewind;
3126
use function strlen;
3227
use function strpos;
3328
use function substr_replace;
@@ -100,10 +95,6 @@ public function createEntities(array $entities)
10095
$this->createBucket($id, $def);
10196
break;
10297

103-
case 'stream':
104-
$this->createStream($id, $def);
105-
break;
106-
10798
default:
10899
throw new LogicException('Unsupported entity type: ' . $type);
109100
}
@@ -294,21 +285,6 @@ private function createBucket(string $id, stdClass $o)
294285
$this->entityMap->set($id, $database->selectGridFSBucket($options), $databaseId);
295286
}
296287

297-
private function createStream(string $id, stdClass $o)
298-
{
299-
Util::assertHasOnlyKeys($o, ['id', 'hexBytes']);
300-
301-
$hexBytes = $o->hexBytes ?? null;
302-
assertInternalType('string', $hexBytes);
303-
assertRegExp('/^([0-9a-fA-F]{2})*$/', $hexBytes);
304-
305-
$stream = fopen('php://temp', 'w+b');
306-
fwrite($stream, hex2bin($hexBytes));
307-
rewind($stream);
308-
309-
$this->entityMap->set($id, $stream);
310-
}
311-
312288
private static function prepareCollectionOrDatabaseOptions(array $options) : array
313289
{
314290
Util::assertHasOnlyKeys($options, ['readConcern', 'readPreference', 'writeConcern']);

tests/UnifiedSpecTests/EntityMap.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use MongoDB\Driver\Session;
1111
use MongoDB\GridFS\Bucket;
1212
use MongoDB\Tests\UnifiedSpecTests\Constraint\IsBsonType;
13-
use MongoDB\Tests\UnifiedSpecTests\Constraint\IsStream;
1413
use PHPUnit\Framework\Assert;
1514
use PHPUnit\Framework\Constraint\Constraint;
1615
use function array_key_exists;
@@ -130,8 +129,7 @@ private static function isSupportedType() : Constraint
130129
isInstanceOf(Session::class),
131130
isInstanceOf(Bucket::class),
132131
isInstanceOf(ChangeStream::class),
133-
IsBsonType::any(),
134-
new IsStream()
132+
IsBsonType::any()
135133
);
136134
}
137135

tests/UnifiedSpecTests/Operation.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use MongoDB\Client;
77
use MongoDB\Collection;
88
use MongoDB\Database;
9-
use MongoDB\GridFS\Bucket;
109
use MongoDB\Driver\Server;
1110
use MongoDB\Driver\Session;
11+
use MongoDB\GridFS\Bucket;
1212
use MongoDB\Model\IndexInfo;
1313
use MongoDB\Operation\FindOneAndReplace;
1414
use MongoDB\Operation\FindOneAndUpdate;
@@ -26,16 +26,23 @@
2626
use function assertNotContains;
2727
use function assertNotNull;
2828
use function assertNull;
29+
use function assertObjectHasAttribute;
30+
use function assertRegExp;
2931
use function assertSame;
3032
use function assertThat;
3133
use function current;
3234
use function equalTo;
35+
use function fopen;
36+
use function fwrite;
3337
use function get_class;
38+
use function hex2bin;
3439
use function iterator_to_array;
3540
use function key;
3641
use function logicalOr;
3742
use function MongoDB\with_transaction;
3843
use function property_exists;
44+
use function rewind;
45+
use function stream_get_contents;
3946
use function strtolower;
4047

4148
final class Operation
@@ -161,11 +168,7 @@ private function execute()
161168
}
162169

163170
if ($result instanceof Traversable && ! $result instanceof ChangeStream) {
164-
return iterator_to_array($result, false);
165-
}
166-
167-
if (is_resource($result) && get_resource_type($result) === 'stream') {
168-
return stream_get_contents($result, -1, 0);
171+
return iterator_to_array($result);
169172
}
170173

171174
return $result;
@@ -427,13 +430,26 @@ private function executeForBucket(Bucket $bucket)
427430
switch ($this->name) {
428431
case 'delete':
429432
return $bucket->delete($args['id']);
430-
case 'openDownloadStream':
431-
return $bucket->openDownloadStream($args['id']);
432-
case 'uploadFromStream':
433+
case 'downloadByName':
434+
return stream_get_contents($bucket->openDownloadStream(
435+
$args['filename'],
436+
array_diff_key($args, ['filename' => 1])
437+
));
438+
case 'download':
439+
return stream_get_contents($bucket->openDownloadStream($args['id']));
440+
case 'uploadWithId':
441+
$args['_id'] = $args['id'];
442+
unset($args['id']);
443+
444+
// Fall through
445+
446+
case 'upload':
447+
$args = self::prepareUploadArguments($args);
448+
433449
return $bucket->uploadFromStream(
434450
$args['filename'],
435-
$this->entityMap[$args['source']],
436-
array_diff_key($args, ['filename' => 1, 'source'])
451+
$args['source'],
452+
array_diff_key($args, ['filename' => 1, 'source' => 1])
437453
);
438454
default:
439455
Assert::fail('Unsupported bucket operation: ' . $this->name);
@@ -527,7 +543,7 @@ private function prepareArguments() : array
527543
return Util::prepareCommonOptions($args);
528544
}
529545

530-
private function prepareBulkWriteRequest(stdClass $request) : array
546+
private static function prepareBulkWriteRequest(stdClass $request) : array
531547
{
532548
$request = (array) $request;
533549
assertCount(1, $request);
@@ -569,4 +585,23 @@ private function prepareBulkWriteRequest(stdClass $request) : array
569585
Assert::fail('Unsupported bulk write request: ' . $type);
570586
}
571587
}
588+
589+
private static function prepareUploadArguments(array $args) : array
590+
{
591+
$source = $args['source'] ?? null;
592+
assertInternalType('object', $source);
593+
assertObjectHasAttribute('$$hexBytes', $source);
594+
Util::assertHasOnlyKeys($source, ['$$hexBytes']);
595+
$hexBytes = $source->{'$$hexBytes'};
596+
assertInternalType('string', $hexBytes);
597+
assertRegExp('/^([0-9a-fA-F]{2})*$/', $hexBytes);
598+
599+
$stream = fopen('php://temp', 'w+b');
600+
fwrite($stream, hex2bin($hexBytes));
601+
rewind($stream);
602+
603+
$args['source'] = $stream;
604+
605+
return $args;
606+
}
572607
}

0 commit comments

Comments
 (0)