Skip to content

Commit 4e6fc86

Browse files
authored
Merge pull request jasny#48 from Minstel/Fixes_to_methods_inheritens
Fixes to methods inheritens
2 parents ec321e9 + 0e66511 commit 4e6fc86

File tree

3 files changed

+40
-94
lines changed

3 files changed

+40
-94
lines changed

src/Collection.php

+9-40
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ public function __construct(Manager $manager, $dbName, $name, $options = [])
5353
* @param array $options
5454
* @return boolean
5555
*/
56-
public function createIndex(array $keys, array $options = [])
56+
public function createIndex($keys, array $options = [])
5757
{
5858
$ret = false;
59+
$keys = (array)$keys;
5960

6061
try {
6162
$ret = $this->parent('createIndex', $keys, $options);
@@ -107,15 +108,12 @@ public function getDocumentClass()
107108
* @param array $options Options for the save.
108109
* @return array|boolean
109110
*/
110-
public function replaceOne($filter, &$doc, array $options = [])
111+
public function replaceOne($filter, $doc, array $options = [])
111112
{
112113
$typeCast = $this->getTypeCaster();
113114
$values = $typeCast->toMongoType($doc, true);
114-
$ret = $this->parent('replaceOne', $filter, $values, $options);
115115

116-
$this->setDocId($doc, $ret->getUpsertedId());
117-
118-
return $ret;
116+
return $this->parent('replaceOne', $filter, $values, $options);
119117
}
120118

121119
/**
@@ -126,15 +124,12 @@ public function replaceOne($filter, &$doc, array $options = [])
126124
* @param array $options Options for the save.
127125
* @return array|boolean
128126
*/
129-
public function insertOne(&$doc, array $options = [])
127+
public function insertOne($doc, array $options = [])
130128
{
131129
$typeCast = $this->getTypeCaster();
132130
$values = $typeCast->toMongoType($doc, true);
133-
$ret = $this->parent('insertOne', $values, $options);
134-
135-
$this->setDocId($doc, $ret->getInsertedId());
136131

137-
return $ret;
132+
return $this->parent('insertOne', $values, $options);
138133
}
139134

140135
/**
@@ -145,7 +140,7 @@ public function insertOne(&$doc, array $options = [])
145140
* @param array $options
146141
* @return mixed
147142
*/
148-
public function insertMany(array &$docs, array $options = [])
143+
public function insertMany(array $docs, array $options = [])
149144
{
150145
$data = [];
151146
$typeCast = $this->getTypeCaster();
@@ -154,14 +149,7 @@ public function insertMany(array &$docs, array $options = [])
154149
$data[] = $typeCast->toMongoType($doc, true);
155150
}
156151

157-
$ret = $this->parent('insertMany', $data, $options);
158-
$ids = $ret->getInsertedIds();
159-
160-
foreach ($ids as $i => $id) {
161-
$this->setDocId($docs[$i], $id);
162-
}
163-
164-
return $ret;
152+
return $this->parent('insertMany', $data, $options);
165153
}
166154

167155
/**
@@ -171,7 +159,7 @@ public function insertMany(array &$docs, array $options = [])
171159
* @param array $options Options for the save.
172160
* @return array|boolean
173161
*/
174-
public function save(&$doc, array $options = [])
162+
public function save($doc, array $options = [])
175163
{
176164
$typeCast = $this->getTypeCaster();
177165
$values = $typeCast->toMongoType($doc, true);
@@ -199,25 +187,6 @@ protected function createReplaceOneFilter($values)
199187
return $filter;
200188
}
201189

202-
/**
203-
* Update the identifier
204-
*
205-
* @param array|object $doc
206-
* @param MongoDB\BSON\ObjectId $id
207-
*/
208-
protected function setDocId(&$doc, $id)
209-
{
210-
if (!$id) {
211-
return;
212-
}
213-
214-
if (is_array($doc)) {
215-
$doc['_id'] = $id;
216-
} else {
217-
$doc->_id = $id;
218-
}
219-
}
220-
221190
/**
222191
* Convert values to a document
223192
*

src/DB.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private function createClientFromOptions($options)
7171
* @param array $options
7272
* @return Mongo\DB\Collection
7373
*/
74-
public function selectCollection($name, $options = [])
74+
public function selectCollection($name, array $options = [])
7575
{
7676
return new Collection(
7777
$this->getManager(),

tests/CollectionTest.php

+30-53
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,31 @@ public function testConstructorExceptionEntity()
2828
$collection = new Collection($manager, 'test-db', 'test-collection', ['documentClass' => \stdClass::class]);
2929
}
3030

31+
/**
32+
* Provide data for testing 'createIndex' method
33+
*
34+
* @return array
35+
*/
36+
public function createIndexProvider()
37+
{
38+
return [
39+
[['foo' => 1]],
40+
[(object)['foo' => 1]]
41+
];
42+
}
43+
3144
/**
3245
* Test 'createIndex' method with no deletion or exception
46+
*
47+
* @dataProvider createIndexProvider
3348
*/
34-
public function testCreateIndex()
49+
public function testCreateIndex($keys)
3550
{
36-
$keys = ['foo' => 1];
3751
$options = ['zoo' => 'baz'];
3852
$expected = 'foo_result';
3953

4054
$collection = $this->createPartialMock(Collection::class, ['parent']);
41-
$collection->expects($this->once())->method('parent', $keys, $options)->willReturn($expected);
55+
$collection->expects($this->once())->method('parent', (array)$keys, $options)->willReturn($expected);
4256

4357
$result = $collection->createIndex($keys, $options);
4458

@@ -67,7 +81,7 @@ public function createIndexExceptionProvider()
6781
*/
6882
public function testCreateIndexException($exception, $options)
6983
{
70-
$keys = ['foo' => 'bar'];
84+
$keys = ['foo' => 1];
7185

7286
$collection = $this->createPartialMock(Collection::class, ['parent']);
7387
$collection->expects($this->once())->method('parent', $keys, $options)->willThrowException($exception);
@@ -78,10 +92,11 @@ public function testCreateIndexException($exception, $options)
7892

7993
/**
8094
* Test 'createIndex' method with deleting existing index
95+
*
96+
* @dataProvider createIndexProvider
8197
*/
82-
public function testCreateIndexForce()
98+
public function testCreateIndexForce($keys)
8399
{
84-
$keys = ['foo' => 1];
85100
$options = ['force' => true];
86101
$callbackState = (object)['called' => 0];
87102

@@ -96,7 +111,7 @@ public function testCreateIndexForce()
96111
};
97112

98113
$collection = $this->createPartialMock(Collection::class, ['parent', 'dropIndex']);
99-
$collection->expects($this->exactly(2))->method('parent', $keys, $options)->will($this->returnCallback($callback));
114+
$collection->expects($this->exactly(2))->method('parent', (array)$keys, $options)->will($this->returnCallback($callback));
100115
$collection->expects($this->once())->method('dropIndex')->with('foo');
101116

102117
$result = $collection->createIndex($keys, $options);
@@ -106,18 +121,19 @@ public function testCreateIndexForce()
106121

107122
/**
108123
* Test 'createIndex' method with deleting existing index
124+
*
125+
* @dataProvider createIndexProvider
109126
*/
110-
public function testCreateIndexIgnore()
127+
public function testCreateIndexIgnore($keys)
111128
{
112-
$keys = ['foo' => 1];
113129
$options = ['ignore' => true];
114130

115131
$callback = function($keysArg, $optionsArg) {
116132
throw new \MongoDB\Driver\Exception\RuntimeException('', 85);
117133
};
118134

119135
$collection = $this->createPartialMock(Collection::class, ['parent', 'dropIndex']);
120-
$collection->expects($this->once())->method('parent', $keys, $options)->will($this->returnCallback($callback));
136+
$collection->expects($this->once())->method('parent', (array)$keys, $options)->will($this->returnCallback($callback));
121137
$collection->expects($this->never())->method('dropIndex');
122138

123139
$result = $collection->createIndex($keys, $options);
@@ -167,8 +183,8 @@ public function testWithoutCasting()
167183
public function insertOneProvider()
168184
{
169185
return [
170-
[['foo' => 'bar'], ['foo' => 'bar', '_id' => 'foo_id']],
171-
[(object)['foo' => 'bar'], (object)['foo' => 'bar', '_id' => 'foo_id']],
186+
[['foo' => 'bar']],
187+
[(object)['foo' => 'bar']],
172188
];
173189
}
174190

@@ -177,9 +193,8 @@ public function insertOneProvider()
177193
*
178194
* @dataProvider insertOneProvider
179195
*/
180-
public function testInsertOne($document, $expectedDocument)
196+
public function testInsertOne($document)
181197
{
182-
$id = 'foo_id';
183198
$options = ['opt1' => 'val1'];
184199
$values = (array)$document;
185200
$queryResult = $this->createMock(\MongoDB\InsertOneResult::class);
@@ -190,11 +205,9 @@ public function testInsertOne($document, $expectedDocument)
190205
$collection->expects($this->once())->method('getTypeCaster')->willReturn($typeCast);
191206
$typeCast->expects($this->once())->method('toMongoType')->with($document, true)->willReturn($values);
192207
$collection->expects($this->once())->method('parent')->with('insertOne', $values, $options)->willReturn($queryResult);
193-
$queryResult->expects($this->once())->method('getInsertedId')->willReturn($id);
194208

195209
$result = $collection->insertOne($document, $options);
196210
$this->assertSame($queryResult, $result);
197-
$this->assertEquals($expectedDocument, $document);
198211
}
199212

200213
/**
@@ -215,12 +228,6 @@ public function testInsertMany()
215228
['foo3' => 'bar3']
216229
];
217230

218-
$expectedDocs = [
219-
['foo' => 'bar', '_id' => 'a'],
220-
['foo2' => 'bar2', '_id' => 'b'],
221-
(object)['foo3' => 'bar3', '_id' => 'c']
222-
];
223-
224231
$queryResult = $this->createMock(\MongoDB\InsertManyResult::class);
225232

226233
$typeCast = $this->createMock(DeepCast::class);
@@ -233,46 +240,18 @@ public function testInsertMany()
233240
$collection = $this->createPartialMock(Collection::class, ['getTypeCaster', 'parent']);
234241
$collection->expects($this->once())->method('getTypeCaster')->willReturn($typeCast);
235242
$collection->expects($this->once())->method('parent')->with('insertMany', $data, $options)->willReturn($queryResult);
236-
$queryResult->expects($this->once())->method('getInsertedIds')->willReturn(['a', 'b', 'c']);
237243

238244
$result = $collection->insertMany($docs, $options);
239245
$this->assertSame($queryResult, $result);
240-
$this->assertEquals($expectedDocs, $docs);
241246
}
242247

243248
/**
244249
* Test 'replaceOne' method
245250
*
246251
* @dataProvider insertOneProvider
247252
*/
248-
public function testReplaceOne($document, $expectedDocument)
249-
{
250-
$id = 'foo_id';
251-
$options = ['opt1' => 'val1'];
252-
$filter = ['match_key' => 'val'];
253-
$values = (array)$document;
254-
$queryResult = $this->createMock(\MongoDB\UpdateResult::class);
255-
256-
$typeCast = $this->createMock(DeepCast::class);
257-
258-
$collection = $this->createPartialMock(Collection::class, ['getTypeCaster', 'parent']);
259-
$collection->expects($this->once())->method('getTypeCaster')->willReturn($typeCast);
260-
$typeCast->expects($this->once())->method('toMongoType')->with($document, true)->willReturn($values);
261-
$collection->expects($this->once())->method('parent')->with('replaceOne', $filter, $values, $options)->willReturn($queryResult);
262-
$queryResult->expects($this->once())->method('getUpsertedId')->willReturn($id);
263-
264-
$result = $collection->replaceOne($filter, $document, $options);
265-
$this->assertSame($queryResult, $result);
266-
$this->assertEquals($expectedDocument, $document);
267-
}
268-
269-
/**
270-
* Test 'replaceOne' method, if no record was replaced
271-
*/
272-
public function testReplaceOneNoReplaced()
253+
public function testReplaceOne($document)
273254
{
274-
$id = 'foo_id';
275-
$document = ['foo' => 'bar'];
276255
$options = ['opt1' => 'val1'];
277256
$filter = ['match_key' => 'val'];
278257
$values = (array)$document;
@@ -284,11 +263,9 @@ public function testReplaceOneNoReplaced()
284263
$collection->expects($this->once())->method('getTypeCaster')->willReturn($typeCast);
285264
$typeCast->expects($this->once())->method('toMongoType')->with($document, true)->willReturn($values);
286265
$collection->expects($this->once())->method('parent')->with('replaceOne', $filter, $values, $options)->willReturn($queryResult);
287-
$queryResult->expects($this->once())->method('getUpsertedId')->willReturn(null);
288266

289267
$result = $collection->replaceOne($filter, $document, $options);
290268
$this->assertSame($queryResult, $result);
291-
$this->assertEquals(['foo' => 'bar'], $document);
292269
}
293270

294271
/**

0 commit comments

Comments
 (0)