Skip to content

Commit 8b71737

Browse files
committed
wip
1 parent 4f34c29 commit 8b71737

File tree

10 files changed

+584
-319
lines changed

10 files changed

+584
-319
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\UnifiedSpecTests;
4+
5+
use MongoDB\Client;
6+
use MongoDB\Driver\ReadConcern;
7+
use MongoDB\Driver\ReadPreference;
8+
use MongoDB\Driver\WriteConcern;
9+
use MongoDB\Tests\UnifiedSpecTests\Constraint\DocumentsMatch;
10+
use ArrayIterator;
11+
use IteratorIterator;
12+
use MultipleIterator;
13+
use stdClass;
14+
15+
class CollectionData
16+
{
17+
private $collectionName;
18+
private $databaseName;
19+
private $documents;
20+
21+
public function __construct(stdClass $o)
22+
{
23+
assertIsString($o->collectionName);
24+
$this->collectionName = $o->collectionName;
25+
26+
assertIsString($o->databaseName);
27+
$this->databaseName = $o->databaseName;
28+
29+
assertIsArray($o->documents);
30+
assertContainsOnly('object', $o->documents);
31+
$this->documents = $o->documents;
32+
}
33+
34+
/**
35+
* Prepare collection state for "initialData".
36+
*
37+
* @param Client $client
38+
*/
39+
public function prepare(Client $client)
40+
{
41+
$database = $client->selectDatabase(
42+
$this->databaseName,
43+
['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]
44+
);
45+
46+
$database->dropCollection($this->collectionName);
47+
48+
if (empty($this->documents)) {
49+
$database->createCollection($this->collectionName);
50+
return;
51+
}
52+
53+
$collection = $database->selectCollection($this->collectionName);
54+
$collection->insertMany($this->documents);
55+
}
56+
57+
/**
58+
* Assert collection contents for "outcome".
59+
*
60+
* @param Client $client
61+
*/
62+
public function assertOutcome(Client $client)
63+
{
64+
$collection = $client->selectCollection(
65+
$this->databaseName,
66+
$this->collectionName,
67+
[
68+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
69+
'readPreference' => new ReadPreference(ReadPreference::PRIMARY),
70+
]
71+
);
72+
73+
$cursor = $collection->find([], ['sort' => ['_id' => 1]]);
74+
75+
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
76+
$mi->attachIterator(new ArrayIterator($this->documents));
77+
$mi->attachIterator(new IteratorIterator($cursor));
78+
79+
foreach ($mi as $i => $documents) {
80+
list($expectedDocument, $actualDocument) = $documents;
81+
assertNotNull($expectedDocument);
82+
assertNotNull($actualDocument);
83+
84+
$constraint = new DocumentsMatch($expectedDocument, false, false);
85+
assertThat($actualDocument, $constraint, sprintf('documents[%d] match', $i));
86+
}
87+
}
88+
}

tests/UnifiedSpecTests/DocumentsMatchConstraint.php renamed to tests/UnifiedSpecTests/Constraint/DocumentsMatch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace MongoDB\Tests\SpecTests;
3+
namespace MongoDB\Tests\UnifiedSpecTests\Constraint;
44

55
use ArrayObject;
66
use InvalidArgumentException;
@@ -47,7 +47,7 @@
4747
*
4848
* The expected value is passed in the constructor.
4949
*/
50-
class DocumentsMatchConstraint extends Constraint
50+
class DocumentsMatch extends Constraint
5151
{
5252
use ConstraintTrait;
5353

tests/UnifiedSpecTests/DocumentsMatchConstraintTest.php renamed to tests/UnifiedSpecTests/Constraint/DocumentsMatchTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace MongoDB\Tests\SpecTests;
3+
namespace MongoDB\Tests\UnifiedSpecTests\Constraint;
44

55
use MongoDB\BSON\Binary;
66
use MongoDB\BSON\Decimal128;
@@ -20,11 +20,11 @@
2020
use function unserialize;
2121
use const PHP_INT_SIZE;
2222

23-
class DocumentsMatchConstraintTest extends TestCase
23+
class DocumentsMatchTest extends TestCase
2424
{
2525
public function testIgnoreExtraKeysInRoot()
2626
{
27-
$c = new DocumentsMatchConstraint(['x' => 1, 'y' => ['a' => 1, 'b' => 2]], true, false);
27+
$c = new DocumentsMatch(['x' => 1, 'y' => ['a' => 1, 'b' => 2]], true, false);
2828

2929
$this->assertResult(false, $c, ['x' => 1, 'y' => 2], 'Incorrect value');
3030
$this->assertResult(true, $c, ['x' => 1, 'y' => ['a' => 1, 'b' => 2]], 'Exact match');
@@ -33,7 +33,7 @@ public function testIgnoreExtraKeysInRoot()
3333
$this->assertResult(true, $c, ['y' => ['b' => 2, 'a' => 1], 'x' => 1], 'Root and embedded key order is not significant');
3434

3535
// Arrays are always interpreted as root documents
36-
$c = new DocumentsMatchConstraint([1, ['a' => 1]], true, false);
36+
$c = new DocumentsMatch([1, ['a' => 1]], true, false);
3737

3838
$this->assertResult(false, $c, [1, 2], 'Incorrect value');
3939
$this->assertResult(true, $c, [1, ['a' => 1]], 'Exact match');
@@ -43,7 +43,7 @@ public function testIgnoreExtraKeysInRoot()
4343

4444
public function testIgnoreExtraKeysInEmbedded()
4545
{
46-
$c = new DocumentsMatchConstraint(['x' => 1, 'y' => ['a' => 1, 'b' => 2]], false, true);
46+
$c = new DocumentsMatch(['x' => 1, 'y' => ['a' => 1, 'b' => 2]], false, true);
4747

4848
$this->assertResult(false, $c, ['x' => 1, 'y' => 2], 'Incorrect value');
4949
$this->assertResult(false, $c, ['x' => 1, 'y' => ['a' => 1, 'b' => 3]], 'Incorrect value');
@@ -53,7 +53,7 @@ public function testIgnoreExtraKeysInEmbedded()
5353
$this->assertResult(true, $c, ['y' => ['b' => 2, 'a' => 1], 'x' => 1], 'Root and embedded Key order is not significant');
5454

5555
// Arrays are always interpreted as root documents
56-
$c = new DocumentsMatchConstraint([1, ['a' => 1]], false, true);
56+
$c = new DocumentsMatch([1, ['a' => 1]], false, true);
5757

5858
$this->assertResult(false, $c, [1, 2], 'Incorrect value');
5959
$this->assertResult(true, $c, [1, ['a' => 1]], 'Exact match');
@@ -64,7 +64,7 @@ public function testIgnoreExtraKeysInEmbedded()
6464

6565
public function testPlaceholders()
6666
{
67-
$c = new DocumentsMatchConstraint(['x' => '42', 'y' => 42, 'z' => ['a' => 24]], false, false, [24, 42]);
67+
$c = new DocumentsMatch(['x' => '42', 'y' => 42, 'z' => ['a' => 24]], false, false, [24, 42]);
6868

6969
$this->assertResult(true, $c, ['x' => '42', 'y' => 'foo', 'z' => ['a' => 1]], 'Placeholders accept any value');
7070
$this->assertResult(false, $c, ['x' => 42, 'y' => 'foo', 'z' => ['a' => 1]], 'Placeholder type must match');
@@ -76,7 +76,7 @@ public function testPlaceholders()
7676
*/
7777
public function testBSONTypeAssertions($type, $value)
7878
{
79-
$constraint = new DocumentsMatchConstraint(['x' => ['$$type' => $type]]);
79+
$constraint = new DocumentsMatch(['x' => ['$$type' => $type]]);
8080

8181
$this->assertResult(true, $constraint, ['x' => $value], 'Type matches');
8282
}
@@ -114,7 +114,7 @@ public function provideBSONTypes()
114114
/**
115115
* @dataProvider errorMessageProvider
116116
*/
117-
public function testErrorMessages($expectedMessagePart, DocumentsMatchConstraint $constraint, $actualValue)
117+
public function testErrorMessages($expectedMessagePart, DocumentsMatch $constraint, $actualValue)
118118
{
119119
try {
120120
$constraint->evaluate($actualValue);
@@ -130,38 +130,38 @@ public function errorMessageProvider()
130130
return [
131131
'Root type mismatch' => [
132132
'MongoDB\Model\BSONArray Object (...) is not instance of expected class "MongoDB\Model\BSONDocument"',
133-
new DocumentsMatchConstraint(['foo' => 'bar']),
133+
new DocumentsMatch(['foo' => 'bar']),
134134
new BSONArray(['foo' => 'bar']),
135135
],
136136
'Missing key' => [
137137
'$actual is missing key: "foo.bar"',
138-
new DocumentsMatchConstraint(['foo' => ['bar' => 'baz']]),
138+
new DocumentsMatch(['foo' => ['bar' => 'baz']]),
139139
['foo' => ['foo' => 'bar']],
140140
],
141141
'Extra key' => [
142142
'$actual has extra key: "foo.foo"',
143-
new DocumentsMatchConstraint(['foo' => ['bar' => 'baz']]),
143+
new DocumentsMatch(['foo' => ['bar' => 'baz']]),
144144
['foo' => ['foo' => 'bar', 'bar' => 'baz']],
145145
],
146146
'Scalar value not equal' => [
147147
'Field path "foo": Failed asserting that two values are equal.',
148-
new DocumentsMatchConstraint(['foo' => 'bar']),
148+
new DocumentsMatch(['foo' => 'bar']),
149149
['foo' => 'baz'],
150150
],
151151
'Scalar type mismatch' => [
152152
'Field path "foo": Failed asserting that two values are equal.',
153-
new DocumentsMatchConstraint(['foo' => 42]),
153+
new DocumentsMatch(['foo' => 42]),
154154
['foo' => '42'],
155155
],
156156
'Type mismatch' => [
157157
'Field path "foo": MongoDB\Model\BSONDocument Object (...) is not instance of expected type "MongoDB\Model\BSONArray".',
158-
new DocumentsMatchConstraint(['foo' => ['bar']]),
158+
new DocumentsMatch(['foo' => ['bar']]),
159159
['foo' => (object) ['bar']],
160160
],
161161
];
162162
}
163163

164-
private function assertResult($expectedResult, DocumentsMatchConstraint $constraint, $value, $message)
164+
private function assertResult($expectedResult, DocumentsMatch $constraint, $value, $message)
165165
{
166166
$this->assertSame($expectedResult, $constraint->evaluate($value, '', true), $message);
167167
}

0 commit comments

Comments
 (0)