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
d4645414
Unverified
Commit
d4645414
authored
Sep 05, 2019
by
Andreas Braun
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #679
parents
5c635438
5b2b306c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
0 deletions
+67
-0
DocumentationExamplesTest.php
tests/DocumentationExamplesTest.php
+67
-0
No files found.
tests/DocumentationExamplesTest.php
View file @
d4645414
...
...
@@ -1540,6 +1540,73 @@ class DocumentationExamplesTest extends FunctionalTestCase
ob_end_clean
();
}
/**
* @doesNotPerformAssertions
*/
public
function
testWithTransactionExample
()
{
$this
->
skipIfTransactionsAreNotSupported
();
$uriString
=
static
::
getUri
(
true
);
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
// Start Transactions withTxn API Example 1
/*
* For a replica set, include the replica set name and a seedlist of the members in the URI string; e.g.
* uriString = 'mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=myRepl'
* For a sharded cluster, connect to the mongos instances; e.g.
* uriString = 'mongodb://mongos0.example.com:27017,mongos1.example.com:27017/'
*/
$client
=
new
\MongoDB\Client
(
$uriString
);
// Prerequisite: Create collections. CRUD operations in transactions must be on existing collections.
$client
->
selectCollection
(
'mydb1'
,
'foo'
,
[
'writeConcern'
=>
new
\MongoDB\Driver\WriteConcern
(
\MongoDB\Driver\WriteConcern
::
MAJORITY
,
1000
),
]
)
->
insertOne
([
'abc'
=>
0
]);
$client
->
selectCollection
(
'mydb2'
,
'bar'
,
[
'writeConcern'
=>
new
\MongoDB\Driver\WriteConcern
(
\MongoDB\Driver\WriteConcern
::
MAJORITY
,
1000
),
]
)
->
insertOne
([
'xyz'
=>
0
]);
// Step 1: Define the callback that specifies the sequence of operations to perform inside the transactions.
$callback
=
function
(
\MongoDB\Driver\Session
$session
)
use
(
$client
)
{
$client
->
selectCollection
(
'mydb1'
,
'foo'
)
->
insertOne
([
'abc'
=>
1
],
[
'session'
=>
$session
]);
$client
->
selectCollection
(
'mydb2'
,
'bar'
)
->
insertOne
([
'xyz'
=>
999
],
[
'session'
=>
$session
]);
};
// Step 2: Start a client session.
$session
=
$client
->
startSession
();
// Step 3: Use with_transaction to start a transaction, execute the callback, and commit (or abort on error).
$transactionOptions
=
[
'readConcern'
=>
new
\MongoDB\Driver\ReadConcern
(
\MongoDB\Driver\ReadConcern
::
LOCAL
),
'writeConcern'
=>
new
\MongoDB\Driver\WriteConcern
(
\MongoDB\Driver\WriteConcern
::
MAJORITY
,
1000
),
'readPreference'
=>
new
\MongoDB\Driver\ReadPreference
(
\MongoDB\Driver\ReadPreference
::
RP_PRIMARY
),
];
\MongoDB\with_transaction
(
$session
,
$callback
,
$transactionOptions
);
// End Transactions withTxn API Example 1
// phpcs:enable
}
/**
* Return the test collection name.
*
...
...
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