3 namespace Drupal\Tests\Component\Utility;
5 use Drupal\Component\Utility\Random;
6 use PHPUnit\Framework\TestCase;
9 * Tests random data generation.
13 * @coversDefaultClass \Drupal\Component\Utility\Random
15 class RandomTest extends TestCase {
18 * The first random string passed to the test callback.
20 * @see \Drupal\Tests\Component\Utility\RandomTest::_RandomStringValidate()
24 protected $firstStringGenerated = '';
27 * Tests unique random string generation.
31 public function testRandomStringUniqueness() {
33 $random = new Random();
34 for ($i = 0; $i <= 50; $i++) {
35 $str = $random->string(1, TRUE);
36 $this->assertFalse(isset($strings[$str]), 'Generated duplicate random string ' . $str);
37 $strings[$str] = TRUE;
42 * Tests unique random name generation.
46 public function testRandomNamesUniqueness() {
48 $random = new Random();
49 for ($i = 0; $i <= 10; $i++) {
50 $str = $random->name(1, TRUE);
51 $this->assertFalse(isset($names[$str]), 'Generated duplicate random name ' . $str);
57 * Tests infinite loop prevention whilst generating random names.
61 public function testRandomNameException() {
62 // There are fewer than 100 possibilities so an exception should occur to
63 // prevent infinite loops.
64 $random = new Random();
65 if (method_exists($this, 'expectException')) {
66 $this->expectException(\RuntimeException::class);
69 $this->setExpectedException(\RuntimeException::class);
71 for ($i = 0; $i <= 100; $i++) {
72 $str = $random->name(1, TRUE);
78 * Tests infinite loop prevention whilst generating random strings.
82 public function testRandomStringException() {
83 // There are fewer than 100 possibilities so an exception should occur to
84 // prevent infinite loops.
85 $random = new Random();
86 if (method_exists($this, 'expectException')) {
87 $this->expectException(\RuntimeException::class);
90 $this->setExpectedException(\RuntimeException::class);
92 for ($i = 0; $i <= 100; $i++) {
93 $str = $random->string(1, TRUE);
99 * Tests random name generation if uniqueness is not enforced.
103 public function testRandomNameNonUnique() {
104 // There are fewer than 100 possibilities if we were forcing uniqueness so
105 // exception would occur.
106 $random = new Random();
107 for ($i = 0; $i <= 100; $i++) {
110 $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.');
114 * Tests random string if uniqueness is not enforced.
118 public function testRandomStringNonUnique() {
119 // There are fewer than 100 possibilities if we were forcing uniqueness so
120 // exception would occur.
121 $random = new Random();
122 for ($i = 0; $i <= 100; $i++) {
125 $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.');
129 * Tests random object generation to ensure the expected number of properties.
133 public function testRandomObject() {
134 // For values of 0 and 1 \Drupal\Component\Utility\Random::object() will
135 // have different execution paths.
136 $random = new Random();
137 for ($i = 0; $i <= 1; $i++) {
138 $obj = $random->object($i);
139 $this->assertEquals($i, count(get_object_vars($obj)), 'Generated random object has expected number of properties');
144 * Tests random string validation callbacks.
148 public function testRandomStringValidator() {
149 $random = new Random();
150 $this->firstStringGenerated = '';
151 $str = $random->string(1, TRUE, [$this, '_RandomStringValidate']);
152 $this->assertNotEquals($this->firstStringGenerated, $str);
156 * Callback for random string validation.
158 * @see \Drupal\Component\Utility\Random::name()
159 * @see \Drupal\Tests\Component\Utility\RandomTest::testRandomStringValidator()
161 * @param string $string
162 * The random string to validate.
165 * TRUE if the random string is valid, FALSE if not.
167 public function _RandomStringValidate($string) {
168 // Return FALSE for the first generated string and any string that is the
169 // same, as the test expects a different string to be returned.
170 if (empty($this->firstStringGenerated) || $string == $this->firstStringGenerated) {
171 $this->firstStringGenerated = $string;