Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
d9c1c537
Commit
d9c1c537
authored
Nov 30, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-239: Unpack MongoDB\BSON\Serializable instances before reading properties
parent
8117675e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
157 additions
and
7 deletions
+157
-7
functions.php
src/functions.php
+8
-0
FunctionsTest.php
tests/FunctionsTest.php
+59
-1
ReplaceOneTest.php
tests/Operation/ReplaceOneTest.php
+30
-2
UpdateManyTest.php
tests/Operation/UpdateManyTest.php
+30
-2
UpdateOneTest.php
tests/Operation/UpdateOneTest.php
+30
-2
No files found.
src/functions.php
View file @
d9c1c537
...
...
@@ -41,6 +41,10 @@ function extract_id_from_inserted_document($document)
*/
function
generate_index_name
(
$document
)
{
if
(
$document
instanceof
Serializable
)
{
$document
=
$document
->
bsonSerialize
();
}
if
(
is_object
(
$document
))
{
$document
=
get_object_vars
(
$document
);
}
...
...
@@ -70,6 +74,10 @@ function generate_index_name($document)
*/
function
is_first_key_operator
(
$document
)
{
if
(
$document
instanceof
Serializable
)
{
$document
=
$document
->
bsonSerialize
();
}
if
(
is_object
(
$document
))
{
$document
=
get_object_vars
(
$document
);
}
...
...
tests/FunctionsTest.php
View file @
d9c1c537
...
...
@@ -2,14 +2,72 @@
namespace
MongoDB\Tests
;
use
MongoDB\Model\BSONDocument
;
use
MongoDB\Driver\ReadConcern
;
use
MongoDB\Driver\WriteConcern
;
/**
* Unit tests for utility functions.
*/
class
FunctionsTest
extends
\PHPUnit_Framework_
TestCase
class
FunctionsTest
extends
TestCase
{
/**
* @dataProvider provideIndexSpecificationDocumentsAndGeneratedNames
*/
public
function
testGenerateIndexName
(
$document
,
$expectedName
)
{
$this
->
assertSame
(
$expectedName
,
\MongoDB\generate_index_name
(
$document
));
}
public
function
provideIndexSpecificationDocumentsAndGeneratedNames
()
{
return
[
[
[
'x'
=>
1
],
'x_1'
],
[
[
'x'
=>
-
1
,
'y'
=>
1
],
'x_-1_y_1'
],
[
[
'x'
=>
'2dsphere'
,
'y'
=>
1
],
'x_2dsphere_y_1'
],
[
(
object
)
[
'x'
=>
1
],
'x_1'
],
[
new
BSONDocument
([
'x'
=>
1
]),
'x_1'
],
];
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidDocumentValues
*/
public
function
testGenerateIndexNameArgumentTypeCheck
(
$document
)
{
\MongoDB\generate_index_name
(
$document
);
}
/**
* @dataProvider provideIsFirstKeyOperatorDocuments
*/
public
function
testIsFirstKeyOperator
(
$document
,
$isFirstKeyOperator
)
{
$this
->
assertSame
(
$isFirstKeyOperator
,
\MongoDB\is_first_key_operator
(
$document
));
}
public
function
provideIsFirstKeyOperatorDocuments
()
{
return
[
[
[
'y'
=>
1
],
false
],
[
(
object
)
[
'y'
=>
1
],
false
],
[
new
BSONDocument
([
'y'
=>
1
]),
false
],
[
[
'$set'
=>
[
'y'
=>
1
]],
true
],
[
(
object
)
[
'$set'
=>
[
'y'
=>
1
]],
true
],
[
new
BSONDocument
([
'$set'
=>
[
'y'
=>
1
]]),
true
],
];
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidDocumentValues
*/
public
function
testIsFirstKeyOperatorArgumentTypeCheck
(
$document
)
{
\MongoDB\is_first_key_operator
(
$document
);
}
/**
* @dataProvider provideReadConcernsAndDocuments
*/
...
...
tests/Operation/ReplaceOneTest.php
View file @
d9c1c537
...
...
@@ -2,6 +2,7 @@
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Model\BSONDocument
;
use
MongoDB\Operation\ReplaceOne
;
class
ReplaceOneTest
extends
TestCase
...
...
@@ -24,12 +25,39 @@ class ReplaceOneTest extends TestCase
new
ReplaceOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
],
$replacement
);
}
/**
* @dataProvider provideReplacementDocuments
*/
public
function
testConstructorReplacementArgument
(
$replacement
)
{
new
ReplaceOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
],
$replacement
);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $replacement argument is an update operator
* @dataProvider provideUpdateDocuments
*/
public
function
testConstructorReplacementArgumentRequiresNoOperators
()
public
function
testConstructorReplacementArgumentRequiresNoOperators
(
$replacement
)
{
new
ReplaceOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
],
$replacement
);
}
public
function
provideReplacementDocuments
()
{
return
$this
->
wrapValuesForDataProvider
([
[
'y'
=>
1
],
(
object
)
[
'y'
=>
1
],
new
BSONDocument
([
'y'
=>
1
]),
]);
}
public
function
provideUpdateDocuments
()
{
new
ReplaceOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
],
[
'$set'
=>
[
'x'
=>
1
]]);
return
$this
->
wrapValuesForDataProvider
([
[
'$set'
=>
[
'y'
=>
1
]],
(
object
)
[
'$set'
=>
[
'y'
=>
1
]],
new
BSONDocument
([
'$set'
=>
[
'y'
=>
1
]]),
]);
}
}
tests/Operation/UpdateManyTest.php
View file @
d9c1c537
...
...
@@ -2,6 +2,7 @@
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Model\BSONDocument
;
use
MongoDB\Operation\UpdateMany
;
class
UpdateManyTest
extends
TestCase
...
...
@@ -24,12 +25,39 @@ class UpdateManyTest extends TestCase
new
UpdateMany
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
],
$update
);
}
/**
* @dataProvider provideUpdateDocuments
*/
public
function
testConstructorUpdateArgument
(
$update
)
{
new
UpdateMany
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
],
$update
);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $update argument is not an update operator
* @dataProvider provideReplacementDocuments
*/
public
function
testConstructorUpdateArgumentRequiresOperators
()
public
function
testConstructorUpdateArgumentRequiresOperators
(
$replacement
)
{
new
UpdateMany
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
],
$replacement
);
}
public
function
provideReplacementDocuments
()
{
return
$this
->
wrapValuesForDataProvider
([
[
'y'
=>
1
],
(
object
)
[
'y'
=>
1
],
new
BSONDocument
([
'y'
=>
1
]),
]);
}
public
function
provideUpdateDocuments
()
{
new
UpdateMany
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
],
[
'y'
=>
1
]);
return
$this
->
wrapValuesForDataProvider
([
[
'$set'
=>
[
'y'
=>
1
]],
(
object
)
[
'$set'
=>
[
'y'
=>
1
]],
new
BSONDocument
([
'$set'
=>
[
'y'
=>
1
]]),
]);
}
}
tests/Operation/UpdateOneTest.php
View file @
d9c1c537
...
...
@@ -2,6 +2,7 @@
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Model\BSONDocument
;
use
MongoDB\Operation\UpdateOne
;
class
UpdateOneTest
extends
TestCase
...
...
@@ -24,12 +25,39 @@ class UpdateOneTest extends TestCase
new
UpdateOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
],
$update
);
}
/**
* @dataProvider provideUpdateDocuments
*/
public
function
testConstructorUpdateArgument
(
$update
)
{
new
UpdateOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
],
$update
);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage First key in $update argument is not an update operator
* @dataProvider provideReplacementDocuments
*/
public
function
testConstructorUpdateArgumentRequiresOperators
()
public
function
testConstructorUpdateArgumentRequiresOperators
(
$replacement
)
{
new
UpdateOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
],
$replacement
);
}
public
function
provideReplacementDocuments
()
{
return
$this
->
wrapValuesForDataProvider
([
[
'y'
=>
1
],
(
object
)
[
'y'
=>
1
],
new
BSONDocument
([
'y'
=>
1
]),
]);
}
public
function
provideUpdateDocuments
()
{
new
UpdateOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
],
[
'y'
=>
1
]);
return
$this
->
wrapValuesForDataProvider
([
[
'$set'
=>
[
'y'
=>
1
]],
(
object
)
[
'$set'
=>
[
'y'
=>
1
]],
new
BSONDocument
([
'$set'
=>
[
'y'
=>
1
]]),
]);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment