Skip to content

Commit

Permalink
Update codebase to PHP 7.4 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
TavoNiievez authored Dec 3, 2021
1 parent 540ea60 commit 353189a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
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 = [])
{
$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 {

}
}

0 comments on commit 353189a

Please sign in to comment.