]> git.mxchange.org Git - friendica.git/commitdiff
Using random_int directly
authorPhilipp Holzer <admin@philipp.info>
Mon, 5 Nov 2018 20:15:30 +0000 (21:15 +0100)
committerPhilipp Holzer <admin@philipp.info>
Mon, 5 Nov 2018 20:15:30 +0000 (21:15 +0100)
src/Util/Crypto.php
tests/src/Util/CryptoTest.php

index 785860c182887d0104bafa66e741ab9e36860372..7dd0dee5c0dc9c603114540e635808894aeedb24 100644 (file)
@@ -483,21 +483,16 @@ class Crypto
         *
         * @param string $digits The count of digits
         * @return int The random Digits
+        *
+        * @throws \Exception In case 'random_int' isn't usable
         */
        public static function randomDigits($digits)
        {
                $rn = '';
 
-               if (!function_exists('random_int')) {
-                       // using rand() function for PHP 5.x compatibility
-                       for ($i = 0; $i < $digits; $i++) {
-                               $rn .= rand(0, 9);
-                       }
-               } else {
-                       // generating cryptographically secure pseudo-random integers
-                       for ($i = 0; $i < $digits; $i++) {
-                               $rn .= random_int(0, 9);
-                       }
+               // generating cryptographically secure pseudo-random integers
+               for ($i = 0; $i < $digits; $i++) {
+                       $rn .= random_int(0, 9);
                }
 
                return $rn;
index 7c6af0070cb11dc66ca5b1ae6c5e1081a6f915b6..b976a09838d38c0de5e16d87a5cd46716933c762 100644 (file)
@@ -7,24 +7,6 @@ use PHPUnit\Framework\TestCase;
 
 class CryptoTest extends TestCase
 {
-       /**
-        * Replaces function_exists results with given mocks
-        *
-        * @param array $functions a list from function names and their result
-        */
-       private function setFunctions($functions)
-       {
-               global $phpMock;
-               $phpMock['function_exists'] = function($function) use ($functions) {
-                       foreach ($functions as $name => $value) {
-                               if ($function == $name) {
-                                       return $value;
-                               }
-                       }
-                       return '__phpunit_continue__';
-               };
-       }
-
        /**
         * Replaces rand results with given mocks
         *
@@ -53,24 +35,8 @@ class CryptoTest extends TestCase
                };
        }
 
-       public function testRandomDigitsRand()
-       {
-               $this->setFunctions(['random_int' => false]);
-               $this->assertRand(0, 9);
-
-               $test = Crypto::randomDigits(1);
-               $this->assertEquals(1, strlen($test));
-               $this->assertEquals(1, $test);
-
-               $test = Crypto::randomDigits(8);
-               $this->assertEquals(8, strlen($test));
-               $this->assertEquals(11111111, $test);
-       }
-
-
        public function testRandomDigitsRandomInt()
        {
-               $this->setFunctions(['random_int' => true]);
                $this->assertRandomInt(0, 9);
 
                $test = Crypto::randomDigits(1);
@@ -83,39 +49,6 @@ class CryptoTest extends TestCase
        }
 }
 
-/**
- * A workaround to replace the PHP native function_exists() with a mocked function
- *
- * @param string $function_name the Name of the function
- *
- * @return bool true or false
- */
-function function_exists($function_name)
-{
-       global $phpMock;
-       if (isset($phpMock['function_exists'])) {
-               $result = call_user_func_array($phpMock['function_exists'], func_get_args());
-               if ($result !== '__phpunit_continue__') {
-                       return $result;
-               }
-       }
-       return call_user_func_array('\function_exists', func_get_args());
-}
-
-/**
- * A workaround to replace the PHP native rand() (< 7.0) with a mocked function
- *
- * @return int
- */
-function rand($min, $max)
-{
-       global $phpMock;
-       if (isset($phpMock['rand'])) {
-               $result = call_user_func_array($phpMock['rand'], func_get_args());
-               return $result;
-       }
-}
-
 /**
  * A workaround to replace the PHP native random_int() (>= 7.0) with a mocked function
  *