annotate core/tests/Drupal/Tests/Component/Utility/RandomTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\Component\Utility;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Random;
Chris@0 6 use PHPUnit\Framework\TestCase;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests random data generation.
Chris@0 10 *
Chris@0 11 * @group Utility
Chris@0 12 *
Chris@0 13 * @coversDefaultClass \Drupal\Component\Utility\Random
Chris@0 14 */
Chris@0 15 class RandomTest extends TestCase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * The first random string passed to the test callback.
Chris@0 19 *
Chris@0 20 * @see \Drupal\Tests\Component\Utility\RandomTest::_RandomStringValidate()
Chris@0 21 *
Chris@0 22 * @var string
Chris@0 23 */
Chris@0 24 protected $firstStringGenerated = '';
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Tests unique random string generation.
Chris@0 28 *
Chris@0 29 * @covers ::string
Chris@0 30 */
Chris@0 31 public function testRandomStringUniqueness() {
Chris@0 32 $strings = [];
Chris@0 33 $random = new Random();
Chris@0 34 for ($i = 0; $i <= 50; $i++) {
Chris@0 35 $str = $random->string(1, TRUE);
Chris@0 36 $this->assertFalse(isset($strings[$str]), 'Generated duplicate random string ' . $str);
Chris@0 37 $strings[$str] = TRUE;
Chris@0 38 }
Chris@0 39 }
Chris@0 40
Chris@0 41 /**
Chris@0 42 * Tests unique random name generation.
Chris@0 43 *
Chris@0 44 * @covers ::name
Chris@0 45 */
Chris@0 46 public function testRandomNamesUniqueness() {
Chris@0 47 $names = [];
Chris@0 48 $random = new Random();
Chris@0 49 for ($i = 0; $i <= 10; $i++) {
Chris@0 50 $str = $random->name(1, TRUE);
Chris@0 51 $this->assertFalse(isset($names[$str]), 'Generated duplicate random name ' . $str);
Chris@0 52 $names[$str] = TRUE;
Chris@0 53 }
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Tests infinite loop prevention whilst generating random names.
Chris@0 58 *
Chris@0 59 * @covers ::name
Chris@0 60 */
Chris@0 61 public function testRandomNameException() {
Chris@0 62 // There are fewer than 100 possibilities so an exception should occur to
Chris@0 63 // prevent infinite loops.
Chris@0 64 $random = new Random();
Chris@14 65 if (method_exists($this, 'expectException')) {
Chris@14 66 $this->expectException(\RuntimeException::class);
Chris@14 67 }
Chris@14 68 else {
Chris@14 69 $this->setExpectedException(\RuntimeException::class);
Chris@14 70 }
Chris@0 71 for ($i = 0; $i <= 100; $i++) {
Chris@0 72 $str = $random->name(1, TRUE);
Chris@0 73 $names[$str] = TRUE;
Chris@0 74 }
Chris@0 75 }
Chris@0 76
Chris@0 77 /**
Chris@0 78 * Tests infinite loop prevention whilst generating random strings.
Chris@0 79 *
Chris@0 80 * @covers ::string
Chris@0 81 */
Chris@0 82 public function testRandomStringException() {
Chris@0 83 // There are fewer than 100 possibilities so an exception should occur to
Chris@0 84 // prevent infinite loops.
Chris@0 85 $random = new Random();
Chris@14 86 if (method_exists($this, 'expectException')) {
Chris@14 87 $this->expectException(\RuntimeException::class);
Chris@14 88 }
Chris@14 89 else {
Chris@14 90 $this->setExpectedException(\RuntimeException::class);
Chris@14 91 }
Chris@0 92 for ($i = 0; $i <= 100; $i++) {
Chris@0 93 $str = $random->string(1, TRUE);
Chris@0 94 $names[$str] = TRUE;
Chris@0 95 }
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Tests random name generation if uniqueness is not enforced.
Chris@0 100 *
Chris@0 101 * @covers ::name
Chris@0 102 */
Chris@0 103 public function testRandomNameNonUnique() {
Chris@0 104 // There are fewer than 100 possibilities if we were forcing uniqueness so
Chris@0 105 // exception would occur.
Chris@0 106 $random = new Random();
Chris@0 107 for ($i = 0; $i <= 100; $i++) {
Chris@0 108 $random->name(1);
Chris@0 109 }
Chris@0 110 $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.');
Chris@0 111 }
Chris@0 112
Chris@0 113 /**
Chris@0 114 * Tests random string if uniqueness is not enforced.
Chris@0 115 *
Chris@0 116 * @covers ::string
Chris@0 117 */
Chris@0 118 public function testRandomStringNonUnique() {
Chris@0 119 // There are fewer than 100 possibilities if we were forcing uniqueness so
Chris@0 120 // exception would occur.
Chris@0 121 $random = new Random();
Chris@0 122 for ($i = 0; $i <= 100; $i++) {
Chris@0 123 $random->string(1);
Chris@0 124 }
Chris@0 125 $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.');
Chris@0 126 }
Chris@0 127
Chris@0 128 /**
Chris@0 129 * Tests random object generation to ensure the expected number of properties.
Chris@0 130 *
Chris@0 131 * @covers ::object
Chris@0 132 */
Chris@0 133 public function testRandomObject() {
Chris@0 134 // For values of 0 and 1 \Drupal\Component\Utility\Random::object() will
Chris@0 135 // have different execution paths.
Chris@0 136 $random = new Random();
Chris@0 137 for ($i = 0; $i <= 1; $i++) {
Chris@0 138 $obj = $random->object($i);
Chris@0 139 $this->assertEquals($i, count(get_object_vars($obj)), 'Generated random object has expected number of properties');
Chris@0 140 }
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * Tests random string validation callbacks.
Chris@0 145 *
Chris@0 146 * @covers ::string
Chris@0 147 */
Chris@0 148 public function testRandomStringValidator() {
Chris@0 149 $random = new Random();
Chris@0 150 $this->firstStringGenerated = '';
Chris@0 151 $str = $random->string(1, TRUE, [$this, '_RandomStringValidate']);
Chris@0 152 $this->assertNotEquals($this->firstStringGenerated, $str);
Chris@0 153 }
Chris@0 154
Chris@0 155 /**
Chris@0 156 * Callback for random string validation.
Chris@0 157 *
Chris@0 158 * @see \Drupal\Component\Utility\Random::name()
Chris@0 159 * @see \Drupal\Tests\Component\Utility\RandomTest::testRandomStringValidator()
Chris@0 160 *
Chris@0 161 * @param string $string
Chris@0 162 * The random string to validate.
Chris@0 163 *
Chris@0 164 * @return bool
Chris@0 165 * TRUE if the random string is valid, FALSE if not.
Chris@0 166 */
Chris@0 167 public function _RandomStringValidate($string) {
Chris@0 168 // Return FALSE for the first generated string and any string that is the
Chris@0 169 // same, as the test expects a different string to be returned.
Chris@0 170 if (empty($this->firstStringGenerated) || $string == $this->firstStringGenerated) {
Chris@0 171 $this->firstStringGenerated = $string;
Chris@0 172 return FALSE;
Chris@0 173 }
Chris@0 174 return TRUE;
Chris@0 175 }
Chris@0 176
Chris@0 177 }