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
d06c288e
Commit
d06c288e
authored
Jul 05, 2018
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-367: BSONArray/Document cloning should respect uncloneable objects
parent
9420a3b4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
82 additions
and
0 deletions
+82
-0
functions.php
src/functions.php
+5
-0
BSONArrayTest.php
tests/Model/BSONArrayTest.php
+32
-0
BSONDocumentTest.php
tests/Model/BSONDocumentTest.php
+32
-0
UncloneableObject.php
tests/Model/UncloneableObject.php
+13
-0
No files found.
src/functions.php
View file @
d06c288e
...
...
@@ -23,6 +23,7 @@ use MongoDB\Driver\Server;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Exception\InvalidArgumentException
;
use
stdClass
;
use
ReflectionClass
;
/**
* Applies a type map to a document.
...
...
@@ -216,5 +217,9 @@ function recursive_copy($element) {
return
$element
;
}
if
(
!
(
new
ReflectionClass
(
$element
))
->
isCloneable
())
{
return
$element
;
}
return
clone
$element
;
}
tests/Model/BSONArrayTest.php
View file @
d06c288e
...
...
@@ -2,10 +2,12 @@
namespace
MongoDB\Tests\Model
;
use
MongoDB\BSON\ObjectId
;
use
MongoDB\Model\BSONArray
;
use
MongoDB\Model\BSONDocument
;
use
MongoDB\Tests\TestCase
;
use
stdClass
;
use
ReflectionClass
;
class
BSONArrayTest
extends
TestCase
{
...
...
@@ -43,6 +45,36 @@ class BSONArrayTest extends TestCase
$this
->
assertNotSame
(
$array
[
1
][
2
][
1
],
$arrayClone
[
1
][
2
][
1
]);
}
public
function
testCloneRespectsUncloneableObjects
()
{
$this
->
assertFalse
((
new
ReflectionClass
(
UncloneableObject
::
class
))
->
isCloneable
());
$array
=
new
BSONArray
([
[
new
UncloneableObject
],
new
BSONArray
([
new
UncloneableObject
]),
]);
$arrayClone
=
clone
$array
;
$this
->
assertNotSame
(
$array
,
$arrayClone
);
$this
->
assertSame
(
$array
[
0
][
0
],
$arrayClone
[
0
][
0
]);
$this
->
assertNotSame
(
$array
[
1
],
$arrayClone
[
1
]);
$this
->
assertSame
(
$array
[
1
][
0
],
$arrayClone
[
1
][
0
]);
}
public
function
testCloneSupportsBSONTypes
()
{
/* Note: this test does not check that the BSON type itself is cloned,
* as that is not yet supported in the driver (see: PHPC-1230). */
$array
=
new
BSONArray
([
[
new
ObjectId
],
new
BSONArray
([
new
ObjectId
]),
]);
$arrayClone
=
clone
$array
;
$this
->
assertNotSame
(
$array
,
$arrayClone
);
$this
->
assertNotSame
(
$array
[
1
],
$arrayClone
[
1
]);
}
public
function
testJsonSerialize
()
{
$document
=
new
BSONArray
([
...
...
tests/Model/BSONDocumentTest.php
View file @
d06c288e
...
...
@@ -2,11 +2,13 @@
namespace
MongoDB\Tests\Model
;
use
MongoDB\BSON\ObjectId
;
use
MongoDB\Model\BSONArray
;
use
MongoDB\Model\BSONDocument
;
use
MongoDB\Tests\TestCase
;
use
ArrayObject
;
use
stdClass
;
use
ReflectionClass
;
class
BSONDocumentTest
extends
TestCase
{
...
...
@@ -51,6 +53,36 @@ class BSONDocumentTest extends TestCase
$this
->
assertNotSame
(
$document
[
'b'
][
'c'
][
1
],
$documentClone
[
'b'
][
'c'
][
1
]);
}
public
function
testCloneRespectsUncloneableObjects
()
{
$this
->
assertFalse
((
new
ReflectionClass
(
UncloneableObject
::
class
))
->
isCloneable
());
$document
=
new
BSONDocument
([
'a'
=>
[
'a'
=>
new
UncloneableObject
],
'b'
=>
new
BSONDocument
([
'a'
=>
new
UncloneableObject
]),
]);
$documentClone
=
clone
$document
;
$this
->
assertNotSame
(
$document
,
$documentClone
);
$this
->
assertSame
(
$document
[
'a'
][
'a'
],
$documentClone
[
'a'
][
'a'
]);
$this
->
assertNotSame
(
$document
[
'b'
],
$documentClone
[
'b'
]);
$this
->
assertSame
(
$document
[
'b'
][
'a'
],
$documentClone
[
'b'
][
'a'
]);
}
public
function
testCloneSupportsBSONTypes
()
{
/* Note: this test does not check that the BSON type itself is cloned,
* as that is not yet supported in the driver (see: PHPC-1230). */
$document
=
new
BSONDocument
([
'a'
=>
[
'a'
=>
new
ObjectId
],
'b'
=>
new
BSONDocument
([
'a'
=>
new
ObjectId
]),
]);
$documentClone
=
clone
$document
;
$this
->
assertNotSame
(
$document
,
$documentClone
);
$this
->
assertNotSame
(
$document
[
'b'
],
$documentClone
[
'b'
]);
}
public
function
testJsonSerialize
()
{
$document
=
new
BSONDocument
([
...
...
tests/Model/UncloneableObject.php
0 → 100644
View file @
d06c288e
<?php
namespace
MongoDB\Tests\Model
;
/**
* This class is used by the BSONArray and BSONDocument clone tests.
*/
class
UncloneableObject
{
private
function
__clone
()
{
}
}
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