Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert expected data types in tests #9268

Merged
merged 4 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions plugins/managesieve/tests/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function test_parser($input, $output, $message)
$script = new rcube_sieve_script($input, $caps);
$result = $script->as_text();

$this->assertEquals(trim($output), trim($result), $message);
$this->assertSame(trim($output), trim($result), $message);
}

/**
Expand Down Expand Up @@ -81,6 +81,6 @@ function test_tokenizer($num, $input, $output)
{
$res = json_encode(rcube_sieve_script::tokenize($input, $num));

$this->assertEquals(trim($output), trim($res));
$this->assertSame(trim($output), trim($res));
}
}
14 changes: 7 additions & 7 deletions plugins/password/tests/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ function test_driver_cpanel()
$driver_class = $this->load_driver('cpanel');

$error_result = $driver_class::decode_response(false);
$this->assertEquals($error_result, PASSWORD_CONNECT_ERROR);
$this->assertSame($error_result, PASSWORD_CONNECT_ERROR);

$bad_result = $driver_class::decode_response(null);
$this->assertEquals($bad_result, PASSWORD_CONNECT_ERROR);
$this->assertSame($bad_result, PASSWORD_CONNECT_ERROR);

$null_result = $driver_class::decode_response('null');
$this->assertEquals($null_result, PASSWORD_ERROR);
$this->assertSame($null_result, PASSWORD_ERROR);

$malformed_result = $driver_class::decode_response('random {string]!');
$this->assertEquals($malformed_result, PASSWORD_ERROR);
$this->assertSame($malformed_result, PASSWORD_ERROR);

$other_result = $driver_class::decode_response('{"a":"b"}');
$this->assertEquals($other_result, PASSWORD_ERROR);
$this->assertSame($other_result, PASSWORD_ERROR);

$fail_response = '{"data":null,"errors":["Execution of Email::passwdp'
. 'op (api version:3) is not permitted inside of webmail"],"sta'
Expand All @@ -65,12 +65,12 @@ function test_driver_cpanel()
'message' => $error_message,
];
$fail_result = $driver_class::decode_response($fail_response);
$this->assertEquals($expected_result, $fail_result);
$this->assertSame($expected_result, $fail_result);

$success_response = '{"metadata":{},"data":null,"messages":null,"errors'
. '":null,"status":1}';
$good_result = $driver_class::decode_response($success_response);
$this->assertEquals($good_result, PASSWORD_SUCCESS);
$this->assertSame($good_result, PASSWORD_SUCCESS);
}

/**
Expand Down
16 changes: 9 additions & 7 deletions tests/Actions/Mail/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function test_search_non_empty_result()
function data_search_input(): iterable
{
$week = new DateInterval('P1W');
$weekDate = (new DateTime('now'))->sub($week)->format('j-M-Y');
$weekDate = (new DateTime('now', new DateTimeZone('UTC')))->sub($week)->format('j-M-Y');

return [
[
Expand Down Expand Up @@ -307,14 +307,16 @@ function data_search_interval_criteria(): iterable
$month = new DateInterval('P1M');
$year = new DateInterval('P1Y');

$utcTz = new DateTimeZone('UTC');

return [
['', null],
['1W', 'SINCE ' . (new DateTime('now'))->sub($week)->format('j-M-Y')],
['1M', 'SINCE ' . (new DateTime('now'))->sub($month)->format('j-M-Y')],
['1Y', 'SINCE ' . (new DateTime('now'))->sub($year)->format('j-M-Y')],
['-1W', 'BEFORE ' . (new DateTime('now'))->sub($week)->format('j-M-Y')],
['-1M', 'BEFORE ' . (new DateTime('now'))->sub($month)->format('j-M-Y')],
['-1Y', 'BEFORE ' . (new DateTime('now'))->sub($year)->format('j-M-Y')],
['1W', 'SINCE ' . (new DateTime('now', $utcTz))->sub($week)->format('j-M-Y')],
['1M', 'SINCE ' . (new DateTime('now', $utcTz))->sub($month)->format('j-M-Y')],
['1Y', 'SINCE ' . (new DateTime('now', $utcTz))->sub($year)->format('j-M-Y')],
['-1W', 'BEFORE ' . (new DateTime('now', $utcTz))->sub($week)->format('j-M-Y')],
['-1M', 'BEFORE ' . (new DateTime('now', $utcTz))->sub($month)->format('j-M-Y')],
['-1Y', 'BEFORE ' . (new DateTime('now', $utcTz))->sub($year)->format('j-M-Y')],
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Browser/Mail/GetunreadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testGetunread()
// Folders list state
$browser->assertVisible('.folderlist li.inbox.unread');

$this->assertEquals(strval(self::$msgcount), $browser->text('.folderlist li.inbox span.unreadcount'));
$this->assertSame(strval(self::$msgcount), $browser->text('.folderlist li.inbox span.unreadcount'));
});
}
}
2 changes: 1 addition & 1 deletion tests/Browser/Mail/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testList()
// check message list
$browser->assertVisible('#messagelist tbody tr:first-child.unread');

$this->assertEquals('Test HTML with local and remote image',
$this->assertSame('Test HTML with local and remote image',
$browser->text('#messagelist tbody tr:first-child span.subject'));

// Note: This element icon has width=0, use assertPresent() not assertVisible()
Expand Down
6 changes: 3 additions & 3 deletions tests/Browser/Settings/Preferences/GeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ public function testPreferencesChange()
$options = array_diff(array_keys($this->settings), ['refresh_interval', 'pretty_date']);

foreach ($options as $option) {
$this->assertEquals($this->settings[$option], $prefs[$option]);
$this->assertSame($this->settings[$option], $prefs[$option]);
}

$this->assertEquals($this->settings['pretty_date'], $prefs['prettydate']);
$this->assertEquals($this->settings['refresh_interval'], $prefs['refresh_interval'] / 60);
$this->assertSame($this->settings['pretty_date'], $prefs['prettydate']);
$this->assertSame($this->settings['refresh_interval'], $prefs['refresh_interval'] / 60);
}
}
22 changes: 11 additions & 11 deletions tests/Framework/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function test_parse_bytes()

foreach ($data as $value => $expected) {
$result = parse_bytes($value);
$this->assertEquals($expected, $result, "Invalid parse_bytes() result for $value");
$this->{'assertEquals'}($expected, $result, "Invalid parse_bytes() result for $value");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???? here and in some other places.

Copy link
Contributor Author

@mvorisek mvorisek Dec 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{'assertEquals'} is a hack for PHP CS Fixer to still allow TestCase::assertEquals() to be used.

The (fixed) php_unit_strict rule is risky, so I reviewed them manually to prevent bad changed. Here, I would expect bytes to be parsed to integer, so I kept original assertEquals, so with a fix in future, the test will still pass. The fix/improvement is here: #9268. In that PR, the test is hardened to assertSame.

}

$this->assertSame(0.0, parse_bytes(null));
Expand All @@ -74,7 +74,7 @@ function test_slashify()

foreach ($data as $value => $expected) {
$result = slashify($value);
$this->assertEquals($expected, $result, "Invalid slashify() result for $value");
$this->assertSame($expected, $result, "Invalid slashify() result for $value");
}

}
Expand All @@ -96,7 +96,7 @@ function test_unslashify()

foreach ($data as $value => $expected) {
$result = unslashify($value);
$this->assertEquals($expected, $result, "Invalid unslashify() result for $value");
$this->assertSame($expected, $result, "Invalid unslashify() result for $value");
}

}
Expand All @@ -112,13 +112,13 @@ function test_get_offset_sec()
'1h' => 1 * 60 * 60,
'1d' => 1 * 60 * 60 * 24,
'1w' => 1 * 60 * 60 * 24 * 7,
'1y' => (int) '1y',
'1y' => 1,
'100' => 100,
];

foreach ($data as $value => $expected) {
$result = get_offset_sec($value);
$this->assertEquals($expected, $result, "Invalid get_offset_sec() result for $value");
$this->assertSame($expected, $result, "Invalid get_offset_sec() result for $value");
}

}
Expand All @@ -142,7 +142,7 @@ function test_array_keys_recursive()
$input_str = 'one,two,three,four,five';
$result_str = implode(',', $result);

$this->assertEquals($input_str, $result_str, "Invalid array_keys_recursive() result");
$this->assertSame($input_str, $result_str, "Invalid array_keys_recursive() result");
}

/**
Expand Down Expand Up @@ -175,7 +175,7 @@ function test_abbreviate_string()

foreach ($data as $set) {
$result = abbreviate_string($set[1], $set[2], $set[3], $set[4]);
$this->assertEquals($set[0], $result);
$this->assertSame($set[0], $result);
}
}

Expand All @@ -194,7 +194,7 @@ function test_format_email()

foreach ($data as $value => $expected) {
$result = format_email($value);
$this->assertEquals($expected, $result, "Invalid format_email() result for $value");
$this->assertSame($expected, $result, "Invalid format_email() result for $value");
}
}

Expand All @@ -215,7 +215,7 @@ function test_format_email_recipient()

foreach ($data as $expected => $value) {
$result = format_email_recipient($value[0], isset($value[1]) ? $value[1] : null);
$this->assertEquals($expected, $result, "Invalid format_email_recipient()");
$this->assertSame($expected, $result, "Invalid format_email_recipient()");
}

}
Expand Down Expand Up @@ -253,7 +253,7 @@ function test_is_ascii()
*/
function test_version_parse()
{
$this->assertEquals('0.9.0', version_parse('0.9-stable'));
$this->assertEquals('0.9.99', version_parse('0.9-git'));
$this->assertSame('0.9.0', version_parse('0.9-stable'));
$this->assertSame('0.9.99', version_parse('0.9-git'));
}
}
34 changes: 17 additions & 17 deletions tests/Framework/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ function test_browser($useragent, $opera, $chrome, $ie, $edge, $safari, $mz)
{
$object = $this->getBrowser($useragent);

$this->assertEquals($opera, $object->opera, 'Check for Opera failed');
$this->assertEquals($chrome, $object->chrome, 'Check for Chrome failed');
$this->assertEquals($ie, $object->ie, 'Check for IE failed');
$this->assertEquals($edge, $object->edge, 'Check for Edge failed');
$this->assertEquals($safari, $object->safari, 'Check for Safari failed');
$this->assertEquals($mz, $object->mz, 'Check for MZ failed');
$this->assertSame($opera, $object->opera, 'Check for Opera failed');
$this->assertSame($chrome, $object->chrome, 'Check for Chrome failed');
$this->assertSame($ie, $object->ie, 'Check for IE failed');
$this->assertSame($edge, $object->edge, 'Check for Edge failed');
$this->assertSame($safari, $object->safari, 'Check for Safari failed');
$this->assertSame($mz, $object->mz, 'Check for MZ failed');
}

/**
Expand All @@ -27,10 +27,10 @@ function test_os($useragent, $windows, $linux, $unix, $mac)
{
$object = $this->getBrowser($useragent);

$this->assertEquals($windows, $object->win, 'Check Result of Windows');
$this->assertEquals($linux, $object->linux, 'Check Result of Linux');
$this->assertEquals($mac, $object->mac, 'Check Result of Mac');
$this->assertEquals($unix, $object->unix, 'Check Result of Unix');
$this->assertSame($windows, $object->win, 'Check Result of Windows');
$this->assertSame($linux, $object->linux, 'Check Result of Linux');
$this->assertSame($mac, $object->mac, 'Check Result of Mac');
$this->assertSame($unix, $object->unix, 'Check Result of Unix');

}

Expand All @@ -40,7 +40,7 @@ function test_os($useragent, $windows, $linux, $unix, $mac)
function test_version($useragent, $version)
{
$object = $this->getBrowser($useragent);
$this->assertEquals($version, $object->ver);
$this->assertSame($version, $object->ver);
}

function versions(): iterable
Expand All @@ -58,7 +58,7 @@ function useragents()
return [
'WIN: Mozilla Firefox ' => [
'useragent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1',
'version' => '1.8',
'version' => 1.8,
'isWin' => true,
'isLinux' => false,
'isMac' => false,
Expand All @@ -73,7 +73,7 @@ function useragents()

'LINUX: Bon Echo ' => [
'useragent' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.1) Gecko/20070222 BonEcho/2.0.0.1',
'version' => '1.8',
'version' => 1.8,
'isWin' => false,
'isLinux' => true,
'isMac' => false,
Expand All @@ -88,7 +88,7 @@ function useragents()

'Chrome Mac' => [
'useragent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.461.0 Safari/534.3',
'version' => '6',
'version' => 6.0,
'isWin' => false,
'isLinux' => false,
'isMac' => true,
Expand All @@ -103,7 +103,7 @@ function useragents()

'IE 11' => [
'useragent' => 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C; rv:11.0) like Gecko',
'version' => '11.0',
'version' => 11.0,
'isWin' => true,
'isLinux' => false,
'isMac' => false,
Expand All @@ -118,7 +118,7 @@ function useragents()

'Opera 15' => [
'useragent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36 OPR/15.0.1147.24',
'version' => '15.0',
'version' => 15.0,
'isWin' => true,
'isLinux' => false,
'isMac' => false,
Expand All @@ -133,7 +133,7 @@ function useragents()

'Edge 14' => [
'useragent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14931',
'version' => '14.14931',
'version' => 14.14931,
'isWin' => true,
'isLinux' => false,
'isMac' => false,
Expand Down
16 changes: 8 additions & 8 deletions tests/Framework/Charset.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function data_parse_charset(): iterable
*/
function test_parse_charset($input, $output)
{
$this->assertEquals($output, rcube_charset::parse_charset($input));
$this->assertSame($output, rcube_charset::parse_charset($input));
}

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ function data_convert(): iterable
*/
function test_convert($input, $output, $from, $to)
{
$this->assertEquals($output, rcube_charset::convert($input, $from, $to));
$this->assertSame($output, rcube_charset::convert($input, $from, $to));
}

/**
Expand All @@ -131,7 +131,7 @@ function data_utf7_to_utf8(): iterable
function test_utf7_to_utf8($input, $output)
{
// @phpstan-ignore-next-line
$this->assertEquals($output, rcube_charset::utf7_to_utf8($input));
$this->assertSame($output, rcube_charset::utf7_to_utf8($input));
}

/**
Expand All @@ -150,7 +150,7 @@ function data_utf7imap_to_utf8(): iterable
function test_utf7imap_to_utf8($input, $output)
{
// @phpstan-ignore-next-line
$this->assertEquals($output, rcube_charset::utf7imap_to_utf8($input));
$this->assertSame($output, rcube_charset::utf7imap_to_utf8($input));
}

/**
Expand All @@ -169,7 +169,7 @@ function data_utf8_to_utf7imap(): iterable
function test_utf8_to_utf7imap($input, $output)
{
// @phpstan-ignore-next-line
$this->assertEquals($output, rcube_charset::utf8_to_utf7imap($input));
$this->assertSame($output, rcube_charset::utf8_to_utf7imap($input));
}

/**
Expand All @@ -188,7 +188,7 @@ function data_utf16_to_utf8(): iterable
function test_utf16_to_utf8($input, $output)
{
// @phpstan-ignore-next-line
$this->assertEquals($output, rcube_charset::utf16_to_utf8($input));
$this->assertSame($output, rcube_charset::utf16_to_utf8($input));
}

/**
Expand All @@ -208,7 +208,7 @@ function data_detect(): iterable
function test_detect($input, $fallback, $output)
{
// @phpstan-ignore-next-line
$this->assertEquals($output, rcube_charset::detect($input, $fallback));
$this->assertSame($output, rcube_charset::detect($input, $fallback));
}

/**
Expand All @@ -227,6 +227,6 @@ function data_detect_with_lang(): iterable
function test_detect_with_lang($input, $lang, $output)
{
// @phpstan-ignore-next-line
$this->assertEquals($output, rcube_charset::detect($input, $output, $lang));
$this->assertSame($output, rcube_charset::detect($input, $output, $lang));
}
}
Loading