-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathConditionalAssertion.php
More file actions
64 lines (51 loc) · 1.8 KB
/
ConditionalAssertion.php
File metadata and controls
64 lines (51 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types=1);
namespace Codeception\Step;
use Codeception\Exception\ConditionalAssertionFailed;
use Codeception\Lib\ModuleContainer;
use Codeception\Util\Template;
use PHPUnit\Framework\AssertionFailedError;
use function preg_replace;
use function str_replace;
use function ucfirst;
class ConditionalAssertion extends Assertion implements GeneratedStep
{
public function run(ModuleContainer $container = null): void
{
try {
parent::run($container);
} catch (AssertionFailedError $e) {
throw new ConditionalAssertionFailed($e->getMessage(), $e->getCode(), $e);
}
}
public function getAction(): string
{
$action = 'can' . ucfirst($this->action);
return (string)preg_replace('#^canDont#', 'cant', $action);
}
public function getHumanizedAction(): string
{
return $this->humanize($this->action . ' ' . $this->getHumanizedArguments());
}
public static function getTemplate(Template $template): ?Template
{
$action = $template->getVar('action');
if ((!str_starts_with($action, 'see')) && (!str_starts_with($action, 'dontSee'))) {
return null;
}
$conditionalDoc = "* [!] Conditional Assertion: Test won't be stopped on fail\n " . $template->getVar('doc');
$prefix = 'can';
if (str_starts_with($action, 'dontSee')) {
$prefix = 'cant';
$action = str_replace('dont', '', $action);
}
return $template
->place('doc', $conditionalDoc)
->place('action', $prefix . ucfirst($action))
->place('step', 'ConditionalAssertion');
}
public function match(string $name): bool
{
return str_starts_with($name, 'see') || str_starts_with($name, 'dontSee');
}
}