Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
php: [7.1, 7.2, 7.3, 7.4, 8.0]
php: [7.4, 8.0, 8.1]

steps:
- name: Checkout code
Expand Down
24 changes: 11 additions & 13 deletions AssertThrows.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Codeception;

Expand All @@ -8,10 +10,6 @@

trait AssertThrows
{
/**
* @param string|null $message
* @param Throwable $exception
*/
private function checkException(?string $message, Throwable $exception)
{
$actualMessage = strtolower($exception->getMessage());
Expand All @@ -32,12 +30,12 @@ private function checkException(?string $message, Throwable $exception)
/**
* Asserts that callback throws an exception
*
* @param $throws
* @param string|Throwable $throws
* @param callable $func
* @param mixed ...$params
* @throws Throwable
*/
public function assertThrows($throws, callable $func, ...$params)
public function assertThrows($throws, callable $func, array $params = [])
Copy link
Contributor

Choose a reason for hiding this comment

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

Might this be backward incompatible to many use cases? If any number of arguments are passed into a variable length argument list, PHP treats it as an array by default. How does this fix PHP 7.4 compatibility?

Wondering what this broke and why my team might need to update all of our tests... 😄

Would be happy to put in a PR to revert this, if that's acceptable.

Copy link
Member Author

Choose a reason for hiding this comment

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

@boboudreau you can send a PR for this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I should have explicitly stated the issue; there are some tests in which we want to call a method with multiple arguments, but have passed them as the 4th, 5th, 6th, etc. argument to this method, and not as an array.

I see that this was at least versioned as 1.2 vs. 1.1 but generally there shouldn't be code changes that cause Errors that are Throwable (in this case, a TypeError) in a minor version release (according to semver), correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

is correct, please include how you suggest your change be tagged in your PR description.

{
$this->assertThrowsWithMessage($throws, null, $func, $params);
}
Expand All @@ -51,7 +49,7 @@ public function assertThrows($throws, callable $func, ...$params)
* @param mixed ...$params
* @throws Throwable
*/
public function assertThrowsWithMessage($throws, ?string $message, callable $func, ...$params)
public function assertThrowsWithMessage($throws, ?string $message, callable $func, array $params = [])
{
if ($throws instanceof Throwable) {
$message = $throws->getMessage();
Expand All @@ -63,7 +61,7 @@ public function assertThrowsWithMessage($throws, ?string $message, callable $fun
}

try {
if ($params) {
if ($params !== []) {
call_user_func_array($func, $params);
} else {
call_user_func($func);
Expand Down Expand Up @@ -119,7 +117,7 @@ public function assertThrowsWithMessage($throws, ?string $message, callable $fun
* @param callable $func
* @param mixed ...$params
*/
public function assertDoesNotThrow($throws, callable $func, ...$params)
public function assertDoesNotThrow($throws, callable $func, array $params = [])
{
$this->assertDoesNotThrowWithMessage($throws, null, $func, $params);
}
Expand All @@ -132,15 +130,15 @@ public function assertDoesNotThrow($throws, callable $func, ...$params)
* @param callable $func
* @param mixed ...$params
*/
public function assertDoesNotThrowWithMessage($throws, ?string $message, callable $func, ...$params)
public function assertDoesNotThrowWithMessage($throws, ?string $message, callable $func, array $params = [])
{
if ($throws instanceof Throwable) {
$message = $throws->getMessage();
$throws = get_class($throws);
}

try {
if ($params) {
if ($params !== []) {
call_user_func_array($func, $params);
} else {
call_user_func($func);
Expand All @@ -165,7 +163,7 @@ public function assertDoesNotThrowWithMessage($throws, ?string $message, callabl

$actualMessage = $exception->getMessage();

if ($message != $actualMessage) {
if ($message !== $actualMessage) {
Assert::assertNotSame($message, $actualMessage);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"require": {
"phpunit/phpunit": "^7.5|^8.0|^9.0"
"phpunit/phpunit": "^8.0|^9.0"
},
"autoload": {
"psr-4": {
Expand Down
17 changes: 9 additions & 8 deletions tests/AssertThrowsTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

use Codeception\AssertThrows;
use PHPUnit\Framework\Assert;
Expand Down Expand Up @@ -35,13 +37,14 @@ public function testExceptionMessageFails()
$this->assertThrowsWithMessage(MyException::class, 'hello', function() {
throw new MyException('hallo');
});
} catch (AssertionFailedError $e) {
} catch (AssertionFailedError $error) {
$this->assertEquals(
"Exception message 'hello' was expected, but 'hallo' was received",
$e->getMessage()
$error->getMessage()
);
return;
}

$this->fail('Ups :(');
}

Expand All @@ -66,12 +69,11 @@ public function testAssertThrowsWithParams()
Exception::class,
'foobar',
$func,
'foo',
'bar'
['foo', 'bar']
);
}

public function testAssertDoesNotThrow(): void
public function testAssertDoesNotThrow()
{
$func = function (): void {
throw new Exception('foo');
Expand All @@ -88,7 +90,6 @@ public function testAssertDoesNotThrow(): void
}
}


final class MyException extends Exception {

}
}