-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCodeGeneratorTest.php
More file actions
62 lines (54 loc) · 1.78 KB
/
CodeGeneratorTest.php
File metadata and controls
62 lines (54 loc) · 1.78 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
<?php
namespace PHPUnitTests\Extension\FunctionMocker;
use PHPUnit\Extension\FunctionMocker\CodeGenerator;
use PHPUnit\Framework\TestCase;
class CodeGeneratorTest extends TestCase
{
public function testGenerateFunctionMock()
{
$code = CodeGenerator::generateFunction('Test\Namespace', 'strlen');
$expected = <<<'EOS'
namespace Test\Namespace
{
function strlen(...$args)
{
if (!isset($GLOBALS['__PHPUNIT_EXTENSION_FUNCTIONMOCKER'][__NAMESPACE__])) {
return \strlen(...$args);
}
return $GLOBALS['__PHPUNIT_EXTENSION_FUNCTIONMOCKER'][__NAMESPACE__]->strlen(...$args);
}
}
EOS;
self::assertSame($expected, $code);
}
public function testGenerateStringConstantMock()
{
$code = CodeGenerator::generateConstant('Test\Namespace', 'CONSTANT', 'value');
$expected = <<<'EOS'
namespace Test\Namespace
{
if (!defined(__NAMESPACE__ . '\\CONSTANT')) {
define(__NAMESPACE__ . '\\CONSTANT', 'value');
} elseif (CONSTANT !== 'value') {
throw new \RuntimeException(sprintf('Cannot redeclare constant "CONSTANT" in namespace "%s". Already defined as "%s"', __NAMESPACE__, 'value'));
}
}
EOS;
self::assertSame($expected, $code);
}
public function testGenerateIntegerConstantMock(): void
{
$code = CodeGenerator::generateConstant('Test\Namespace', 'CONSTANT', 123);
$expected = <<<'EOS'
namespace Test\Namespace
{
if (!defined(__NAMESPACE__ . '\\CONSTANT')) {
define(__NAMESPACE__ . '\\CONSTANT', 123);
} elseif (CONSTANT !== 123) {
throw new \RuntimeException(sprintf('Cannot redeclare constant "CONSTANT" in namespace "%s". Already defined as "%s"', __NAMESPACE__, 123));
}
}
EOS;
self::assertSame($expected, $code);
}
}