Repository navigation
Expand file tree
/
Copy pathDescriptor.php
More file actions
121 lines (107 loc) · 3.87 KB
/
Copy pathDescriptor.php
File metadata and controls
121 lines (107 loc) · 3.87 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
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace Codeception\Test;
use Codeception\Test\Interfaces\Descriptive;
use Codeception\Test\Interfaces\Plain;
use Codeception\Util\ReflectionHelper;
use PHPUnit\Framework\SelfDescribing;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use function codecept_relative_path;
use function get_class;
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(SelfDescribing $testCase): string
{
if ($testCase instanceof Descriptive) {
return $testCase->getSignature();
}
if ($testCase instanceof TestCase) {
return get_class($testCase) . ':' . $testCase->getName(false);
}
return $testCase->toString();
}
/**
* 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
{
if ($testCase instanceof TestCase) {
$text = self::getTestCaseNameAsString($testCase->getName());
return ReflectionHelper::getClassShortName($testCase) . ': ' . $text;
}
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(SelfDescribing $testCase): string
{
if ($testCase instanceof Descriptive) {
return codecept_relative_path(realpath($testCase->getFileName()));
}
return (new ReflectionClass($testCase))->getFileName();
}
public static function getTestFullName(SelfDescribing $testCase): string
{
if ($testCase instanceof Plain) {
return self::getTestFileName($testCase);
}
if ($testCase instanceof Descriptive) {
$signature = $testCase->getSignature(); // cut everything before ":" from signature
return self::getTestFileName($testCase) . ':' . preg_replace('#^(.*?):#', '', $signature);
}
if ($testCase instanceof TestCase) {
return self::getTestFileName($testCase) . ':' . $testCase->getName(false);
}
return self::getTestFileName($testCase) . ':' . $testCase->toString();
}
/**
* Provides a test data set index
*/
public static function getTestDataSetIndex(SelfDescribing $testCase): ?int
{
if ($testCase instanceof Descriptive) {
return $testCase->getMetadata()->getIndex();
}
return null;
}
}