public function testIfWillDeleteIndexOfTrashableDocumentAfterTrashingAndRestoreItOnRestore()
 {
     $model = new TrashableModel();
     $model->_id = new MongoId();
     $model->title = 'Connecticut is a state';
     // Mock signal receive save
     (new Receiver())->onSave(new AfterSave($model));
     $mnl = Manganel::fly();
     $client = $mnl->getClient();
     $get = ['index' => $mnl->index, 'type' => CollectionNamer::nameCollection($model), 'id' => (string) $model->_id];
     $found = $client->get($get)['_source'];
     $this->assertSame($model->title, $found['title']);
     // Should be handle by event handler from Receiver
     $model->trash();
     try {
         $client->get($get);
         $this->assertFalse(true, 'That missing exception was not thrown');
     } catch (Missing404Exception $e) {
         $this->assertTrue(true, 'That missing exception was thrown');
     }
     $trash = new Trash();
     $trashed = $trash->find();
     $this->assertNotNull($trashed, 'That item was found in trash');
     $restored = $trashed->restore();
     $this->assertTrue($restored, 'That item was successfully restored');
     $restoredIndex = $client->get($get)['_source'];
     $this->assertSame($model->title, $restoredIndex['title'], 'That restored item is back in index');
 }
Exemplo n.º 2
0
 public function testIfWillReadCollectionNameFromMethod()
 {
     $expected = WithMethodCollectionName::CollectionName;
     $model = new WithMethodCollectionName();
     $name = CollectionNamer::nameCollection($model);
     $this->assertSame($expected, $name);
 }
Exemplo n.º 3
0
 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;
 }
Exemplo n.º 4
0
 public function testIfWillStoreNestedDocumentFromMockSignal()
 {
     $model = new NestedModel();
     $model->_id = new MongoId();
     codecept_debug((string) $model->_id);
     $model->username = '******';
     $image = new Image();
     $image->_id = new MongoId();
     $image->filename = 'my-photo.jpg';
     $model->avatar = $image;
     // Mock signal receive
     (new Receiver())->onSave(new AfterSave($model));
     $mnl = Manganel::fly();
     $client = $mnl->getClient();
     $get = ['index' => $mnl->index, 'type' => CollectionNamer::nameCollection($model), 'id' => (string) $model->_id];
     $found = $client->get($get)['_source'];
     codecept_debug($found);
     $this->assertSame($model->username, $found['username']);
 }
Exemplo n.º 5
0
 private function getParams($params = [])
 {
     // Check refresh option
     if ($this->manganel->refresh instanceof Closure) {
         $func = $this->manganel->refresh;
         $refresh = (bool) $func($this->model);
     } else {
         $refresh = $this->manganel->refresh;
     }
     $result = ['index' => strtolower($this->manganel->index), 'type' => CollectionNamer::nameCollection($this->model), 'id' => (string) $this->model->_id, 'refresh' => $refresh];
     return array_merge($result, $params);
 }
Exemplo n.º 6
0
 private function getParams($q = null)
 {
     $body = [];
     // Try to get query from criteria if empty
     $criteria = $this->getCriteria();
     if (empty($criteria)) {
         $criteria = new SearchCriteria();
     }
     if (!empty($q)) {
         $criteria->search($q);
     }
     $decorator = new QueryBuilderDecorator($this->manganel);
     $decorator->decorate($body, $criteria);
     if (empty($this->models)) {
         $type = '_all';
     } else {
         $types = [];
         foreach ($this->models as $model) {
             if (!$model instanceof AnnotatedInterface) {
                 throw new UnexpectedValueException(sprintf('Expected `%s` instance, got `%s`', AnnotatedInterface::class, is_object($model) ? get_class($model) : gettype($model)));
             }
             $types[] = CollectionNamer::nameCollection($model);
         }
         $type = implode(',', array_unique($types));
     }
     $params = ['index' => strtolower($this->manganel->index), 'type' => $type, 'body' => $body];
     return $params;
 }
Exemplo n.º 7
0
 public function testIfWillIgnoreExplicitlyNonIndexableFromMockSignal()
 {
     $model = new ExplicitlyNonIndexableModel();
     $model->_id = new MongoId();
     $model->title = 'Alabama is a state';
     // Mock signal receive
     (new Receiver())->onSave(new AfterSave($model));
     $mnl = Manganel::fly();
     $client = $mnl->getClient();
     $get = ['index' => $mnl->index, 'type' => CollectionNamer::nameCollection($model), 'id' => (string) $model->_id];
     try {
         $found = $client->get($get)['_source'];
         $this->assertFalse(true, 'That missing exception was not thrown');
     } catch (Missing404Exception $e) {
         $this->assertTrue(true, 'That missing exception was thrown');
     }
 }
Exemplo n.º 8
0
 /**
  * Create entity manager
  * @param AnnotatedInterface $model
  * @param Mangan $mangan
  * @throws ManganException
  */
 public function __construct(AnnotatedInterface $model, Mangan $mangan = null)
 {
     $this->model = $model;
     $this->sm = new ScopeManager($model);
     $this->options = new EntityOptions($model);
     $this->collectionName = CollectionNamer::nameCollection($model);
     $this->meta = ManganMeta::create($model);
     $this->validator = new Validator($model);
     if (null === $mangan) {
         $mangan = Mangan::fromModel($model);
     }
     if (!$this->collectionName) {
         throw new ManganException(sprintf('Invalid collection name for model: `%s`', $this->meta->type()->name));
     }
     $this->_collection = new MongoCollection($mangan->getDbInstance(), $this->collectionName);
 }