Skip to content

Commit a42e347

Browse files
committed
Merge branch 'v1.5'
* v1.5: PHPLIB-494: Fix validation of options with default values PHPLIB-494: Test against null for options with default values Simplify generator setup in tests PHPLIB-501: Use IteratorIterator to prevent memory leak with generators
2 parents 384649d + 9480f99 commit a42e347

18 files changed

+79
-73
lines changed

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function __construct($uri = 'mongodb://127.0.0.1/', array $uriOptions = [
9595
{
9696
$driverOptions += ['typeMap' => self::$defaultTypeMap];
9797

98-
if (isset($driverOptions['typeMap']) && ! is_array($driverOptions['typeMap'])) {
98+
if (! is_array($driverOptions['typeMap'])) {
9999
throw InvalidArgumentException::invalidType('"typeMap" driver option', $driverOptions['typeMap'], 'array');
100100
}
101101

src/GridFS/Bucket.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,19 @@ public function __construct(Manager $manager, $databaseName, array $options = []
143143
'disableMD5' => false,
144144
];
145145

146-
if (isset($options['bucketName']) && ! is_string($options['bucketName'])) {
146+
if (! is_string($options['bucketName'])) {
147147
throw InvalidArgumentException::invalidType('"bucketName" option', $options['bucketName'], 'string');
148148
}
149149

150-
if (isset($options['chunkSizeBytes']) && ! is_integer($options['chunkSizeBytes'])) {
150+
if (! is_integer($options['chunkSizeBytes'])) {
151151
throw InvalidArgumentException::invalidType('"chunkSizeBytes" option', $options['chunkSizeBytes'], 'integer');
152152
}
153153

154-
if (isset($options['chunkSizeBytes']) && $options['chunkSizeBytes'] < 1) {
154+
if ($options['chunkSizeBytes'] < 1) {
155155
throw new InvalidArgumentException(sprintf('Expected "chunkSizeBytes" option to be >= 1, %d given', $options['chunkSizeBytes']));
156156
}
157157

158-
if (isset($options['disableMD5']) && ! is_bool($options['disableMD5'])) {
158+
if (! is_bool($options['disableMD5'])) {
159159
throw InvalidArgumentException::invalidType('"disableMD5" option', $options['disableMD5'], 'boolean');
160160
}
161161

src/GridFS/WritableStream.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ public function __construct(CollectionWrapper $collectionWrapper, $filename, arr
114114
throw InvalidArgumentException::invalidType('"aliases" option', $options['aliases'], 'array of strings');
115115
}
116116

117-
if (isset($options['chunkSizeBytes']) && ! is_integer($options['chunkSizeBytes'])) {
117+
if (! is_integer($options['chunkSizeBytes'])) {
118118
throw InvalidArgumentException::invalidType('"chunkSizeBytes" option', $options['chunkSizeBytes'], 'integer');
119119
}
120120

121-
if (isset($options['chunkSizeBytes']) && $options['chunkSizeBytes'] < 1) {
121+
if ($options['chunkSizeBytes'] < 1) {
122122
throw new InvalidArgumentException(sprintf('Expected "chunkSizeBytes" option to be >= 1, %d given', $options['chunkSizeBytes']));
123123
}
124124

125-
if (isset($options['disableMD5']) && ! is_bool($options['disableMD5'])) {
125+
if (! is_bool($options['disableMD5'])) {
126126
throw InvalidArgumentException::invalidType('"disableMD5" option', $options['disableMD5'], 'boolean');
127127
}
128128

src/Model/CachingIterator.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
namespace MongoDB\Model;
1919

2020
use Countable;
21-
use Generator;
2221
use Iterator;
22+
use IteratorIterator;
2323
use Traversable;
2424
use function count;
2525
use function current;
@@ -41,7 +41,7 @@ class CachingIterator implements Countable, Iterator
4141
/** @var array */
4242
private $items = [];
4343

44-
/** @var Generator */
44+
/** @var IteratorIterator */
4545
private $iterator;
4646

4747
/** @var boolean */
@@ -61,7 +61,9 @@ class CachingIterator implements Countable, Iterator
6161
*/
6262
public function __construct(Traversable $traversable)
6363
{
64-
$this->iterator = $this->wrapTraversable($traversable);
64+
$this->iterator = new IteratorIterator($traversable);
65+
66+
$this->iterator->rewind();
6567
$this->storeCurrentItem();
6668
}
6769

@@ -101,8 +103,12 @@ public function key()
101103
public function next()
102104
{
103105
if (! $this->iteratorExhausted) {
106+
$this->iteratorAdvanced = true;
104107
$this->iterator->next();
108+
105109
$this->storeCurrentItem();
110+
111+
$this->iteratorExhausted = ! $this->iterator->valid();
106112
}
107113

108114
next($this->items);
@@ -156,20 +162,4 @@ private function storeCurrentItem()
156162

157163
$this->items[$key] = $this->iterator->current();
158164
}
159-
160-
/**
161-
* Wraps the Traversable with a Generator.
162-
*
163-
* @param Traversable $traversable
164-
* @return Generator
165-
*/
166-
private function wrapTraversable(Traversable $traversable)
167-
{
168-
foreach ($traversable as $key => $value) {
169-
yield $key => $value;
170-
$this->iteratorAdvanced = true;
171-
}
172-
173-
$this->iteratorExhausted = true;
174-
}
175165
}

src/Operation/Watch.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,14 @@ public function __construct(Manager $manager, $databaseName, $collectionName, ar
178178
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY),
179179
];
180180

181-
if (isset($options['fullDocument']) && ! is_string($options['fullDocument'])) {
181+
if (! is_string($options['fullDocument'])) {
182182
throw InvalidArgumentException::invalidType('"fullDocument" option', $options['fullDocument'], 'string');
183183
}
184184

185+
if (! $options['readPreference'] instanceof ReadPreference) {
186+
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], ReadPreference::class);
187+
}
188+
185189
if (isset($options['resumeAfter']) && ! is_array($options['resumeAfter']) && ! is_object($options['resumeAfter'])) {
186190
throw InvalidArgumentException::invalidType('"resumeAfter" option', $options['resumeAfter'], 'array or object');
187191
}

tests/ClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function provideInvalidConstructorDriverOptions()
3333
{
3434
$options = [];
3535

36-
foreach ($this->getInvalidArrayValues() as $value) {
36+
foreach ($this->getInvalidArrayValues(true) as $value) {
3737
$options[][] = ['typeMap' => $value];
3838
}
3939

tests/GridFS/BucketFunctionalTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ public function provideInvalidConstructorOptions()
6262
{
6363
$options = [];
6464

65-
foreach ($this->getInvalidStringValues() as $value) {
65+
foreach ($this->getInvalidStringValues(true) as $value) {
6666
$options[][] = ['bucketName' => $value];
6767
}
6868

69-
foreach ($this->getInvalidIntegerValues() as $value) {
69+
foreach ($this->getInvalidIntegerValues(true) as $value) {
7070
$options[][] = ['chunkSizeBytes' => $value];
7171
}
7272

73-
foreach ($this->getInvalidBooleanValues() as $value) {
73+
foreach ($this->getInvalidBooleanValues(true) as $value) {
7474
$options[][] = ['disableMD5' => $value];
7575
}
7676

tests/GridFS/WritableStreamFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public function provideInvalidConstructorOptions()
5050
{
5151
$options = [];
5252

53-
foreach ($this->getInvalidIntegerValues() as $value) {
53+
foreach ($this->getInvalidIntegerValues(true) as $value) {
5454
$options[][] = ['chunkSizeBytes' => $value];
5555
}
5656

57-
foreach ($this->getInvalidBooleanValues() as $value) {
57+
foreach ($this->getInvalidBooleanValues(true) as $value) {
5858
$options[][] = ['disableMD5' => $value];
5959
}
6060

tests/Model/CachingIteratorTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testIterationWithEmptySet()
5353

5454
public function testPartialIterationDoesNotExhaust()
5555
{
56-
$traversable = $this->getTraversableThatThrows([1, 2, new Exception()]);
56+
$traversable = $this->getTraversable([1, 2, new Exception()]);
5757
$iterator = new CachingIterator($traversable);
5858

5959
$expectedKey = 0;
@@ -110,13 +110,6 @@ public function testCountWithEmptySet()
110110
}
111111

112112
private function getTraversable($items)
113-
{
114-
foreach ($items as $item) {
115-
yield $item;
116-
}
117-
}
118-
119-
private function getTraversableThatThrows($items)
120113
{
121114
foreach ($items as $item) {
122115
if ($item instanceof Exception) {

tests/Operation/AggregateTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function provideInvalidConstructorOptions()
2727
{
2828
$options = [];
2929

30-
foreach ($this->getInvalidBooleanValues() as $value) {
30+
foreach ($this->getInvalidBooleanValues(true) as $value) {
3131
$options[][] = ['allowDiskUse' => $value];
3232
}
3333

@@ -79,7 +79,7 @@ public function provideInvalidConstructorOptions()
7979
$options[][] = ['typeMap' => $value];
8080
}
8181

82-
foreach ($this->getInvalidBooleanValues() as $value) {
82+
foreach ($this->getInvalidBooleanValues(true) as $value) {
8383
$options[][] = ['useCursor' => $value];
8484
}
8585

tests/Operation/BulkWriteTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ public function provideInvalidConstructorOptions()
412412
$options[][] = ['bypassDocumentValidation' => $value];
413413
}
414414

415-
foreach ($this->getInvalidBooleanValues() as $value) {
415+
foreach ($this->getInvalidBooleanValues(true) as $value) {
416416
$options[][] = ['ordered' => $value];
417417
}
418418

tests/Operation/FindAndModifyTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public function provideInvalidConstructorOptions()
4040
$options[][] = ['maxTimeMS' => $value];
4141
}
4242

43-
foreach ($this->getInvalidBooleanValues() as $value) {
43+
foreach ($this->getInvalidBooleanValues(true) as $value) {
4444
$options[][] = ['new' => $value];
4545
}
4646

4747
foreach ($this->getInvalidDocumentValues() as $value) {
4848
$options[][] = ['query' => $value];
4949
}
5050

51-
foreach ($this->getInvalidBooleanValues() as $value) {
51+
foreach ($this->getInvalidBooleanValues(true) as $value) {
5252
$options[][] = ['remove' => $value];
5353
}
5454

@@ -68,7 +68,7 @@ public function provideInvalidConstructorOptions()
6868
$options[][] = ['update' => $value];
6969
}
7070

71-
foreach ($this->getInvalidBooleanValues() as $value) {
71+
foreach ($this->getInvalidBooleanValues(true) as $value) {
7272
$options[][] = ['upsert' => $value];
7373
}
7474

tests/Operation/FindOneAndReplaceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function provideInvalidConstructorOptions()
4949
$options[][] = ['projection' => $value];
5050
}
5151

52-
foreach ($this->getInvalidIntegerValues() as $value) {
52+
foreach ($this->getInvalidIntegerValues(true) as $value) {
5353
$options[][] = ['returnDocument' => $value];
5454
}
5555

tests/Operation/FindOneAndUpdateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function provideInvalidConstructorOptions()
4949
$options[][] = ['projection' => $value];
5050
}
5151

52-
foreach ($this->getInvalidIntegerValues() as $value) {
52+
foreach ($this->getInvalidIntegerValues(true) as $value) {
5353
$options[][] = ['returnDocument' => $value];
5454
}
5555

tests/Operation/InsertManyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function provideInvalidConstructorOptions()
4848
$options[][] = ['bypassDocumentValidation' => $value];
4949
}
5050

51-
foreach ($this->getInvalidBooleanValues() as $value) {
51+
foreach ($this->getInvalidBooleanValues(true) as $value) {
5252
$options[][] = ['ordered' => $value];
5353
}
5454

tests/Operation/UpdateTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ public function provideInvalidConstructorOptions()
5252
$options[][] = ['collation' => $value];
5353
}
5454

55-
foreach ($this->getInvalidBooleanValues() as $value) {
55+
foreach ($this->getInvalidBooleanValues(true) as $value) {
5656
$options[][] = ['multi' => $value];
5757
}
5858

5959
foreach ($this->getInvalidSessionValues() as $value) {
6060
$options[][] = ['session' => $value];
6161
}
6262

63-
foreach ($this->getInvalidBooleanValues() as $value) {
63+
foreach ($this->getInvalidBooleanValues(true) as $value) {
6464
$options[][] = ['upsert' => $value];
6565
}
6666

tests/Operation/WatchTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function provideInvalidConstructorOptions()
5252
$options[][] = ['collation' => $value];
5353
}
5454

55-
foreach ($this->getInvalidStringValues() as $value) {
55+
foreach ($this->getInvalidStringValues(true) as $value) {
5656
$options[][] = ['fullDocument' => $value];
5757
}
5858

@@ -64,7 +64,7 @@ public function provideInvalidConstructorOptions()
6464
$options[][] = ['readConcern' => $value];
6565
}
6666

67-
foreach ($this->getInvalidReadPreferenceValues() as $value) {
67+
foreach ($this->getInvalidReadPreferenceValues(true) as $value) {
6868
$options[][] = ['readPreference' => $value];
6969
}
7070

0 commit comments

Comments
 (0)