Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RandomTest.php
Go to the documentation of this file.
1 <?php
9 
10 class RandomTest extends \PHPUnit\Framework\TestCase
11 {
18  public function testGetRandomString($length, $chars = null)
19  {
20  $mathRandom = new \Magento\Framework\Math\Random();
21  $string = $mathRandom->getRandomString($length, $chars);
22 
23  $this->assertEquals($length, strlen($string));
24  if ($chars !== null) {
25  $this->_assertContainsOnlyChars($string, $chars);
26  }
27  }
28 
32  public function getRandomStringDataProvider()
33  {
34  return [
35  [0],
36  [10],
40  [
41  20,
45  ]
46  ];
47  }
48 
49  public function testGetUniqueHash()
50  {
51  $mathRandom = new \Magento\Framework\Math\Random();
52  $hashOne = $mathRandom->getUniqueHash();
53  $hashTwo = $mathRandom->getUniqueHash();
54  $this->assertTrue(is_string($hashOne));
55  $this->assertTrue(is_string($hashTwo));
56  $this->assertNotEquals($hashOne, $hashTwo);
57  }
58 
63  protected function _assertContainsOnlyChars($string, $chars)
64  {
65  if (preg_match('/[^' . $chars . ']+/', $string, $matches)) {
66  $this->fail(sprintf('Unexpected char "%s" found', $matches[0]));
67  }
68  }
69 
76  public function testGetRandomNumber($min, $max)
77  {
79  $this->assertLessThanOrEqual($max, $number);
80  $this->assertGreaterThanOrEqual($min, $number);
81  }
82 
86  public function testGetRandomNumberProvider()
87  {
88  return [
89  [0, 100],
90  [0, 1],
91  [0, 0],
92  [-1, 0],
93  [-100, 0],
94  [-1, 1],
95  [-100, 100]
96  ];
97  }
98 }
static getRandomNumber($min=0, $max=null)
Definition: Random.php:62
$number
Definition: details.phtml:22