-
Notifications
You must be signed in to change notification settings - Fork 6
/
FunctionsTest.php
121 lines (100 loc) · 3.37 KB
/
FunctionsTest.php
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
118
119
120
121
<?php
declare(strict_types=1);
namespace Tests\Kernel;
use Leevel\Di\Container;
use Leevel\Http\Request;
use Leevel\I18n\II18n;
use Leevel\Kernel\App;
use Leevel\Kernel\Proxy\App as ProxyApp;
use Leevel\Kernel\Utils\Api;
use Tests\Kernel\Fixtures\DemoI18n;
use Tests\TestCase;
#[Api([
'zh-CN:title' => '内核助手函数',
'path' => 'architecture/kernel/functions',
'zh-CN:description' => <<<'EOT'
QueryPHP 在内核助手函数中为代理应用 `\Leevel\Kernel\Proxy\App` 提供了全局别名类 `\App`,提供简洁的访问入口。
例外还提供了一个语言包函数 `__`,为应用提供国际化支持。
EOT,
])]
final class FunctionsTest extends TestCase
{
#[\Override]
protected function setUp(): void
{
$this->tearDown();
}
#[\Override]
protected function tearDown(): void
{
Container::singletons()->clear();
}
#[Api([
'zh-CN:title' => 'App 应用代理别名类调用应用',
])]
public function testApp(): void
{
$this->createContainer();
self::assertSame(App::VERSION, ProxyApp::proxy()->version());
}
#[Api([
'zh-CN:title' => 'App 应用代理别名类调用 IOC 容器',
])]
public function testAppWithContainerMethod(): void
{
$this->createContainer();
self::assertNull(ProxyApp::proxy()->make('foo', throw: false));
}
#[Api([
'zh-CN:title' => '全局语言函数 __()',
])]
public function testFunctionLang(): void
{
$this->createContainer();
self::assertNull(ProxyApp::proxy()->make('foo', throw: false));
$i18n = $this->createMock(II18n::class);
$map = [
['hello', 'hello'],
['hello %s', 'foo', 'hello foo'],
['hello %d', 5, 'hello 5'],
];
$i18n->method('gettext')->willReturnMap($map);
self::assertSame('hello', $i18n->gettext('hello'));
self::assertSame('hello foo', $i18n->gettext('hello %s', 'foo'));
self::assertSame('hello 5', $i18n->gettext('hello %d', 5));
$container = $this->createContainer();
$container->singleton('i18n', static function () use ($i18n): II18n {
return $i18n;
});
self::assertSame('hello', __('hello'));
self::assertSame('hello foo', __('hello %s', 'foo'));
self::assertSame('hello 5', __('hello %d', 5));
}
public function test1(): void
{
$i18n = new DemoI18n();
$container = $this->createContainer();
$container->singleton('i18n', static function () use ($i18n): DemoI18n {
return $i18n;
});
self::assertSame('hello', __('hello'));
self::assertSame('hello foo', __('hello %s', 'foo'));
self::assertSame('hello 5', __('hello %d', 5));
}
protected function createContainer(): Container
{
$container = Container::singletons();
$container->clear();
$container->instance('app', new App($container, ''));
return $container;
}
protected function makeRequest(bool $isSecure = false): Request
{
$request = $this->createMock(Request::class);
$request->method('getEnter')->willReturn('');
self::assertSame('', $request->getEnter());
$request->method('isSecure')->willReturn($isSecure);
self::assertSame($isSecure, $request->isSecure());
return $request;
}
}