comparison core/tests/Drupal/Tests/Component/Utility/RandomTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 $this->setExpectedException(\RuntimeException::class);
66 for ($i = 0; $i <= 100; $i++) {
67 $str = $random->name(1, TRUE);
68 $names[$str] = TRUE;
69 }
70 }
71
72 /**
73 * Tests infinite loop prevention whilst generating random strings.
74 *
75 * @covers ::string
76 */
77 public function testRandomStringException() {
78 // There are fewer than 100 possibilities so an exception should occur to
79 // prevent infinite loops.
80 $random = new Random();
81 $this->setExpectedException(\RuntimeException::class);
82 for ($i = 0; $i <= 100; $i++) {
83 $str = $random->string(1, TRUE);
84 $names[$str] = TRUE;
85 }
86 }
87
88 /**
89 * Tests random name generation if uniqueness is not enforced.
90 *
91 * @covers ::name
92 */
93 public function testRandomNameNonUnique() {
94 // There are fewer than 100 possibilities if we were forcing uniqueness so
95 // exception would occur.
96 $random = new Random();
97 for ($i = 0; $i <= 100; $i++) {
98 $random->name(1);
99 }
100 $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.');
101 }
102
103 /**
104 * Tests random string if uniqueness is not enforced.
105 *
106 * @covers ::string
107 */
108 public function testRandomStringNonUnique() {
109 // There are fewer than 100 possibilities if we were forcing uniqueness so
110 // exception would occur.
111 $random = new Random();
112 for ($i = 0; $i <= 100; $i++) {
113 $random->string(1);
114 }
115 $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.');
116 }
117
118 /**
119 * Tests random object generation to ensure the expected number of properties.
120 *
121 * @covers ::object
122 */
123 public function testRandomObject() {
124 // For values of 0 and 1 \Drupal\Component\Utility\Random::object() will
125 // have different execution paths.
126 $random = new Random();
127 for ($i = 0; $i <= 1; $i++) {
128 $obj = $random->object($i);
129 $this->assertEquals($i, count(get_object_vars($obj)), 'Generated random object has expected number of properties');
130 }
131 }
132
133 /**
134 * Tests random string validation callbacks.
135 *
136 * @covers ::string
137 */
138 public function testRandomStringValidator() {
139 $random = new Random();
140 $this->firstStringGenerated = '';
141 $str = $random->string(1, TRUE, [$this, '_RandomStringValidate']);
142 $this->assertNotEquals($this->firstStringGenerated, $str);
143 }
144
145 /**
146 * Callback for random string validation.
147 *
148 * @see \Drupal\Component\Utility\Random::name()
149 * @see \Drupal\Tests\Component\Utility\RandomTest::testRandomStringValidator()
150 *
151 * @param string $string
152 * The random string to validate.
153 *
154 * @return bool
155 * TRUE if the random string is valid, FALSE if not.
156 */
157 public function _RandomStringValidate($string) {
158 // Return FALSE for the first generated string and any string that is the
159 // same, as the test expects a different string to be returned.
160 if (empty($this->firstStringGenerated) || $string == $this->firstStringGenerated) {
161 $this->firstStringGenerated = $string;
162 return FALSE;
163 }
164 return TRUE;
165 }
166
167 }