From 646b19fd422a2362d19e5f524a3305704a2ad3cb Mon Sep 17 00:00:00 2001 From: lexxorlov Date: Tue, 26 Apr 2016 17:42:09 +0300 Subject: [PATCH] Remove some old files --- EMongoDocument.php | 6 +- EMongoFile.php | 264 ------------------------------------ EMongoQueryBuilder.php | 7 + composer.json | 2 +- tests/MongoCursorTest._php | 85 ------------ tests/MongoDocumentTest.php | 10 +- tests/MongoFileTest.php | 25 ---- util/EMongoCache.php | 8 +- 8 files changed, 21 insertions(+), 386 deletions(-) delete mode 100644 EMongoFile.php delete mode 100644 tests/MongoCursorTest._php delete mode 100644 tests/MongoFileTest.php diff --git a/EMongoDocument.php b/EMongoDocument.php index ebb1665..907d5d4 100644 --- a/EMongoDocument.php +++ b/EMongoDocument.php @@ -931,7 +931,7 @@ public function findAllByPk($pk, $fields = []) * @param array|string[] $fields * @return [] */ - public function find($criteria = [], $fields = []) + protected function find($criteria = [], $fields = []) { $this->trace(__FUNCTION__); @@ -996,7 +996,7 @@ public function find($criteria = [], $fields = []) * @param array|string[] $fields * @return EMongoDocument|null */ - public function findBy_id($_id, $fields = []) + public function findByObjectID($_id, $fields = []) { $this->trace(__FUNCTION__); $_id = $this->getPrimaryKey($_id); @@ -1012,7 +1012,7 @@ public function findBy_id($_id, $fields = []) public function findByPk($pk, $fields = []) { $this->trace(__FUNCTION__); - return $this->findBy_id($pk, $fields); + return $this->findByObjectID($pk, $fields); } /** diff --git a/EMongoFile.php b/EMongoFile.php deleted file mode 100644 index f09d49a..0000000 --- a/EMongoFile.php +++ /dev/null @@ -1,264 +0,0 @@ -getFile() instanceof MongoGridFSFile){ - return $this->getFile()->getFilename(); - } - if($this->getFile() instanceof CUploadedFile){ - return $this->getFile()->getTempName(); - } - if(is_string($this->getFile()) && is_file($this->getFile())){ - return $this->getFile(); - } - return false; - } - - - /** - * @return int|bool - */ - public function getSize() - { - if($this->getFile() instanceof MongoGridFSFile || $this->getFile() instanceof CUploadedFile){ - return $this->getFile()->getSize(); - } - if(is_file($this->getFile())){ - return filesize($this->getFile()); - } - return false; - } - - - /** - * @return string|bool - */ - public function getBytes() - { - if($this->getFile() instanceof MongoGridFSFile){ - return $this->getFile()->getBytes(); - } - if($this->getFile() instanceof CUploadedFile || (is_file($this->getFile()) && is_readable($this->getFile()))){ - return file_get_contents($this->getFilename()); - } - return false; - } - - /** - * Gets the file object - */ - public function getFile() - { - // This if statement allows for you to continue using this class AFTER insert - // basically it will only get the file if you plan on using it further which means that - // otherwise it omits at least one database call each time - if($this->_id instanceof MongoId && !$this->_file instanceof MongoGridFSFile){ - return $this->_file = $this->getCollection()->get($this->_id); - } - return $this->_file; - } - - /** - * Sets the file object - */ - public function setFile($v) - { - $this->_file = $v; - } - - /** - * This denotes the prefix to all gridfs collections set by this class - * @return string - */ - public function collectionPrefix() - { - return 'fs'; - } - - /** - * Returns the static model of the specified AR class. - * @param string $className - * @return EMongoDocument - User the static model class - */ - public static function model($className = __CLASS__) - { - return parent::model($className); - } - - /** - * Magic will either call a function on the file if it exists or bubble to parent - * @see EMongoDocument::__call() - */ - public function __call($name, $parameters) - { - if($this->getFile() instanceof MongoGridFSFile && method_exists($this->getFile(), $name)){ - return call_user_func_array([$this->getFile(), $name], $parameters); - } - return parent::__call($name, $parameters); - } - - /** - * This can populate from a $_FILES instance - * @param CModel $model - * @param string $attribute - * @return boolean|EMongoFile|null - */ - public static function populate($model, $attribute) - { - if($file = CUploadedFile::getInstance($model, $attribute)){ - $model=new EMongoFile(); - $model->setFile($file); - return $model; - } - return null; - } - - /** - * This function populates from a stream - * - * You must unlink the tempfile yourself by calling unlink($file->getFilename()) - * @param string $stream - * @return EMongoFile the new file generated from the stream - - public static function stream($stream){ - $tempFile = tempnam(null, 'tmp'); // returns a temporary filename - - $fp = fopen($tempFile, 'wb'); // open temporary file - $putData = fopen($stream, 'rb'); // open input stream - - stream_copy_to_stream($putData, $fp); // write input stream directly into file - - fclose($putData); - fclose($fp); - - $file = new EMongoFile(); - $file->setFile($tempFile); - return $file; - } - */ - - /** - * Replaces the normal populateRecord specfically for GridFS by setting the attributes from the - * MongoGridFsFile object correctly and other file details like size and name. - * @see EMongoDocument::populateRecord() - * @param array $attributes - * @param bool $callAfterFind - * @param bool $partial - * @return EMongoDocument|null - */ - public function populateRecord($attributes, $callAfterFind = true, $partial = false) - { - if($attributes === false){ - return null; - } - // the cursor will actually input a MongoGridFSFile object as the "document" - // so what we wanna do is get the attributes or metadata attached to the file object - // set it as our attributes and then set this classes file as the first param we got - $file = $attributes; - $attributes = $file->file; - $record = $this->instantiate($attributes); - $record->setFile($file); - $record->setScenario('update'); - $record->setIsNewRecord(false); - $record->init(); - - $labels = []; - foreach($attributes as $name => $value){ - $labels[$name] = 1; - $record->$name = $value; - } - - if($partial){ - $record->setIsPartial(true); - $record->setProjectedFields($labels); - } - //$record->_pk=$record->primaryKey(); - $record->attachBehaviors($record->behaviors()); - if($callAfterFind){ - $record->afterFind(); - } - return $record; - } - - /** - * Inserts the file. - * - * The only difference between the normal insert is that this uses the storeFile function on the GridFS object - * @see EMongoDocument::insert() - * @param array $attributes - * @return bool - * @throws EMongoException - */ - public function insert($attributes = null) - { - if(!$this->getIsNewRecord()){ - throw new EMongoException(Yii::t('yii','The active record cannot be inserted to database because it is not new.')); - } - if(!$this->beforeSave()){ - return false; - } - - $this->trace(__FUNCTION__); - if($attributes === null){ - $document=$this->getRawDocument(); - }else{ - $document=$this->filterRawDocument($this->getAttributes($attributes)); - } - - if(YII_DEBUG){ - // we're actually physically testing for Yii debug mode here to stop us from - // having to do the serialisation on the update doc normally. - Yii::trace('Executing storeFile: {$document:' . json_encode($document) . '}', 'extensions.MongoYii.EMongoDocument'); - } - if($this->getDbConnection()->enableProfiling){ - $this->profile('extensions.MongoYii.EMongoFile.insert({$document:' . json_encode($document) . '})', 'extensions.MongoYii.EMongoFile.insert'); - } - - if($_id = $this->getCollection()->storeFile($this->getFilename(), $document)){ // The key change - $this->_id = $_id; - $this->afterSave(); - $this->setIsNewRecord(false); - $this->setScenario('update'); - return true; - } - return false; - } - - /** - * Get collection will now return the GridFS object from the driver - * @see EMongoDocument::getCollection() - */ - public function getCollection() - { - return $this->getDbConnection()->getDB()->getGridFS($this->collectionPrefix()); - } - - /** - * Produces a trace message for functions in this class - * @param string $func - */ - public function trace($func) - { - Yii::trace(get_class($this) . '.' . $func.'()', 'extensions.MongoYii.EMongoFile'); - } -} \ No newline at end of file diff --git a/EMongoQueryBuilder.php b/EMongoQueryBuilder.php index 5ef2ac9..e3104a3 100644 --- a/EMongoQueryBuilder.php +++ b/EMongoQueryBuilder.php @@ -18,7 +18,14 @@ class EMongoQueryBuilder */ public $model; + /** + * @var array|EMongoCriteria|MongoCursor|string + */ private $query = ''; + + /** + * @var array + */ private $options = []; /** diff --git a/composer.json b/composer.json index 38ac836..9ec6cc4 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "role": "Developer" }, { - "name": "Virchenko Maksimv", + "name": "Virchenko Maksim", "email": "muslim1992@gmail.com", "role": "Developer" } diff --git a/tests/MongoCursorTest._php b/tests/MongoCursorTest._php deleted file mode 100644 index 1a2bd25..0000000 --- a/tests/MongoCursorTest._php +++ /dev/null @@ -1,85 +0,0 @@ -username = 'sammaye'; - $u->save(); - } - - $c = User::model()->find(); - - $this->assertInstanceOf('EMongoCursor', $c); - $this->assertTrue($c->count() > 0); - - foreach($c as $doc){ - $this->assertTrue($doc instanceof EMongoDocument); - $this->assertEquals('update', $doc->getScenario()); - $this->assertFalse($doc->getIsNewRecord()); - $this->assertInstanceOf('MongoId', $doc->_id); - break; - } - } - - /** - * @covers EMongoCursor::__construct - */ - public function testDirectInstantiation() - { - for($i=0;$i<=4;$i++){ - $u = new User(); - $u->username = 'sammaye'; - $u->save(); - } - - $c = new EMongoCursor('User', array('username' => 'sammaye')); - - $this->assertInstanceOf('EMongoCursor', $c); - $this->assertTrue($c->count() > 0); - } - - /** - * @covers EMongoCriteria - */ - public function testEMongoCriteria() - { - for($i=0;$i<=4;$i++){ - $u = new User(); - $u->username = 'sammaye'; - $u->save(); - } - - $criteria = new EMongoCriteria(array('condition' => array('username' => 'sammaye'), 'limit' => 3, 'skip' => 1)); - $c = new EMongoCursor('User', $criteria); - $this->assertInstanceOf('EMongoCursor', $c); - $this->assertTrue($c->count() > 0); - // see also $this->testSkipLimit() - $this->assertEquals(3, $c->count(true)); - - } - - public function testSkipLimit() - { - for($i=0;$i<=4;$i++){ - $u = new User(); - $u->username = 'sammaye'; - $u->save(); - } - - $c = User::model()->find()->skip(1)->limit(3); - - $this->assertInstanceOf('EMongoCursor', $c); - $this->assertTrue($c->count(true) == 3); - } - - public function tearDown() - { - Yii::app()->mongodb->drop(); - parent::tearDown(); - } -} \ No newline at end of file diff --git a/tests/MongoDocumentTest.php b/tests/MongoDocumentTest.php index 348b3ba..a00db26 100644 --- a/tests/MongoDocumentTest.php +++ b/tests/MongoDocumentTest.php @@ -51,13 +51,13 @@ public function setUpRelationalModel() } // Lets make sure those child docs actually went in - $skills = Skill::model()->find(); + $skills = Skill::model()->findAll(); $this->assertTrue(count($skills) > 0); $c = Interest::model()->count(); $this->assertTrue($c > 0); // Let's build an array of the all the _ids of the child docs - $c = Interest::model()->find(); + $c = Interest::model()->findAll(); $interest_ids = []; foreach ($c as $row) { @@ -175,7 +175,7 @@ public function testDeleting() $r = $c->delete(); $this->assertTrue($r->getDeletedCount() == 1); - $r = User::model()->find(); + $r = User::model()->findAll(); $this->assertFalse(User::model()->count() > 0); } @@ -282,7 +282,7 @@ public function testUpdateAll() $r = User::model()->findOne(['username' => 'gdgdgd']); $this->assertInstanceOf('EMongoDocument', $r); - $r = User::model()->find(['username' => 'gdgdgd']); + $r = User::model()->findAll(['username' => 'gdgdgd']); $this->assertEquals(4, User::model()->count(['username' => 'gdgdgd'])); } @@ -357,7 +357,7 @@ public function testPartialDocuments() $this->assertTrue(isset($p['username'], $p['_id'])); $this->assertFalse(isset($p['addresses'])); - $r2 = User::model()->find([], ['username' => 1]); + $r2 = User::model()->findAll([], ['username' => 1]); foreach ($r2 as $row) { $this->assertTrue($row->getIsPartial()); } diff --git a/tests/MongoFileTest.php b/tests/MongoFileTest.php deleted file mode 100644 index e33bc59..0000000 --- a/tests/MongoFileTest.php +++ /dev/null @@ -1,25 +0,0 @@ -mongodb->drop(); - parent::tearDown(); - } - - public function testAddingFile() - { - // Hmm this is blank until I can figure out how best to unit test an upload - } - - public function testFindingFile() - { - } - - public function testDeletingFile() - { - } -} \ No newline at end of file diff --git a/util/EMongoCache.php b/util/EMongoCache.php index 7c0e74d..2919ac1 100644 --- a/util/EMongoCache.php +++ b/util/EMongoCache.php @@ -223,9 +223,11 @@ protected function addValue($key, $value, $expire) $criteria = ['key' => (string) $key]; $data = [ - 'key' => (string) $key, - 'value' => (string) $value, - 'expire' => (int) $expire, + '$set' => [ + 'key' => (string) $key, + 'value' => (string) $value, + 'expire' => (int) $expire, + ] ]; $options = ['upsert' => true];