1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Atrapalo\PHPTools\Enum; |
4
|
|
|
|
5
|
|
|
use Atrapalo\PHPTools\Parser\PHPDocClass; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class EnumPHPDocTester |
9
|
|
|
* @package Atrapalo\PHPTools\Enum |
10
|
|
|
* |
11
|
|
|
* @author Guillermo González <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
trait EnumPHPDocTester |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
abstract protected function enumClass(): string; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @test |
22
|
|
|
*/ |
23
|
1 |
|
public function staticAccessByPhpDoc() |
24
|
|
|
{ |
25
|
1 |
|
$className = $this->enumClass(); |
26
|
|
|
|
27
|
1 |
|
$staticMethods = PHPDocClass::staticMethods($className); |
28
|
1 |
|
if (!empty($staticMethods)) { |
29
|
1 |
|
foreach ($staticMethods as $staticMethod) { |
30
|
1 |
|
$data = call_user_func([$className, $staticMethod->name()]); |
31
|
|
|
|
32
|
1 |
|
$this->assertInstanceOf($className, $data); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
} else { |
35
|
|
|
$this->markTestSkipped('Skipped because no static methods were found for '.get_called_class()); |
|
|
|
|
36
|
|
|
} |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @test |
41
|
|
|
*/ |
42
|
1 |
|
public function accessByPhpDoc() |
43
|
|
|
{ |
44
|
1 |
|
$className = $this->enumClass(); |
45
|
|
|
|
46
|
1 |
|
$methods = PHPDocClass::methods($className); |
47
|
1 |
|
if (!empty($methods)) { |
48
|
|
|
|
49
|
1 |
|
$enum = $this->createValidEnum($className); |
50
|
1 |
|
$actualMethod = $this->snakeCaseToCamelCase("IS_".$enum->key()); |
51
|
1 |
|
foreach ($methods as $method) { |
52
|
1 |
|
$methodName = $method->name(); |
53
|
1 |
|
if ($methodName === $actualMethod) { |
54
|
1 |
|
$this->assertTrue($enum->$methodName()); |
|
|
|
|
55
|
|
|
} else { |
56
|
1 |
|
$this->assertFalse($enum->$methodName()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
|
|
$this->markTestSkipped('Skipped because no methods were found for '.get_called_class()); |
|
|
|
|
61
|
|
|
} |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Enum|string $className |
66
|
|
|
* @return Enum |
67
|
|
|
*/ |
68
|
1 |
|
private function createValidEnum($className) |
69
|
|
|
{ |
70
|
1 |
|
$values = $className::toArray(); |
71
|
|
|
|
72
|
1 |
|
return new $className($values[array_rand($values)]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $string |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
private function snakeCaseToCamelCase(string $string): string |
80
|
|
|
{ |
81
|
1 |
|
return preg_replace_callback('/_(.?)/', function($matches) { |
82
|
1 |
|
return ucfirst($matches[1]); |
83
|
1 |
|
}, strtolower($string)); |
84
|
|
|
} |
85
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.