|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Reactor; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Spiral\Reactor\Aggregator\Methods; |
|
16
|
|
|
use Spiral\Reactor\Partial; |
|
17
|
|
|
|
|
18
|
|
|
class MethodsTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @dataProvider staticMethodDataProvider |
|
22
|
|
|
* @param bool $static |
|
23
|
|
|
* @param string $body |
|
24
|
|
|
*/ |
|
25
|
|
|
public function testStatic(bool $static, string $body): void |
|
26
|
|
|
{ |
|
27
|
|
|
$methods = new Methods([]); |
|
28
|
|
|
$method = $methods->get('sample'); |
|
29
|
|
|
$method->parameter('input')->setType('int'); |
|
30
|
|
|
$method->parameter('output')->setType('int')->setDefaultValue(null)->setPBR(true); |
|
31
|
|
|
$method->setAccess(Partial\Method::ACCESS_PUBLIC)->setStatic($static); |
|
32
|
|
|
|
|
33
|
|
|
$method->setSource([ |
|
34
|
|
|
'$output = $input;', |
|
35
|
|
|
'return true;' |
|
36
|
|
|
]); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertSame( |
|
39
|
|
|
preg_replace('/\s+/', '', $body), |
|
40
|
|
|
preg_replace('/\s+/', '', $method->render()) |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function staticMethodDataProvider(): array |
|
45
|
|
|
{ |
|
46
|
|
|
$body = 'public static function sample(int $input, int &$output = null) |
|
47
|
|
|
{ |
|
48
|
|
|
$output = $input; |
|
49
|
|
|
return true; |
|
50
|
|
|
}'; |
|
51
|
|
|
|
|
52
|
|
|
return [ |
|
53
|
|
|
[true, $body], |
|
54
|
|
|
[false, str_replace('static function', 'function', $body)], |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @dataProvider returnMethodDataProvider |
|
60
|
|
|
* @param string $returnType |
|
61
|
|
|
* @param string $body |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testReturnType(string $returnType, string $body): void |
|
64
|
|
|
{ |
|
65
|
|
|
$methods = new Methods([]); |
|
66
|
|
|
$method = $methods->get('sample'); |
|
67
|
|
|
$method->parameter('input')->setType('int'); |
|
68
|
|
|
$method->parameter('output')->setType('int')->setDefaultValue(null)->setPBR(true); |
|
69
|
|
|
$method->setAccess(Partial\Method::ACCESS_PUBLIC); |
|
70
|
|
|
$method->setReturn($returnType); |
|
71
|
|
|
|
|
72
|
|
|
$method->setSource([ |
|
73
|
|
|
'$output = $input;', |
|
74
|
|
|
'return true;' |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertSame( |
|
78
|
|
|
preg_replace('/\s+/', '', $body), |
|
79
|
|
|
preg_replace('/\s+/', '', $method->render()) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function returnMethodDataProvider(): array |
|
84
|
|
|
{ |
|
85
|
|
|
return [ |
|
86
|
|
|
[ |
|
87
|
|
|
'', |
|
88
|
|
|
'public function sample(int $input, int &$output = null) |
|
89
|
|
|
{ |
|
90
|
|
|
$output = $input; |
|
91
|
|
|
return true; |
|
92
|
|
|
}' |
|
93
|
|
|
], |
|
94
|
|
|
[ |
|
95
|
|
|
'void', |
|
96
|
|
|
'public function sample(int $input, int &$output = null): void |
|
97
|
|
|
{ |
|
98
|
|
|
$output = $input; |
|
99
|
|
|
return true; |
|
100
|
|
|
}' |
|
101
|
|
|
], |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|