PECL UUID tests

- Added test case to share common stuff
- Added tests for Random type
This commit is contained in:
Jessica Mauerhan
2016-03-23 09:50:49 -04:00
parent 77dd2bc8f0
commit e97b62a643
4 changed files with 141 additions and 22 deletions
@@ -0,0 +1,41 @@
<?php
namespace Ramsey\Uuid\Test\Generator;
use Ramsey\Uuid\Generator\PeclUuidRandomGenerator;
use Ramsey\Uuid\Test\TestCase;
use AspectMock\Test as AspectMock;
class PeclUuidRandomGeneratorTest extends PeclUuidTestCase
{
private $length = 10; //Doesn't matter, it isn't used
/**
* This test is just to check collaboration with the PECL UUID extension - not to check
* the correctness of the methods defined in that extension.
* So we are just checking that the UUID methods are called with the right parameters.
*/
public function testGenerateCreatesUuidUsingPeclUuidMethods()
{
$create = AspectMock::func('Ramsey\Uuid\Generator', 'uuid_create', $this->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);
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace Ramsey\Uuid\Test\Generator;
use Ramsey\Uuid\Test\TestCase;
use AspectMock\Test as AspectMock;
if (!defined('UUID_TYPE_TIME')) {
define('UUID_TYPE_TIME', 1);
}
if (!defined('UUID_TYPE_RANDOM')) {
define('UUID_TYPE_RANDOM', 4);
}
class PeclUuidTestCase extends TestCase
{
protected $uuidString = 'b08c6fff-7dc5-e111-9b21-0800200c9a66';
protected $uuidBinary = '62303863366666662d376463352d653131312d396232312d303830303230306339613636';
public function setUp()
{
$this->skipIfHhvm();
parent::setUp();
}
public function tearDown()
{
AspectMock::clean();
parent::tearDown();
}
}
@@ -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);
}
}
@@ -0,0 +1,64 @@
<?php
namespace Ramsey\Uuid\Test\Generator;
use Ramsey\Uuid\Generator\RandomBytesGenerator;
use Ramsey\Uuid\Test\TestCase;
use AspectMock\Test as AspectMock;
class RandomBytesGeneratorTest extends TestCase
{
public function setUp()
{
$this->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));
}
}