public function call($command, $arguments = []) { $arg = $this->model ? CollectionNamer::nameCollection($this->model) : true; $cmd = [$command => $arg]; if (is_array($arguments) && count($arguments)) { $cmd = array_merge($cmd, $arguments); } $result = $this->mn->getDbInstance()->command($cmd); if (array_key_exists('errmsg', $result) && array_key_exists('ok', $result) && $result['ok'] == 0) { if (array_key_exists('bad cmd', $result)) { $badCmd = key($result['bad cmd']); if ($badCmd == $command) { throw new CommandNotFoundException(sprintf('Command `%s` not found', $command)); } } throw new CommandException(sprintf('Could not execute command `%s`, mongo returned: "%s"', $command, $result['errmsg'])); } return $result; }
public function testIfTwoInstancesOfManganHaveProperConnections() { // Default $mangan = new Mangan(); $this->assertSame($mangan->dbName, ManganFirstDbName); $this->assertInstanceOf(MongoDB::class, $mangan->getDbInstance(), 'That first connection is active'); // Second $second = new Mangan('second'); $this->assertSame($second->dbName, ManganSecondDbName); $this->assertInstanceOf(MongoDB::class, $second->getDbInstance(), 'That second connection is active'); }
public function testIfWillDeleteEmbeddedImage() { $fileName = __DIR__ . '/logo-1024.png'; $md5 = md5_file($fileName); // NOTE: Must work fine even if _id is not set $model = new ModelWithEmbeddedImage(); $model->file = new Image(); $model->file->set($fileName); $em = new EntityManager($model); $em->save(); $finder = new Finder($model); $found = $finder->findByPk($model->_id); /* @var $found ModelWithEmbeddedImage */ $file = $found->file->get()->getBytes(); $this->assertSame($md5, md5($file)); // Resize image $params = new ImageParams(); $params->width = 100; $params->height = 100; $resized = $found->file->get($params)->getBytes(); // Check if was resized $this->assertTrue($file > $resized); $mangan = new Mangan(); $gfs = $mangan->getDbInstance()->getGridFS(); $tmp = $mangan->getDbInstance()->getGridFS(File::TmpPrefix); $criteria = ['parentId' => $found->file->_id]; $this->assertSame(1, $gfs->count($criteria)); $this->assertSame(1, $tmp->count($criteria)); $deleted = $found->delete(); $this->assertTrue($deleted); $this->assertSame(0, $gfs->count($criteria)); $this->assertSame(0, $tmp->count($criteria)); }