Skip to content

Commit

Permalink
Add MongoCollection Uniqueness compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurigag committed Mar 14, 2017
1 parent 29e6cb8 commit 65d8907
Show file tree
Hide file tree
Showing 13 changed files with 648 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ env:
- ZEND_DONT_UNLOAD_MODULES=1
- CC="ccache gcc"
- PATH="$PATH:~/bin"
- PHALCON_VERSION="v3.0.4"
- PHALCON_VERSION="3.1.x"

before_install:
- phpenv config-rm xdebug.ini || true
Expand Down
21 changes: 20 additions & 1 deletion Library/Phalcon/Mvc/MongoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public function save()

if (false === $exists) {
$this->_id = $status->getInsertedId();
$this->_dirtyState = self::DIRTY_STATE_PERSISTENT;
}
}

Expand Down Expand Up @@ -225,6 +226,10 @@ protected static function _getResultset($params, CollectionInterface $collection
$base = $collection;
}

if ($base instanceof PhalconCollection) {
$base->setDirtyState(PhalconCollection::DIRTY_STATE_PERSISTENT);
}

$source = $collection->getSource();

if (empty($source)) {
Expand Down Expand Up @@ -388,6 +393,7 @@ public function delete()
$success = true;

$this->fireEvent("afterDelete");
$this->_dirtyState = self::DIRTY_STATE_DETACHED;
}

return $success;
Expand All @@ -407,6 +413,10 @@ protected function _exists($collection)
return false;
}

if (!$this->_dirtyState) {
return true;
}

if (is_object($id)) {
$mongoId = $id;
} else {
Expand All @@ -423,7 +433,15 @@ protected function _exists($collection)
/**
* Perform the count using the function provided by the driver
*/
return $collection->count(["_id"=>$mongoId])>0;
$exists = $collection->count(["_id" => $mongoId]) > 0;

if ($exists) {
$this->_dirtyState = self::DIRTY_STATE_PERSISTENT;
} else {
$this->_dirtyState = self::DIRTY_STATE_TRANSIENT;
}

return $exists;
}

/**
Expand Down Expand Up @@ -505,6 +523,7 @@ public function create()
$result = $collection->insert($data, ['writeConcern' => new WriteConcern(1)]);
if ($result instanceof InsertOneResult && $result->getInsertedId()) {
$success = true;
$this->_dirtyState = self::DIRTY_STATE_PERSISTENT;
$this->_id = $result->getInsertedId();
}

Expand Down
4 changes: 2 additions & 2 deletions Library/Phalcon/Validation/Validator/CardNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Phalcon\Validation;
use Phalcon\Validation\Message;
use Phalcon\Validation\Validator;
use Phalcon\Validation\Exception;
use Phalcon\Validation\Exception as ValidationException;

/**
* Phalcon\Mvc\Model\Validator\CardNumber
Expand Down Expand Up @@ -75,7 +75,7 @@ public function validate(Validation $validation, $attribute)
$result = ($issuer == 4);
break;
default:
throw new Exception('Incorrect type specifier');
throw new ValidationException('Incorrect type specifier');
}

if (false === $result) {
Expand Down
4 changes: 2 additions & 2 deletions Library/Phalcon/Validation/Validator/ConfirmationOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use Phalcon\Validation;
use Phalcon\Validation\Validator;
use Phalcon\Validation\Exception;
use Phalcon\Validation\Exception as ValidationException;

/**
* Validates confirmation of other field value
Expand Down Expand Up @@ -50,7 +50,7 @@ class ConfirmationOf extends Validator
public function validate(Validation $validation, $attribute)
{
if (!$this->hasOption('origField')) {
throw new Exception('Original field must be set');
throw new ValidationException('Original field must be set');
}

$allowEmpty = $this->getOption('allowEmpty');
Expand Down
4 changes: 2 additions & 2 deletions Library/Phalcon/Validation/Validator/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Phalcon\Validation;
use Phalcon\Validation\Message;
use Phalcon\Validation\Validator;
use Phalcon\Validation\Exception;
use Phalcon\Validation\Exception as ValidationException;

/**
* Phalcon\Validation\Validator\Decimal
Expand Down Expand Up @@ -47,7 +47,7 @@ public function validate(Validation $validation, $attribute)
}

if (false === $this->hasOption('places')) {
throw new Exception('A number of decimal places must be set');
throw new ValidationException('A number of decimal places must be set');
}

if ($this->hasOption('digits')) {
Expand Down
4 changes: 2 additions & 2 deletions Library/Phalcon/Validation/Validator/MongoId.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Phalcon\Validation;
use Phalcon\Validation\Validator;
use Phalcon\Validation\Message;
use Phalcon\Validation\Exception;
use Phalcon\Validation\Exception as ValidationException;

/**
* MongoId validator
Expand All @@ -41,7 +41,7 @@ class MongoId extends Validator
public function validate(Validation $validation, $attribute)
{
if (!extension_loaded('mongo')) {
throw new Exception('Mongo extension is not available');
throw new ValidationException('Mongo extension is not available');
}

$value = $validation->getValue($attribute);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"require": {
"php": ">=5.5",
"ext-phalcon": "^3.0",
"ext-phalcon": "^3.1",
"swiftmailer/swiftmailer": "~5.2"
},
"require-dev": {
Expand Down
25 changes: 25 additions & 0 deletions tests/_data/collections/Robots.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Phalcon\Test\Collections;

use Phalcon\Mvc\MongoCollection;

/**
* Robots Collection
*
* @method static Robots[] find(array $parameters = null)
* @method static Robots findFirst(array $parameters = null)
*
* @property \MongoId _id
* @property string name
* @property string type
* @property int year
* @property string datetime
* @property string deleted
* @property string text
*
* @package Phalcon\Test\Collections
*/
class Robots extends MongoCollection
{
}
33 changes: 33 additions & 0 deletions tests/_data/collections/Users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Phalcon\Test\Collections;

use Phalcon\Mvc\MongoCollection;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\ExclusionIn;
use Phalcon\Validation\Validator\InclusionIn;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Regex;
use Phalcon\Validation\Validator\StringLength;
use Phalcon\Validation\Validator\Uniqueness;

class Users extends MongoCollection
{
public function validation()
{
$validator = new Validation();
$validator
->add('created_at', new PresenceOf())

->add('email', new StringLength(['min' => '7', 'max' => '50']))
->add('email', new Email())
->add('email', new Uniqueness())

->add('status', new ExclusionIn(['domain' => ['P', 'I', 'w']]))
->add('status', new InclusionIn(['domain' => ['A', 'y', 'Z']]))
->add('status', new Regex(['pattern' => '/[A-Z]/']));

return $this->validate($validator);
}
}
38 changes: 38 additions & 0 deletions tests/_support/Helper/CollectionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Helper;

use Codeception\Actor;
use Phalcon\Mvc\Collection\Manager;
use Phalcon\Db\Adapter\MongoDB\Client;
use Phalcon\Di;

/**
* Collection Initializer
*
* @package Helper
*/
trait CollectionTrait
{
/**
* Executed before each test
*/
protected function setupMongo(Actor $I)
{
if (!extension_loaded('mongodb')) {
$I->markTestSkipped('mongodb extension not loaded');
}

Di::reset();

$di = new Di();
$di->set('mongo', function() {
$dsn = 'mongodb://' . env('TEST_MONGODB_HOST', '127.0.0.1') . ':' . env('TEST_MONGODB_PORT', 27017);
$mongo = new Client($dsn);

return $mongo->selectDatabase(env('TEST_MONGODB_NAME', 'incubator'));
});

$di->set('collectionManager', Manager::class);
}
}
Loading

0 comments on commit 65d8907

Please sign in to comment.