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
42683dc5
Commit
42683dc5
authored
Nov 13, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Functional tests for Delete, Insert, and Update operations
parent
ab15cdac
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
293 additions
and
0 deletions
+293
-0
DeleteFunctionalTest.php
tests/Operation/DeleteFunctionalTest.php
+69
-0
InsertManyFunctionalTest.php
tests/Operation/InsertManyFunctionalTest.php
+36
-0
InsertOneFunctionalTest.php
tests/Operation/InsertOneFunctionalTest.php
+44
-0
UpdateFunctionalTest.php
tests/Operation/UpdateFunctionalTest.php
+144
-0
No files found.
tests/Operation/DeleteFunctionalTest.php
0 → 100644
View file @
42683dc5
<?php
namespace
MongoDB\Tests\Collection
;
use
MongoDB\Driver\BulkWrite
;
use
MongoDB\Operation\Delete
;
class
DeleteFunctionalTest
extends
FunctionalTestCase
{
public
function
testDeleteOne
()
{
$this
->
createFixtures
(
3
);
$filter
=
[
'_id'
=>
1
];
$operation
=
new
Delete
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
,
1
);
$result
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertInstanceOf
(
'MongoDB\DeleteResult'
,
$result
);
$this
->
assertSame
(
1
,
$result
->
getDeletedCount
());
$expected
=
[
[
'_id'
=>
2
,
'x'
=>
22
],
[
'_id'
=>
3
,
'x'
=>
33
],
];
$this
->
assertSameDocuments
(
$expected
,
$this
->
collection
->
find
());
}
public
function
testDeleteMany
()
{
$this
->
createFixtures
(
3
);
$filter
=
[
'_id'
=>
[
'$gt'
=>
1
]];
$operation
=
new
Delete
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
,
0
);
$result
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertInstanceOf
(
'MongoDB\DeleteResult'
,
$result
);
$this
->
assertSame
(
2
,
$result
->
getDeletedCount
());
$expected
=
[
[
'_id'
=>
1
,
'x'
=>
11
],
];
$this
->
assertSameDocuments
(
$expected
,
$this
->
collection
->
find
());
}
/**
* Create data fixtures.
*
* @param integer $n
*/
private
function
createFixtures
(
$n
)
{
$bulkWrite
=
new
BulkWrite
([
'ordered'
=>
true
]);
for
(
$i
=
1
;
$i
<=
$n
;
$i
++
)
{
$bulkWrite
->
insert
([
'_id'
=>
$i
,
'x'
=>
(
integer
)
(
$i
.
$i
),
]);
}
$result
=
$this
->
manager
->
executeBulkWrite
(
$this
->
getNamespace
(),
$bulkWrite
);
$this
->
assertEquals
(
$n
,
$result
->
getInsertedCount
());
}
}
tests/Operation/InsertManyFunctionalTest.php
0 → 100644
View file @
42683dc5
<?php
namespace
MongoDB\Tests\Collection
;
use
MongoDB\Operation\InsertMany
;
class
InsertManyFunctionalTest
extends
FunctionalTestCase
{
public
function
testInsertMany
()
{
$documents
=
[
[
'_id'
=>
'foo'
,
'x'
=>
11
],
[
'x'
=>
22
],
[
'_id'
=>
'bar'
,
'x'
=>
22
],
];
$operation
=
new
InsertMany
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$documents
);
$result
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertInstanceOf
(
'MongoDB\InsertManyResult'
,
$result
);
$this
->
assertSame
(
3
,
$result
->
getInsertedCount
());
$insertedIds
=
$result
->
getInsertedIds
();
$this
->
assertSame
(
'foo'
,
$insertedIds
[
0
]);
$this
->
assertInstanceOf
(
'MongoDB\BSON\ObjectId'
,
$insertedIds
[
1
]);
$this
->
assertSame
(
'bar'
,
$insertedIds
[
2
]);
$expected
=
[
[
'_id'
=>
'foo'
,
'x'
=>
11
],
[
'_id'
=>
$insertedIds
[
1
],
'x'
=>
22
],
[
'_id'
=>
'bar'
,
'x'
=>
22
],
];
$this
->
assertSameDocuments
(
$expected
,
$this
->
collection
->
find
());
}
}
tests/Operation/InsertOneFunctionalTest.php
0 → 100644
View file @
42683dc5
<?php
namespace
MongoDB\Tests\Collection
;
use
MongoDB\Operation\InsertOne
;
class
InsertOneFunctionalTest
extends
FunctionalTestCase
{
public
function
testInsertOneWithExistingId
()
{
$document
=
[
'_id'
=>
'foo'
,
'x'
=>
11
];
$operation
=
new
InsertOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$document
);
$result
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertInstanceOf
(
'MongoDB\InsertOneResult'
,
$result
);
$this
->
assertSame
(
1
,
$result
->
getInsertedCount
());
$this
->
assertSame
(
'foo'
,
$result
->
getInsertedId
());
$expected
=
[
[
'_id'
=>
'foo'
,
'x'
=>
11
],
];
$this
->
assertSameDocuments
(
$expected
,
$this
->
collection
->
find
());
}
public
function
testInsertOneWithGeneratedId
()
{
$document
=
[
'x'
=>
11
];
$operation
=
new
InsertOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$document
);
$result
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertInstanceOf
(
'MongoDB\InsertOneResult'
,
$result
);
$this
->
assertSame
(
1
,
$result
->
getInsertedCount
());
$this
->
assertInstanceOf
(
'MongoDB\BSON\ObjectId'
,
$result
->
getInsertedId
());
$expected
=
[
[
'_id'
=>
$result
->
getInsertedId
(),
'x'
=>
11
],
];
$this
->
assertSameDocuments
(
$expected
,
$this
->
collection
->
find
());
}
}
tests/Operation/UpdateFunctionalTest.php
0 → 100644
View file @
42683dc5
<?php
namespace
MongoDB\Tests\Collection
;
use
MongoDB\Driver\BulkWrite
;
use
MongoDB\Operation\Update
;
class
UpdateFunctionalTest
extends
FunctionalTestCase
{
private
$omitModifiedCount
;
public
function
setUp
()
{
parent
::
setUp
();
$this
->
omitModifiedCount
=
version_compare
(
$this
->
getServerVersion
(),
'2.6.0'
,
'<'
);
}
public
function
testUpdateOne
()
{
$this
->
createFixtures
(
3
);
$filter
=
[
'_id'
=>
[
'$gt'
=>
1
]];
$update
=
[
'$inc'
=>
[
'x'
=>
1
]];
$operation
=
new
Update
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
,
$update
);
$result
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertInstanceOf
(
'MongoDB\UpdateResult'
,
$result
);
$this
->
assertSame
(
1
,
$result
->
getMatchedCount
());
$this
->
omitModifiedCount
or
$this
->
assertSame
(
1
,
$result
->
getModifiedCount
());
$this
->
assertSame
(
0
,
$result
->
getUpsertedCount
());
$this
->
assertNull
(
$result
->
getUpsertedId
());
$expected
=
[
[
'_id'
=>
1
,
'x'
=>
11
],
[
'_id'
=>
2
,
'x'
=>
23
],
[
'_id'
=>
3
,
'x'
=>
33
],
];
$this
->
assertSameDocuments
(
$expected
,
$this
->
collection
->
find
());
}
public
function
testUpdateMany
()
{
$this
->
createFixtures
(
3
);
$filter
=
[
'_id'
=>
[
'$gt'
=>
1
]];
$update
=
[
'$inc'
=>
[
'x'
=>
1
]];
$options
=
[
'multi'
=>
true
];
$operation
=
new
Update
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
,
$update
,
$options
);
$result
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertInstanceOf
(
'MongoDB\UpdateResult'
,
$result
);
$this
->
assertSame
(
2
,
$result
->
getMatchedCount
());
$this
->
omitModifiedCount
or
$this
->
assertSame
(
2
,
$result
->
getModifiedCount
());
$this
->
assertSame
(
0
,
$result
->
getUpsertedCount
());
$this
->
assertNull
(
$result
->
getUpsertedId
());
$expected
=
[
[
'_id'
=>
1
,
'x'
=>
11
],
[
'_id'
=>
2
,
'x'
=>
23
],
[
'_id'
=>
3
,
'x'
=>
34
],
];
$this
->
assertSameDocuments
(
$expected
,
$this
->
collection
->
find
());
}
public
function
testUpdateManyWithExistingId
()
{
$this
->
createFixtures
(
3
);
$filter
=
[
'_id'
=>
5
];
$update
=
[
'$set'
=>
[
'x'
=>
55
]];
$options
=
[
'upsert'
=>
true
];
$operation
=
new
Update
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
,
$update
,
$options
);
$result
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertInstanceOf
(
'MongoDB\UpdateResult'
,
$result
);
$this
->
assertSame
(
0
,
$result
->
getMatchedCount
());
$this
->
omitModifiedCount
or
$this
->
assertSame
(
0
,
$result
->
getModifiedCount
());
$this
->
assertSame
(
1
,
$result
->
getUpsertedCount
());
$this
->
assertSame
(
5
,
$result
->
getUpsertedId
());
$expected
=
[
[
'_id'
=>
1
,
'x'
=>
11
],
[
'_id'
=>
2
,
'x'
=>
22
],
[
'_id'
=>
3
,
'x'
=>
33
],
[
'_id'
=>
5
,
'x'
=>
55
],
];
$this
->
assertSameDocuments
(
$expected
,
$this
->
collection
->
find
());
}
public
function
testUpdateManyWithGeneratedId
()
{
$this
->
createFixtures
(
3
);
$filter
=
[
'x'
=>
66
];
$update
=
[
'$set'
=>
[
'x'
=>
66
]];
$options
=
[
'upsert'
=>
true
];
$operation
=
new
Update
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
,
$update
,
$options
);
$result
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertInstanceOf
(
'MongoDB\UpdateResult'
,
$result
);
$this
->
assertSame
(
0
,
$result
->
getMatchedCount
());
$this
->
omitModifiedCount
or
$this
->
assertSame
(
0
,
$result
->
getModifiedCount
());
$this
->
assertSame
(
1
,
$result
->
getUpsertedCount
());
$this
->
assertInstanceOf
(
'MongoDB\BSON\ObjectId'
,
$result
->
getUpsertedId
());
$expected
=
[
[
'_id'
=>
1
,
'x'
=>
11
],
[
'_id'
=>
2
,
'x'
=>
22
],
[
'_id'
=>
3
,
'x'
=>
33
],
[
'_id'
=>
$result
->
getUpsertedId
(),
'x'
=>
66
],
];
$this
->
assertSameDocuments
(
$expected
,
$this
->
collection
->
find
());
}
/**
* Create data fixtures.
*
* @param integer $n
*/
private
function
createFixtures
(
$n
)
{
$bulkWrite
=
new
BulkWrite
([
'ordered'
=>
true
]);
for
(
$i
=
1
;
$i
<=
$n
;
$i
++
)
{
$bulkWrite
->
insert
([
'_id'
=>
$i
,
'x'
=>
(
integer
)
(
$i
.
$i
),
]);
}
$result
=
$this
->
manager
->
executeBulkWrite
(
$this
->
getNamespace
(),
$bulkWrite
);
$this
->
assertEquals
(
$n
,
$result
->
getInsertedCount
());
}
}
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