-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MongoCollection Uniqueness compatibility
- Loading branch information
Showing
9 changed files
with
640 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: User | ||
* Date: 2017-03-09 | ||
* Time: 17:11 | ||
*/ | ||
|
||
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: User | ||
* Date: 2017-03-09 | ||
* Time: 17:07 | ||
*/ | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Helper; | ||
|
||
use Mongo; | ||
use MongoClient; | ||
use Codeception\Actor; | ||
use Phalcon\Mvc\Collection\Manager; | ||
|
||
/** | ||
* Collection Initializer | ||
* | ||
* @package Helper | ||
*/ | ||
trait CollectionTrait | ||
{ | ||
/** | ||
* Executed before each test | ||
*/ | ||
protected function setupMongo() | ||
{ | ||
if (!extension_loaded('mongodb')) { | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
<?php | ||
|
||
namespace Phalcon\Test\Mvc\Collection\Helpers; | ||
|
||
use Phalcon\Validation; | ||
use Phalcon\Validation\Validator\Uniqueness; | ||
use UnitTester; | ||
|
||
trait UniquenessTrait | ||
{ | ||
private function testSingleField(UnitTester $I) | ||
{ | ||
$validation = new Validation(); | ||
$validation->add('type', new Uniqueness()); | ||
$messages = $validation->validate(null, $this->robot); | ||
$I->assertCount(0, $messages); | ||
$I->assertTrue($this->robot->save()); | ||
$messages = $validation->validate(null, $this->robot); | ||
$I->assertCount(1, $messages); | ||
$messages = $validation->validate(null, $this->anotherRobot); | ||
$I->assertCount(0, $messages); | ||
} | ||
|
||
private function testSingleFieldConvert(UnitTester $I) | ||
{ | ||
$validation = new Validation(); | ||
$validation->add( | ||
'type', | ||
new Uniqueness( | ||
[ | ||
'convert' => function (array $values) { | ||
$values['type'] = 'hydraulic'; // mechanical -> hydraulic | ||
return $values; | ||
}, | ||
] | ||
) | ||
); | ||
$messages = $validation->validate(null, $this->robot); | ||
$I->assertCount(0, $messages); | ||
} | ||
|
||
private function testSingleFieldWithNull(UnitTester $I) | ||
{ | ||
$validation = new Validation(); | ||
$validation->add('deleted', new Uniqueness()); | ||
$messages = $validation->validate(null, $this->robot); | ||
$I->assertCount(1, $messages); | ||
$messages = $validation->validate(null, $this->anotherRobot); | ||
$I->assertCount(1, $messages); | ||
$messages = $validation->validate(null, $this->deletedRobot); | ||
$I->assertCount(0, $messages); | ||
} | ||
|
||
private function testMultipleFields(UnitTester $I) | ||
{ | ||
$validation = new Validation(); | ||
$validation->add(['name', 'type'], new Uniqueness()); | ||
$messages = $validation->validate(null, $this->robot); | ||
$I->assertCount(1, $messages); | ||
$messages = $validation->validate(null, $this->anotherRobot); | ||
$I->assertCount(0, $messages); | ||
} | ||
|
||
private function testMultipleFieldsConvert(UnitTester $I) | ||
{ | ||
$validation = new Validation(); | ||
$validation->add( | ||
['name', 'type'], | ||
new Uniqueness( | ||
[ | ||
'convert' => function (array $values) { | ||
$values['type'] = 'hydraulic'; // mechanical -> hydraulic | ||
return $values; | ||
}, | ||
] | ||
) | ||
); | ||
$messages = $validation->validate(null, $this->robot); | ||
$I->assertCount(0, $messages); | ||
} | ||
|
||
private function testMultipleFieldsWithNull(UnitTester $I) | ||
{ | ||
$validation = new Validation(); | ||
$validation->add(['type', 'deleted'], new Uniqueness()); | ||
$messages = $validation->validate(null, $this->robot); | ||
$I->assertCount(1, $messages); | ||
$messages = $validation->validate(null, $this->anotherRobot); | ||
$I->assertCount(0, $messages); | ||
$messages = $validation->validate(null, $this->deletedRobot); | ||
$I->assertCount(0, $messages); | ||
} | ||
|
||
private function testExceptSingleFieldSingleExcept(UnitTester $I) | ||
{ | ||
$validation = new Validation(); | ||
$validation->add( | ||
'year', | ||
new Uniqueness( | ||
[ | ||
'except' => 1972, | ||
] | ||
) | ||
); | ||
$messages = $validation->validate(null, $this->robot); | ||
$I->assertCount(0, $messages); | ||
$I->assertTrue($this->anotherRobot->save()); | ||
$messages = $validation->validate(null, $this->anotherRobot); | ||
$I->assertCount(1, $messages); | ||
} | ||
|
||
private function testExceptSingleFieldMultipleExcept(UnitTester $I) | ||
{ | ||
$validation = new Validation(); | ||
$validation->add( | ||
'year', | ||
new Uniqueness( | ||
[ | ||
'except' => [1972, 1952], | ||
] | ||
) | ||
); | ||
$messages = $validation->validate(null, $this->robot); | ||
$I->assertCount(0, $messages); | ||
$messages = $validation->validate(null, $this->anotherRobot); | ||
$I->assertCount(0, $messages); | ||
} | ||
|
||
private function testExceptMultipleFieldSingleExcept(UnitTester $I) | ||
{ | ||
$validation = new Validation(); | ||
$validation->add( | ||
['type', 'year'], | ||
new Uniqueness( | ||
[ | ||
'except' => [ | ||
'type' => 'mechanical', | ||
'year' => 1972, | ||
], | ||
] | ||
) | ||
); | ||
$messages = $validation->validate(null, $this->robot); | ||
$I->assertCount(0, $messages); | ||
$messages = $validation->validate(null, $this->anotherRobot); | ||
$I->assertCount(1, $messages); | ||
} | ||
|
||
private function testExceptMultipleFieldMultipleExcept(UnitTester $I) | ||
{ | ||
$validation = new Validation(); | ||
$validation->add( | ||
['year', 'type'], | ||
new Uniqueness( | ||
[ | ||
'except' => [ | ||
'year' => [1942, 1972], | ||
'type' => ['hydraulic', 'cyborg'], | ||
], | ||
] | ||
) | ||
); | ||
$messages = $validation->validate(null, $this->robot); | ||
$I->assertCount(0, $messages); | ||
$messages = $validation->validate(null, $this->anotherRobot); | ||
$I->assertCount(0, $messages); | ||
} | ||
|
||
private function testConvertArrayReturnsArray(UnitTester $I) | ||
{ | ||
$validation = new Validation(); | ||
$validation->add( | ||
'type', | ||
new Uniqueness( | ||
[ | ||
'convert' => function (array $values) { | ||
($values); | ||
|
||
return null; | ||
}, | ||
] | ||
) | ||
); | ||
try { | ||
$validation->validate(null, $this->robot); | ||
$I->assertTrue(false); | ||
} catch (\Exception $e) { | ||
$I->assertTrue(true); | ||
} | ||
} | ||
} |
Oops, something went wrong.