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