Skip to content

Commit

Permalink
Testing install:broadcasting command
Browse files Browse the repository at this point in the history
  • Loading branch information
slavarazum committed Mar 19, 2024
1 parent c720ccb commit 4a1fcc3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.php_cs
.php_cs.cache
.phpunit.result.cache
/.phpunit.cache
build
composer.lock
coverage
Expand Down
12 changes: 6 additions & 6 deletions src/Console/Commands/BroadcastingInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function handle(): void
$relativeBroadcastingRoutesStub = 'laravel/framework/src/Illuminate/Foundation/Console/stubs/broadcasting-routes.stub';

if (file_exists(__DIR__.'/../../../../'.$relativeBroadcastingRoutesStub)) {
copy(__DIR__.'/../../../stubs/broadcasting-routes.stub', $broadcastingRoutesPath);
File::copy(__DIR__.'/../../../stubs/broadcasting-routes.stub', $broadcastingRoutesPath);
} else {
copy(__DIR__.'/../../../vendor/'.$relativeBroadcastingRoutesStub, $broadcastingRoutesPath);
File::copy(__DIR__.'/../../../vendor/'.$relativeBroadcastingRoutesStub, $broadcastingRoutesPath);
}
}

Expand All @@ -37,7 +37,7 @@ public function handle(): void

// Install bootstrapping...
if (! file_exists($echoScriptPath = $this->laravel->resourcePath('js/echo.js'))) {
copy(__DIR__.'/../../../stubs/echo-js.stub', $echoScriptPath);
File::copy(__DIR__.'/../../../stubs/echo-js.stub', $echoScriptPath);
}

if (file_exists($bootstrapScriptPath = $this->laravel->resourcePath('js/bootstrap.js'))) {
Expand All @@ -46,9 +46,9 @@ public function handle(): void
);

if (! str_contains($bootstrapScript, './echo')) {
file_put_contents(
File::append(
$bootstrapScriptPath,
trim($bootstrapScript.PHP_EOL.file_get_contents(__DIR__.'/../../../stubs/echo-bootstrap-js.stub')).PHP_EOL,
PHP_EOL.file_get_contents(__DIR__.'/../../../stubs/echo-bootstrap-js.stub')
);
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ protected function updateBroadcastingDriver(): void
$env,
Str::of(File::get($env))->replaceMatches('/(BROADCAST_(?:DRIVER|CONNECTION))=\w*/', function (array $matches) {
return $matches[1].'=redis';
})
})->value()
);
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Feature/BroadcastingInstallCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;

it('completely installs all required assets for broadcasting in default scenario', function () {
File::shouldReceive('copy')
->once()
->withArgs(fn($path, $target) => str_ends_with($path, 'broadcasting-routes.stub') && str_ends_with($target, 'routes/channels.php'));

File::shouldReceive('copy')
->once()
->withArgs(fn($path, $target) => str_ends_with($path, 'echo-js.stub') && str_ends_with($target, 'js/echo.js'));

File::shouldReceive('missing')
->once()
->withArgs([app()->environmentFile()])
->andReturn(false);

File::shouldReceive('get')
->once()
->withArgs([app()->environmentFile()])
->andReturn('BROADCAST_CONNECTION=log');

File::shouldReceive('put')
->once()
->withArgs([app()->environmentFile(), 'BROADCAST_CONNECTION=redis']);

Process::fake([
'npm install --save-dev laravel-echo laravel-wave && npm run build' => Process::result()
]);

$this->artisan('install:broadcasting')
->expectsConfirmation('Would you like to enable the Redis broadcasting driver for Wave?', 'yes')
->expectsConfirmation('Would you like to install and build the Node dependencies required for broadcasting?', 'yes')
->expectsOutputToContain('Installing and building Node dependencies.')
->expectsOutputToContain('Node dependencies installed successfully.')
->expectsConfirmation('Would you like to publish the Wave configuration file?')
->assertExitCode(0);

$this->assertFileExists($this->app->configPath('broadcasting.php'));
unlink($this->app->configPath('broadcasting.php'));
});

0 comments on commit 4a1fcc3

Please sign in to comment.