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

miaoxing / plugin / 7322930040

25 Dec 2023 04:02PM UTC coverage: 37.918% (-1.7%) from 39.661%
7322930040

push

github

twinh
ci: add PHP 8, remove PHP 7.2, 7.3

907 of 2392 relevant lines covered (37.92%)

5.95 hits per line

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

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

3
namespace Miaoxing\Plugin\Test;
4

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

12
#[\AllowDynamicProperties]
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;
241✔
40

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

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

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

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

73
        return $service;
4✔
74
    }
75

76
    /**
77
     * 获取模型的 Mock 对象
78
     *
79
     * @param string $class
80
     * @param array<string> $methods
81
     * @return Base&MockObject
82
     *
83
     * @phpstan-template T
84
     * @phpstan-param class-string<Base>&class-string<T> $class
85
     * @phpstan-return MockObject&Base&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&Base&T $model */
97
        $model = $this->getMockBuilder($class)
×
98
            ->onlyMethods($methods)
×
99
            ->setConstructorArgs([
×
100
                [
×
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));
4✔
120
        $service->setOption('wei', $this->wei);
4✔
121
        $this->wei->set($name, $service);
4✔
122
        $this->mockServices[] = $name;
4✔
123
    }
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);
4✔
133

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

139
        $this->assertArrayContains($expected, $ret->toArray(), $assertMessage);
4✔
140
    }
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);
6✔
151
        $expected = [];
6✔
152

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

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

161
        $this->assertArrayContains($expected, $ret->toArray(), $assertMessage);
6✔
162
    }
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);
3✔
174
    }
175

176
    public static function assertArrayContains($subset, $array, $message = '')
177
    {
178
        $array = array_intersect_key($array, array_flip(array_keys($subset)));
12✔
179
        parent::assertEquals($subset, $array, $message);
12✔
180
    }
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);
10✔
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