• 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

0.68
/src/Service/Tester.php
1
<?php
2

3
namespace Miaoxing\Plugin\Service;
4

5
use Miaoxing\Plugin\RetException;
6
use Wei\Req;
7

8
/**
9
 * 测试
10
 *
11
 * @mixin \ReqMixin
12
 */
13
class Tester extends \Miaoxing\Plugin\BaseService
14
{
15
    protected static $createNewInstance = true;
16

17
    /**
18
     * @var string
19
     */
20
    protected $controller;
21

22
    /**
23
     * @var string
24
     */
25
    protected $action = 'index';
26

27
    /**
28
     * @var string
29
     */
30
    protected $method = 'GET';
31

32
    /**
33
     * @var array
34
     */
35
    protected $request = [];
36

37
    /**
38
     * @var array
39
     */
40
    protected $data = [];
41

42
    /**
43
     * @var array
44
     */
45
    protected $query = [];
46

47
    /**
48
     * @var array
49
     */
50
    protected $session = [];
51

52
    /**
53
     * @var string
54
     */
55
    protected $content;
56

57
    /**
58
     * @var string
59
     */
60
    protected $dataType;
61

62
    /**
63
     * @var mixed
64
     */
65
    protected $response;
66

67
    /**
68
     * @param string|null $controller
69
     * @param string|null $action
70
     * @return static
71
     */
72
    public function __invoke($controller = null, $action = null)
73
    {
74
        return new static([
×
75
            'wei' => $this->wei,
×
UNCOV
76
            'controller' => $controller,
×
77
            'action' => $action ?: $this->action,
×
78
        ]);
79
    }
80

81
    /**
82
     * @param string $controller
83
     * @return $this
84
     */
85
    public function controller($controller)
86
    {
87
        $this->controller = $controller;
×
88

89
        return $this;
×
90
    }
91

92
    /**
93
     * @param string $action
94
     * @return $this
95
     */
96
    public function action($action)
97
    {
98
        $this->action = $action;
×
99

100
        return $this;
×
101
    }
102

103
    /**
104
     * @param string $method
105
     * @return $this
106
     */
107
    public function method($method)
108
    {
109
        $this->method = $method;
×
110

111
        return $this;
×
112
    }
113

114
    /**
115
     * @param array $query
116
     * @return $this
117
     * @svc
118
     */
119
    protected function query(array $query)
120
    {
121
        $this->query = $query;
×
122

123
        return $this;
×
124
    }
125

126
    /**
127
     * @param array $data
128
     * @return $this
129
     */
130
    public function data(array $data)
131
    {
132
        $this->data = $data;
×
133

134
        return $this;
×
135
    }
136

137
    /**
138
     * @param array $session
139
     * @return $this
140
     */
141
    public function session(array $session)
142
    {
143
        $this->session = $session;
×
144

145
        return $this;
×
146
    }
147

148
    /**
149
     * @param string $content
150
     * @return $this
151
     */
152
    public function content($content)
153
    {
154
        $this->content = $content;
×
155

156
        return $this;
×
157
    }
158

159
    /**
160
     * @param string $dataType
161
     * @return $this
162
     */
163
    public function dataType($dataType)
164
    {
165
        $this->dataType = $dataType;
×
166

167
        return $this;
×
168
    }
169

170
    /**
171
     * @return $this
172
     */
173
    public function json()
174
    {
175
        return $this->dataType('json');
×
176
    }
177

178
    /**
179
     * 调用指定的控制器盒方法
180
     *
181
     * @return $this
182
     * @throws \Exception
183
     */
184
    public function exec()
185
    {
186
        if (!$this->controller) {
×
187
            $traces = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 3);
×
188
            if (isset($traces[1]['class']) && __CLASS__ !== $traces[1]['class']) {
×
189
                $match = $traces[1];
×
190
            } else {
191
                $match = $traces[2];
×
192
            }
193
            $this->controller = $this->getControllerByClass($match['class']);
×
194
            $this->action = $this->getActionByMethod($match['function']);
×
195
        }
196

197
        $wei = $this->wei;
×
198

199
        // 1. 注入各种配置
200
        $this->req->clear();
×
201
        $this->req->set($this->request);
×
202
        $this->req->setOption('gets', $this->query);
×
203
        $this->req->setOption('posts', $this->data);
×
204
        $this->req->setMethod($this->method);
×
205
        $this->req->set('_format', $this->dataType);
×
206
        $this->req->setContent($this->content);
×
207

208
        $wei->session->set($this->session);
×
209

210
        // 2. 调用相应的URL
211
        ob_start();
×
212
        try {
213
            $res = $wei->app->dispatch($this->controller, $this->action);
×
214
            $this->response = $this->parseResponse($res->getContent(), $exception);
×
215
        } catch (\Exception $e) {
×
216
            ob_end_clean();
×
217
            throw $e;
×
218
        }
219
        ob_end_clean();
×
220

221
        // 3. 还原原来的配置
222
        $this->req->clear();
×
223
        $this->req->setOption('gets', []);
×
224
        $this->req->setOption('posts', []);
×
225
        $this->req->setMethod('GET');
×
226
        $this->req->set('_format', '');
×
227
        $this->req->setContent('');
×
228

229
        foreach ($this->session as $key => $value) {
×
230
            $wei->session->remove($key);
×
231
        }
232

233
        return $this;
×
234
    }
235

236
    /**
237
     * @return string|array
238
     * @throws \Exception
239
     */
240
    public function response()
241
    {
242
        if (null === $this->response) {
×
243
            $this->exec();
×
244
        }
245

246
        return $this->response;
×
247
    }
248

249
    /**
250
     * Helper: 设置登录用户
251
     *
252
     * @param int $userId
253
     * @return $this
254
     */
255
    public function login($userId = 1)
256
    {
257
        return $this->session(['user' => ['id' => $userId]]);
×
258
    }
259

260
    /**
261
     * Helper: 销毁用户登录状态
262
     *
263
     * @return $this
264
     */
265
    public function logout()
266
    {
267
        unset($this->session['user']);
×
268

269
        return $this;
×
270
    }
271

272
    /**
273
     * 运行任务
274
     *
275
     * @param string $name
276
     * @param array $data
277
     * @return array
278
     */
279
    public function runJob($name, array $data = [])
280
    {
281
        $parts = explode('/', $name);
×
282
        $action = array_pop($parts);
×
283
        $controller = implode('/', $parts);
×
284

285
        return wei()->tester()
×
286
            ->login(1)
×
287
            ->controller($controller)
×
288
            ->action($action)
×
289
            ->req($data)
×
290
            ->json()
×
291
            ->exec()
×
292
            ->response();
×
293
    }
294

295
    public function req($data = [])
296
    {
297
        if (!User::isLogin()) {
×
298
            $this->login();
×
299
        }
300

301
        return $this->request($data)
×
302
            ->json()
×
303
            ->exec()
×
304
            ->response();
×
305
    }
306

307
    /**
308
     * @param string $page
309
     * @return mixed
310
     * @svc
311
     */
312
    protected function get(string $page)
313
    {
314
        return $this->call($page, 'get');
×
315
    }
316

317
    /**
318
     * Execute a POST request
319
     *
320
     * @param string $page
321
     * @return mixed
322
     * @svc
323
     */
324
    protected function post(string $page)
325
    {
326
        return $this->call($page, 'post');
3✔
327
    }
328

329
    /**
330
     * @param array $request
331
     * @return $this
332
     * @svc
333
     */
334
    protected function request(array $request)
335
    {
336
        $this->request = $request;
×
337

338
        return $this;
×
339
    }
340

341
    /**
342
     * @param string $page
343
     * @param string $method
344
     * @return mixed
345
     * @svc
346
     */
347
    protected function call(string $page, string $method)
348
    {
349
        $wei = $this->wei;
×
350

351
        // 1. 注入各种配置
352
        $this->req->clear();
×
353
        $this->req->set($this->request);
×
354
        $this->req->setOption('gets', $this->query);
×
355
        $this->req->setOption('posts', $this->data);
×
356
        $this->req->setMethod($method);
×
357
        $this->req->set('_format', $this->dataType);
×
358
        $this->req->setContent($this->content);
×
359

360
        $wei->session->set($this->session);
×
361

362
        // 2. 调用相应的URL
363
        ob_start();
×
364
        try {
365
            $result = wei()->pageRouter->match($page);
×
366
            $this->req->set($result['params']);
×
367
            $page = require $result['file'];
×
368
            $ret = $page->{$method}($this->req, $wei->res);
×
369
        } catch (RetException $e) {
×
370
            $ret = err($e->getMessage(), $e->getCode());
×
371
        } catch (\Exception $e) {
×
372
            ob_end_clean();
×
373
            throw $e;
×
374
        }
375
        ob_end_clean();
×
376

377
        // 3. 还原原来的配置
378
        $this->req->clear();
×
379
        $this->req->setOption('gets', []);
×
380
        $this->req->setOption('posts', []);
×
381
        $this->req->setMethod('GET');
×
382
        $this->req->set('_format', '');
×
383
        $this->req->setContent('');
×
384

385
        foreach ($this->session as $key => $value) {
×
386
            $wei->session->remove($key);
×
387
        }
388

389
        return $ret;
×
390
    }
391

392
    /**
393
     * Set the request service
394
     *
395
     * @param Req $req
396
     * @return $this
397
     * @svc
398
     */
399
    protected function setReq(Req $req)
400
    {
401
        $this->req = $req;
×
402
        return $this;
×
403
    }
404

405
    /**
406
     * @param string $page
407
     * @return mixed
408
     * @svc
409
     */
410
    protected function patch(string $page)
411
    {
412
        return $this->call($page, 'patch');
×
413
    }
414

415
    /**
416
     * @param string $page
417
     * @return mixed
418
     * @svc
419
     */
420
    protected function put(string $page)
421
    {
422
        return $this->call($page, 'put');
×
423
    }
424

425
    /**
426
     * @param string $page
427
     * @return mixed
428
     * @svc
429
     */
430
    protected function delete(string $page)
431
    {
432
        return $this->call($page, 'delete');
×
433
    }
434

435
    /**
436
     * @param string $page
437
     * @return mixed
438
     * @svc
439
     */
440
    protected function getAdminApi(string $page)
441
    {
442
        return $this->get('/admin-api/' . $page);
×
443
    }
444

445
    /**
446
     * @param string $page
447
     * @param array $data
448
     * @return mixed
449
     * @svc
450
     */
451
    protected function postAdminApi(string $page, $data = [])
452
    {
453
        return $this->request($data)->call('/admin-api/' . $page, 'post');
×
454
    }
455

456
    /**
457
     * @param string $page
458
     * @param array $data
459
     * @return mixed
460
     * @svc
461
     */
462
    protected function patchAdminApi(string $page, $data = [])
463
    {
464
        return $this->request($data)->patch('/admin-api/' . $page);
×
465
    }
466

467
    /**
468
     * @param string $page
469
     * @param array $data
470
     * @return mixed
471
     * @svc
472
     */
473
    protected function putAdminApi(string $page, $data = [])
474
    {
475
        return $this->request($data)->put('/admin-api/' . $page);
×
476
    }
477

478
    /**
479
     * @param string $page
480
     * @return mixed
481
     * @svc
482
     */
483
    protected function deleteAdminApi(string $page)
484
    {
485
        return $this->delete('/admin-api/' . $page);
×
486
    }
487

488
    protected function getControllerByClass($class)
489
    {
490
        $parts = explode('Controller\\', $class);
×
491
        $controller = substr($parts[1], 0, -4);
×
492
        return implode('/', array_map('lcfirst', explode('\\', $controller)));
×
493
    }
494

495
    protected function getActionByMethod($method)
496
    {
497
        preg_match('/^test(.+?)Action/', $method, $match);
×
498
        if (isset($match[1])) {
×
499
            return lcfirst($match[1]);
×
500
        }
501

502
        return null;
×
503
    }
504

505
    /**
506
     * Parse response data by specified type
507
     *
508
     * @param string $data
509
     * @param null $exception A variable to store exception when parsing error
510
     * @return mixed
511
     */
512
    protected function parseResponse($data, &$exception)
513
    {
514
        switch ($this->dataType) {
×
515
            case 'json':
×
516
            case 'jsonObject':
×
517
                $data = json_decode($data, 'json' === $this->dataType);
×
518
                if (null === $data && \JSON_ERROR_NONE != json_last_error()) {
×
519
                    $exception = new \ErrorException('JSON parsing error', json_last_error());
×
520
                }
521
                break;
×
522

523
            case 'xml':
×
524
            case 'serialize':
×
525
                $methods = [
UNCOV
526
                    'xml' => 'simplexml_load_string',
×
527
                    'serialize' => 'unserialize',
528
                ];
529
                // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
530
                $data = @$methods[$this->dataType]($data);
×
531
                if (false === $data && $e = error_get_last()) {
×
532
                    $exception = new \ErrorException($e['message'], $e['type'], 0, $e['file'], $e['line']);
×
533
                }
534
                break;
×
535

536
            case 'query':
×
537
                // Parse $data(string) and assign the result to $data(array)
538
                parse_str($data, $data);
×
539
                break;
×
540

541
            case 'text':
×
542
            default:
543
                break;
×
544
        }
545

546
        return $data;
×
547
    }
548
}
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

© 2026 Coveralls, Inc