This repository was archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathStartDashboardCommand.php
More file actions
147 lines (109 loc) · 3.81 KB
/
StartDashboardCommand.php
File metadata and controls
147 lines (109 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace BeyondCode\DuskDashboard\Console;
use BeyondCode\DuskDashboard\DuskProcessFactory;
use BeyondCode\DuskDashboard\Ratchet\Http\DashboardController;
use BeyondCode\DuskDashboard\Ratchet\Http\EventController;
use BeyondCode\DuskDashboard\Ratchet\Server\App;
use BeyondCode\DuskDashboard\Ratchet\Socket;
use BeyondCode\DuskDashboard\Watcher;
use React\Http\Browser;
use Illuminate\Console\Command;
use Ratchet\WebSocket\WsServer;
use React\EventLoop\Loop as LoopFactory;
use React\EventLoop\LoopInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Routing\Route;
class StartDashboardCommand extends Command
{
public const PORT = 9773;
protected $signature = 'dusk:dashboard';
protected $description = 'Start the Laravel Dusk Dashboard';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->ignoreValidationErrors();
}
/** @var App */
protected $app;
/** @var LoopInterface */
protected $loop;
public function handle()
{
$url = parse_url(config('dusk-dashboard.host', config('app.url')));
$this->loop = LoopFactory::get();
$this->loop->futureTick(function () use ($url) {
$dashboardUrl = 'http://'.$url['host'].':'.self::PORT.'/dashboard';
$this->info('Started Dusk Dashboard on port '.self::PORT);
$this->info('Your Dusk tests are now being watched.');
$this->info('If the dashboard does not automatically open, visit: '.$dashboardUrl);
$this->tryToOpenInBrowser($dashboardUrl);
});
$this->createTestWatcher();
$this->createApp($url);
}
protected function addRoutes()
{
$eventRoute = new Route('/events', ['_controller' => new EventController()], [], [], null, [], ['POST']);
$this->app->routes->add('events', $eventRoute);
$dashboardRoute = new Route('/dashboard', ['_controller' => new DashboardController()], [], [], null, [], ['GET']);
$this->app->routes->add('dashboard', $dashboardRoute);
}
protected function createTestWatcher()
{
$finder = (new Finder())
->name('*.php')
->files()
->in($this->getTestSuitePath());
(new Watcher($finder, $this->loop))->startWatching(function () {
$client = new Browser($this->loop);
$client->post(
'http://127.0.0.1:'.self::PORT.'/events',
[
'Content-Type' => 'application/json',
],
json_encode([
'channel' => 'dusk-dashboard',
'name' => 'dusk-reset',
'data' => [],
])
);
$process = DuskProcessFactory::make();
$process->start();
});
}
protected function getTestSuitePath()
{
$directories = [];
if (file_exists(base_path('phpunit.dusk.xml'))) {
$xml = simplexml_load_file(base_path('phpunit.dusk.xml'));
foreach ($xml->testsuites->testsuite as $testsuite) {
$directories[] = trim((string) $testsuite->directory);
}
} else {
$directories[] = base_path('tests/Browser');
}
return $directories;
}
protected function createApp(array $url)
{
$socket = new Socket();
$this->app = new App($url['host'], self::PORT, '0.0.0.0', $this->loop);
$this->app->route('/socket', new WsServer($socket), ['*']);
$this->addRoutes();
$this->app->run();
}
protected function tryToOpenInBrowser($url)
{
if (PHP_OS === 'Darwin') {
exec('open '.$url);
}
if (PHP_OS === 'Linux') {
exec('xdg-open '.$url);
}
}
}