From 04d4c6d8f0c2b48788906deb0fde54eb08aae40a Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 2 Nov 2024 20:57:11 +0100 Subject: [PATCH] Fix mocking namespace --- src/Util/Crypto.php | 2 +- tests/src/Core/InstallerTest.php | 32 +++++++++++++++--------------- tests/src/Util/CryptoTest.php | 34 +++++--------------------------- 3 files changed, 22 insertions(+), 46 deletions(-) diff --git a/src/Util/Crypto.php b/src/Util/Crypto.php index 4bfa7fcddd..ba3c46bbc3 100644 --- a/src/Util/Crypto.php +++ b/src/Util/Crypto.php @@ -313,6 +313,6 @@ class Crypto $rn .= random_int(0, 9); } - return $rn; + return (int) $rn; } } diff --git a/tests/src/Core/InstallerTest.php b/tests/src/Core/InstallerTest.php index f07ad602c5..c6dbfd6be1 100644 --- a/tests/src/Core/InstallerTest.php +++ b/tests/src/Core/InstallerTest.php @@ -151,7 +151,7 @@ class InstallerTest extends MockedTest */ public function testCheckKeys($function, $expected) { - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) use ($function, $expected) { if ($function_name === $function) { return $expected; @@ -170,7 +170,7 @@ class InstallerTest extends MockedTest */ public function testCheckFunctionsWithoutIntlChar() { - $class_exists = $this->getFunctionMock(__NAMESPACE__, 'class_exists'); + $class_exists = $this->getFunctionMock('Friendica\Core', 'class_exists'); $class_exists->expects($this->any())->willReturnCallback(function($class_name) { if ($class_name === 'IntlChar') { return false; @@ -195,7 +195,7 @@ class InstallerTest extends MockedTest */ public function testCheckFunctionsWithoutCurlInit() { - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if ($function_name === 'curl_init') { return false; @@ -220,7 +220,7 @@ class InstallerTest extends MockedTest */ public function testCheckFunctionsWithoutImagecreateformjpeg() { - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if ($function_name === 'imagecreatefromjpeg') { return false; @@ -245,7 +245,7 @@ class InstallerTest extends MockedTest */ public function testCheckFunctionsWithoutOpensslpublicencrypt() { - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if ($function_name === 'openssl_public_encrypt') { return false; @@ -270,7 +270,7 @@ class InstallerTest extends MockedTest */ public function testCheckFunctionsWithoutMbStrlen() { - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if ($function_name === 'mb_strlen') { return false; @@ -295,7 +295,7 @@ class InstallerTest extends MockedTest */ public function testCheckFunctionsWithoutIconvStrlen() { - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if ($function_name === 'iconv_strlen') { return false; @@ -320,7 +320,7 @@ class InstallerTest extends MockedTest */ public function testCheckFunctionsWithoutPosixkill() { - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if ($function_name === 'posix_kill') { return false; @@ -345,7 +345,7 @@ class InstallerTest extends MockedTest */ public function testCheckFunctionsWithoutProcOpen() { - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if ($function_name === 'proc_open') { return false; @@ -370,7 +370,7 @@ class InstallerTest extends MockedTest */ public function testCheckFunctionsWithoutJsonEncode() { - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if ($function_name === 'json_encode') { return false; @@ -395,7 +395,7 @@ class InstallerTest extends MockedTest */ public function testCheckFunctionsWithoutFinfoOpen() { - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if ($function_name === 'finfo_open') { return false; @@ -420,7 +420,7 @@ class InstallerTest extends MockedTest */ public function testCheckFunctionsWithoutGmpStrval() { - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if ($function_name === 'gmp_strval') { return false; @@ -445,7 +445,7 @@ class InstallerTest extends MockedTest */ public function testCheckFunctions() { - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if (in_array( $function_name, @@ -500,7 +500,7 @@ class InstallerTest extends MockedTest public function testCheckHtAccessFail() { // Mocking that we can use CURL - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if ($function_name === 'curl_init') { return true; @@ -553,7 +553,7 @@ class InstallerTest extends MockedTest public function testCheckHtAccessWork() { // Mocking that we can use CURL - $function_exists = $this->getFunctionMock(__NAMESPACE__, 'function_exists'); + $function_exists = $this->getFunctionMock('Friendica\Core', 'function_exists'); $function_exists->expects($this->any())->willReturnCallback(function($function_name) { if ($function_name === 'curl_init') { return true; @@ -604,7 +604,7 @@ class InstallerTest extends MockedTest */ public function testCheckImagickWithImagick() { - $class_exists = $this->getFunctionMock(__NAMESPACE__, 'class_exists'); + $class_exists = $this->getFunctionMock('Friendica\Core', 'class_exists'); $class_exists->expects($this->any())->willReturnCallback(function($class_name) { if ($class_name === 'Imagick') { return true; diff --git a/tests/src/Util/CryptoTest.php b/tests/src/Util/CryptoTest.php index f27a8ddf9d..544561bc3b 100644 --- a/tests/src/Util/CryptoTest.php +++ b/tests/src/Util/CryptoTest.php @@ -24,39 +24,15 @@ class CryptoTest extends TestCase parent::tearDownAfterClass(); } - /** - * Replaces random_int results with given mocks - * - */ - private function assertRandomInt($min, $max) - { - global $phpMock; - $phpMock['random_int'] = function ($mMin, $mMax) use ($min, $max) { - self::assertEquals($min, $mMin); - self::assertEquals($max, $mMax); - return 1; - }; - } - public function testRandomDigitsRandomInt() { - $random_int = $this->getFunctionMock(__NAMESPACE__, 'random_int'); + $random_int = $this->getFunctionMock('Friendica\Util', 'random_int'); $random_int->expects($this->any())->willReturnCallback(function($min, $max) { - global $phpMock; - if (isset($phpMock['random_int'])) { - return call_user_func_array($phpMock['random_int'], func_get_args()); - } + return 1; }); - self::assertRandomInt(0, 9); - - $test = Crypto::randomDigits(1); - self::assertEquals(1, strlen($test)); - self::assertEquals(1, $test); - - $test = Crypto::randomDigits(8); - self::assertEquals(8, strlen($test)); - self::assertEquals(11111111, $test); + self::assertSame(1, Crypto::randomDigits(1)); + self::assertSame(11111111, Crypto::randomDigits(8)); } public function dataRsa(): array @@ -74,7 +50,7 @@ class CryptoTest extends TestCase */ public function testPubRsaToMe(string $key, string $expected) { - self::assertEquals($expected, Crypto::rsaToPem(base64_decode($key))); + self::assertSame($expected, Crypto::rsaToPem(base64_decode($key))); }