• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / plugin / 3766633246

pending completion
3766633246

push

github

twinh
feat(plugin): `BasePage` 增加 `pageInit` 事件

0 of 1 new or added line in 1 file covered. (0.0%)

84 existing lines in 24 files now uncovered.

902 of 2275 relevant lines covered (39.65%)

19.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.18
/src/Test/BaseTestCase.php
1
<?php
2

3
namespace Miaoxing\Plugin\Test;
4

5
use Miaoxing\Plugin\BaseService;
6
use PHPUnit\Framework\MockObject\MockObject;
7
use Wei\Base;
8
use Wei\Cls;
9
use Wei\Ret;
10
use Wei\ServiceTrait;
11
use Wei\Wei;
12

13
abstract class BaseTestCase extends \PHPUnit\Framework\TestCase
14
{
15
    use ServiceTrait;
16

17
    /**
18
     * @var Wei
19
     */
20
    protected $wei;
21

22
    /**
23
     * @var array
24
     */
25
    protected $mockServices = [];
26

27
    public function __construct($name = null, array $data = [], $dataName = '')
28
    {
29
        parent::__construct($name, $data, $dataName);
×
30

31
        $this->wei = wei();
×
32

33
        // 将当前测试用例作为一个服务,允许其他服务调用
34
        $this->wei->set('testCase', $this);
×
35
    }
36

37
    protected function tearDown(): void
38
    {
39
        $wei = $this->wei;
756✔
40

41
        // 移除被 mock 的服务
42
        foreach ($this->mockServices as $name) {
756✔
43
            $wei->remove($name);
12✔
44
        }
45

46
        $wei->req->clear();
756✔
47
        $wei->req->setOption('gets', []);
756✔
48
        $wei->res->setStatusCode(200);
756✔
49
        $wei->app->setOption('plugin', null);
756✔
50
        $wei->block->setOption('data', []);
756✔
51
    }
252✔
52

53
    /**
54
     * 获取服务的 Mock 对象
55
     *
56
     * @param string $class
57
     * @param array<string> $methods
58
     * @return BaseService&MockObject
59
     *
60
     * @phpstan-template T
61
     * @phpstan-param class-string<BaseService>&class-string<T> $class
62
     * @phpstan-return MockObject&BaseService&T
63
     */
64
    public function getServiceMock(string $class, array $methods = [])
65
    {
66
        /** @var MockObject&BaseService&T $service */
67
        $service = $this->getMockBuilder($class)
12✔
68
            ->onlyMethods($methods)
12✔
69
            ->getMock();
12✔
70

71
        $this->registerMockServices($class, $service);
12✔
72

73
        return $service;
12✔
74
    }
75

76
    /**
77
     * 获取模型的 Mock 对象
78
     *
79
     * @param string $class
80
     * @param array<string> $methods
81
     * @return BaseService&MockObject
82
     *
83
     * @phpstan-template T
84
     * @phpstan-param class-string<BaseService>&class-string<T> $class
85
     * @phpstan-return MockObject&BaseService&T
86
     */
87
    public function getModelServiceMock(string $class, array $methods = [])
88
    {
89
        $name = Cls::baseName($class);
×
90
        if ('Model' !== $name && 'Model' === substr($name, -5)) {
×
91
            $name = substr($name, 0, -5);
×
92
        }
93
        $str = $this->wei->str;
×
94
        $table = $str->pluralize($str->snake($name));
×
95

96
        /** @var MockObject&BaseService&T $model */
97
        $model = $this->getMockBuilder($class)
×
98
            ->onlyMethods($methods)
×
99
            ->setConstructorArgs([
×
100
                [
UNCOV
101
                    'table' => $table,
×
102
                ],
103
            ])
104
            ->getMock();
×
105

106
        $this->registerMockServices($class, $model);
×
107

108
        return $model;
×
109
    }
110

111
    /**
112
     * 记录 Mock 对象
113
     *
114
     * @param string $class
115
     * @param Base $service
116
     */
117
    protected function registerMockServices(string $class, Base $service)
118
    {
119
        $name = lcfirst(Cls::baseName($class));
12✔
120
        $service->setOption('wei', $this->wei);
12✔
121
        $this->wei->set($name, $service);
12✔
122
        $this->mockServices[] = $name;
12✔
123
    }
4✔
124

125
    /**
126
     * @param Ret $ret
127
     * @param string $message
128
     * @param string $assertMessage
129
     */
130
    public function assertRetSuc(Ret $ret, $message = null, $assertMessage = null)
131
    {
132
        $assertMessage = $this->buildRetMessage($ret, $assertMessage);
12✔
133

134
        $expected = ['code' => $ret->getOption('defaultSucCode')];
12✔
135
        if (null !== $message) {
12✔
136
            $expected['message'] = $message;
×
137
        }
138

139
        $this->assertArrayContains($expected, $ret->toArray(), $assertMessage);
12✔
140
    }
4✔
141

142
    /**
143
     * @param Ret $ret
144
     * @param string $message
145
     * @param string $assertMessage
146
     * @param mixed $code
147
     */
148
    public function assertRetErr(Ret $ret, $message = null, $code = null, $assertMessage = null)
149
    {
150
        $assertMessage = $this->buildRetMessage($ret, $assertMessage);
18✔
151
        $expected = [];
18✔
152

153
        if (null !== $code) {
18✔
154
            $expected['code'] = $code;
3✔
155
        }
156

157
        if (null !== $message) {
18✔
158
            $expected['message'] = $message;
15✔
159
        }
160

161
        $this->assertArrayContains($expected, $ret->toArray(), $assertMessage);
18✔
162
    }
6✔
163

164
    /**
165
     * 测试两个 Ret 的内容是否完全相等
166
     *
167
     * @param Ret $expected
168
     * @param Ret $actual
169
     * @param string $message
170
     */
171
    public function assertSameRet(Ret $expected, Ret $actual, string $message = ''): void
172
    {
173
        $this->assertSame($expected->toArray(), $actual->toArray(), $message);
9✔
174
    }
1✔
175

176
    public static function assertArrayContains($subset, $array, $message = '')
177
    {
178
        $array = array_intersect_key($array, array_flip(array_keys($subset)));
36✔
179
        parent::assertEquals($subset, $array, $message);
36✔
180
    }
12✔
181

182
    /**
183
     * 根据类名,查找HTML文本中的节点
184
     *
185
     * @param string $html
186
     * @param string $class
187
     * @return \DOMNodeList
188
     * @todo 换为DomCrawler或codecption
189
     */
190
    public function findByClass($html, $class)
191
    {
192
        $dom = new \DomDocument();
×
193
        libxml_use_internal_errors(true);
×
194
        $dom->loadHTML($html);
×
195
        libxml_clear_errors();
×
196
        $finder = new \DomXPath($dom);
×
197

198
        return $finder->query("//*[contains(@class, '$class')]");
×
199
    }
200

201
    protected function buildRetMessage($ret, $assertMessage = null)
202
    {
203
        return $assertMessage . ' ret is ' . json_encode($ret, \JSON_UNESCAPED_UNICODE);
30✔
204
    }
205

206
    /**
207
     * 获取当前测试类所在的插件名称
208
     *
209
     * @return string
210
     */
211
    protected function getPluginName()
212
    {
213
        // 类名如 MiaoxingTest\App\PluginTest
214
        // 分为3部分取第2部分
215
        return explode('\\', static::class, 3)[1];
×
216
    }
217
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc