diff --git a/src/MongoClient.php b/src/MongoClient.php index c33c841517..2eceafa253 100644 --- a/src/MongoClient.php +++ b/src/MongoClient.php @@ -2,10 +2,11 @@ namespace Utopia\Mongo; +use Exception; +use Mongo\Exception\Duplicate; use MongoDB\BSON; use Swoole\Client; use Swoole\Coroutine\Client as CoroutineClient; -use Mongo\Exception\Duplicate as DuplicateException; use stdClass; class MongoClient @@ -119,18 +120,18 @@ class MongoClient * * @param mixed $data * @return stdClass|array|int - * @throws DuplicateException + * @throws Exception */ public function send(mixed $data): stdClass|array|int { $this->client->send($data); - $res = $this->receive(); - //var_dump($res); - return $res; + return $this->receive(); } /** * Receive a message from connection. + * @throws Duplicate + * @throws Exception */ private function receive(): stdClass|array|int { @@ -157,24 +158,23 @@ class MongoClient $result = BSON\toPHP(substr($res, 21, $responseLength - 21)); if (property_exists($result, "writeErrors")) { - throw new DuplicateException($result->writeErrors[0]->errmsg); + throw new Duplicate($result->writeErrors[0]->errmsg); } - if (property_exists($result, "n") && $result->ok == 1) { + if (property_exists($result, "n") && $result->ok === 1.0) { return $result->n; } - if (property_exists($result, "nonce") && $result->ok == 1) { + if (property_exists($result, "nonce") && $result->ok === 1.0) { return $result; } - if (property_exists($result, 'errmsg')) { - throw new \Exception($result->errmsg); + throw new Exception($result->errmsg); } - if ($result->ok == 1) { - return $result; + if ($result->ok === 1.0) { + return $result; } return $result->cursor->firstBatch; @@ -219,15 +219,13 @@ class MongoClient * * @param array $options * @param string|null $db - * @return MongoClient + * @return bool */ - public function dropDatabase(array $options = [], ?string $db = null): MongoClient + public function dropDatabase(array $options = [], ?string $db = null): bool { $db ??= $this->options->name; - - $this->query(array_merge(["dropDatabase" => 1], $options), $db); - - return $this; + $res = $this->query(array_merge(["dropDatabase" => 1], $options), $db); + return $res->ok === 1.0; } public function selectCollection($name): MongoClient @@ -241,20 +239,22 @@ class MongoClient * * @param string $name * @param array $options + * @return bool + * @throws Duplicate */ - public function createCollection(string $name, array $options = []): MongoClient + public function createCollection(string $name, array $options = []): bool { $list = $this->listCollectionNames(["name" => $name]); if (\count($list->cursor->firstBatch) > 0) { - return $this; + throw new Duplicate('Collection Exists'); } - $this->query(array_merge([ + $res = $this->query(array_merge([ 'create' => $name, ], $options)); - return $this; + return $res->ok === 1.0; } /** @@ -359,16 +359,17 @@ class MongoClient * https://docs.mongodb.com/manual/reference/command/insert/#mongodb-dbcommand-dbcmd.insert * * @param string $collection - * @param array $documents + * @param array $document * @param array $options * * @return array + * @throws Exception */ - public function insert(string $collection, array $documents, array $options = []): array + public function insert(string $collection, array $document, array $options = []): array { $docObj = new stdClass(); - foreach ($documents as $key => $value) { + foreach ($document as $key => $value) { if(\is_null($value)) continue; $docObj->{$key} = $value; @@ -379,7 +380,6 @@ class MongoClient 'documents' => [$docObj], ], $options)); - return $this->lastInsertedDocument($collection); } @@ -415,11 +415,11 @@ class MongoClient public function update(string $collection, array $where = [], array $updates = [], array $options = []): MongoClient { - $clean_updates = []; + $cleanUpdates = []; foreach($updates as $k => $v) { if(\is_null($v)) continue; - $clean_updates[$k] = $v; + $cleanUpdates[$k] = $v; } $this->query( @@ -428,7 +428,7 @@ class MongoClient 'updates' => [ [ 'q' => $this->toObject($where), - 'u' => $this->toObject($clean_updates), + 'u' => $this->toObject($cleanUpdates), 'multi' => false, 'upsert' => false ] @@ -453,11 +453,11 @@ class MongoClient public function upsert(string $collection, array $where = [], array $updates = [], array $options = []): MongoClient { - $clean_updates = []; + $cleanUpdates = []; foreach($updates as $k => $v) { if(\is_null($v)) continue; - $clean_updates[$k] = $v; + $cleanUpdates[$k] = $v; } @@ -468,7 +468,7 @@ class MongoClient 'updates' => [ [ 'q' => ['_uid' => $where['_uid']], - 'u' => ['$set' => $clean_updates], + 'u' => ['$set' => $cleanUpdates], ] ], ], diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 7fd60ec22b..568b969828 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -2,9 +2,9 @@ namespace Utopia\Tests; +use Mongo\Exception\Duplicate; use MongoDB\BSON\ObjectId; use PHPUnit\Framework\TestCase; -use Utopia\Database\Query; use Utopia\Mongo\MongoClient; use Utopia\Mongo\MongoClientOptions; @@ -35,12 +35,17 @@ class MongoTest extends TestCase public function testDeleteDatabase() { - self::assertTrue(!!$this->getDatabase()->dropDatabase([])); + self::assertTrue($this->getDatabase()->dropDatabase([])); } + /** + * @throws Duplicate + */ public function testCreateCollection() { - self::assertTrue(!!$this->getDatabase()->createCollection('movies')); + self::assertTrue($this->getDatabase()->createCollection('movies')); + self::expectExceptionMessage(Duplicate::class); + self::assertTrue($this->getDatabase()->createCollection('movies')); } public function testListDatabases() @@ -56,6 +61,7 @@ class MongoTest extends TestCase 'language' => 'English' ] ); + $id = (string)$doc['_id']; self::assertEquals(24, strlen($id)); @@ -69,6 +75,35 @@ class MongoTest extends TestCase $id = (string)$doc['_id']; self::assertEquals(24, strlen($id)); + $doc = $this->getDatabase()->insert('movies', [ + 'name' => 300, + 'country' => 'USA', + 'language' => 'English' + ] + ); + + $id = 999; + $doc = $this->getDatabase()->insert('movies', [ + '_id' => $id, + 'array' => ['USA', 'UK', 'India'], + 'language' => 'English', + 'float' => 9.9, + 'integer' => 9, + 'is_open' => true, + 'date_string' => (new \DateTime())->format('Y-m-d\TH:i:s.vP'), + 'date_object' => new \DateTime() + ] + ); + + $doc = $this->getDatabase()->find('movies', ['_id' => $id])->cursor->firstBatch ?? []; + $doc = $doc[0]; + + self::assertTrue($doc->is_open); + self::assertIsFloat($doc->float); + self::assertIsInt($doc->integer); + self::assertIsArray($doc->array); + self::assertIsObject($doc->date_object); // Todo: This is not working can'tretrive the object value + }