Add uuid console application and tests for generating UUIDs from CLI

This commit is contained in:
Ben Ramsey
2013-12-27 08:42:55 -06:00
parent 4f84d6e573
commit 7bebd01e31
12 changed files with 1054 additions and 2 deletions
Executable
+31
View File
@@ -0,0 +1,31 @@
#!/usr/bin/env php
<?php
/**
* This file is part of the Rhumsaa\Uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
date_default_timezone_set('UTC');
if (PHP_SAPI !== 'cli') {
echo 'Warning: uuid should be invoked via the CLI version of PHP, not the ' . PHP_SAPI . ' SAPI' . PHP_EOL;
}
if (!file_exists(__DIR__ . '/../vendor/autoload.php')) {
echo 'You must set up the project dependencies, run the following commands:' . PHP_EOL .
'curl -sS https://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL;
exit(1);
}
require __DIR__ . '/../vendor/autoload.php';
use Rhumsaa\Uuid\Console\Application;
$app = new Application();
$app->run();
+4 -2
View File
@@ -20,13 +20,15 @@
"source": "https://github.com/ramsey/uuid"
},
"require": {
"php": ">=5.3.3"
"php": ">=5.3.3",
"symfony/console": "~2.4.0"
},
"require-dev": {
"doctrine/dbal": ">=2.3",
"moontoast/math": "1.1.0",
"phpunit/phpunit": "~3.7"
},
"bin": ["bin/uuid"],
"suggest": {
"doctrine/dbal": "Allow the use of a UUID as doctrine field type.",
"moontoast/math": "Provides support for large integers."
@@ -36,7 +38,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.5.x-dev"
"dev-master": "2.6.x-dev"
}
}
}
+49
View File
@@ -0,0 +1,49 @@
<?php
/**
* This file is part of the Rhumsaa\Uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Rhumsaa\Uuid\Console;
use Rhumsaa\Uuid\Console\Command;
use Rhumsaa\Uuid\Console\Util;
use Rhumsaa\Uuid\Uuid;
use Symfony\Component\Console\Application as BaseApplication;
/**
* The console application that handles CLI commands
*/
class Application extends BaseApplication
{
/**
* Constructor
*/
public function __construct()
{
if (function_exists('date_default_timezone_set') && function_exists('date_default_timezone_get')) {
date_default_timezone_set(@date_default_timezone_get());
}
Util\ErrorHandler::register();
parent::__construct('uuid', Uuid::VERSION);
}
/**
* Initializes all the composer commands we have
*
* @return array The array of Command classes
*/
protected function getDefaultCommands()
{
$commands = parent::getDefaultCommands();
$commands[] = new Command\GenerateCommand();
return $commands;
}
}
@@ -0,0 +1,164 @@
<?php
/**
* This file is part of the Rhumsaa\Uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Rhumsaa\Uuid\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Rhumsaa\Uuid\Console\Exception;
use Rhumsaa\Uuid\Uuid;
/**
* Provides the console command to generate UUIDs
*/
class GenerateCommand extends Command
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this->setName('generate')
->setDescription('Generate a UUID')
->addArgument(
'version',
InputArgument::OPTIONAL,
'The UUID version to generate. Supported are version "1", "3", '
. '"4" and "5".',
1
)
->addArgument(
'namespace',
InputArgument::OPTIONAL,
'For version 3 or 5 UUIDs, the namespace to create a UUID for. '
. 'May be either a UUID in string representation or an identifier '
. 'for internally pre-defined namespace UUIDs (currently known '
. 'are "ns:DNS", "ns:URL", "ns:OID", and "ns:X500").'
)
->addArgument(
'name',
InputArgument::OPTIONAL,
'For version 3 or 5 UUIDs, the name to create a UUID for. '
. 'The name is a string of arbitrary length.'
)
->addOption(
'count',
'c',
InputOption::VALUE_REQUIRED,
'Generate count UUIDs instead of just a single one.',
1
);
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$uuids = array();
$count = filter_var(
$input->getOption('count'),
FILTER_VALIDATE_INT,
array(
'default' => 1,
'min_range' => 1,
)
);
for ($i = 0; $i < $count; $i++) {
$uuids[] = $this->createUuid(
$input->getArgument('version'),
$input->getArgument('namespace'),
$input->getArgument('name')
);
}
foreach ($uuids as $uuid) {
$output->writeln((string) $uuid);
}
}
/**
* Creates the requested UUID
*
* @param int $version
* @param string $namespace
* @param string $name
* @return Uuid
*/
protected function createUuid($version, $namespace = null, $name = null)
{
switch ((int) $version) {
case 1:
$uuid = Uuid::uuid1();
break;
case 4:
$uuid = Uuid::uuid4();
break;
case 3:
case 5:
$ns = $this->validateNamespace($namespace);
if (empty($name)) {
throw new Exception('The name argument is required for version 3 or 5 UUIDs');
}
if ($version == 3) {
$uuid = Uuid::uuid3($ns, $name);
} else {
$uuid = Uuid::uuid5($ns, $name);
}
break;
default:
throw new Exception('Invalid UUID version. Supported are version "1", "3", "4", and "5".');
}
return $uuid;
}
/**
* Validates the namespace argument
*
* @param string $namespace
* @return string The namespace, if valid
* @throws Exception
*/
protected function validateNamespace($namespace)
{
switch ($namespace) {
case 'ns:DNS':
return Uuid::NAMESPACE_DNS;
break;
case 'ns:URL':
return Uuid::NAMESPACE_URL;
break;
case 'ns:OID':
return Uuid::NAMESPACE_OID;
break;
case 'ns:X500':
return Uuid::NAMESPACE_X500;
break;
}
if (Uuid::isValid($namespace)) {
return $namespace;
}
throw new Exception('Invalid namespace. '
. 'May be either a UUID in string representation or an identifier '
. 'for internally pre-defined namespace UUIDs (currently known '
. 'are "ns:DNS", "ns:URL", "ns:OID", and "ns:X500").');
}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the Rhumsaa\Uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Rhumsaa\Uuid\Console;
/**
* Console exception
*/
class Exception extends \RuntimeException
{
}
@@ -0,0 +1,49 @@
<?php
/**
* This file is part of the Rhumsaa\Uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Rhumsaa\Uuid\Console\Util;
/**
* Convert PHP errors into exceptions
*/
class ErrorHandler
{
/**
* Error handler
*
* @param int $level Level of the error raised
* @param string $message Error message
* @param string $file Filename that the error was raised in
* @param int $line Line number the error was raised at
*
* @static
* @throws \ErrorException
*/
public static function handle($level, $message, $file, $line)
{
// respect error_reporting being disabled
if (!error_reporting()) {
return;
}
throw new \ErrorException($message, 0, $level, $file, $line);
}
/**
* Register error handler
*
* @static
*/
public static function register()
{
set_error_handler(array(__CLASS__, 'handle'));
}
}
+5
View File
@@ -85,6 +85,11 @@ final class Uuid
*/
const RESERVED_FUTURE = 7;
/**
* Version of the Rhumsaa\Uuid package
*/
const VERSION = '2.6.0-dev';
/**
* For testing, 64-bit system override; if true, treat the system as 32-bit
*
@@ -0,0 +1,36 @@
<?php
namespace Rhumsaa\Uuid\Console;
class ApplicationTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Rhumsaa\Uuid\Console\Application::__construct
*/
public function testConstructor()
{
$app = new Application();
// Reset the error handler, since the constructor sets it
restore_error_handler();
$this->assertInstanceOf('Rhumsaa\\Uuid\\Console\\Application', $app);
$this->assertEquals('uuid', $app->getName());
$this->assertEquals(\Rhumsaa\Uuid\Uuid::VERSION, $app->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Console\Application::getDefaultCommands
*/
public function testGetDefaultCommands()
{
$app = new Application();
// Reset the error handler, since the constructor sets it
restore_error_handler();
$method = new \ReflectionMethod('Rhumsaa\\Uuid\\Console\\Application', 'getDefaultCommands');
$method->setAccessible(true);
$this->assertInternalType('array', $method->invoke($app));
}
}
@@ -0,0 +1,635 @@
<?php
namespace Rhumsaa\Uuid\Console\Command;
use Rhumsaa\Uuid\Console\Util\TestOutput;
use Rhumsaa\Uuid\Uuid;
use Symfony\Component\Console\Input\StringInput;
class GenerateCommandTest extends \PHPUnit_Framework_TestCase
{
const FOO_NS = 'bbd8a651-6f00-11e3-8ad8-28cfe91e4895';
protected $execute;
protected function setUp()
{
$this->execute = new \ReflectionMethod('Rhumsaa\\Uuid\\Console\\Command\\GenerateCommand', 'execute');
$this->execute->setAccessible(true);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::configure
*/
public function testConfigure()
{
$generate = new GenerateCommand();
$this->assertEquals('generate', $generate->getName());
$this->assertEquals('Generate a UUID', $generate->getDescription());
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
*/
public function testExecuteForUuidDefault()
{
$generate = new GenerateCommand();
$input = new StringInput(
'',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(1, Uuid::fromString($output->messages[0])->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
*/
public function testExecuteForUuidDefaultWithCount()
{
$generate = new GenerateCommand();
//
// Test using the "-c" option
//
$input1 = new StringInput(
'-c 9',
$generate->getDefinition()
);
$output1 = new TestOutput();
$this->execute->invoke($generate, $input1, $output1);
$this->assertCount(9, $output1->messages);
foreach ($output1->messages as $uuid) {
$this->assertTrue(Uuid::isValid($uuid));
$this->assertEquals(1, Uuid::fromString($uuid)->getVersion());
}
//
// Test using the "--count" option
//
$input2 = new StringInput(
'--count=12',
$generate->getDefinition()
);
$output2 = new TestOutput();
$this->execute->invoke($generate, $input2, $output2);
$this->assertCount(12, $output2->messages);
foreach ($output2->messages as $uuid) {
$this->assertTrue(Uuid::isValid($uuid));
$this->assertEquals(1, Uuid::fromString($uuid)->getVersion());
}
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
*/
public function testExecuteForUuidSpecifyVersion1()
{
$generate = new GenerateCommand();
$input = new StringInput(
'1',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(1, Uuid::fromString($output->messages[0])->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
*/
public function testExecuteForUuidSpecifyVersion1WithCount()
{
$generate = new GenerateCommand();
//
// Test using the "-c" option
//
$input1 = new StringInput(
'1 -c 3',
$generate->getDefinition()
);
$output1 = new TestOutput();
$this->execute->invoke($generate, $input1, $output1);
$this->assertCount(3, $output1->messages);
foreach ($output1->messages as $uuid) {
$this->assertTrue(Uuid::isValid($uuid));
$this->assertEquals(1, Uuid::fromString($uuid)->getVersion());
}
//
// Test using the "--count" option
//
$input2 = new StringInput(
'1 --count=8',
$generate->getDefinition()
);
$output2 = new TestOutput();
$this->execute->invoke($generate, $input2, $output2);
$this->assertCount(8, $output2->messages);
foreach ($output2->messages as $uuid) {
$this->assertTrue(Uuid::isValid($uuid));
$this->assertEquals(1, Uuid::fromString($uuid)->getVersion());
}
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
*/
public function testExecuteForUuidSpecifyVersion4()
{
$generate = new GenerateCommand();
$input = new StringInput(
'4',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(4, Uuid::fromString($output->messages[0])->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
*/
public function testExecuteForUuidSpecifyVersion4WithCount()
{
$generate = new GenerateCommand();
//
// Test using the "-c" option
//
$input1 = new StringInput(
'4 -c 3',
$generate->getDefinition()
);
$output1 = new TestOutput();
$this->execute->invoke($generate, $input1, $output1);
$this->assertCount(3, $output1->messages);
foreach ($output1->messages as $uuid) {
$this->assertTrue(Uuid::isValid($uuid));
$this->assertEquals(4, Uuid::fromString($uuid)->getVersion());
}
//
// Test using the "--count" option
//
$input2 = new StringInput(
'4 --count=8',
$generate->getDefinition()
);
$output2 = new TestOutput();
$this->execute->invoke($generate, $input2, $output2);
$this->assertCount(8, $output2->messages);
foreach ($output2->messages as $uuid) {
$this->assertTrue(Uuid::isValid($uuid));
$this->assertEquals(4, Uuid::fromString($uuid)->getVersion());
}
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
*/
public function testExecuteForUuidSpecifyVersion3WithDnsNs()
{
$generate = new GenerateCommand();
$input = new StringInput(
'3 ns:DNS "python.org"',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(3, Uuid::fromString($output->messages[0])->getVersion());
$this->assertEquals('6fa459ea-ee8a-3ca4-894e-db77e160355e', $output->messages[0]);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
*/
public function testExecuteForUuidSpecifyVersion3WithUrlNs()
{
$generate = new GenerateCommand();
$input = new StringInput(
'3 ns:URL "http://python.org/"',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(3, Uuid::fromString($output->messages[0])->getVersion());
$this->assertEquals('9fe8e8c4-aaa8-32a9-a55c-4535a88b748d', $output->messages[0]);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
*/
public function testExecuteForUuidSpecifyVersion3WithOidNs()
{
$generate = new GenerateCommand();
$input = new StringInput(
'3 ns:OID "1.3.6.1"',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(3, Uuid::fromString($output->messages[0])->getVersion());
$this->assertEquals('dd1a1cef-13d5-368a-ad82-eca71acd4cd1', $output->messages[0]);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
*/
public function testExecuteForUuidSpecifyVersion3WithX500Ns()
{
$generate = new GenerateCommand();
$input = new StringInput(
'3 ns:X500 "c=ca"',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(3, Uuid::fromString($output->messages[0])->getVersion());
$this->assertEquals('658d3002-db6b-3040-a1d1-8ddd7d189a4d', $output->messages[0]);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
*/
public function testExecuteForUuidSpecifyVersion3WithOtherNs()
{
$generate = new GenerateCommand();
$input = new StringInput(
'3 bbd8a651-6f00-11e3-8ad8-28cfe91e4895 foobar',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(3, Uuid::fromString($output->messages[0])->getVersion());
$this->assertEquals('0707b2c0-1f0f-3b2b-9a90-371396a90a86', $output->messages[0]);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
* @expectedException Rhumsaa\Uuid\Console\Exception
* @expectedExceptionMessage May be either a UUID in string representation or an identifier
*/
public function testExecuteForUuidSpecifyVersion3WithInvalidNs()
{
$generate = new GenerateCommand();
$input = new StringInput(
'3 foo foobar',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
*/
public function testExecuteForUuidSpecifyVersion3WithCount()
{
$generate = new GenerateCommand();
$input = new StringInput(
'3 ns:DNS "python.org" -c 21',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(21, $output->messages);
foreach ($output->messages as $uuid) {
$this->assertTrue(Uuid::isValid($uuid));
$this->assertEquals(3, Uuid::fromString($uuid)->getVersion());
$this->assertEquals('6fa459ea-ee8a-3ca4-894e-db77e160355e', $uuid);
}
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
* @expectedException Rhumsaa\Uuid\Console\Exception
* @expectedExceptionMessage The name argument is required for version 3 or 5 UUIDs
*/
public function testExecuteForUuidSpecifyVersion3WithoutName()
{
$generate = new GenerateCommand();
$input = new StringInput(
'3 ns:DNS',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
*/
public function testExecuteForUuidSpecifyVersion5WithDnsNs()
{
$generate = new GenerateCommand();
$input = new StringInput(
'5 ns:DNS "python.org"',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(5, Uuid::fromString($output->messages[0])->getVersion());
$this->assertEquals('886313e1-3b8a-5372-9b90-0c9aee199e5d', $output->messages[0]);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
*/
public function testExecuteForUuidSpecifyVersion5WithUrlNs()
{
$generate = new GenerateCommand();
$input = new StringInput(
'5 ns:URL "http://python.org/"',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(5, Uuid::fromString($output->messages[0])->getVersion());
$this->assertEquals('4c565f0d-3f5a-5890-b41b-20cf47701c5e', $output->messages[0]);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
*/
public function testExecuteForUuidSpecifyVersion5WithOidNs()
{
$generate = new GenerateCommand();
$input = new StringInput(
'5 ns:OID "1.3.6.1"',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(5, Uuid::fromString($output->messages[0])->getVersion());
$this->assertEquals('1447fa61-5277-5fef-a9b3-fbc6e44f4af3', $output->messages[0]);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
*/
public function testExecuteForUuidSpecifyVersion5WithX500Ns()
{
$generate = new GenerateCommand();
$input = new StringInput(
'5 ns:X500 "c=ca"',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(5, Uuid::fromString($output->messages[0])->getVersion());
$this->assertEquals('cc957dd1-a972-5349-98cd-874190002798', $output->messages[0]);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
*/
public function testExecuteForUuidSpecifyVersion5WithOtherNs()
{
$generate = new GenerateCommand();
$input = new StringInput(
'5 bbd8a651-6f00-11e3-8ad8-28cfe91e4895 foobar',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(1, $output->messages);
$this->assertTrue(Uuid::isValid($output->messages[0]));
$this->assertEquals(5, Uuid::fromString($output->messages[0])->getVersion());
$this->assertEquals('385c280b-1d07-5d6b-932b-ca7a11d2e7e5', $output->messages[0]);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
* @expectedException Rhumsaa\Uuid\Console\Exception
* @expectedExceptionMessage May be either a UUID in string representation or an identifier
*/
public function testExecuteForUuidSpecifyVersion5WithInvalidNs()
{
$generate = new GenerateCommand();
$input = new StringInput(
'5 foo foobar',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
*/
public function testExecuteForUuidSpecifyVersion5WithCount()
{
$generate = new GenerateCommand();
$input = new StringInput(
'5 ns:DNS "python.org" -c 21',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
$this->assertCount(21, $output->messages);
foreach ($output->messages as $uuid) {
$this->assertTrue(Uuid::isValid($uuid));
$this->assertEquals(5, Uuid::fromString($uuid)->getVersion());
$this->assertEquals('886313e1-3b8a-5372-9b90-0c9aee199e5d', $uuid);
}
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::validateNamespace
* @expectedException Rhumsaa\Uuid\Console\Exception
* @expectedExceptionMessage The name argument is required for version 3 or 5 UUIDs
*/
public function testExecuteForUuidSpecifyVersion5WithoutName()
{
$generate = new GenerateCommand();
$input = new StringInput(
'5 ns:DNS',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
}
/**
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::execute
* @covers Rhumsaa\Uuid\Console\Command\GenerateCommand::createUuid
* @expectedException Rhumsaa\Uuid\Console\Exception
* @expectedExceptionMessage Invalid UUID version. Supported are version "1", "3", "4", and "5".
*/
public function testExecuteForUuidSpecifyInvalidVersion()
{
$generate = new GenerateCommand();
$input = new StringInput(
'6',
$generate->getDefinition()
);
$output = new TestOutput();
$this->execute->invoke($generate, $input, $output);
}
}
@@ -0,0 +1,47 @@
<?php
namespace Rhumsaa\Uuid\Console\Util;
class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Rhumsaa\Uuid\Console\Util\ErrorHandler::register
*/
public function testRegister()
{
$expected = array (
'Rhumsaa\\Uuid\Console\\Util\\ErrorHandler',
'handle',
);
$originalHandler = set_error_handler(function () {});
ErrorHandler::register();
$testHandler = set_error_handler(function () {});
// Set handler back to original
set_error_handler($originalHandler);
$this->assertEquals($expected, $testHandler);
}
/**
* @covers Rhumsaa\Uuid\Console\Util\ErrorHandler::handle
* @expectedException ErrorException
* @expectedExceptionMessage Test exception
*/
public function testHandle()
{
error_reporting(E_ALL);
ErrorHandler::handle(1, 'Test exception', __FILE__, __LINE__);
}
/**
* @covers Rhumsaa\Uuid\Console\Util\ErrorHandler::handle
*/
public function testHandleNoException()
{
error_reporting(0);
$this->assertEmpty(ErrorHandler::handle(1, 'Test exception', __FILE__, __LINE__));
}
}
@@ -0,0 +1,14 @@
<?php
namespace Rhumsaa\Uuid\Console\Util;
use Symfony\Component\Console\Output\Output;
class TestOutput extends Output
{
public $messages = array();
protected function doWrite($message, $newline)
{
$this->messages[] = $message;
}
}
+1
View File
@@ -12,3 +12,4 @@ if (!file_exists(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'composer.lock')) {
$loader = include realpath(dirname(__FILE__) . '/../vendor/autoload.php');
$loader->add("Doctrine\Tests\DBAL", __DIR__."/../vendor/doctrine/dbal/tests");
$loader->add('Rhumsaa\Uuid', __DIR__);