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

orchestral / testbench-dusk / 44455010276

19 Nov 2026 03:57AM UTC coverage: 85.892% (-5.3%) from 91.15%
44455010276

Pull #128

github

web-flow
Update HTTP server start method with middleware
Pull Request #128: Use `amp/http-server`

1 of 15 new or added lines in 1 file covered. (6.67%)

207 of 241 relevant lines covered (85.89%)

7.78 hits per line

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

64.15
/src/DuskServer.php
1
<?php
2

3
namespace Orchestra\Testbench\Dusk;
4

5
use Orchestra\Testbench\Foundation\Process\RemoteWebServer;
6
use ReflectionClass;
7
use Symfony\Component\Process\Process;
8

9
use function Orchestra\Sidekick\Filesystem\join_paths;
10
use function Orchestra\Sidekick\php_binary;
11
use function Orchestra\Testbench\defined_environment_variables;
12
use function Orchestra\Testbench\package_path;
13

14
/**
15
 * @internal
16
 */
17
class DuskServer extends RemoteWebServer
18
{
19
    /**
20
     * Laravel working path.
21
     *
22
     * @var string|null
23
     */
24
    protected ?string $basePath = null;
25

26
    /**
27
     * Laravel working URL.
28
     *
29
     * @var string|null
30
     */
31
    protected ?string $baseUrl = null;
32

33
    /**
34
     * List of local IPv6 hosts.
35
     *
36
     * @var array<int, string>
37
     */
38
    protected array $localIpv6Hosts = ['::0', '[::0]'];
39

40
    /**
41
     * Construct a new web server.
42
     *
43
     * @param  string  $host
44
     * @param  int  $port
45
     * @param  int  $timeout
46
     */
47
    public function __construct(
48
        public readonly string $host = '127.0.0.1',
49
        public readonly int $port = 8001,
50
        public readonly int $timeout = 6000,
51
    ) {}
15 ✔
52

53
    /**
54
     * Resolve application instnace.
55
     *
56
     * Identify the calling test class based on the content of the stash,
57
     * use it to retrieve the built application as specificed in all
58
     * the setup methods for the test class.
59
     *
60
     * @param  string  $name
61
     * @param  string  $host
62
     * @param  int  $port
63
     * @return \Illuminate\Foundation\Application
64
     */
65
    public static function laravel(
66
        string $name = 'laravel',
67
        string $host = '127.0.0.1',
68
        int $port = 8001,
69
    ) {
NEW
70
        $server = new static($host, $port);
×
71

72
        /** @var class-string<\Orchestra\Testbench\Dusk\TestCase> $originatingTestClass */
NEW
73
        $originatingTestClass = $server->getStash('class');
×
74

75
        // Abstract base classes (e.g. when using Pest) need a concrete class to be instantiated.
NEW
76
        if ((new ReflectionClass($originatingTestClass))->isAbstract()) {
×
77
            /** @var class-string<\Orchestra\Testbench\Dusk\TestCase> $concreteTestClass */
NEW
78
            $concreteTestClass = 'TestbenchDuskServing'.md5($originatingTestClass);
×
79

NEW
80
            if (! class_exists($concreteTestClass, false)) {
×
NEW
81
                eval(\sprintf('final class %s extends \\%s {}', $concreteTestClass, ltrim($originatingTestClass, '\\')));
×
82
            }
83

NEW
84
            $originatingTestClass = $concreteTestClass;
×
85
        }
86

NEW
87
        return (new $originatingTestClass($name))->createServingApplicationForDuskServer($server);
×
88
    }
89

90
    /**
91
     * Set Laravel working path.
92
     *
93
     * @param  string|null  $basePath
94
     * @param  string|null  $baseUrl
95
     * @return void
96
     */
97
    public function setLaravel(?string $basePath = null, ?string $baseUrl = null): void
98
    {
99
        $this->basePath = $basePath;
4 ✔
100
        $this->baseUrl = $baseUrl;
4 ✔
101
    }
102

103
    /**
104
     * Store some temp contents in a file for later use.
105
     *
106
     * @param  mixed  $content
107
     * @return void
108
     */
109
    public function stash($content): void
110
    {
111
        file_put_contents($this->temp(), json_encode($content));
6 ✔
112
    }
113

114
    /**
115
     * Prepare the path of the temp file for a particular server.
116
     *
117
     * @return string
118
     */
119
    protected function temp(): string
120
    {
121
        return join_paths(
6 ✔
122
            \dirname(__DIR__),
6 ✔
123
            'tmp',
6 ✔
124
            \sprintf('%s__%d', ! \in_array($this->host, $this->localIpv6Hosts) ? $this->host : 'localhost', $this->port),
6 ✔
125
        );
6 ✔
126
    }
127

128
    /**
129
     * Retrieve the contents of the relevant file.
130
     *
131
     * @param  string|null  $key
132
     * @return mixed
133
     */
134
    public function getStash(?string $key = null)
135
    {
136
        $content = json_decode((string) file_get_contents($this->temp()), true);
×
137

138
        return $key ? ($content[$key] ?? null) : $content;
×
139
    }
140

141
    /**
142
     * Stop the php server.
143
     *
144
     * @return void
145
     */
146
    public function restart(): void
147
    {
148
        $this->stop();
×
149
        $this->clearOutput();
×
150
        $this->startServer();
×
151
    }
152

153
    /**
154
     * {@inheritDoc}
155
     *
156
     * Start the server. Execute the command and open a
157
     * pointer to it. Output is discarded: the built-in PHP
158
     * server logs every request to stderr, and if that pipe
159
     * is never drained it fills and blocks the server.
160
     *
161
     * @see https://github.com/orchestral/testbench-dusk/issues/123
162
     */
163
    protected function createWebServerProcess(): Process
164
    {
165
        $process = new Process(
7 ✔
166
            command: $this->prepareCommand(),
7 ✔
167
            cwd: join_paths($this->basePath(), 'public'),
7 ✔
168
            env: array_merge(defined_environment_variables(), [
7 ✔
169
                'APP_BASE_PATH' => $this->basePath(),
7 ✔
170
                'APP_URL' => $this->baseUrl(),
7 ✔
171
                'APP_ENV' => 'testing',
7 ✔
172
                'TESTBENCH_WORKING_PATH' => package_path(),
7 ✔
173
            ]),
7 ✔
174
            timeout: $this->timeout
7 ✔
175
        );
7 ✔
176

177
        // Discard stdout/stderr so the OS pipe cannot fill and hang php -S
178
        // mid-suite when request log volume exceeds the pipe buffer.
179
        $process->disableOutput();
7 ✔
180

181
        return $process;
7 ✔
182
    }
183

184
    /**
185
     * Prepare the command for starting the PHP server.
186
     *
187
     * @return array
188
     */
189
    protected function prepareCommand(): array
190
    {
191
        if (Options::$usesAmphpServer) {
7 ✔
NEW
192
            return [
×
NEW
193
                php_binary(),
×
NEW
194
                join_paths(__DIR__, 'amphp-server.php'),
×
NEW
195
                $this->host,
×
NEW
196
                (string) $this->port,
×
NEW
197
            ];
×
198
        }
199

200
        return [
7 ✔
201
            php_binary(),
7 ✔
202
            '-S',
7 ✔
203
            \sprintf('%s:%s', $this->host, $this->port),
7 ✔
204
            join_paths(__DIR__, 'server.php'),
7 ✔
205
            '-t',
7 ✔
206
            join_paths($this->basePath(), 'public'),
7 ✔
207
        ];
7 ✔
208
    }
209

210
    /**
211
     * Figure out the path to the laravel application path for Testbench purposes.
212
     *
213
     * @return string
214
     */
215
    public function basePath(): string
216
    {
217
        return $this->basePath ?? default_skeleton_path();
9 ✔
218
    }
219

220
    /**
221
     * Figure out the path to the laravel application URL for testbench purposes.
222
     *
223
     * @return string
224
     */
225
    public function baseUrl(): string
226
    {
227
        return $this->baseUrl ?? \sprintf('http://%s:%d', $this->host, $this->port);
7 ✔
228
    }
229

230
    /**
231
     * {@inheritDoc}
232
     *
233
     * @throws \Orchestra\Testbench\Dusk\Exceptions\UnableToStartServer
234
     */
235
    #[\Override]
236
    protected function throwUnableToStartWebServer(): never
237
    {
238
        throw new Exceptions\UnableToStartServer(\sprintf('%s:%d', $this->host, $this->port));
1 ✔
239
    }
240
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc