From f02da50e50d2890065013d2bfebacfec30797f78 Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Fri, 21 Feb 2014 18:01:35 -0600 Subject: [PATCH] Ensure that tests run even in cases when dev packages are not present --- tests/Rhumsaa/Uuid/Console/TestCase.php | 11 ++-- tests/Rhumsaa/Uuid/Doctrine/UuidTypeTest.php | 16 +++--- tests/Rhumsaa/Uuid/TestCase.php | 56 ++++++++++++++++++++ tests/Rhumsaa/Uuid/UuidTest.php | 29 +++++----- 4 files changed, 83 insertions(+), 29 deletions(-) create mode 100644 tests/Rhumsaa/Uuid/TestCase.php diff --git a/tests/Rhumsaa/Uuid/Console/TestCase.php b/tests/Rhumsaa/Uuid/Console/TestCase.php index 7935050..19f4695 100644 --- a/tests/Rhumsaa/Uuid/Console/TestCase.php +++ b/tests/Rhumsaa/Uuid/Console/TestCase.php @@ -1,14 +1,13 @@ markTestSkipped( - 'symfony/console and moontoast/math are required to run these tests' - ); - } + $this->skipIfNoSymfonyConsole(); + $this->skipIfNoMoontoastMath(); } } diff --git a/tests/Rhumsaa/Uuid/Doctrine/UuidTypeTest.php b/tests/Rhumsaa/Uuid/Doctrine/UuidTypeTest.php index c010deb..00356e2 100644 --- a/tests/Rhumsaa/Uuid/Doctrine/UuidTypeTest.php +++ b/tests/Rhumsaa/Uuid/Doctrine/UuidTypeTest.php @@ -3,26 +3,24 @@ namespace Rhumsaa\Uuid; use Doctrine\DBAL\Types\Type; use Doctrine\Tests\DBAL\Mocks\MockPlatform; -use PHPUnit_Framework_TestCase; +use Rhumsaa\Uuid\TestCase; -class UuidTypeTest extends PHPUnit_Framework_TestCase +class UuidTypeTest extends TestCase { private $platform; private $type; public static function setUpBeforeClass() { - Type::addType('uuid', 'Rhumsaa\Uuid\Doctrine\UuidType'); + if (class_exists('Doctrine\\DBAL\\Types\\Type')) { + Type::addType('uuid', 'Rhumsaa\Uuid\Doctrine\UuidType'); + } } protected function setUp() { - // Skip these tests if run on a 32-bit build of PHP - if (PHP_INT_SIZE == 4) { - $this->markTestSkipped( - 'Running tests on a 32-bit build of PHP; 64-bit build required.' - ); - } + $this->skipIfNoDoctrineDbal(); + $this->skip64BitTest(); $this->platform = $this->getPlatformMock(); $this->platform->expects($this->any()) diff --git a/tests/Rhumsaa/Uuid/TestCase.php b/tests/Rhumsaa/Uuid/TestCase.php new file mode 100644 index 0000000..7427eb7 --- /dev/null +++ b/tests/Rhumsaa/Uuid/TestCase.php @@ -0,0 +1,56 @@ +markTestSkipped( + 'Skipping test that can run only on a 64-bit build of PHP.' + ); + } + } + + protected function skipIfNoMoontoastMath() + { + if (!$this->hasMoontoastMath()) { + $this->markTestSkipped( + 'Skipping test that requires moontoast/math.' + ); + } + } + + protected function skipIfNoSymfonyConsole() + { + if (!$this->hasSymfonyConsole()) { + $this->markTestSkipped( + 'Skipping test that requires symfony/console.' + ); + } + } + + protected function skipIfNoDoctrineDbal() + { + if (!$this->hasDoctrineDbal()) { + $this->markTestSkipped( + 'Skipping test that requires doctrine/dbal.' + ); + } + } + + protected function hasMoontoastMath() + { + return class_exists('Moontoast\\Math\\BigNumber'); + } + + protected function hasSymfonyConsole() + { + return class_exists('Symfony\\Component\\Console\\Application'); + } + + protected function hasDoctrineDbal() + { + return class_exists('Doctrine\\DBAL\\Types\\Type'); + } +} diff --git a/tests/Rhumsaa/Uuid/UuidTest.php b/tests/Rhumsaa/Uuid/UuidTest.php index 5f829ce..19b1017 100644 --- a/tests/Rhumsaa/Uuid/UuidTest.php +++ b/tests/Rhumsaa/Uuid/UuidTest.php @@ -1,7 +1,7 @@ markTestSkipped( - 'Skipping test that can run only on a 64-bit build of PHP.' - ); - } - } - /** * @covers Rhumsaa\Uuid\Uuid::fromString * @covers Rhumsaa\Uuid\Uuid::__construct @@ -160,6 +148,7 @@ class UuidTest extends \PHPUnit_Framework_TestCase */ public function testGetDateTime32Bit() { + $this->skipIfNoMoontoastMath(); Uuid::$force32Bit = true; // Check a recent date @@ -265,6 +254,8 @@ class UuidTest extends \PHPUnit_Framework_TestCase */ public function testGetLeastSignificantBits() { + $this->skipIfNoMoontoastMath(); + $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); $this->assertInstanceOf('Moontoast\Math\BigNumber', $uuid->getLeastSignificantBits()); $this->assertEquals('11178224546741000806', $uuid->getLeastSignificantBits()->getValue()); @@ -295,6 +286,8 @@ class UuidTest extends \PHPUnit_Framework_TestCase */ public function testGetMostSignificantBits() { + $this->skipIfNoMoontoastMath(); + $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); $this->assertInstanceOf('Moontoast\Math\BigNumber', $uuid->getMostSignificantBits()); $this->assertEquals('18406084892941947361', $uuid->getMostSignificantBits()->getValue()); @@ -991,6 +984,7 @@ class UuidTest extends \PHPUnit_Framework_TestCase */ public function testCalculateUuidTimeForce32BitPath() { + $this->skipIfNoMoontoastMath(); Uuid::$force32Bit = true; $timeOfDay = array( @@ -1078,6 +1072,7 @@ class UuidTest extends \PHPUnit_Framework_TestCase */ public function testCalculateUuidTimeUpperLowerBounds64BitThrough32BitPath() { + $this->skipIfNoMoontoastMath(); $this->skip64BitTest(); Uuid::$force32Bit = true; @@ -1121,6 +1116,7 @@ class UuidTest extends \PHPUnit_Framework_TestCase */ public function testCalculateUuidTimeUpperLowerBounds32Bit() { + $this->skipIfNoMoontoastMath(); Uuid::$force32Bit = true; // 2038-01-19T03:14:07+00:00 @@ -1206,6 +1202,7 @@ class UuidTest extends \PHPUnit_Framework_TestCase */ public function test32BitMatch64BitForOneHourPeriod() { + $this->skipIfNoMoontoastMath(); $this->skip64BitTest(); $currentTime = strtotime('2012-12-11T00:00:00+00:00'); @@ -1262,6 +1259,8 @@ class UuidTest extends \PHPUnit_Framework_TestCase */ public function testHasBigNumber() { + $this->skipIfNoMoontoastMath(); + $hasBigNumber = new \ReflectionMethod( 'Rhumsaa\Uuid\Uuid', 'hasBigNumber' ); @@ -1778,7 +1777,9 @@ class UuidTest extends \PHPUnit_Framework_TestCase $this->assertEquals($test['string'], (string) $uuid); $this->assertEquals($test['hex'], $uuid->getHex()); $this->assertEquals(base64_decode($test['bytes']), $uuid->getBytes()); - $this->assertEquals($test['int'], (string) $uuid->getInteger()); + if ($this->hasMoontoastMath()) { + $this->assertEquals($test['int'], (string) $uuid->getInteger()); + } $this->assertEquals($test['fields'], $uuid->getFieldsHex()); $this->assertEquals($test['fields']['time_low'], $uuid->getTimeLowHex()); $this->assertEquals($test['fields']['time_mid'], $uuid->getTimeMidHex());