Guest User

Untitled

a guest
Apr 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use RuntimeException;
  6. use Laravel\Dusk\Console\DuskCommand;
  7. use Symfony\Component\Process\Process;
  8.  
  9. class DuskServeCommand extends DuskCommand
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'dusk:serve {--host=} {--port=}';
  17.  
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Serve the application and run Dusk tests';
  24.  
  25. /**
  26. * @var Process
  27. */
  28. protected $serve;
  29.  
  30. /**
  31. * @var string
  32. */
  33. protected $defaultHost = '127.0.0.1';
  34.  
  35. /**
  36. * @var string
  37. */
  38. protected $defaultPort = '8000';
  39.  
  40. /**
  41. * Execute the console command.
  42. *
  43. * @return mixed
  44. */
  45. public function handle()
  46. {
  47. // Snippets copied from DuskCommand::handle()
  48. $this->purgeScreenshots();
  49. $this->purgeConsoleLogs();
  50.  
  51. return $this->withDuskEnvironment(function () {
  52. // Start the Web Server AFTER Dusk handled the environment, but before running dusk testing
  53. $this->serve();
  54.  
  55. // Run dusk testing
  56. return $this->runDusk();
  57. });
  58. }
  59.  
  60. /**
  61. * Build a process to run php artisan serve
  62. *
  63. * @return Process
  64. */
  65. protected function serve()
  66. {
  67. // Compatibility with Windows and Linux environment
  68. $arguments = [
  69. PHP_BINARY,
  70. 'artisan',
  71. 'serve',
  72. '--host=' . ($this->option('host') ?? $this->defaultHost),
  73. '--port=' . ($this->option('port') ?? $this->defaultPort),
  74. ];
  75.  
  76. // Build the process
  77. $serve = (new Process($arguments))->setTimeout(null);
  78.  
  79. return tap($serve, function (Process $serve) {
  80. $serve->start(function ($type, $line) {
  81. $this->output->writeln($line);
  82. });
  83. });
  84. }
  85.  
  86. /**
  87. * Snippet copied from DuskCommand::handle() to actually run PHP Unit
  88. *
  89. * @return int
  90. */
  91. protected function runDusk()
  92. {
  93. $process = (new Process([PHP_BINARY, 'artisan', 'dusk']))->setTimeout(null);
  94.  
  95. try {
  96. $process->setTty(true);
  97. } catch (RuntimeException $e) {
  98. $this->output->writeln('Warning: '.$e->getMessage());
  99. }
  100.  
  101. return $process->run(function ($type, $line) {
  102. $this->output->write($line);
  103. });
  104. }
  105. }
Add Comment
Please, Sign In to add comment