Fix mocking namespace

This commit is contained in:
Art4 2024-11-02 20:57:11 +01:00
parent 5ecee2b9f7
commit 04d4c6d8f0
3 changed files with 22 additions and 46 deletions

View file

@ -313,6 +313,6 @@ class Crypto
$rn .= random_int(0, 9); $rn .= random_int(0, 9);
} }
return $rn; return (int) $rn;
} }
} }

View file

@ -151,7 +151,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckKeys($function, $expected) 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) use ($function, $expected) {
if ($function_name === $function) { if ($function_name === $function) {
return $expected; return $expected;
@ -170,7 +170,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckFunctionsWithoutIntlChar() 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) { $class_exists->expects($this->any())->willReturnCallback(function($class_name) {
if ($class_name === 'IntlChar') { if ($class_name === 'IntlChar') {
return false; return false;
@ -195,7 +195,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckFunctionsWithoutCurlInit() 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if ($function_name === 'curl_init') { if ($function_name === 'curl_init') {
return false; return false;
@ -220,7 +220,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckFunctionsWithoutImagecreateformjpeg() 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if ($function_name === 'imagecreatefromjpeg') { if ($function_name === 'imagecreatefromjpeg') {
return false; return false;
@ -245,7 +245,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckFunctionsWithoutOpensslpublicencrypt() 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if ($function_name === 'openssl_public_encrypt') { if ($function_name === 'openssl_public_encrypt') {
return false; return false;
@ -270,7 +270,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckFunctionsWithoutMbStrlen() 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if ($function_name === 'mb_strlen') { if ($function_name === 'mb_strlen') {
return false; return false;
@ -295,7 +295,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckFunctionsWithoutIconvStrlen() 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if ($function_name === 'iconv_strlen') { if ($function_name === 'iconv_strlen') {
return false; return false;
@ -320,7 +320,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckFunctionsWithoutPosixkill() 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if ($function_name === 'posix_kill') { if ($function_name === 'posix_kill') {
return false; return false;
@ -345,7 +345,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckFunctionsWithoutProcOpen() 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if ($function_name === 'proc_open') { if ($function_name === 'proc_open') {
return false; return false;
@ -370,7 +370,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckFunctionsWithoutJsonEncode() 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if ($function_name === 'json_encode') { if ($function_name === 'json_encode') {
return false; return false;
@ -395,7 +395,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckFunctionsWithoutFinfoOpen() 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if ($function_name === 'finfo_open') { if ($function_name === 'finfo_open') {
return false; return false;
@ -420,7 +420,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckFunctionsWithoutGmpStrval() 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if ($function_name === 'gmp_strval') { if ($function_name === 'gmp_strval') {
return false; return false;
@ -445,7 +445,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckFunctions() 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if (in_array( if (in_array(
$function_name, $function_name,
@ -500,7 +500,7 @@ class InstallerTest extends MockedTest
public function testCheckHtAccessFail() public function testCheckHtAccessFail()
{ {
// Mocking that we can use CURL // 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if ($function_name === 'curl_init') { if ($function_name === 'curl_init') {
return true; return true;
@ -553,7 +553,7 @@ class InstallerTest extends MockedTest
public function testCheckHtAccessWork() public function testCheckHtAccessWork()
{ {
// Mocking that we can use CURL // 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) { $function_exists->expects($this->any())->willReturnCallback(function($function_name) {
if ($function_name === 'curl_init') { if ($function_name === 'curl_init') {
return true; return true;
@ -604,7 +604,7 @@ class InstallerTest extends MockedTest
*/ */
public function testCheckImagickWithImagick() 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) { $class_exists->expects($this->any())->willReturnCallback(function($class_name) {
if ($class_name === 'Imagick') { if ($class_name === 'Imagick') {
return true; return true;

View file

@ -24,39 +24,15 @@ class CryptoTest extends TestCase
parent::tearDownAfterClass(); 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() 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) { $random_int->expects($this->any())->willReturnCallback(function($min, $max) {
global $phpMock; return 1;
if (isset($phpMock['random_int'])) {
return call_user_func_array($phpMock['random_int'], func_get_args());
}
}); });
self::assertRandomInt(0, 9); self::assertSame(1, Crypto::randomDigits(1));
self::assertSame(11111111, Crypto::randomDigits(8));
$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);
} }
public function dataRsa(): array public function dataRsa(): array
@ -74,7 +50,7 @@ class CryptoTest extends TestCase
*/ */
public function testPubRsaToMe(string $key, string $expected) public function testPubRsaToMe(string $key, string $expected)
{ {
self::assertEquals($expected, Crypto::rsaToPem(base64_decode($key))); self::assertSame($expected, Crypto::rsaToPem(base64_decode($key)));
} }