diff --git a/tests/src/Generator/PeclUuidRandomGeneratorTest.php b/tests/src/Generator/PeclUuidRandomGeneratorTest.php new file mode 100644 index 0000000..6eaec3e --- /dev/null +++ b/tests/src/Generator/PeclUuidRandomGeneratorTest.php @@ -0,0 +1,41 @@ +uuidString); + $parse = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidBinary); + + $generator = new PeclUuidRandomGenerator(); + $generator->generate($this->length); + + $create->verifyInvoked(UUID_TYPE_RANDOM); + $parse->verifyInvoked($this->uuidString); + } + + /** + * This test is for the return type of the generate method + * It ensures that the generate method returns whatever value uuid_parse returns. + */ + public function testGenerateReturnsUuidString() + { + AspectMock::func('Ramsey\Uuid\Generator', 'uuid_create', $this->uuidString); + AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidBinary); + $generator = new PeclUuidRandomGenerator; + $uuid = $generator->generate($this->length); + $this->assertEquals($this->uuidBinary, $uuid); + } +} diff --git a/tests/src/Generator/PeclUuidTestCase.php b/tests/src/Generator/PeclUuidTestCase.php new file mode 100644 index 0000000..07cba88 --- /dev/null +++ b/tests/src/Generator/PeclUuidTestCase.php @@ -0,0 +1,32 @@ +skipIfHhvm(); + parent::setUp(); + } + + public function tearDown() + { + AspectMock::clean(); + parent::tearDown(); + } + +} diff --git a/tests/src/Generator/PeclUuidTimeGeneratorTest.php b/tests/src/Generator/PeclUuidTimeGeneratorTest.php index fa8e781..c945d2a 100644 --- a/tests/src/Generator/PeclUuidTimeGeneratorTest.php +++ b/tests/src/Generator/PeclUuidTimeGeneratorTest.php @@ -5,26 +5,8 @@ use Ramsey\Uuid\Generator\PeclUuidTimeGenerator; use Ramsey\Uuid\Test\TestCase; use AspectMock\Test as AspectMock; -if (!defined('UUID_TYPE_TIME')) { - define('UUID_TYPE_TIME', 1); -} - -class PeclUuidTimeGeneratorTest extends TestCase +class PeclUuidTimeGeneratorTest extends PeclUuidTestCase { - private $uuidString = 'b08c6fff-7dc5-e111-9b21-0800200c9a66'; - private $uuidPack = '62303863366666662d376463352d653131312d396232312d303830303230306339613636'; - - public function setUp() - { - $this->skipIfHhvm(); - parent::setUp(); // TODO: Change the autogenerated stub - } - - public function tearDown() - { - AspectMock::clean(); // remove all registered test doubles - parent::tearDown(); - } /** * This test is just to check collaboration with the PECL UUID extension - not to check @@ -34,7 +16,7 @@ class PeclUuidTimeGeneratorTest extends TestCase public function testGenerateCreatesUuidUsingPeclUuidMethods() { $create = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_create', $this->uuidString); - $parse = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidPack); + $parse = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidBinary); $generator = new PeclUuidTimeGenerator; $generator->generate(); @@ -50,9 +32,9 @@ class PeclUuidTimeGeneratorTest extends TestCase public function testGenerateReturnsUuidString() { AspectMock::func('Ramsey\Uuid\Generator', 'uuid_create', $this->uuidString); - AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidPack); + AspectMock::func('Ramsey\Uuid\Generator', 'uuid_parse', $this->uuidBinary); $generator = new PeclUuidTimeGenerator; $uuid = $generator->generate(); - $this->assertEquals($this->uuidPack, $uuid); + $this->assertEquals($this->uuidBinary, $uuid); } } diff --git a/tests/src/Generator/RandomBytesGeneratorTest.php b/tests/src/Generator/RandomBytesGeneratorTest.php new file mode 100644 index 0000000..bfa760b --- /dev/null +++ b/tests/src/Generator/RandomBytesGeneratorTest.php @@ -0,0 +1,64 @@ +skipIfHhvm(); + parent::setUp(); + } + + public function tearDown() + { + parent::tearDown(); + AspectMock::clean(); + } + + public function lengthAndHexDataProvider() + { + return [ + [6, '4f17dd046fb8'], + [10, '4d25f6fe5327cb04267a'], + [12, '1ea89f83bd49cacfdf119e24'] + ]; + } + + /** + * @dataProvider lengthAndHexDataProvider + * @runInSeparateProcess + * @preserveGlobalState disabled + * @param int $length + * @param string $hex + */ + public function testGenerateUsesOpenSsl($length, $hex) + { + $bytes = hex2bin($hex); + $openSsl = AspectMock::func('Ramsey\Uuid\Generator', 'random_bytes', $bytes); + $generator = new RandomBytesGenerator(); + $generator->generate($length); + + $openSsl->verifyInvokedOnce([$length]); + } + + /** + * @dataProvider lengthAndHexDataProvider + * @runInSeparateProcess + * @preserveGlobalState disabled + * @param int $length + * @param string $hex + */ + public function testGenerateReturnsRandomBytes($length, $hex) + { + $bytes = hex2bin($hex); + AspectMock::func('Ramsey\Uuid\Generator', 'random_bytes', $bytes); + $generator = new RandomBytesGenerator(); + $this->assertEquals($bytes, $generator->generate($length)); + } + +}