You are here

class UuidTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Component/Uuid/UuidTest.php \Drupal\Tests\Component\Uuid\UuidTest

Tests the handling of Universally Unique Identifiers (UUIDs).

@group Uuid

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
    • class \Drupal\Tests\Component\Uuid\UuidTest

Expanded class hierarchy of UuidTest

File

core/tests/Drupal/Tests/Component/Uuid/UuidTest.php, line 22
Contains \Drupal\Tests\Component\Uuid\UuidTest.

Namespace

Drupal\Tests\Component\Uuid
View source
class UuidTest extends UnitTestCase {

  /**
   * Tests generating valid UUIDs.
   *
   * @dataProvider providerUuidInstances
   */
  public function testGenerateUuid(UuidInterface $instance) {
    $this
      ->assertTrue(Uuid::isValid($instance
      ->generate()), sprintf('UUID generation for %s works.', get_class($instance)));
  }

  /**
   * Tests that generated UUIDs are unique.
   *
   * @dataProvider providerUuidInstances
   */
  public function testUuidIsUnique(UuidInterface $instance) {
    $this
      ->assertNotEquals($instance
      ->generate(), $instance
      ->generate(), sprintf('Same UUID was not generated twice with %s.', get_class($instance)));
  }

  /**
   * Dataprovider for UUID instance tests.
   *
   * @return array
   */
  public function providerUuidInstances() {
    $instances = array();
    $instances[][] = new Php();

    // If valid PECL extensions exists add to list.
    if (function_exists('uuid_create') && !function_exists('uuid_make')) {
      $instances[][] = new Pecl();
    }

    // If we are on Windows add the com implementation as well.
    if (function_exists('com_create_guid')) {
      $instances[][] = new Com();
    }
    return $instances;
  }

  /**
   * Tests UUID validation.
   *
   * @param string $uuid
   *   The uuid to check against.
   * @param bool $is_valid
   *   Whether the uuid is valid or not.
   * @param string $message
   *   The message to display on failure.
   *
   * @dataProvider providerTestValidation
   */
  public function testValidation($uuid, $is_valid, $message) {
    $this
      ->assertSame($is_valid, Uuid::isValid($uuid), $message);
  }

  /**
   * Dataprovider for UUID instance tests.
   *
   * @return array
   *  An array of arrays containing
   *   - The Uuid to check against.
   *   - (bool) Whether or not the Uuid is valid.
   *   - Failure message.
   */
  public function providerTestValidation() {
    return array(
      // These valid UUIDs.
      array(
        '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
        TRUE,
        'Basic FQDN UUID did not validate',
      ),
      array(
        '00000000-0000-0000-0000-000000000000',
        TRUE,
        'Minimum UUID did not validate',
      ),
      array(
        'ffffffff-ffff-ffff-ffff-ffffffffffff',
        TRUE,
        'Maximum UUID did not validate',
      ),
      // These are invalid UUIDs.
      array(
        '0ab26e6b-f074-4e44-9da-601205fa0e976',
        FALSE,
        'Invalid format was validated',
      ),
      array(
        '0ab26e6b-f074-4e44-9daf-1205fa0e9761f',
        FALSE,
        'Invalid length was validated',
      ),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 259
UuidTest::providerTestValidation public function Dataprovider for UUID instance tests.
UuidTest::providerUuidInstances public function Dataprovider for UUID instance tests.
UuidTest::testGenerateUuid public function Tests generating valid UUIDs.
UuidTest::testUuidIsUnique public function Tests that generated UUIDs are unique.
UuidTest::testValidation public function Tests UUID validation.