Skip to content

Commit 1d3b80b

Browse files
authored
support codeception 5 (fixes #99, via #107)
1 parent 47f6707 commit 1d3b80b

16 files changed

+125
-118
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
php-version:
2020
- "8.0"
2121
- "8.1"
22+
- "8.2"
2223
os:
2324
- ubuntu-latest
2425
- windows-latest
@@ -48,10 +49,10 @@ jobs:
4849
${{ matrix.composer-options }}
4950

5051
- name: Run tests
51-
if: ${{ matrix.php-version != '8.1' }}
52+
if: ${{ matrix.php-version != '8.2' }}
5253
run: composer test
5354

5455
- name: Run tests (experimental)
55-
if: ${{ matrix.php-version == '8.1' }}
56+
if: ${{ matrix.php-version == '8.2' }}
5657
continue-on-error: true
5758
run: composer test

composer.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,16 @@
2323
"require": {
2424
"php": "^8",
2525
"ext-json": "*",
26-
"codeception/codeception": "^4.1",
26+
"codeception/codeception": "^5",
2727
"allure-framework/allure-php-commons": "^2"
2828
},
2929
"require-dev": {
3030
"phpunit/phpunit": "^9",
31-
"psalm/plugin-phpunit": "^0.16.1",
31+
"psalm/plugin-phpunit": "^0.18.4",
3232
"remorhaz/php-json-data": "^0.5.3",
3333
"remorhaz/php-json-path": "^0.7.7",
34-
"squizlabs/php_codesniffer": "^3.6.2",
35-
"vimeo/psalm": "^4.20"
36-
},
37-
"conflict": {
38-
"codeception/phpunit-wrapper": "<9.0.1"
34+
"squizlabs/php_codesniffer": "^3.7.1",
35+
"vimeo/psalm": "^5.2"
3936
},
4037
"autoload": {
4138
"psr-4": {

src/AllureCodeception.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Codeception\Event\TestEvent;
1313
use Codeception\Events;
1414
use Codeception\Exception\ConfigurationException;
15-
use Codeception\Step;
1615
use Qameta\Allure\Allure;
1716
use Qameta\Allure\Allure as QametaAllure;
1817
use Qameta\Allure\Codeception\Internal\DefaultThreadDetector;
@@ -26,7 +25,6 @@
2625
use Qameta\Allure\Setup\DefaultStatusDetector;
2726
use Qameta\Allure\Setup\LinkTemplate;
2827
use Qameta\Allure\Setup\LinkTemplateInterface;
29-
use Throwable;
3028

3129
use function class_exists;
3230
use function is_a;
@@ -46,6 +44,7 @@ final class AllureCodeception extends Extension
4644
private const DEFAULT_RESULTS_DIRECTORY = 'allure-results';
4745

4846
protected static array $events = [
47+
Events::MODULE_INIT => 'moduleInit',
4948
Events::SUITE_BEFORE => 'suiteBefore',
5049
Events::SUITE_AFTER => 'suiteAfter',
5150
Events::TEST_START => 'testStart',
@@ -69,11 +68,11 @@ final class AllureCodeception extends Extension
6968
* @throws ConfigurationException
7069
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
7170
*/
72-
public function _initialize(): void
71+
public function moduleInit(): void
7372
{
74-
// phpcs:enable PSR2.Methods.MethodDeclaration.Underscore
75-
parent::_initialize();
7673
QametaAllure::reset();
74+
$this->testLifecycle = null;
75+
$this->threadDetector = null;
7776
QametaAllure::getLifecycleConfigurator()
7877
->setStatusDetector(new StatusDetector(new DefaultStatusDetector()))
7978
->setOutputDirectory($this->getOutputDirectory());
@@ -148,7 +147,11 @@ class_exists($linkConfig) && is_a($linkConfig, LinkTemplateInterface::class, tru
148147
public function suiteBefore(SuiteEvent $suiteEvent): void
149148
{
150149
/** @psalm-suppress InternalMethod */
151-
$suiteName = $suiteEvent->getSuite()->getName();
150+
$suiteName = $suiteEvent->getSuite()?->getName();
151+
if (!isset($suiteName)) {
152+
return;
153+
}
154+
152155
$this
153156
->getTestLifecycle()
154157
->switchToSuite(new SuiteInfo($suiteName));
@@ -185,33 +188,28 @@ private function getThreadDetector(): ThreadDetectorInterface
185188
*/
186189
public function testError(FailEvent $failEvent): void
187190
{
188-
/** @var Throwable $error */
189-
$error = $failEvent->getFail();
190191
$this
191192
->getTestLifecycle()
192193
->switchToTest($failEvent->getTest())
193-
->updateTestFailure($error);
194+
->updateTestFailure($failEvent->getFail());
194195
}
195196

196197
/**
197198
* @psalm-suppress MissingDependency
198199
*/
199200
public function testFail(FailEvent $failEvent): void
200201
{
201-
/** @var Throwable $error */
202-
$error = $failEvent->getFail();
203202
$this
204203
->getTestLifecycle()
205204
->switchToTest($failEvent->getTest())
206-
->updateTestFailure($error, Status::failed());
205+
->updateTestFailure($failEvent->getFail(), Status::failed());
207206
}
208207

209208
/**
210209
* @psalm-suppress MissingDependency
211210
*/
212211
public function testIncomplete(FailEvent $failEvent): void
213212
{
214-
/** @var Throwable $error */
215213
$error = $failEvent->getFail();
216214
$this
217215
->getTestLifecycle()
@@ -228,7 +226,6 @@ public function testIncomplete(FailEvent $failEvent): void
228226
*/
229227
public function testSkipped(FailEvent $failEvent): void
230228
{
231-
/** @var Throwable $error */
232229
$error = $failEvent->getFail();
233230
$this
234231
->getTestLifecycle()
@@ -269,12 +266,10 @@ public function testEnd(TestEvent $testEvent): void
269266
*/
270267
public function stepBefore(StepEvent $stepEvent): void
271268
{
272-
/** @psalm-var Step $step */
273-
$step = $stepEvent->getStep();
274269
$this
275270
->getTestLifecycle()
276271
->switchToTest($stepEvent->getTest())
277-
->startStep($step)
272+
->startStep($stepEvent->getStep())
278273
->updateStep();
279274
}
280275

@@ -283,12 +278,10 @@ public function stepBefore(StepEvent $stepEvent): void
283278
*/
284279
public function stepAfter(StepEvent $stepEvent): void
285280
{
286-
/** @psalm-var Step $step */
287-
$step = $stepEvent->getStep();
288281
$this
289282
->getTestLifecycle()
290283
->switchToTest($stepEvent->getTest())
291-
->switchToStep($step)
284+
->switchToStep($stepEvent->getStep())
292285
->updateStepResult()
293286
->stopStep();
294287
}

src/Internal/ArgumentAsString.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Qameta\Allure\Codeception\Internal;
66

7-
use Codeception\Util\Locator;
7+
use InvalidArgumentException;
88
use Stringable;
99

1010
use function array_map;
@@ -15,6 +15,7 @@
1515
use function is_resource;
1616
use function is_string;
1717
use function json_encode;
18+
use function method_exists;
1819
use function strtr;
1920
use function trim;
2021

@@ -77,7 +78,7 @@ private function prepareObject(object $argument): string
7778

7879
$webdriverByClass = '\Facebook\WebDriver\WebDriverBy';
7980
if (class_exists($webdriverByClass) && is_a($argument, $webdriverByClass)) {
80-
return Locator::humanReadableString($argument);
81+
return $this->webDriverByAsString($argument);
8182
}
8283

8384
return trim($argument::class, "\\");
@@ -90,4 +91,21 @@ public function __toString(): string
9091
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
9192
);
9293
}
94+
95+
private function webDriverByAsString(object $selector): string
96+
{
97+
$type = method_exists($selector, 'getMechanism')
98+
? (string) $selector->getMechanism()
99+
: null;
100+
101+
$locator = method_exists($selector, 'getValue')
102+
? (string) $selector->getValue()
103+
: null;
104+
105+
if (!isset($type, $locator)) {
106+
throw new InvalidArgumentException("Unrecognized selector");
107+
}
108+
109+
return "$type '$locator'";
110+
}
93111
}

src/Internal/CeptInfoBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public function build(?string $host, ?string $thread): TestInfo
1717
{
1818
return new TestInfo(
1919
originalTest: $this->test,
20-
signature: (string) $this->test->getSignature(),
21-
class: (string) $this->test->getName(),
22-
method: (string) $this->test->getName(),
20+
signature: $this->test->getSignature(),
21+
class: $this->test->getName(),
22+
method: $this->test->getName(),
2323
host: $host,
2424
thread: $thread,
2525
);

src/Internal/CeptProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function getDisplayName(): ?string
100100

101101
public function getFullName(): ?string
102102
{
103-
return (string) $this->test->getSignature();
103+
return $this->test->getSignature();
104104
}
105105

106106
public function getDescription(): ?string
@@ -118,7 +118,7 @@ public function getDescriptionHtml(): ?string
118118
private function getLegacyAnnotation(string $name): ?string
119119
{
120120
/**
121-
* @var mixed $annotations
121+
* @psalm-var mixed $annotations
122122
* @psalm-suppress InvalidArgument
123123
*/
124124
$annotations = $this->test->getMetadata()->getParam($name);
@@ -140,7 +140,7 @@ private function getLegacyAnnotation(string $name): ?string
140140
private function getLegacyAnnotations(string $name): array
141141
{
142142
/**
143-
* @var mixed $annotations
143+
* @psalm-var mixed $annotations
144144
* @psalm-suppress InvalidArgument
145145
*/
146146
$annotations = $this->test->getMetadata()->getParam($name);

src/Internal/CestInfoBuilder.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Codeception\Test\Cest;
88

99
use function is_int;
10-
use function is_object;
1110
use function is_string;
1211

1312
final class CestInfoBuilder implements TestInfoBuilderInterface
@@ -19,16 +18,11 @@ public function __construct(
1918

2019
public function build(?string $host, ?string $thread): TestInfo
2120
{
22-
/** @var mixed $testClass */
23-
$testClass = $this->test->getTestClass();
24-
/** @var mixed $testMethod */
25-
$testMethod = $this->test->getTestMethod();
26-
2721
return new TestInfo(
2822
originalTest: $this->test,
29-
signature: (string) $this->test->getSignature(),
30-
class: is_object($testClass) ? $testClass::class : null,
31-
method: is_string($testMethod) ? $testMethod : null,
23+
signature: $this->test->getSignature(),
24+
class: $this->test->getTestInstance()::class,
25+
method: $this->test->getTestMethod(),
3226
dataLabel: $this->getDataLabel(),
3327
host: $host,
3428
thread: $thread,

src/Internal/CestProvider.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use function array_values;
1717
use function is_array;
1818
use function is_int;
19-
use function is_object;
2019
use function is_string;
2120

2221
/**
@@ -37,16 +36,12 @@ public function __construct(
3736
*/
3837
public static function createForChain(Cest $test, LinkTemplateCollectionInterface $linkTemplates): array
3938
{
40-
/** @var mixed $testClass */
41-
$testClass = $test->getTestClass();
42-
/** @var mixed $testMethod */
43-
$testMethod = $test->getTestMethod();
44-
/** @var callable-string|null $callableTestMethod */
45-
$callableTestMethod = is_string($testMethod) ? $testMethod : null;
39+
/** @psalm-var callable-string $callableTestMethod */
40+
$callableTestMethod = $test->getTestMethod();
4641

4742
return [
4843
...AttributeParser::createForChain(
49-
classOrObject: is_object($testClass) ? $testClass : null,
44+
classOrObject: $test->getTestInstance(),
5045
methodOrFunction: $callableTestMethod,
5146
linkTemplates: $linkTemplates,
5247
),
@@ -111,6 +106,6 @@ public function getDescriptionHtml(): ?string
111106
*/
112107
public function getFullName(): ?string
113108
{
114-
return get_class($this->test->getTestClass()) . "::" . $this->test->getTestMethod();
109+
return $this->test->getTestInstance()::class . "::" . $this->test->getTestMethod();
115110
}
116111
}

src/Internal/GherkinInfoBuilder.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@ public function __construct(
1717

1818
public function build(?string $host, ?string $thread): TestInfo
1919
{
20-
/** @psalm-var mixed $className */
21-
$className = $this->test->getFeature();
22-
/** @psalm-var mixed $methodName */
23-
$methodName = $this->test->getScenarioTitle();
24-
2520
return new TestInfo(
2621
originalTest: $this->test,
27-
signature: (string) $this->test->getSignature(),
28-
class: is_string($className) ? $className : null,
29-
method: is_string($methodName) ? $methodName : null,
22+
signature: $this->test->getSignature(),
23+
class: $this->test->getFeature(),
24+
method: $this->test->getScenarioTitle(),
3025
host: $host,
3126
thread: $thread,
3227
);

src/Internal/GherkinProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function getParameters(): array
4949

5050
public function getDisplayName(): ?string
5151
{
52-
return (string) $this->test->toString();
52+
return $this->test->toString();
5353
}
5454

5555
public function getDescription(): ?string

0 commit comments

Comments
 (0)