Repository navigation
Expand file tree
/
Copy pathDescriptor.php
More file actions
109 lines (94 loc) · 3.17 KB
/
Copy pathDescriptor.php
File metadata and controls
109 lines (94 loc) · 3.17 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
namespace Codeception\Test;
use Codeception\Test\Interfaces\Descriptive;
use Codeception\Test\Interfaces\Plain;
use Codeception\TestInterface;
use PHPUnit\Framework\SelfDescribing;
use function codecept_relative_path;
use function json_encode;
use function method_exists;
use function preg_replace;
use function realpath;
use function sha1;
use function str_replace;
use function strtolower;
use function substr;
use function ucfirst;
class Descriptor
{
/**
* Provides a test name which can be located by
*/
public static function getTestSignature(Descriptive $test): string
{
return $test->getSignature();
}
/**
* Provides a test name which is unique for individual iterations of tests using examples
*/
public static function getTestSignatureUnique(SelfDescribing $testCase): string
{
$env = '';
$example = '';
if (
method_exists($testCase, 'getScenario')
&& !empty($testCase->getScenario()->current('env'))
) {
$env = ':' . $testCase->getScenario()->current('env');
}
if (
method_exists($testCase, 'getMetaData')
&& !empty($testCase->getMetadata()->getCurrent('example'))
) {
$currentExample = json_encode($testCase->getMetadata()->getCurrent('example'), JSON_THROW_ON_ERROR);
$example = ':' . substr(sha1($currentExample), 0, 7);
}
return self::getTestSignature($testCase) . $env . $example;
}
public static function getTestAsString(SelfDescribing $testCase): string
{
return $testCase->toString();
}
public static function getTestCaseNameAsString(string $testCaseName): string
{
$text = $testCaseName;
$text = preg_replace('#([A-Z]+)([A-Z][a-z])#', '\\1 \\2', $text);
$text = preg_replace('#([a-z\d])([A-Z])#', '\\1 \\2', $text);
$text = preg_replace('#^test #', '', $text);
$text = ucfirst(strtolower($text));
return str_replace(['::', 'with data set'], [':', '|'], $text);
}
/**
* Provides a test file name relative to Codeception root
*/
public static function getTestFileName(Descriptive $test): string
{
return codecept_relative_path(realpath($test->getFileName()));
}
public static function getTestFullName(Plain|Descriptive $test): string
{
if ($test instanceof Plain) {
return self::getTestFileName($test);
}
$signature = $test->getSignature(); // cut everything before ":" from signature
return self::getTestFileName($test) . ':' . preg_replace('#^(.*?):#', '', $signature);
}
/**
* Provides a test data set index
*/
public static function getTestDataSetIndex(SelfDescribing $testCase): string
{
if ($testCase instanceof TestInterface) {
$index = $testCase->getMetadata()->getIndex();
if ($index === null) {
return '';
}
if (is_int($index)) {
return ' with data set #' . $index;
}
return ' with data set "' . $index . '"';
}
return '';
}
}