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
43aa6b57
Commit
43aa6b57
authored
Jan 07, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #87
parents
581188c9
c1c6fab8
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
83 additions
and
42 deletions
+83
-42
BulkWrite.php
src/Operation/BulkWrite.php
+1
-2
InsertMany.php
src/Operation/InsertMany.php
+1
-2
InsertOne.php
src/Operation/InsertOne.php
+1
-2
functions.php
src/functions.php
+50
-28
BulkWriteFunctionalTest.php
tests/Operation/BulkWriteFunctionalTest.php
+9
-2
InsertManyFunctionalTest.php
tests/Operation/InsertManyFunctionalTest.php
+7
-3
InsertOneFunctionalTest.php
tests/Operation/InsertOneFunctionalTest.php
+14
-3
No files found.
src/Operation/BulkWrite.php
View file @
43aa6b57
...
...
@@ -239,8 +239,7 @@ class BulkWrite implements Executable
if
(
$insertedId
!==
null
)
{
$insertedIds
[
$i
]
=
$insertedId
;
}
else
{
// TODO: This may be removed if PHPC-382 is implemented
$insertedIds
[
$i
]
=
is_array
(
$args
[
0
])
?
$args
[
0
][
'_id'
]
:
$args
[
0
]
->
_id
;
$insertedIds
[
$i
]
=
\MongoDB\extract_id_from_inserted_document
(
$args
[
0
]);
}
break
;
...
...
src/Operation/InsertMany.php
View file @
43aa6b57
...
...
@@ -109,8 +109,7 @@ class InsertMany implements Executable
if
(
$insertedId
!==
null
)
{
$insertedIds
[
$i
]
=
$insertedId
;
}
else
{
// TODO: This may be removed if PHPC-382 is implemented
$insertedIds
[
$i
]
=
is_array
(
$document
)
?
$document
[
'_id'
]
:
$document
->
_id
;
$insertedIds
[
$i
]
=
\MongoDB\extract_id_from_inserted_document
(
$document
);
}
}
...
...
src/Operation/InsertOne.php
View file @
43aa6b57
...
...
@@ -79,8 +79,7 @@ class InsertOne implements Executable
$insertedId
=
$bulk
->
insert
(
$this
->
document
);
if
(
$insertedId
===
null
)
{
// TODO: This may be removed if PHPC-382 is implemented
$insertedId
=
is_array
(
$this
->
document
)
?
$this
->
document
[
'_id'
]
:
$this
->
document
->
_id
;
$insertedId
=
\MongoDB\extract_id_from_inserted_document
(
$this
->
document
);
}
$writeConcern
=
isset
(
$this
->
options
[
'writeConcern'
])
?
$this
->
options
[
'writeConcern'
]
:
null
;
...
...
src/functions.php
View file @
43aa6b57
...
...
@@ -2,11 +2,61 @@
namespace
MongoDB
;
use
MongoDB\BSON\Serializable
;
use
MongoDB\Driver\ReadConcern
;
use
MongoDB\Driver\Server
;
use
MongoDB\Exception\InvalidArgumentTypeException
;
use
stdClass
;
/**
* Extracts an ID from an inserted document.
*
* This function is used when BulkWrite::insert() does not return a generated
* ID, which means that the ID should be fetched from an array offset, public
* property, or in the data returned by bsonSerialize().
*
* @internal
* @see https://jira.mongodb.org/browse/PHPC-382
* @param array|object $document Inserted document
* @return mixed
*/
function
extract_id_from_inserted_document
(
$document
)
{
if
(
$document
instanceof
Serializable
)
{
return
extract_id_from_inserted_document
(
$document
->
bsonSerialize
());
}
return
is_array
(
$document
)
?
$document
[
'_id'
]
:
$document
->
_id
;
}
/**
* Generate an index name from a key specification.
*
* @internal
* @param array|object $document Document containing fields mapped to values,
* which denote order or an index type
* @return string
* @throws InvalidArgumentTypeException
*/
function
generate_index_name
(
$document
)
{
if
(
is_object
(
$document
))
{
$document
=
get_object_vars
(
$document
);
}
if
(
!
is_array
(
$document
))
{
throw
new
InvalidArgumentTypeException
(
'$document'
,
$document
,
'array or object'
);
}
$name
=
''
;
foreach
(
$document
as
$field
=>
$type
)
{
$name
.=
(
$name
!=
''
?
'_'
:
''
)
.
$field
.
'_'
.
$type
;
}
return
$name
;
}
/**
* Return whether the first key in the document starts with a "$" character.
*
...
...
@@ -55,34 +105,6 @@ function is_last_pipeline_operator_out(array $pipeline)
return
key
(
$lastOp
)
===
'$out'
;
}
/**
* Generate an index name from a key specification.
*
* @internal
* @param array|object $document Document containing fields mapped to values,
* which denote order or an index type
* @return string
* @throws InvalidArgumentTypeException
*/
function
generate_index_name
(
$document
)
{
if
(
is_object
(
$document
))
{
$document
=
get_object_vars
(
$document
);
}
if
(
!
is_array
(
$document
))
{
throw
new
InvalidArgumentTypeException
(
'$document'
,
$document
,
'array or object'
);
}
$name
=
''
;
foreach
(
$document
as
$field
=>
$type
)
{
$name
.=
(
$name
!=
''
?
'_'
:
''
)
.
$field
.
'_'
.
$type
;
}
return
$name
;
}
/**
* Converts a ReadConcern instance to a stdClass for use in a BSON document.
*
...
...
tests/Operation/BulkWriteFunctionalTest.php
View file @
43aa6b57
...
...
@@ -5,6 +5,7 @@ namespace MongoDB\Tests\Collection;
use
MongoDB\BulkWriteResult
;
use
MongoDB\Driver\BulkWrite
as
Bulk
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Model\BSONDocument
;
use
MongoDB\Operation\BulkWrite
;
class
BulkWriteFunctionalTest
extends
FunctionalTestCase
...
...
@@ -23,21 +24,27 @@ class BulkWriteFunctionalTest extends FunctionalTestCase
$ops
=
[
[
'insertOne'
=>
[[
'_id'
=>
1
,
'x'
=>
11
]]],
[
'insertOne'
=>
[[
'x'
=>
22
]]],
[
'insertOne'
=>
[(
object
)
[
'_id'
=>
'foo'
,
'x'
=>
33
]]],
[
'insertOne'
=>
[
new
BSONDocument
([
'_id'
=>
'bar'
,
'x'
=>
44
])]],
];
$operation
=
new
BulkWrite
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$ops
);
$result
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertInstanceOf
(
'MongoDB\BulkWriteResult'
,
$result
);
$this
->
assertSame
(
2
,
$result
->
getInsertedCount
());
$this
->
assertSame
(
4
,
$result
->
getInsertedCount
());
$insertedIds
=
$result
->
getInsertedIds
();
$this
->
assertSame
(
1
,
$insertedIds
[
0
]);
$this
->
assertInstanceOf
(
'MongoDB\BSON\ObjectId'
,
$insertedIds
[
1
]);
$this
->
assertSame
(
'foo'
,
$insertedIds
[
2
]);
$this
->
assertSame
(
'bar'
,
$insertedIds
[
3
]);
$expected
=
[
[
'_id'
=>
$insertedIds
[
0
]
,
'x'
=>
11
],
[
'_id'
=>
1
,
'x'
=>
11
],
[
'_id'
=>
$insertedIds
[
1
],
'x'
=>
22
],
[
'_id'
=>
'foo'
,
'x'
=>
33
],
[
'_id'
=>
'bar'
,
'x'
=>
44
],
];
$this
->
assertSameDocuments
(
$expected
,
$this
->
collection
->
find
());
...
...
tests/Operation/InsertManyFunctionalTest.php
View file @
43aa6b57
...
...
@@ -4,6 +4,7 @@ namespace MongoDB\Tests\Collection;
use
MongoDB\InsertManyResult
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Model\BSONDocument
;
use
MongoDB\Operation\InsertMany
;
class
InsertManyFunctionalTest
extends
FunctionalTestCase
...
...
@@ -13,24 +14,27 @@ class InsertManyFunctionalTest extends FunctionalTestCase
$documents
=
[
[
'_id'
=>
'foo'
,
'x'
=>
11
],
[
'x'
=>
22
],
[
'_id'
=>
'bar'
,
'x'
=>
22
],
(
object
)
[
'_id'
=>
'bar'
,
'x'
=>
33
],
new
BSONDocument
([
'_id'
=>
'baz'
,
'x'
=>
44
]),
];
$operation
=
new
InsertMany
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$documents
);
$result
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertInstanceOf
(
'MongoDB\InsertManyResult'
,
$result
);
$this
->
assertSame
(
3
,
$result
->
getInsertedCount
());
$this
->
assertSame
(
4
,
$result
->
getInsertedCount
());
$insertedIds
=
$result
->
getInsertedIds
();
$this
->
assertSame
(
'foo'
,
$insertedIds
[
0
]);
$this
->
assertInstanceOf
(
'MongoDB\BSON\ObjectId'
,
$insertedIds
[
1
]);
$this
->
assertSame
(
'bar'
,
$insertedIds
[
2
]);
$this
->
assertSame
(
'baz'
,
$insertedIds
[
3
]);
$expected
=
[
[
'_id'
=>
'foo'
,
'x'
=>
11
],
[
'_id'
=>
$insertedIds
[
1
],
'x'
=>
22
],
[
'_id'
=>
'bar'
,
'x'
=>
22
],
[
'_id'
=>
'bar'
,
'x'
=>
33
],
[
'_id'
=>
'baz'
,
'x'
=>
44
],
];
$this
->
assertSameDocuments
(
$expected
,
$this
->
collection
->
find
());
...
...
tests/Operation/InsertOneFunctionalTest.php
View file @
43aa6b57
...
...
@@ -4,14 +4,16 @@ namespace MongoDB\Tests\Collection;
use
MongoDB\InsertOneResult
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Model\BSONDocument
;
use
MongoDB\Operation\InsertOne
;
class
InsertOneFunctionalTest
extends
FunctionalTestCase
{
public
function
testInsertOneWithExistingId
()
/**
* @dataProvider provideDocumentWithExistingId
*/
public
function
testInsertOneWithExistingId
(
$document
)
{
$document
=
[
'_id'
=>
'foo'
,
'x'
=>
11
];
$operation
=
new
InsertOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$document
);
$result
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
...
...
@@ -26,6 +28,15 @@ class InsertOneFunctionalTest extends FunctionalTestCase
$this
->
assertSameDocuments
(
$expected
,
$this
->
collection
->
find
());
}
public
function
provideDocumentWithExistingId
()
{
return
[
[[
'_id'
=>
'foo'
,
'x'
=>
11
]],
[(
object
)
[
'_id'
=>
'foo'
,
'x'
=>
11
]],
[
new
BSONDocument
([
'_id'
=>
'foo'
,
'x'
=>
11
])],
];
}
public
function
testInsertOneWithGeneratedId
()
{
$document
=
[
'x'
=>
11
];
...
...
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