Skip to content

Commit 3e4d54b

Browse files
committed
Merge pull request #21
2 parents 60931d8 + 0437bb8 commit 3e4d54b

23 files changed

+143
-87
lines changed

src/Operation/Aggregate.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,21 @@ public function execute(Server $server)
122122
return $cursor;
123123
}
124124

125+
$cursor->setTypeMap(array('document' => 'stdClass'));
125126
$result = current($cursor->toArray());
126127

127-
if (empty($result['ok'])) {
128-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
128+
// TODO: Remove this once PHPC-318 is implemented
129+
is_array($result) and $result = (object) $result;
130+
131+
if (empty($result->ok)) {
132+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
129133
}
130134

131-
if ( ! isset($result['result']) || ! is_array($result['result'])) {
135+
if ( ! isset($result->result) || ! is_array($result->result)) {
132136
throw new UnexpectedValueException('aggregate command did not return a "result" array');
133137
}
134138

135-
return new ArrayIterator(array_map(
136-
function (stdClass $document) { return (array) $document; },
137-
$result['result']
138-
));
139+
return new ArrayIterator($result->result);
139140
}
140141

141142
/**

src/Operation/Count.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function __construct($databaseName, $collectionName, array $filter = arra
8585
public function execute(Server $server)
8686
{
8787
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
88+
$cursor->setTypeMap(array('document' => 'array'));
8889
$result = current($cursor->toArray());
8990

9091
if (empty($result['ok'])) {

src/Operation/CreateCollection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,14 @@ public function __construct($databaseName, $collectionName, array $options = arr
106106
public function execute(Server $server)
107107
{
108108
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
109+
$cursor->setTypeMap(array('document' => 'stdClass'));
109110
$result = current($cursor->toArray());
110111

111-
if (empty($result['ok'])) {
112-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
112+
// TODO: Remove this once PHPC-318 is implemented
113+
is_array($result) and $result = (object) $result;
114+
115+
if (empty($result->ok)) {
116+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
113117
}
114118

115119
return $result;

src/Operation/CreateIndexes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ private function executeCommand(Server $server)
9191
));
9292

9393
$cursor = $server->executeCommand($this->databaseName, $command);
94+
$cursor->setTypeMap(array('document' => 'array'));
9495
$result = current($cursor->toArray());
9596

9697
if (empty($result['ok'])) {

src/Operation/Distinct.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,21 @@ public function __construct($databaseName, $collectionName, $fieldName, array $f
6262
public function execute(Server $server)
6363
{
6464
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
65+
$cursor->setTypeMap(array('document' => 'stdClass'));
6566
$result = current($cursor->toArray());
6667

67-
if (empty($result['ok'])) {
68-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
68+
// TODO: Remove this once PHPC-318 is implemented
69+
is_array($result) and $result = (object) $result;
70+
71+
if (empty($result->ok)) {
72+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
6973
}
7074

71-
if ( ! isset($result['values']) || ! is_array($result['values'])) {
75+
if ( ! isset($result->values) || ! is_array($result->values)) {
7276
throw new UnexpectedValueException('distinct command did not return a "values" array');
7377
}
7478

75-
return $result['values'];
79+
return $result->values;
7680
}
7781

7882
/**

src/Operation/DropCollection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ public function __construct($databaseName, $collectionName)
4141
public function execute(Server $server)
4242
{
4343
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
44+
$cursor->setTypeMap(array('document' => 'stdClass'));
4445
$result = current($cursor->toArray());
4546

46-
if (empty($result['ok'])) {
47-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
47+
// TODO: Remove this once PHPC-318 is implemented
48+
is_array($result) and $result = (object) $result;
49+
50+
if (empty($result->ok)) {
51+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
4852
}
4953

5054
return $result;

src/Operation/DropDatabase.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ public function __construct($databaseName)
3939
public function execute(Server $server)
4040
{
4141
$cursor = $server->executeCommand($this->databaseName, new Command(array('dropDatabase' => 1)));
42+
$cursor->setTypeMap(array('document' => 'stdClass'));
4243
$result = current($cursor->toArray());
4344

44-
if (empty($result['ok'])) {
45-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
45+
// TODO: Remove this once PHPC-318 is implemented
46+
is_array($result) and $result = (object) $result;
47+
48+
if (empty($result->ok)) {
49+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
4650
}
4751

4852
return $result;

src/Operation/DropIndexes.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ public function execute(Server $server)
5656
);
5757

5858
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
59+
$cursor->setTypeMap(array('document' => 'stdClass'));
5960
$result = current($cursor->toArray());
6061

61-
if (empty($result['ok'])) {
62-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
62+
// TODO: Remove this once PHPC-318 is implemented
63+
is_array($result) and $result = (object) $result;
64+
65+
if (empty($result->ok)) {
66+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
6367
}
6468

6569
return $result;

src/Operation/FindAndModify.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,17 @@ public function __construct($databaseName, $collectionName, array $options)
118118
public function execute(Server $server)
119119
{
120120
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
121+
$cursor->setTypeMap(array('document' => 'stdClass'));
121122
$result = current($cursor->toArray());
122123

123-
if (empty($result['ok'])) {
124-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
124+
// TODO: Remove this once PHPC-318 is implemented
125+
is_array($result) and $result = (object) $result;
126+
127+
if (empty($result->ok)) {
128+
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
125129
}
126130

127-
if ( ! isset($result['value'])) {
131+
if ( ! isset($result->value)) {
128132
return null;
129133
}
130134

@@ -133,17 +137,17 @@ public function execute(Server $server)
133137
* requested.
134138
*/
135139
if ($this->options['upsert'] && ! $this->options['new'] &&
136-
isset($result['lastErrorObject']->updatedExisting) &&
137-
! $result['lastErrorObject']->updatedExisting) {
140+
isset($result->lastErrorObject->updatedExisting) &&
141+
! $result->lastErrorObject->updatedExisting) {
138142

139143
return null;
140144
}
141145

142-
if ( ! is_object($result['value'])) {
146+
if ( ! is_object($result->value)) {
143147
throw new UnexpectedValueException('findAndModify command did not return a "value" document');
144148
}
145149

146-
return $result['value'];
150+
return $result->value;
147151
}
148152

149153
/**

tests/Collection/BulkWriteFunctionalTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testInserts()
3535
array('_id' => $insertedIds[1], 'x' => 22),
3636
);
3737

38-
$this->assertEquals($expected, $this->collection->find()->toArray());
38+
$this->assertSameDocuments($expected, $this->collection->find());
3939
}
4040

4141
public function testUpdates()
@@ -69,7 +69,7 @@ public function testUpdates()
6969
array('_id' => $upsertedIds[3], 'x' => 67),
7070
);
7171

72-
$this->assertEquals($expected, $this->collection->find()->toArray());
72+
$this->assertSameDocuments($expected, $this->collection->find());
7373
}
7474

7575
public function testDeletes()
@@ -89,7 +89,7 @@ public function testDeletes()
8989
array('_id' => 2, 'x' => 22),
9090
);
9191

92-
$this->assertEquals($expected, $this->collection->find()->toArray());
92+
$this->assertSameDocuments($expected, $this->collection->find());
9393
}
9494

9595
public function testMixedOrderedOperations()
@@ -123,7 +123,7 @@ public function testMixedOrderedOperations()
123123
array('_id' => 4, 'x' => 44),
124124
);
125125

126-
$this->assertEquals($expected, $this->collection->find()->toArray());
126+
$this->assertSameDocuments($expected, $this->collection->find());
127127
}
128128

129129
/**

tests/Collection/CrudSpec/AggregateFunctionalTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public function testAggregateWithMultipleStages()
3636
array('_id' => 3, 'x' => 33),
3737
);
3838

39-
// Use iterator_to_array() here since aggregate() may return an ArrayIterator
40-
$this->assertEquals($expected, iterator_to_array($cursor));
39+
$this->assertSameDocuments($expected, $cursor);
4140
}
4241

4342
public function testAggregateWithOut()
@@ -64,7 +63,7 @@ public function testAggregateWithOut()
6463
array('_id' => 3, 'x' => 33),
6564
);
6665

67-
$this->assertEquals($expected, $outputCollection->find()->toArray());
66+
$this->assertSameDocuments($expected, $outputCollection->find());
6867

6968
// Manually clean up our output collection
7069
$this->dropCollectionIfItExists($outputCollection);

tests/Collection/CrudSpec/DeleteManyFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testDeleteManyWhenManyDocumentsMatch()
2727
array('_id' => 1, 'x' => 11),
2828
);
2929

30-
$this->assertSame($expected, $this->collection->find()->toArray());
30+
$this->assertSameDocuments($expected, $this->collection->find());
3131
}
3232

3333
public function testDeleteManyWhenNoDocumentsMatch()
@@ -43,6 +43,6 @@ public function testDeleteManyWhenNoDocumentsMatch()
4343
array('_id' => 3, 'x' => 33),
4444
);
4545

46-
$this->assertSame($expected, $this->collection->find()->toArray());
46+
$this->assertSameDocuments($expected, $this->collection->find());
4747
}
4848
}

tests/Collection/CrudSpec/DeleteOneFunctionalTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testDeleteOneWhenManyDocumentsMatch()
2828
array('_id' => 3, 'x' => 33),
2929
);
3030

31-
$this->assertSame($expected, $this->collection->find()->toArray());
31+
$this->assertSameDocuments($expected, $this->collection->find());
3232
}
3333

3434
public function testDeleteOneWhenOneDocumentMatches()
@@ -43,7 +43,7 @@ public function testDeleteOneWhenOneDocumentMatches()
4343
array('_id' => 3, 'x' => 33),
4444
);
4545

46-
$this->assertSame($expected, $this->collection->find()->toArray());
46+
$this->assertSameDocuments($expected, $this->collection->find());
4747
}
4848

4949
public function testDeleteOneWhenNoDocumentsMatch()
@@ -59,6 +59,6 @@ public function testDeleteOneWhenNoDocumentsMatch()
5959
array('_id' => 3, 'x' => 33),
6060
);
6161

62-
$this->assertSame($expected, $this->collection->find()->toArray());
62+
$this->assertSameDocuments($expected, $this->collection->find());
6363
}
6464
}

tests/Collection/CrudSpec/FindFunctionalTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testFindWithFilter()
2424
array('_id' => 1, 'x' => 11),
2525
);
2626

27-
$this->assertSame($expected, $this->collection->find($filter)->toArray());
27+
$this->assertSameDocuments($expected, $this->collection->find($filter));
2828
}
2929

3030
public function testFindWithFilterSortSkipAndLimit()
@@ -40,7 +40,7 @@ public function testFindWithFilterSortSkipAndLimit()
4040
array('_id' => 5, 'x' => 55),
4141
);
4242

43-
$this->assertSame($expected, $this->collection->find($filter, $options)->toArray());
43+
$this->assertSameDocuments($expected, $this->collection->find($filter, $options));
4444
}
4545

4646
public function testFindWithLimitSortAndBatchSize()
@@ -59,6 +59,6 @@ public function testFindWithLimitSortAndBatchSize()
5959
array('_id' => 4, 'x' => 44),
6060
);
6161

62-
$this->assertSame($expected, $this->collection->find($filter, $options)->toArray());
62+
$this->assertSameDocuments($expected, $this->collection->find($filter, $options));
6363
}
6464
}

tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public function testFindOneAndDeleteWhenManyDocumentsMatch()
2525
);
2626

2727
$document = $this->collection->findOneAndDelete($filter, $options);
28-
$this->assertEquals((object) array('x' => 22), $document);
28+
$this->assertSameDocument(array('x' => 22), $document);
2929

3030
$expected = array(
3131
array('_id' => 1, 'x' => 11),
3232
array('_id' => 3, 'x' => 33),
3333
);
3434

35-
$this->assertSame($expected, $this->collection->find()->toArray());
35+
$this->assertSameDocuments($expected, $this->collection->find());
3636
}
3737

3838
public function testFindOneAndDeleteWhenOneDocumentMatches()
@@ -44,14 +44,14 @@ public function testFindOneAndDeleteWhenOneDocumentMatches()
4444
);
4545

4646
$document = $this->collection->findOneAndDelete($filter, $options);
47-
$this->assertEquals((object) array('x' => 22), $document);
47+
$this->assertSameDocument(array('x' => 22), $document);
4848

4949
$expected = array(
5050
array('_id' => 1, 'x' => 11),
5151
array('_id' => 3, 'x' => 33),
5252
);
5353

54-
$this->assertSame($expected, $this->collection->find()->toArray());
54+
$this->assertSameDocuments($expected, $this->collection->find());
5555
}
5656

5757
public function testFindOneAndDeleteWhenNoDocumentsMatch()
@@ -71,6 +71,6 @@ public function testFindOneAndDeleteWhenNoDocumentsMatch()
7171
array('_id' => 3, 'x' => 33),
7272
);
7373

74-
$this->assertSame($expected, $this->collection->find()->toArray());
74+
$this->assertSameDocuments($expected, $this->collection->find());
7575
}
7676
}

0 commit comments

Comments
 (0)