-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathFunctionInjectorTest.php
More file actions
117 lines (100 loc) · 3.93 KB
/
FunctionInjectorTest.php
File metadata and controls
117 lines (100 loc) · 3.93 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
<?php
declare(strict_types=1);
namespace demo;
use AspectMock\Intercept\FunctionInjector;
use AspectMock\Test as test;
use PHPUnit\Framework\ExpectationFailedException;
final class FunctionInjectorTest extends \Codeception\Test\Unit
{
protected FunctionInjector $funcInjector;
protected FunctionInjector $funcOptionalParameterInjector;
protected FunctionInjector $funcReferencedParameterInjector;
public function _before()
{
$this->funcInjector = new FunctionInjector('demo', 'strlen');
$this->funcOptionalParameterInjector = new FunctionInjector('demo', 'explode');
$this->funcReferencedParameterInjector = new FunctionInjector('demo', 'preg_match');
test::clean();
}
public function testTemplate()
{
$php = $this->funcInjector->getPHP();
verify($php)->stringContainsString("function strlen()");
verify($php)->stringContainsString("return call_user_func_array('strlen', func_get_args());");
}
public function testReferencedParameterTemplate()
{
$php = $this->funcReferencedParameterInjector->getPHP();
verify($php)->stringContainsString("function preg_match(string \$p0, string \$p1, &\$p2 = null, int \$p3 = 0, int \$p4 = 0)");
verify($php)->stringContainsString("case 5: \$args = [\$p0, \$p1, &\$p2, \$p3, \$p4]; break;");
verify($php)->stringContainsString("case 4: \$args = [\$p0, \$p1, &\$p2, \$p3]; break;");
verify($php)->stringContainsString("case 3: \$args = [\$p0, \$p1, &\$p2]; break;");
verify($php)->stringContainsString("case 2: \$args = [\$p0, \$p1]; break;");
verify($php)->stringContainsString("case 1: \$args = [\$p0]; break;");
verify($php)->stringContainsString("return call_user_func_array('preg_match', \$args);");
}
public function testSave()
{
$this->funcInjector->save();
exec('php -l '.$this->funcInjector->getFileName(), $output, $code);
verify($code)->equals(0);
codecept_debug($this->funcInjector->getPHP());
}
public function testLoadFunc()
{
$this->funcInjector->save();
codecept_debug($this->funcInjector->getFileName());
$this->funcInjector->inject();
verify(strlen('hello'))->equals(5);
}
public function testReimplementFunc()
{
test::func('demo', 'strlen', 10);
verify(strlen('hello'))->equals(10);
}
public function testFuncReturnsNull()
{
test::func('demo', 'strlen', null);
verify(strlen('hello'))->equals(null);
}
public function testVerifier()
{
$func = test::func('demo', 'strlen', 10);
verify(strlen('hello'))->equals(10);
$func->verifyInvoked();
$func->verifyInvoked(['hello']);
$func->verifyInvokedOnce();
$func->verifyInvokedOnce(['hello']);
$func->verifyInvokedMultipleTimes(1, ['hello']);
$func->verifyNeverInvoked(['hee']);
}
public function testVerifierFullyQualifiedNamespace()
{
$func = test::func('\demo', 'strlen', 10);
verify(strlen('hello'))->equals(10);
$func->verifyInvoked();
$func->verifyInvoked(['hello']);
$func->verifyInvokedOnce();
$func->verifyInvokedOnce(['hello']);
$func->verifyInvokedMultipleTimes(1, ['hello']);
$func->verifyNeverInvoked(['hee']);
}
/**
* @test
*/
public function testFailedVerification()
{
$this->expectException(ExpectationFailedException::class);
$func = test::func('demo', 'strlen', function() { return 10; });
verify(strlen('hello'))->equals(10);
$func->verifyNeverInvoked();
}
public function testReferencedParameter()
{
$func = test::func('\demo', 'preg_match', 10);
verify(preg_match('@[0-9]+@', '1234', $match))->equals(10);
test::clean();
verify(preg_match('@[0-9]+@', '1234#', $match))->equals(1);
verify($match[0])->equals('1234');
}
}