Skip to content

Commit eb1f7c9

Browse files
authored
Merge pull request #31 from Codeception/fix_tests
Update to PHP 8.2, Codeception 5 and static analysis
2 parents a499352 + b05b628 commit eb1f7c9

File tree

20 files changed

+81
-51
lines changed

20 files changed

+81
-51
lines changed

.github/workflows/main.yml

+21-14
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,30 @@ jobs:
88

99
strategy:
1010
matrix:
11-
php: [8.1, 8.2, 8.3, 8.4]
11+
php: [8.2, 8.3, 8.4]
1212

1313
steps:
14-
- name: Checkout code
15-
uses: actions/checkout@v4
14+
- name: Checkout code
15+
uses: actions/checkout@v4
1616

17-
- name: Setup PHP
18-
uses: shivammathur/setup-php@v2
19-
with:
20-
php-version: ${{ matrix.php }}
21-
coverage: none
17+
- name: Setup PHP
18+
uses: shivammathur/setup-php@v2
19+
with:
20+
php-version: ${{ matrix.php }}
21+
coverage: none
22+
tools: phpstan, phpcs
2223

23-
- name: Validate composer.json and composer.lock
24-
run: composer validate
24+
- name: Validate composer.json and composer.lock
25+
run: composer validate
2526

26-
- name: Install dependencies
27-
run: composer install --prefer-dist --no-progress --no-interaction --no-suggest
27+
- name: Install dependencies
28+
run: composer install --prefer-dist --no-progress --no-interaction --no-suggest
2829

29-
- name: Run test suite
30-
run: php vendor/bin/codecept run
30+
- name: Run PHPStan
31+
run: phpstan analyse src
32+
33+
- name: Run PHPCS
34+
run: phpcs --standard=PSR12 src
35+
36+
- name: Run test suite
37+
run: php vendor/bin/codecept run

codeception.yml

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
namespace: Tests
2+
support_namespace: Support
3+
4+
settings:
5+
shuffle: true
6+
lint: true
17
paths:
28
tests: tests
39
output: tests/_output
4-
data: tests/_data
5-
support: tests/_support
6-
envs: tests/_envs
7-
actor_suffix: Tester
10+
support: tests/Support
11+
data: tests/Support/Data
12+

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
"homepage": "https://codeception.com/",
2424
"require": {
25-
"php": "^8.1",
25+
"php": "^8.2",
2626
"codeception/codeception": "*@dev",
2727
"codeception/lib-asserts": "^2.2"
2828
},

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A Codeception module containing various assertions.
99

1010
## Requirements
1111

12-
* `PHP 8.1` or higher.
12+
* `PHP 8.2` or higher.
1313

1414
## Installation
1515

src/Codeception/Module/AbstractAsserts.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,4 @@ abstract class AbstractAsserts extends Module
147147
markTestIncomplete as public;
148148
markTestSkipped as public;
149149
}
150-
}
150+
}

src/Codeception/Module/Asserts.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
namespace Codeception\Module;
66

7+
use Throwable;
8+
9+
use function get_debug_type;
10+
711
/**
812
* Special module for using asserts in your tests.
913
*/
@@ -23,6 +27,7 @@ class Asserts extends AbstractAsserts
2327
* $this->doSomethingBad();
2428
* });
2529
* ```
30+
*
2631
* If you want to check message or throwable code, you can pass them with throwable instance:
2732
* ```php
2833
* <?php
@@ -31,15 +36,13 @@ class Asserts extends AbstractAsserts
3136
* $this->doSomethingBad();
3237
* });
3338
* ```
34-
*
35-
* @param \Throwable|string $throwable
3639
*/
37-
public function expectThrowable($throwable, callable $callback): void
40+
public function expectThrowable(string|Throwable $throwable, callable $callback): void
3841
{
3942
if (is_object($throwable)) {
4043
$class = get_class($throwable);
4144
$msg = $throwable->getMessage();
42-
$code = $throwable->getCode();
45+
$code = (int) $throwable->getCode();
4346
} else {
4447
$class = $throwable;
4548
$msg = null;
@@ -48,7 +51,7 @@ public function expectThrowable($throwable, callable $callback): void
4851

4952
try {
5053
$callback();
51-
} catch (\Throwable $t) {
54+
} catch (Throwable $t) {
5255
$this->checkThrowable($t, $class, $msg, $code);
5356
return;
5457
}
@@ -60,13 +63,17 @@ public function expectThrowable($throwable, callable $callback): void
6063
* Check if the given throwable matches the expected data,
6164
* fail (throws an exception) if it does not.
6265
*/
63-
protected function checkThrowable(\Throwable $throwable, string $expectedClass, ?string $expectedMsg, $expectedCode = null): void
64-
{
66+
protected function checkThrowable(
67+
Throwable $throwable,
68+
string $expectedClass,
69+
?string $expectedMsg,
70+
int|null $expectedCode = null
71+
): void {
6572
if (!($throwable instanceof $expectedClass)) {
6673
$this->fail(sprintf(
6774
"Exception of class '%s' expected to be thrown, but class '%s' was caught",
6875
$expectedClass,
69-
get_class($throwable)
76+
get_debug_type($throwable)
7077
));
7178
}
7279

tests/_data/DummyClass.php renamed to tests/Support/Data/DummyClass.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
declare(strict_types=1);
44

5+
namespace Support\Data;
6+
57
class DummyClass
68
{
79
private int $foo;
8-
10+
911
private static int $staticFoo;
1012
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
namespace Tests\Support;
36

47
/**
58
* Inherited Methods
6-
* @method void wantToTest($text)
79
* @method void wantTo($text)
10+
* @method void wantToTest($text)
811
* @method void execute($callable)
912
* @method void expectTo($prediction)
1013
* @method void expect($prediction)
1114
* @method void amGoingTo($argumentation)
1215
* @method void am($role)
1316
* @method void lookForwardTo($achieveValue)
1417
* @method void comment($description)
15-
* @method void pause()
18+
* @method void pause($vars = [])
1619
*
1720
* @SuppressWarnings(PHPMD)
1821
*/
1922
class UnitTester extends \Codeception\Actor
2023
{
2124
use _generated\UnitTesterActions;
2225

23-
/**
24-
* Define custom actions here
25-
*/
26+
/**
27+
* Define custom actions here
28+
*/
2629
}

tests/Support/_generated/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

tests/_support/_generated/.gitignore

-1
This file was deleted.

tests/unit.suite.yml

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# Codeception Test Suite Configuration
2-
3-
# suite for unit (internal) tests.
4-
error_level: "E_ALL"
5-
class_name: UnitTester
1+
actor: UnitTester
2+
suite_namespace: Tests\Unit
3+
modules:
4+
enabled: []

tests/unit/Codeception/Module/AssertsTest.php

+16-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use PHPUnit\Framework\AssertionFailedError;
1313
use PHPUnit\Framework\Constraint\IsEqual;
1414
use PHPUnit\Framework\IncompleteTestError;
15+
use PHPUnit\Framework\SkippedTestError;
1516
use PHPUnit\Framework\SkippedWithMessageException;
17+
use PHPUnit\Runner\Version as PHPUnitVersion;
1618
use RuntimeException;
1719
use stdClass;
1820

@@ -46,14 +48,14 @@ public function testPHPUnitAsserts()
4648
{
4749
$this->module->assertArrayHasKey('one', ['one' => 1, 'two' => 2]);
4850
$this->module->assertArrayNotHasKey('three', ['one' => 1, 'two' => 2]);
49-
$this->module->assertClassHasAttribute('foo', \DummyClass::class);
50-
$this->module->assertClassHasStaticAttribute('staticFoo', \DummyClass::class);
51-
$this->module->assertClassNotHasAttribute('bar', \DummyClass::class);
52-
$this->module->assertClassNotHasStaticAttribute('staticBar', \DummyClass::class);
51+
$this->module->assertClassHasAttribute('foo', \Support\Data\DummyClass::class);
52+
$this->module->assertClassHasStaticAttribute('staticFoo', \Support\Data\DummyClass::class);
53+
$this->module->assertClassNotHasAttribute('bar', \Support\Data\DummyClass::class);
54+
$this->module->assertClassNotHasStaticAttribute('staticBar', \Support\Data\DummyClass::class);
5355
$this->module->assertContains(1, [1, 2]);
5456
$this->module->assertContainsEquals(2, [1, 2]);
55-
$this->module->assertContainsOnly(\DummyClass::class, [new \DummyClass(), new \DummyClass()]);
56-
$this->module->assertContainsOnlyInstancesOf(\DummyClass::class, [new \DummyClass(), new \DummyClass()]);
57+
$this->module->assertContainsOnly(\Support\Data\DummyClass::class, [new \Support\Data\DummyClass(), new \Support\Data\DummyClass()]);
58+
$this->module->assertContainsOnlyInstancesOf(\Support\Data\DummyClass::class, [new \Support\Data\DummyClass(), new \Support\Data\DummyClass()]);
5759
$this->module->assertCount(3, [1, 2, 3]);
5860
$this->module->assertDirectoryDoesNotExist(__DIR__.'notExist');
5961
$this->module->assertDirectoryExists(__DIR__);
@@ -130,7 +132,7 @@ public function testPHPUnitAsserts()
130132
$this->module->assertNan(sqrt(-1));
131133
$this->module->assertNotContains('three', ['one', 'two']);
132134
$this->module->assertNotContainsEquals(3, [1, 2]);
133-
$this->module->assertNotContainsOnly(\DummyClass::class, [new \DummyClass(), new Exception()]);
135+
$this->module->assertNotContainsOnly(\Support\Data\DummyClass::class, [new \Support\Data\DummyClass(), new Exception()]);
134136
$this->module->assertNotCount(1, ['one', 'two']);
135137
$this->module->assertNotEmpty([1]);
136138
$this->module->assertNotEquals(true, false);
@@ -150,8 +152,8 @@ public function testPHPUnitAsserts()
150152
$this->module->assertNotTrue(null);
151153
$this->module->assertNotTrue('foo');
152154
$this->module->assertNull(null);
153-
$this->module->assertObjectHasAttribute('foo', new \DummyClass());
154-
$this->module->assertObjectNotHasAttribute('bar', new \DummyClass());
155+
$this->module->assertObjectHasAttribute('foo', new \Support\Data\DummyClass());
156+
$this->module->assertObjectNotHasAttribute('bar', new \Support\Data\DummyClass());
155157
$this->module->assertSame(1, 1);
156158
$this->module->assertSameSize([1, 2, 3], [1, 2, 3]);
157159
$this->module->assertStringContainsString('bar', 'foobar');
@@ -280,8 +282,12 @@ public function testMarkTestIncomplete()
280282

281283
public function testMarkTestSkipped()
282284
{
283-
$this->expectException(SkippedWithMessageException::class);
284285
$this->expectExceptionMessage('foobar');
286+
if (PHPUnitVersion::series() < 10) {
287+
$this->expectException(SkippedTestError::class);
288+
} else {
289+
$this->expectException(SkippedWithMessageException::class);
290+
}
285291

286292
$this->module->markTestSkipped('foobar');
287293
}

0 commit comments

Comments
 (0)