|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Phalcon\Test\Validation\Validator; |
| 4 | + |
| 5 | + |
| 6 | +use MongoDB\BSON\ObjectId; |
| 7 | +use Phalcon\Test\Test\UnitTestCaseTest as Test; |
| 8 | +use Phalcon\Validation; |
| 9 | +use Phalcon\Validation\Validator\MongoId; |
| 10 | + |
| 11 | +/** |
| 12 | + * \Phalcon\Test\Validation\Validator\MongoIdTest |
| 13 | + * Tests for Phalcon\Validation\Validator\MongoId component |
| 14 | + * |
| 15 | + * @copyright (c) 2011-2019 Phalcon Team |
| 16 | + * @link http://www.phalconphp.com |
| 17 | + * @author Wajdi Jurry <[email protected]> |
| 18 | + * @package Phalcon\Validation\Validator |
| 19 | + * @group Validation |
| 20 | + * |
| 21 | + * The contents of this file are subject to the New BSD License that is |
| 22 | + * bundled with this package in the file docs/LICENSE.txt |
| 23 | + * |
| 24 | + * If you did not receive a copy of the license and are unable to obtain it |
| 25 | + * through the world-wide-web, please send an email to [email protected] |
| 26 | + * so that we can send you a copy immediately. |
| 27 | + */ |
| 28 | +class MongoIdTest extends Test |
| 29 | +{ |
| 30 | + private $validator; |
| 31 | + |
| 32 | + public function setUp() |
| 33 | + { |
| 34 | + $this->validator = new Validation(); |
| 35 | + $this->validator->add( |
| 36 | + 'id', |
| 37 | + new MongoId([ |
| 38 | + 'allowEmpty' => true |
| 39 | + ]) |
| 40 | + ); |
| 41 | + return parent::setUp(); // TODO: Change the autogenerated stub |
| 42 | + } |
| 43 | + |
| 44 | + public function testValidMongoIds() |
| 45 | + { |
| 46 | + $stringMongoId = "5d4a0496e4895300110e3272"; |
| 47 | + $objectId = new ObjectId("5d4a0496e4895300110e3272"); |
| 48 | + $emptyId = ''; |
| 49 | + |
| 50 | + $this->assertCount(0, $this->validator->validate(['id' => $stringMongoId])); |
| 51 | + $this->assertCount(0, $this->validator->validate(['id' => $objectId])); |
| 52 | + $this->assertCount(0, $this->validator->validate(['id' => $emptyId])); |
| 53 | + } |
| 54 | + |
| 55 | + public function testInvalidMongoId() |
| 56 | + { |
| 57 | + $invalidMongoId = "12345"; |
| 58 | + |
| 59 | + $this->assertCount(1, $this->validator->validate(['id' => $invalidMongoId])); |
| 60 | + } |
| 61 | +} |
0 commit comments