]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/CryptoTest.php
7c6af0070cb11dc66ca5b1ae6c5e1081a6f915b6
[friendica.git] / tests / src / Util / CryptoTest.php
1 <?php
2
3 // this is in the same namespace as Crypto for mocking 'rand' and 'random_init'
4 namespace Friendica\Util;
5
6 use PHPUnit\Framework\TestCase;
7
8 class CryptoTest extends TestCase
9 {
10         /**
11          * Replaces function_exists results with given mocks
12          *
13          * @param array $functions a list from function names and their result
14          */
15         private function setFunctions($functions)
16         {
17                 global $phpMock;
18                 $phpMock['function_exists'] = function($function) use ($functions) {
19                         foreach ($functions as $name => $value) {
20                                 if ($function == $name) {
21                                         return $value;
22                                 }
23                         }
24                         return '__phpunit_continue__';
25                 };
26         }
27
28         /**
29          * Replaces rand results with given mocks
30          *
31          */
32         private function assertRand($min, $max)
33         {
34                 global $phpMock;
35                 $phpMock['rand'] = function($mMin, $mMax) use ($min, $max) {
36                         $this->assertEquals($min, $mMin);
37                         $this->assertEquals($max, $mMax);
38                         return 1;
39                 };
40         }
41
42         /**
43          * Replaces rand results with given mocks
44          *
45          */
46         private function assertRandomInt($min, $max)
47         {
48                 global $phpMock;
49                 $phpMock['random_int'] = function($mMin, $mMax) use ($min, $max) {
50                         $this->assertEquals($min, $mMin);
51                         $this->assertEquals($max, $mMax);
52                         return 1;
53                 };
54         }
55
56         public function testRandomDigitsRand()
57         {
58                 $this->setFunctions(['random_int' => false]);
59                 $this->assertRand(0, 9);
60
61                 $test = Crypto::randomDigits(1);
62                 $this->assertEquals(1, strlen($test));
63                 $this->assertEquals(1, $test);
64
65                 $test = Crypto::randomDigits(8);
66                 $this->assertEquals(8, strlen($test));
67                 $this->assertEquals(11111111, $test);
68         }
69
70
71         public function testRandomDigitsRandomInt()
72         {
73                 $this->setFunctions(['random_int' => true]);
74                 $this->assertRandomInt(0, 9);
75
76                 $test = Crypto::randomDigits(1);
77                 $this->assertEquals(1, strlen($test));
78                 $this->assertEquals(1, $test);
79
80                 $test = Crypto::randomDigits(8);
81                 $this->assertEquals(8, strlen($test));
82                 $this->assertEquals(11111111, $test);
83         }
84 }
85
86 /**
87  * A workaround to replace the PHP native function_exists() with a mocked function
88  *
89  * @param string $function_name the Name of the function
90  *
91  * @return bool true or false
92  */
93 function function_exists($function_name)
94 {
95         global $phpMock;
96         if (isset($phpMock['function_exists'])) {
97                 $result = call_user_func_array($phpMock['function_exists'], func_get_args());
98                 if ($result !== '__phpunit_continue__') {
99                         return $result;
100                 }
101         }
102         return call_user_func_array('\function_exists', func_get_args());
103 }
104
105 /**
106  * A workaround to replace the PHP native rand() (< 7.0) with a mocked function
107  *
108  * @return int
109  */
110 function rand($min, $max)
111 {
112         global $phpMock;
113         if (isset($phpMock['rand'])) {
114                 $result = call_user_func_array($phpMock['rand'], func_get_args());
115                 return $result;
116         }
117 }
118
119 /**
120  * A workaround to replace the PHP native random_int() (>= 7.0) with a mocked function
121  *
122  * @return int
123  */
124 function random_int($min, $max)
125 {
126         global $phpMock;
127         if (isset($phpMock['random_int'])) {
128                 $result = call_user_func_array($phpMock['random_int'], func_get_args());
129                 return $result;
130         }
131 }