• 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.0
/src/Test/BaseControllerTestCase.php
1
<?php
2

3
namespace Miaoxing\Plugin\Test;
4

5
use Miaoxing\Plugin\Service\Tester;
6
use Miaoxing\Plugin\Service\User;
7
use RuntimeException;
8

9
/**
10
 * @mixin \AppMixin
11
 */
12
abstract class BaseControllerTestCase extends BaseTestCase
13
{
14
    /**
15
     * 当前控制器名称
16
     *
17
     * @var string
18
     */
19
    protected $controller;
20

21
    /**
22
     * 预期action返回的code
23
     *
24
     * @var array
25
     */
26
    protected $statusCodes = [];
27

28
    /**
29
     * 页面可以正常访问
30
     *
31
     * @dataProvider providerForActions
32
     * @param string $action
33
     * @param int|null $code
34
     */
35
    public function testActions($action, $code = null)
36
    {
37
        $controller = $this->getController();
×
38

39
        $result = $this->dispatch($controller, $action);
×
40

41
        if ($result instanceof \Exception) {
×
42
            $this->assertEquals(404, $result->getCode(), $action . '返回' . $result->getMessage());
×
43
        } else {
44
            if ($code) {
×
45
                $this->assertEquals($code, $result->getStatusCode());
×
46
            } else {
47
                $this->assertNotEquals(302, $result->getStatusCode());
×
48
            }
49
            $this->assertNotEmpty($result->getContent(), $action . '返回内容不为空');
×
50
        }
51
    }
52

53
    public function providerForActions()
54
    {
55
        $controller = $this->getController();
×
56
        $controllerClasses = $this->app->getControllerClasses($controller);
×
57
        $controllerClass = end($controllerClasses);
×
58
        $actions = get_class_methods($controllerClass);
×
59
        if (!$actions) {
×
60
            throw new RuntimeException(sprintf(
×
UNCOV
61
                'Action method not found in controller %s class %s',
×
UNCOV
62
                $controller,
×
UNCOV
63
                $controllerClass
×
UNCOV
64
            ));
×
65
        }
66

67
        $params = [];
×
68
        foreach ($actions as $action) {
×
69
            if ('Action' === substr($action, -6)) {
×
70
                $action = substr($action, 0, -6);
×
71
                $params[] = [
×
UNCOV
72
                    $action,
×
73
                    isset($this->statusCodes[$action]) ? $this->statusCodes[$action] : null,
×
UNCOV
74
                ];
×
75
            }
76
        }
77

78
        return $params;
×
79
    }
80

81
    public function getController()
82
    {
83
        if (!$this->controller) {
×
84
            preg_match('/Controller\\\\(.+?)ControllerTest/', static::class, $matches);
×
85
            $values = [];
×
86
            foreach (explode('\\', $matches[1]) as $value) {
×
87
                $values[] = lcfirst($value);
×
88
            }
89
            $this->controller = implode('\\', $values);
×
90
        }
91

92
        return $this->controller;
×
93
    }
94

95
    /**
96
     * @param string $controller
97
     * @param string $action
98
     * @return \Exception|\Wei\Res
99
     */
100
    public function dispatch($controller, $action = 'index')
101
    {
102
        User::loginById(1);
×
103

104
        ob_start();
×
105
        try {
106
            $res = $this->app->dispatch($controller, $action);
×
107
        } catch (\Exception $e) {
×
108
            ob_end_clean();
×
109

110
            return $e;
×
111
        }
112

113
        ob_end_clean();
×
114

115
        return $res;
×
116
    }
117

118
    protected function getAction()
119
    {
120
        preg_match('/test(.+?)Action/', $this->getName(), $matches);
×
121
        if (!isset($matches[1])) {
×
122
            throw new \Exception('Invalid test case naming');
×
123
        }
124

125
        return lcfirst($matches[1]);
×
126
    }
127

128
    /**
129
     * @param array $request
130
     * @return Tester
131
     */
132
    protected function visitCurPage(array $request = [])
133
    {
134
        $controller = $this->getController();
×
135
        $action = $this->getAction();
×
136

137
        return wei()->tester($controller, $action)
×
138
            ->request($request);
×
139
    }
140

141
    /**
142
     * 运行任务
143
     *
144
     * @param string $name
145
     * @param array $data
146
     * @return array
147
     */
148
    protected function runJob($name, array $data = [])
149
    {
150
        $parts = explode('/', $name);
×
151
        $action = array_pop($parts);
×
152
        $controller = implode('/', $parts);
×
153

154
        return wei()->tester()
×
155
            ->login(1)
×
156
            ->controller($controller)
×
157
            ->action($action)
×
158
            ->req($data)
×
159
            ->json()
×
160
            ->exec()
×
161
            ->response();
×
162
    }
163
}
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