Skip to content

Commit 4b4a7d9

Browse files
committed
PHPLIB-365: Transactions test runner
Mark incomplete transaction tests to be fixed before 4.2-compat release. Changes JSON decoding of spec tests to preserve array/object types. Bump driver version on Travis CI to include libmongoc 1.14 and PHPC-1373.
1 parent cb595b6 commit 4b4a7d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+26123
-494
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cache:
1414

1515
env:
1616
global:
17-
- DRIVER_VERSION=1.6.0alpha1
17+
- DRIVER_VERSION=1.6.0alpha2
1818
- SERVER_DISTRO=ubuntu1604
1919
- SERVER_VERSION=4.0.9
2020
- DEPLOYMENT=STANDALONE

tests/ClientFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function setUp()
1818
{
1919
parent::setUp();
2020

21-
$this->client = new Client($this->getUri());
21+
$this->client = new Client(static::getUri());
2222
$this->client->dropDatabase($this->getDatabaseName());
2323
}
2424

tests/ClientTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testConstructorDefaultUri()
2626
public function testConstructorDriverOptionTypeChecks(array $driverOptions)
2727
{
2828
$this->expectException(InvalidArgumentException::class);
29-
new Client($this->getUri(), [], $driverOptions);
29+
new Client(static::getUri(), [], $driverOptions);
3030
}
3131

3232
public function provideInvalidConstructorDriverOptions()
@@ -42,9 +42,9 @@ public function provideInvalidConstructorDriverOptions()
4242

4343
public function testToString()
4444
{
45-
$client = new Client($this->getUri());
45+
$client = new Client(static::getUri());
4646

47-
$this->assertSame($this->getUri(), (string) $client);
47+
$this->assertSame(static::getUri(), (string) $client);
4848
}
4949

5050
public function testSelectCollectionInheritsOptions()
@@ -59,7 +59,7 @@ public function testSelectCollectionInheritsOptions()
5959
'typeMap' => ['root' => 'array'],
6060
];
6161

62-
$client = new Client($this->getUri(), $uriOptions, $driverOptions);
62+
$client = new Client(static::getUri(), $uriOptions, $driverOptions);
6363
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
6464
$debug = $collection->__debugInfo();
6565

@@ -82,7 +82,7 @@ public function testSelectCollectionPassesOptions()
8282
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
8383
];
8484

85-
$client = new Client($this->getUri());
85+
$client = new Client(static::getUri());
8686
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
8787
$debug = $collection->__debugInfo();
8888

@@ -100,7 +100,7 @@ public function testGetSelectsDatabaseAndInheritsOptions()
100100
{
101101
$uriOptions = ['w' => WriteConcern::MAJORITY];
102102

103-
$client = new Client($this->getUri(), $uriOptions);
103+
$client = new Client(static::getUri(), $uriOptions);
104104
$database = $client->{$this->getDatabaseName()};
105105
$debug = $database->__debugInfo();
106106

@@ -121,7 +121,7 @@ public function testSelectDatabaseInheritsOptions()
121121
'typeMap' => ['root' => 'array'],
122122
];
123123

124-
$client = new Client($this->getUri(), $uriOptions, $driverOptions);
124+
$client = new Client(static::getUri(), $uriOptions, $driverOptions);
125125
$database = $client->selectDatabase($this->getDatabaseName());
126126
$debug = $database->__debugInfo();
127127

@@ -144,7 +144,7 @@ public function testSelectDatabasePassesOptions()
144144
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
145145
];
146146

147-
$client = new Client($this->getUri());
147+
$client = new Client(static::getUri());
148148
$database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
149149
$debug = $database->__debugInfo();
150150

tests/DocumentationExamplesTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ public function testTransactions_intro_example_1()
12271227
{
12281228
$this->skipIfTransactionsAreNotSupported();
12291229

1230-
$client = new Client($this->getUri());
1230+
$client = new Client(static::getUri());
12311231

12321232
/* The WC is required: https://docs.mongodb.com/manual/core/transactions/#transactions-and-locks */
12331233
$client->hr->dropCollection('employees', ['writeConcern' => new \MongoDB\Driver\WriteConcern('majority')]);
@@ -1387,7 +1387,7 @@ public function testTransactions_retry_example_3()
13871387
{
13881388
$this->skipIfTransactionsAreNotSupported();
13891389

1390-
$client = new Client($this->getUri());
1390+
$client = new Client(static::getUri());
13911391

13921392
/* The WC is required: https://docs.mongodb.com/manual/core/transactions/#transactions-and-locks */
13931393
$client->hr->dropCollection('employees', ['writeConcern' => new \MongoDB\Driver\WriteConcern('majority')]);
@@ -1410,7 +1410,7 @@ function testCausalConsistency()
14101410
$this->skipIfCausalConsistencyIsNotSupported();
14111411

14121412
// Prep
1413-
$client = new Client($this->getUri());
1413+
$client = new Client(static::getUri());
14141414
$items = $client->selectDatabase(
14151415
'test',
14161416
[ 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY) ]

tests/FunctionalTestCase.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717

1818
abstract class FunctionalTestCase extends TestCase
1919
{
20-
protected $manager;
21-
2220
public function setUp()
2321
{
24-
$this->manager = new Manager($this->getUri());
22+
$this->manager = new Manager(static::getUri());
2523
}
2624

2725
protected function assertCollectionCount($namespace, $count)

tests/Operation/FindAndModifyFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class FindAndModifyFunctionalTest extends FunctionalTestCase
1717
*/
1818
public function testManagerReadConcernIsOmitted()
1919
{
20-
$manager = new Manager($this->getUri(), ['readConcernLevel' => 'majority']);
20+
$manager = new Manager(static::getUri(), ['readConcernLevel' => 'majority']);
2121
$server = $manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
2222

2323
(new CommandObserver)->observe(

tests/Operation/WatchFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function testNextResumesAfterConnectionException()
7575
/* In order to trigger a dropped connection, we'll use a new client with
7676
* a socket timeout that is less than the change stream's maxAwaitTimeMS
7777
* option. */
78-
$manager = new Manager($this->getUri(), ['socketTimeoutMS' => 50]);
78+
$manager = new Manager(static::getUri(), ['socketTimeoutMS' => 50]);
7979
$primaryServer = $manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
8080

8181
$operation = new Watch($manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);
@@ -222,7 +222,7 @@ public function testRewindResumesAfterConnectionException()
222222
/* In order to trigger a dropped connection, we'll use a new client with
223223
* a socket timeout that is less than the change stream's maxAwaitTimeMS
224224
* option. */
225-
$manager = new Manager($this->getUri(), ['socketTimeoutMS' => 50]);
225+
$manager = new Manager(static::getUri(), ['socketTimeoutMS' => 50]);
226226
$primaryServer = $manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
227227

228228
$operation = new Watch($manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\SpecTests;
4+
5+
use MongoDB\BSON\Timestamp;
6+
use MongoDB\Driver\Monitoring\CommandFailedEvent;
7+
use MongoDB\Driver\Monitoring\CommandStartedEvent;
8+
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
9+
use MongoDB\Driver\Monitoring\CommandSubscriber;
10+
use ArrayIterator;
11+
use LogicException;
12+
use MultipleIterator;
13+
use stdClass;
14+
15+
/**
16+
* Spec test CommandStartedEvent expectations.
17+
*/
18+
class CommandExpectations implements CommandSubscriber
19+
{
20+
private $commandStartedEvents = [];
21+
private $expectedCommandStartedEvents = [];
22+
23+
public static function fromTransactions(array $expectedEvents)
24+
{
25+
$o = new self;
26+
27+
foreach ($expectedEvents as $expectedEvent) {
28+
if (!isset($expectedEvent->command_started_event)) {
29+
throw new LogicException('$expectedEvent->command_started_event field is not set');
30+
}
31+
32+
$o->expectedCommandStartedEvents[] = $expectedEvent->command_started_event;
33+
}
34+
35+
return $o;
36+
}
37+
38+
/**
39+
* Not used.
40+
*
41+
* @see https://www.php.net/manual/en/mongodb-driver-monitoring-commandsubscriber.commandfailed.php
42+
*/
43+
public function commandFailed(CommandFailedEvent $event)
44+
{
45+
}
46+
47+
/**
48+
* Tracks outgoing commands for spec test APM assertions.
49+
*
50+
* @see https://www.php.net/manual/en/mongodb-driver-monitoring-commandsubscriber.commandstarted.php
51+
*/
52+
public function commandStarted(CommandStartedEvent $event)
53+
{
54+
$this->commandStartedEvents[] = $event;
55+
}
56+
57+
/**
58+
* Not used.
59+
*
60+
* @see https://www.php.net/manual/en/mongodb-driver-monitoring-commandsubscriber.commandsucceeded.php
61+
*/
62+
public function commandSucceeded(CommandSucceededEvent $event)
63+
{
64+
}
65+
66+
/**
67+
* Start command monitoring.
68+
*/
69+
public function startMonitoring()
70+
{
71+
\MongoDB\Driver\Monitoring\addSubscriber($this);
72+
}
73+
74+
/**
75+
* Stop command monitoring.
76+
*/
77+
public function stopMonitoring()
78+
{
79+
\MongoDB\Driver\Monitoring\removeSubscriber($this);
80+
}
81+
82+
/**
83+
* Assert that the command expectations match the monitored events.
84+
*
85+
* @param FunctionalTestCase $test Test instance
86+
* @param Context $context Execution context
87+
*/
88+
public function assert(FunctionalTestCase $test, Context $context)
89+
{
90+
$test->assertCount(count($this->expectedCommandStartedEvents), $this->commandStartedEvents);
91+
92+
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
93+
$mi->attachIterator(new ArrayIterator($this->expectedCommandStartedEvents));
94+
$mi->attachIterator(new ArrayIterator($this->commandStartedEvents));
95+
96+
foreach ($mi as $events) {
97+
list($expectedEvent, $actualEvent) = $events;
98+
$test->assertInternalType('object', $expectedEvent);
99+
$test->assertInstanceOf(CommandStartedEvent::class, $actualEvent);
100+
101+
if (isset($expectedEvent->command_name)) {
102+
$test->assertSame($expectedEvent->command_name, $actualEvent->getCommandName());
103+
}
104+
105+
if (isset($expectedEvent->database_name)) {
106+
$test->assertSame($expectedEvent->database_name, $actualEvent->getDatabaseName());
107+
}
108+
109+
if (isset($expectedEvent->command)) {
110+
$expectedCommand = $expectedEvent->command;
111+
$context->replaceCommandSessionPlaceholder($expectedCommand);
112+
$test->assertSameCommand($expectedCommand, $actualEvent->getCommand());
113+
}
114+
}
115+
}
116+
117+
private function __construct()
118+
{
119+
}
120+
}

0 commit comments

Comments
 (0)