Mercurial > hg > cmmr2012-drupal-site
comparison core/tests/Drupal/Tests/Component/Utility/RandomTest.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\Component\Utility; | |
4 | |
5 use Drupal\Component\Utility\Random; | |
6 use PHPUnit\Framework\TestCase; | |
7 | |
8 /** | |
9 * Tests random data generation. | |
10 * | |
11 * @group Utility | |
12 * | |
13 * @coversDefaultClass \Drupal\Component\Utility\Random | |
14 */ | |
15 class RandomTest extends TestCase { | |
16 | |
17 /** | |
18 * The first random string passed to the test callback. | |
19 * | |
20 * @see \Drupal\Tests\Component\Utility\RandomTest::_RandomStringValidate() | |
21 * | |
22 * @var string | |
23 */ | |
24 protected $firstStringGenerated = ''; | |
25 | |
26 /** | |
27 * Tests unique random string generation. | |
28 * | |
29 * @covers ::string | |
30 */ | |
31 public function testRandomStringUniqueness() { | |
32 $strings = []; | |
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; | |
38 } | |
39 } | |
40 | |
41 /** | |
42 * Tests unique random name generation. | |
43 * | |
44 * @covers ::name | |
45 */ | |
46 public function testRandomNamesUniqueness() { | |
47 $names = []; | |
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); | |
52 $names[$str] = TRUE; | |
53 } | |
54 } | |
55 | |
56 /** | |
57 * Tests infinite loop prevention whilst generating random names. | |
58 * | |
59 * @covers ::name | |
60 */ | |
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); | |
67 } | |
68 else { | |
69 $this->setExpectedException(\RuntimeException::class); | |
70 } | |
71 for ($i = 0; $i <= 100; $i++) { | |
72 $str = $random->name(1, TRUE); | |
73 $names[$str] = TRUE; | |
74 } | |
75 } | |
76 | |
77 /** | |
78 * Tests infinite loop prevention whilst generating random strings. | |
79 * | |
80 * @covers ::string | |
81 */ | |
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); | |
88 } | |
89 else { | |
90 $this->setExpectedException(\RuntimeException::class); | |
91 } | |
92 for ($i = 0; $i <= 100; $i++) { | |
93 $str = $random->string(1, TRUE); | |
94 $names[$str] = TRUE; | |
95 } | |
96 } | |
97 | |
98 /** | |
99 * Tests random name generation if uniqueness is not enforced. | |
100 * | |
101 * @covers ::name | |
102 */ | |
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++) { | |
108 $random->name(1); | |
109 } | |
110 $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.'); | |
111 } | |
112 | |
113 /** | |
114 * Tests random string if uniqueness is not enforced. | |
115 * | |
116 * @covers ::string | |
117 */ | |
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++) { | |
123 $random->string(1); | |
124 } | |
125 $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.'); | |
126 } | |
127 | |
128 /** | |
129 * Tests random object generation to ensure the expected number of properties. | |
130 * | |
131 * @covers ::object | |
132 */ | |
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'); | |
140 } | |
141 } | |
142 | |
143 /** | |
144 * Tests random string validation callbacks. | |
145 * | |
146 * @covers ::string | |
147 */ | |
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); | |
153 } | |
154 | |
155 /** | |
156 * Callback for random string validation. | |
157 * | |
158 * @see \Drupal\Component\Utility\Random::name() | |
159 * @see \Drupal\Tests\Component\Utility\RandomTest::testRandomStringValidator() | |
160 * | |
161 * @param string $string | |
162 * The random string to validate. | |
163 * | |
164 * @return bool | |
165 * TRUE if the random string is valid, FALSE if not. | |
166 */ | |
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; | |
172 return FALSE; | |
173 } | |
174 return TRUE; | |
175 } | |
176 | |
177 } |