Chris@0: string(1, TRUE); Chris@0: $this->assertFalse(isset($strings[$str]), 'Generated duplicate random string ' . $str); Chris@0: $strings[$str] = TRUE; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests unique random name generation. Chris@0: * Chris@0: * @covers ::name Chris@0: */ Chris@0: public function testRandomNamesUniqueness() { Chris@0: $names = []; Chris@0: $random = new Random(); Chris@0: for ($i = 0; $i <= 10; $i++) { Chris@0: $str = $random->name(1, TRUE); Chris@0: $this->assertFalse(isset($names[$str]), 'Generated duplicate random name ' . $str); Chris@0: $names[$str] = TRUE; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests infinite loop prevention whilst generating random names. Chris@0: * Chris@0: * @covers ::name Chris@0: */ Chris@0: public function testRandomNameException() { Chris@0: // There are fewer than 100 possibilities so an exception should occur to Chris@0: // prevent infinite loops. Chris@0: $random = new Random(); Chris@14: if (method_exists($this, 'expectException')) { Chris@14: $this->expectException(\RuntimeException::class); Chris@14: } Chris@14: else { Chris@14: $this->setExpectedException(\RuntimeException::class); Chris@14: } Chris@0: for ($i = 0; $i <= 100; $i++) { Chris@0: $str = $random->name(1, TRUE); Chris@0: $names[$str] = TRUE; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests infinite loop prevention whilst generating random strings. Chris@0: * Chris@0: * @covers ::string Chris@0: */ Chris@0: public function testRandomStringException() { Chris@0: // There are fewer than 100 possibilities so an exception should occur to Chris@0: // prevent infinite loops. Chris@0: $random = new Random(); Chris@14: if (method_exists($this, 'expectException')) { Chris@14: $this->expectException(\RuntimeException::class); Chris@14: } Chris@14: else { Chris@14: $this->setExpectedException(\RuntimeException::class); Chris@14: } Chris@0: for ($i = 0; $i <= 100; $i++) { Chris@0: $str = $random->string(1, TRUE); Chris@0: $names[$str] = TRUE; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests random name generation if uniqueness is not enforced. Chris@0: * Chris@0: * @covers ::name Chris@0: */ Chris@0: public function testRandomNameNonUnique() { Chris@0: // There are fewer than 100 possibilities if we were forcing uniqueness so Chris@0: // exception would occur. Chris@0: $random = new Random(); Chris@0: for ($i = 0; $i <= 100; $i++) { Chris@0: $random->name(1); Chris@0: } Chris@0: $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests random string if uniqueness is not enforced. Chris@0: * Chris@0: * @covers ::string Chris@0: */ Chris@0: public function testRandomStringNonUnique() { Chris@0: // There are fewer than 100 possibilities if we were forcing uniqueness so Chris@0: // exception would occur. Chris@0: $random = new Random(); Chris@0: for ($i = 0; $i <= 100; $i++) { Chris@0: $random->string(1); Chris@0: } Chris@0: $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests random object generation to ensure the expected number of properties. Chris@0: * Chris@0: * @covers ::object Chris@0: */ Chris@0: public function testRandomObject() { Chris@0: // For values of 0 and 1 \Drupal\Component\Utility\Random::object() will Chris@0: // have different execution paths. Chris@0: $random = new Random(); Chris@0: for ($i = 0; $i <= 1; $i++) { Chris@0: $obj = $random->object($i); Chris@0: $this->assertEquals($i, count(get_object_vars($obj)), 'Generated random object has expected number of properties'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests random string validation callbacks. Chris@0: * Chris@0: * @covers ::string Chris@0: */ Chris@0: public function testRandomStringValidator() { Chris@0: $random = new Random(); Chris@0: $this->firstStringGenerated = ''; Chris@0: $str = $random->string(1, TRUE, [$this, '_RandomStringValidate']); Chris@0: $this->assertNotEquals($this->firstStringGenerated, $str); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Callback for random string validation. Chris@0: * Chris@0: * @see \Drupal\Component\Utility\Random::name() Chris@0: * @see \Drupal\Tests\Component\Utility\RandomTest::testRandomStringValidator() Chris@0: * Chris@0: * @param string $string Chris@0: * The random string to validate. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the random string is valid, FALSE if not. Chris@0: */ Chris@0: public function _RandomStringValidate($string) { Chris@0: // Return FALSE for the first generated string and any string that is the Chris@0: // same, as the test expects a different string to be returned. Chris@0: if (empty($this->firstStringGenerated) || $string == $this->firstStringGenerated) { Chris@0: $this->firstStringGenerated = $string; Chris@0: return FALSE; Chris@0: } Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: }