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
<?php
namespace Codeception\Test;
use Codeception\Test\Interfaces\Descriptive;
use Codeception\Test\Interfaces\Plain;
use Codeception\Util\ReflectionHelper;
class Descriptor
{
/**
* Provides a test name which can be located by
*
* @param \PHPUnit\Framework\SelfDescribing $testCase
* @return string
*/
public static function getTestSignature(\PHPUnit\Framework\SelfDescribing $testCase)
{
if ($testCase instanceof Descriptive) {
return $testCase->getSignature();
}
if ($testCase instanceof \PHPUnit\Framework\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
*
* @param \PHPUnit\Framework\SelfDescribing $testCase
* @return string
*/
public static function getTestSignatureUnique(\PHPUnit\Framework\SelfDescribing $testCase)
{
$example = null;
if (is_callable([$testCase, 'getMetadata'])
&& $example = $testCase->getMetadata()->getCurrent('example')
) {
$example = ':' . substr(sha1(json_encode($example)), 0, 7);
}
return self::getTestSignature($testCase) . $example;
}
public static function getTestAsString(\PHPUnit\Framework\SelfDescribing $testCase)
{
if ($testCase instanceof \PHPUnit\Framework\TestCase) {
$text = $testCase->getName();
$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));
$text = str_replace(['::', 'with data set'], [':', '|'], $text);
return ReflectionHelper::getClassShortName($testCase) . ': ' . $text;
}
return $testCase->toString();
}
/**
* Provides a test file name relative to Codeception root
*
* @param \PHPUnit\Framework\SelfDescribing $testCase
* @return mixed
*/
public static function getTestFileName(\PHPUnit\Framework\SelfDescribing $testCase)
{
if ($testCase instanceof Descriptive) {
return codecept_relative_path(realpath($testCase->getFileName()));
}
return (new \ReflectionClass($testCase))->getFileName();
}
/**
* @param \PHPUnit\Framework\SelfDescribing $testCase
* @return mixed|string
*/
public static function getTestFullName(\PHPUnit\Framework\SelfDescribing $testCase)
{
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 \PHPUnit\Framework\TestCase) {
return self::getTestFileName($testCase) . ':' . $testCase->getName(false);
}
return self::getTestFileName($testCase) . ':' . $testCase->toString();
}
/**
* Provides a test data set index
*
* @param \PHPUnit\Framework\SelfDescribing $testCase
* @return int|null
*/
public static function getTestDataSetIndex(\PHPUnit\Framework\SelfDescribing $testCase)
{
if ($testCase instanceof Descriptive) {
return $testCase->getMetadata()->getIndex();
}
return null;
}
}