|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @link http://github.com/zendframework/zend-validator for the canonical source repository |
| 4 | + * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) |
| 5 | + * @license http://framework.zend.com/license/new-bsd New BSD License |
| 6 | + */ |
| 7 | + |
| 8 | +namespace ZendTest\Validator; |
| 9 | + |
| 10 | +use PHPUnit_Framework_TestCase as TestCase; |
| 11 | +use stdClass; |
| 12 | +use Zend\Validator\Uuid; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class UuidTest. |
| 16 | + * |
| 17 | + * Uuid test cases based on https://github.com/beberlei/assert/blob/master/tests/Assert/Tests/AssertTest.php |
| 18 | + */ |
| 19 | +final class UuidTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var Uuid |
| 23 | + */ |
| 24 | + protected $validator; |
| 25 | + |
| 26 | + public function setUp() |
| 27 | + { |
| 28 | + $this->validator = new Uuid(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @param $uuid |
| 33 | + * @dataProvider validUuidProvider |
| 34 | + */ |
| 35 | + public function testValidUuid($uuid) |
| 36 | + { |
| 37 | + $this->assertTrue($this->validator->isValid($uuid)); |
| 38 | + $messages = $this->validator->getMessages(); |
| 39 | + $this->assertCount(0, $messages); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param $uuid |
| 44 | + * @dataProvider invalidUuidProvider |
| 45 | + */ |
| 46 | + public function testInvalidUuid($uuid, $expectedMessageKey) |
| 47 | + { |
| 48 | + $this->assertFalse($this->validator->isValid($uuid)); |
| 49 | + $messages = $this->validator->getMessages(); |
| 50 | + $this->assertCount(1, $messages); |
| 51 | + $this->assertArrayHasKey($expectedMessageKey, $messages); |
| 52 | + $this->assertNotEmpty($messages[$expectedMessageKey]); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @return array |
| 57 | + */ |
| 58 | + public function validUuidProvider() |
| 59 | + { |
| 60 | + return [ |
| 61 | + 'zero-fill' => ['00000000-0000-0000-0000-000000000000'], |
| 62 | + 'version-1' => ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'], |
| 63 | + 'version-2' => ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], |
| 64 | + 'version-3' => ['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'], |
| 65 | + 'version-4' => ['ff6f8cb0-c57d-41e1-9b21-0800200c9a66'], |
| 66 | + 'version-5' => ['ff6f8cb0-c57d-51e1-9b21-0800200c9a66'], |
| 67 | + 'uppercase' => ['FF6F8CB0-C57D-11E1-9B21-0800200C9A66'], |
| 68 | + ]; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return array |
| 73 | + */ |
| 74 | + public function invalidUuidProvider() |
| 75 | + { |
| 76 | + return [ |
| 77 | + 'invalid-characters' => ['zf6f8cb0-c57d-11e1-9b21-0800200c9a66', Uuid::INVALID], |
| 78 | + 'missing-separators' => ['af6f8cb0c57d11e19b210800200c9a66', Uuid::INVALID], |
| 79 | + 'invalid-segment-2' => ['ff6f8cb0-c57da-51e1-9b21-0800200c9a66', Uuid::INVALID], |
| 80 | + 'invalid-segment-1' => ['af6f8cb-c57d-11e1-9b21-0800200c9a66', Uuid::INVALID], |
| 81 | + 'invalid-segement-5' => ['3f6f8cb0-c57d-11e1-9b21-0800200c9a6', Uuid::INVALID], |
| 82 | + 'truncated' => ['3f6f8cb0', Uuid::INVALID], |
| 83 | + 'empty-string' => ['', Uuid::INVALID], |
| 84 | + 'all-numeric' => [123, Uuid::NOT_STRING], |
| 85 | + 'object' => [new stdClass(), Uuid::NOT_STRING], |
| 86 | + ]; |
| 87 | + } |
| 88 | +} |
0 commit comments