Skip to content

PHPLIB-494: Fix validation of options with default values #696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function __construct($uri = 'mongodb://127.0.0.1/', array $uriOptions = [
{
$driverOptions += ['typeMap' => self::$defaultTypeMap];

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

Expand Down
8 changes: 4 additions & 4 deletions src/GridFS/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,19 @@ public function __construct(Manager $manager, $databaseName, array $options = []
'disableMD5' => false,
];

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

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

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

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

Expand Down
6 changes: 3 additions & 3 deletions src/GridFS/WritableStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ public function __construct(CollectionWrapper $collectionWrapper, $filename, arr
throw InvalidArgumentException::invalidType('"aliases" option', $options['aliases'], 'array of strings');
}

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

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

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

Expand Down
6 changes: 5 additions & 1 deletion src/Operation/Watch.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,14 @@ public function __construct(Manager $manager, $databaseName, $collectionName, ar
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY),
];

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

if (! $options['readPreference'] instanceof ReadPreference) {
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], ReadPreference::class);
}

if (isset($options['resumeAfter']) && ! is_array($options['resumeAfter']) && ! is_object($options['resumeAfter'])) {
throw InvalidArgumentException::invalidType('"resumeAfter" option', $options['resumeAfter'], 'array or object');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function provideInvalidConstructorDriverOptions()
{
$options = [];

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

Expand Down
6 changes: 3 additions & 3 deletions tests/GridFS/BucketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public function provideInvalidConstructorOptions()
{
$options = [];

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions tests/GridFS/WritableStreamFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public function provideInvalidConstructorOptions()
{
$options = [];

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

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

Expand Down
4 changes: 2 additions & 2 deletions tests/Operation/AggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function provideInvalidConstructorOptions()
{
$options = [];

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

Expand Down Expand Up @@ -79,7 +79,7 @@ public function provideInvalidConstructorOptions()
$options[][] = ['typeMap' => $value];
}

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

Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/BulkWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public function provideInvalidConstructorOptions()
$options[][] = ['bypassDocumentValidation' => $value];
}

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

Expand Down
6 changes: 3 additions & 3 deletions tests/Operation/FindAndModifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public function provideInvalidConstructorOptions()
$options[][] = ['maxTimeMS' => $value];
}

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

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/FindOneAndReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function provideInvalidConstructorOptions()
$options[][] = ['projection' => $value];
}

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

Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/FindOneAndUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function provideInvalidConstructorOptions()
$options[][] = ['projection' => $value];
}

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

Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/InsertManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function provideInvalidConstructorOptions()
$options[][] = ['bypassDocumentValidation' => $value];
}

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

Expand Down
4 changes: 2 additions & 2 deletions tests/Operation/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ public function provideInvalidConstructorOptions()
$options[][] = ['collation' => $value];
}

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions tests/Operation/WatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function provideInvalidConstructorOptions()
$options[][] = ['collation' => $value];
}

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

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

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

Expand Down
55 changes: 37 additions & 18 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use stdClass;
use Traversable;
use function array_map;
use function array_merge;
use function array_values;
use function call_user_func;
use function getenv;
Expand Down Expand Up @@ -165,91 +166,109 @@ protected function getDatabaseName()
/**
* Return a list of invalid array values.
*
* @param boolean $includeNull
*
* @return array
*/
protected function getInvalidArrayValues()
protected function getInvalidArrayValues($includeNull = false)
{
return [123, 3.14, 'foo', true, new stdClass()];
return array_merge([123, 3.14, 'foo', true, new stdClass()], $includeNull ? [null] : []);
}

/**
* Return a list of invalid boolean values.
*
* @param boolean $includeNull
*
* @return array
*/
protected function getInvalidBooleanValues()
protected function getInvalidBooleanValues($includeNull = false)
{
return [123, 3.14, 'foo', [], new stdClass()];
return array_merge([123, 3.14, 'foo', [], new stdClass()], $includeNull ? [null] : []);
}

/**
* Return a list of invalid document values.
*
* @param boolean $includeNull
*
* @return array
*/
protected function getInvalidDocumentValues()
protected function getInvalidDocumentValues($includeNull = false)
{
return [123, 3.14, 'foo', true];
return array_merge([123, 3.14, 'foo', true], $includeNull ? [null] : []);
}

/**
* Return a list of invalid integer values.
*
* @param boolean $includeNull
*
* @return array
*/
protected function getInvalidIntegerValues()
protected function getInvalidIntegerValues($includeNull = false)
{
return [3.14, 'foo', true, [], new stdClass()];
return array_merge([3.14, 'foo', true, [], new stdClass()], $includeNull ? [null] : []);
}

/**
* Return a list of invalid ReadPreference values.
*
* @param boolean $includeNull
*
* @return array
*/
protected function getInvalidReadConcernValues()
protected function getInvalidReadConcernValues($includeNull = false)
{
return [123, 3.14, 'foo', true, [], new stdClass(), new ReadPreference(ReadPreference::RP_PRIMARY), new WriteConcern(1)];
return array_merge([123, 3.14, 'foo', true, [], new stdClass(), new ReadPreference(ReadPreference::RP_PRIMARY), new WriteConcern(1)], $includeNull ? [null] : []);
}

/**
* Return a list of invalid ReadPreference values.
*
* @param boolean $includeNull
*
* @return array
*/
protected function getInvalidReadPreferenceValues()
protected function getInvalidReadPreferenceValues($includeNull = false)
{
return [123, 3.14, 'foo', true, [], new stdClass(), new ReadConcern(), new WriteConcern(1)];
return array_merge([123, 3.14, 'foo', true, [], new stdClass(), new ReadConcern(), new WriteConcern(1)], $includeNull ? [null] : []);
}

/**
* Return a list of invalid Session values.
*
* @param boolean $includeNull
*
* @return array
*/
protected function getInvalidSessionValues()
protected function getInvalidSessionValues($includeNull = false)
{
return [123, 3.14, 'foo', true, [], new stdClass(), new ReadConcern(), new ReadPreference(ReadPreference::RP_PRIMARY), new WriteConcern(1)];
return array_merge([123, 3.14, 'foo', true, [], new stdClass(), new ReadConcern(), new ReadPreference(ReadPreference::RP_PRIMARY), new WriteConcern(1)], $includeNull ? [null] : []);
}

/**
* Return a list of invalid string values.
*
* @param boolean $includeNull
*
* @return array
*/
protected function getInvalidStringValues()
protected function getInvalidStringValues($includeNull = false)
{
return [123, 3.14, true, [], new stdClass()];
return array_merge([123, 3.14, true, [], new stdClass()], $includeNull ? [null] : []);
}

/**
* Return a list of invalid WriteConcern values.
*
* @param boolean $includeNull
*
* @return array
*/
protected function getInvalidWriteConcernValues()
protected function getInvalidWriteConcernValues($includeNull = false)
{
return [123, 3.14, 'foo', true, [], new stdClass(), new ReadConcern(), new ReadPreference(ReadPreference::RP_PRIMARY)];
return array_merge([123, 3.14, 'foo', true, [], new stdClass(), new ReadConcern(), new ReadPreference(ReadPreference::RP_PRIMARY)], $includeNull ? [null] : []);
}

/**
Expand Down