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