11<?php
22namespace Helmich \MongoMock ;
33
4+ use Helmich \MongoMock \Log \Index ;
5+ use Helmich \MongoMock \Log \Query ;
6+ use MongoDB \BSON \Binary ;
7+ use MongoDB \BSON \ObjectID ;
48use MongoDB \Collection ;
59use MongoDB \Model \BSONDocument ;
610
711class MockCollection extends Collection
812{
913 public $ queries = [];
10-
1114 public $ documents = [];
1215 public $ indices = [];
16+ public $ dropped = false ;
1317
1418 /** @noinspection PhpMissingParentConstructorInspection */
1519 public function __construct ()
1620 {
17-
1821 }
1922
2023 public function insertOne ($ document , array $ options = [])
2124 {
25+ if (!isset ($ document ['_id ' ])) {
26+ $ document ['_id ' ] = new ObjectID ();
27+ }
28+
29+ if (!$ document instanceof BSONDocument) {
30+ $ document = new BSONDocument ($ document );
31+ }
32+
2233 $ document = new BSONDocument ($ document );
2334 $ this ->documents [] = $ document ;
35+
36+ return new MockInsertOneResult ($ document ['_id ' ]);
37+ }
38+
39+ public function insertMany (array $ documents , array $ options = [])
40+ {
41+ $ insertedIds = array_map (function ($ doc ) use ($ options ) {
42+ return $ this ->insertOne ($ doc , $ options )->getInsertedId ();
43+ }, $ documents );
44+
45+ return new MockInsertManyResult ($ insertedIds );
2446 }
2547
2648 public function deleteMany ($ filter , array $ options = [])
@@ -95,11 +117,9 @@ public function find($filter = [], array $options = [])
95117
96118 public function findOne ($ filter = [], array $ options = [])
97119 {
98- $ matcher = $ this ->matcherFromQuery ($ filter );
99- foreach ($ this ->documents as $ doc ) {
100- if ($ matcher ($ doc )) {
101- return $ doc ;
102- }
120+ $ results = $ this ->find ($ filter , $ options );
121+ foreach ($ results as $ result ) {
122+ return $ result ;
103123 }
104124 return null ;
105125 }
@@ -118,20 +138,16 @@ public function count($filter = [], array $options = [])
118138
119139 public function createIndex ($ key , array $ options = [])
120140 {
121- $ this ->indices [] = [ ' key ' => $ key , ' options ' => $ options] ;
141+ $ this ->indices [] = new Index ( $ key , $ options) ;
122142 }
123143
124- public function hasIndex ( $ key , array $ options = [])
144+ public function drop ( array $ options = [])
125145 {
126- foreach ($ this ->indices as $ index ) {
127- if ($ index == ['key ' => $ key , 'options ' => $ options ]) {
128- return true ;
129- }
130- }
131- return false ;
146+ $ this ->documents = [];
147+ $ this ->dropped = true ;
132148 }
133149
134- private function matcherFromQuery (array $ query )
150+ private function matcherFromQuery (array $ query ): callable
135151 {
136152 $ matchers = [];
137153
@@ -149,12 +165,24 @@ private function matcherFromQuery(array $query)
149165 };
150166 }
151167
152- private function matcherFromConstraint ($ constraint )
168+ private function matcherFromConstraint ($ constraint ): callable
153169 {
154170 if (is_callable ($ constraint )) {
155171 return $ constraint ;
156172 }
157173
174+ if ($ constraint instanceof \PHPUnit_Framework_Constraint) {
175+ return function ($ val ) use ($ constraint ): bool {
176+ return $ constraint ->evaluate ($ val , '' , true );
177+ };
178+ }
179+
180+ if ($ constraint instanceof ObjectID) {
181+ return function ($ val ) use ($ constraint ): bool {
182+ return ("" . $ constraint ) == ("" . $ val );
183+ };
184+ }
185+
158186 if (is_array ($ constraint )) {
159187 return function ($ val ) use ($ constraint ): bool {
160188 $ result = true ;
@@ -177,6 +205,10 @@ private function matcherFromConstraint($constraint)
177205 }
178206
179207 return function ($ val ) use ($ constraint ): bool {
208+ if ($ val instanceof Binary && is_string ($ constraint )) {
209+ return $ val ->getData () == $ constraint ;
210+ }
211+
180212 return $ val == $ constraint ;
181213 };
182214 }
0 commit comments