Skip to content

Commit d5e0779

Browse files
alcaeusjmikola
authored andcommitted
PHPC-1435: Fix test checking invalid argument values
1 parent 5ad1733 commit d5e0779

File tree

4 files changed

+83
-21
lines changed

4 files changed

+83
-21
lines changed

tests/session/session-startTransaction_error-003.phpt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
--TEST--
2-
MongoDB\Driver\Session::startTransaction() with wrong argument for options array
2+
MongoDB\Driver\Session::startTransaction() with wrong argument for options array on PHP 5
33
--SKIPIF--
44
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
55
<?php skip_if_not_libmongoc_crypto() ?>
66
<?php skip_if_not_replica_set_or_mongos_with_replica_set(); ?>
77
<?php skip_if_server_version('<', '4.0'); ?>
8+
<?php skip_if_php_version('>=', '7.0.0'); ?>
89
--FILE--
910
<?php
1011
require_once __DIR__ . "/../utils/basic.inc";
@@ -18,14 +19,17 @@ $options = [
1819
];
1920

2021
foreach ($options as $txnOptions) {
21-
$session->startTransaction($txnOptions);
22+
echo raises(function () use ($session, $txnOptions) {
23+
$session->startTransaction($txnOptions);
24+
}, E_RECOVERABLE_ERROR), "\n";
2225
}
2326

2427
?>
2528
===DONE===
2629
<?php exit(0); ?>
2730
--EXPECTF--
28-
Warning: MongoDB\Driver\Session::startTransaction() expects parameter 1 to be array, int%S given in %s on line %d
29-
30-
Warning: MongoDB\Driver\Session::startTransaction() expects parameter 1 to be array, object given in %s on line %d
31+
OK: Got E_RECOVERABLE_ERROR
32+
Argument 1 passed to MongoDB\Driver\Session::startTransaction() must be of the type array, int%S given, called in %S
33+
OK: Got E_RECOVERABLE_ERROR
34+
Argument 1 passed to MongoDB\Driver\Session::startTransaction() must be of the type array, object given, called in %S
3135
===DONE===
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
MongoDB\Driver\Session::startTransaction() with wrong argument for options array on PHP 7
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongoc_crypto() ?>
6+
<?php skip_if_not_replica_set(); ?>
7+
<?php skip_if_server_version('<', '4.0'); ?>
8+
<?php skip_if_php_version('<', '7.0.0'); ?>
9+
--FILE--
10+
<?php
11+
require_once __DIR__ . "/../utils/basic.inc";
12+
13+
$manager = new MongoDB\Driver\Manager(URI);
14+
$session = $manager->startSession();
15+
16+
$options = [
17+
2,
18+
new stdClass,
19+
];
20+
21+
foreach ($options as $txnOptions) {
22+
echo throws(function () use ($session, $txnOptions) {
23+
$session->startTransaction($txnOptions);
24+
}, TypeError::class), "\n";
25+
}
26+
27+
?>
28+
===DONE===
29+
<?php exit(0); ?>
30+
--EXPECTF--
31+
OK: Got TypeError
32+
Argument 1 passed to MongoDB\Driver\Session::startTransaction() must be of the type array or null, int%S given
33+
OK: Got TypeError
34+
Argument 1 passed to MongoDB\Driver\Session::startTransaction() must be of the type array or null, object given
35+
===DONE===

tests/utils/skipif.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,20 @@ function skip_if_server_version($operator, $version)
252252
}
253253
}
254254

255+
/**
256+
* Skips the test if the PHP version satisfies a comparison.
257+
*
258+
* @see http://php.net/version_compare
259+
* @param string $operator Comparison operator
260+
* @param string $version Version to compare against
261+
*/
262+
function skip_if_php_version($operator, $version)
263+
{
264+
if (version_compare(PHP_VERSION, $version, $operator)) {
265+
exit("skip PHP version '" . PHP_VERSION . "' $operator '$version'");
266+
}
267+
}
268+
255269
/**
256270
* Skips the test if the server not using a particular storage engine.
257271
*

tests/utils/tools.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ function destroyTemporaryMongoInstance($id = NULL)
545545

546546
function severityToString($type) {
547547
switch($type) {
548+
case E_RECOVERABLE_ERROR:
549+
return "E_RECOVERABLE_ERROR";
548550
case E_WARNING:
549551
return "E_WARNING";
550552
case E_NOTICE:
@@ -590,26 +592,33 @@ function raises($function, $type, $infunction = null) {
590592
function throws($function, $exceptionname, $infunction = null) {
591593
try {
592594
$function();
595+
} catch (Throwable $e) {
593596
} catch(Exception $e) {
594-
$message = str_replace(array("\n", "\r"), ' ', $e->getMessage());
595-
if ($e instanceof $exceptionname) {
596-
if ($infunction) {
597-
$trace = $e->getTrace();
598-
$function = $trace[0]["function"];
599-
if (strcasecmp($function, $infunction) == 0) {
600-
printf("OK: Got %s thrown from %s\n", $exceptionname, $infunction);
601-
} else {
602-
printf("ALMOST: Got %s - but was thrown in %s, not %s (%s)\n", $exceptionname, $function, $infunction, $message);
603-
}
604-
return $e->getMessage();
597+
}
598+
599+
if ($e === null) {
600+
echo "FAILED: Expected $exceptionname thrown, but no exception thrown!\n";
601+
return;
602+
}
603+
604+
$message = str_replace(array("\n", "\r"), ' ', $e->getMessage());
605+
if ($e instanceof $exceptionname) {
606+
if ($infunction) {
607+
$trace = $e->getTrace();
608+
$function = $trace[0]["function"];
609+
if (strcasecmp($function, $infunction) == 0) {
610+
printf("OK: Got %s thrown from %s\n", $exceptionname, $infunction);
611+
} else {
612+
printf("ALMOST: Got %s - but was thrown in %s, not %s (%s)\n", $exceptionname, $function, $infunction, $message);
605613
}
606-
printf("OK: Got %s\n", $exceptionname);
607-
} else {
608-
printf("ALMOST: Got %s (%s) - expected %s\n", get_class($e), $message, $exceptionname);
614+
return $e->getMessage();
609615
}
610-
return $e->getMessage();
616+
printf("OK: Got %s\n", $exceptionname);
617+
} else {
618+
printf("ALMOST: Got %s (%s) - expected %s\n", get_class($e), $message, $exceptionname);
611619
}
612-
echo "FAILED: Expected $exceptionname thrown, but no exception thrown!\n";
620+
621+
return $e->getMessage();
613622
}
614623

615624
function printServer(Server $server)

0 commit comments

Comments
 (0)