1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SmartWeb\ModuleTesting\Generator\Test; |
5
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use function class_exists; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class BaseClassResolver |
11
|
|
|
* |
12
|
|
|
* @package SmartWeb\ModuleTesting\Generator\Test |
13
|
|
|
*/ |
14
|
|
|
class BaseClassResolver implements BaseClassResolverInterface |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Name used to group global base classes |
19
|
|
|
*/ |
20
|
|
|
const DEFAULT_MODULE = '_default'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string[][][] |
24
|
|
|
*/ |
25
|
|
|
private static $baseClasses = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $module; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $type; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $suite; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* BaseClassResolver constructor. |
44
|
|
|
* |
45
|
|
|
* @param CodeceptionTestType $type |
46
|
|
|
* @param string $suite |
47
|
|
|
* @param string|null $module |
48
|
|
|
*/ |
49
|
|
|
public function __construct(CodeceptionTestType $type, string $suite, string $module = null) |
50
|
|
|
{ |
51
|
|
|
$this->type = $type->key(); |
52
|
|
|
$this->suite = $suite; |
53
|
|
|
$this->module = $module ?? self::DEFAULT_MODULE; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritDoc |
58
|
|
|
*/ |
59
|
|
|
public function registerBaseClass($baseClass) |
60
|
|
|
{ |
61
|
|
|
$this->validateClass($baseClass); |
62
|
|
|
|
63
|
|
|
static::$baseClasses[$this->type][$this->suite][$this->module] = $baseClass; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
|
|
public function getBaseClass() : string |
70
|
|
|
{ |
71
|
|
|
if (isset(static::$baseClasses[$this->type][$this->suite][$this->module])) { |
|
|
|
|
72
|
|
|
return static::$baseClasses[$this->type][$this->suite][$this->module]; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
throw new InvalidArgumentException("No base class is set for type {$this->type}, suite {$this->suite}, module {$this->module}"); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public static function registerBaseClassForModule(CodeceptionTestType $type, string $suite, string $module, $baseClass) |
82
|
|
|
{ |
83
|
|
|
(new static($type, $suite, $module))->registerBaseClass($baseClass); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
|
|
public static function getBaseClassForModule(CodeceptionTestType $type, string $suite, string $module) : string |
90
|
|
|
{ |
91
|
|
|
return (new static($type, $suite, $module))->getBaseClass(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $class |
96
|
|
|
* |
97
|
|
|
* @throws BaseClassDoesNotExist |
98
|
|
|
*/ |
99
|
|
|
protected function validateClass(string $class) |
100
|
|
|
{ |
101
|
|
|
if (!class_exists($class)) { |
102
|
|
|
throw new BaseClassDoesNotExist($class); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: