Skip to content
This repository was archived by the owner on Oct 20, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,30 +127,39 @@ jobs:
path: app/storage/logs

stub-tests:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}

strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.1, 8.0]
laravel: [9.*]
dependency-version: [prefer-lowest, prefer-stable]

name: Test Stubs P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
name: Test Stubs ${{ matrix.os }} - P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, mysql, mysqli, pdo_mysql
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, mysql, mysqli, pdo_mysql, fileinfo
coverage: none

- name: Setup Laravel
run: |
composer create-project laravel/laravel:^9 .
composer create-project laravel/laravel:^9.3 .
composer require protonemedia/laravel-splade
rm -rf vendor/protonemedia/laravel-splade

- name: Remove installed Splade (Unix)
run: rm -rf vendor/protonemedia/laravel-splade
if: matrix.os == 'ubuntu-latest'

- name: Remove installed Splade (Windows)
run: rd "vendor/protonemedia/laravel-splade" /s /q
shell: cmd
if: matrix.os == 'windows-latest'

- name: Checkout code
uses: actions/checkout@v2
Expand All @@ -163,18 +172,53 @@ jobs:
php artisan splade:install

- name: Install NPM dependencies
run: npm i

- name: Remove installed Splade and copy front-end build from Checkout (Unix)
run: |
npm i
rm -rf node_modules/@protonemedia/laravel-splade/dist
cp -R vendor/protonemedia/laravel-splade/dist node_modules/@protonemedia/laravel-splade/
if: matrix.os == 'ubuntu-latest'

- name: Remove installed Splade and copy front-end build from Checkout (Windows)
run: |
rd "node_modules/@protonemedia/laravel-splade/dist" /s /q
mkdir "node_modules/@protonemedia/laravel-splade/dist"
xcopy "vendor/protonemedia/laravel-splade/dist" "node_modules/@protonemedia/laravel-splade/dist" /E /I
shell: cmd
if: matrix.os == 'windows-latest'

- name: Compile assets
run: npm run build

- name: Start SSR server
- name: Run Laravel Server (Unix)
run: php artisan serve &
if: matrix.os == 'ubuntu-latest'

- name: Run Test (Unix)
run: php vendor/protonemedia/laravel-splade/TestStubs.php
if: matrix.os == 'ubuntu-latest'

- name: Run Laravel Server (Windows) and Run Test
run: |
start /b cmd /v:on /c "(php artisan serve) &"
php vendor/protonemedia/laravel-splade/TestStubs.php
shell: cmd
if: matrix.os == 'windows-latest'

- name: Start SSR server (Unix)
run: |
echo "SPLADE_SSR_ENABLED=true" >> .env
node bootstrap/ssr/ssr.mjs &
if: matrix.os == 'ubuntu-latest'

- name: Run Test command
- name: Run Test command (Unix)
run: php artisan splade:ssr-test
if: matrix.os == 'ubuntu-latest'

- name: Start SSR server (Windows) and Run Test command
run: |
echo "SPLADE_SSR_ENABLED=true" >> .env
node bootstrap/ssr/ssr.mjs &
php artisan splade:ssr-test
if: matrix.os == 'windows-latest'
73 changes: 73 additions & 0 deletions TestStubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

function value($value, ...$args)
{
return $value instanceof \Closure ? $value(...$args) : $value;
}

function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)
{
$attempts = 0;

$backoff = [];

if (is_array($times)) {
$backoff = $times;

$times = count($times) + 1;
}

beginning:
$attempts++;
$times--;

try {
return $callback($attempts);
} catch (Exception $e) {
if ($times < 1 || ($when && ! $when($e))) {
throw $e;
}

$sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds;

if ($sleepMilliseconds) {
usleep(value($sleepMilliseconds, $attempts, $e) * 1000);
}

goto beginning;
}
}

function get()
{
$contents = file_get_contents('http://127.0.0.1:8000/');

if (!$contents) {
throw new Exception("No contents");
}

return $contents;
}

$home = retry(10, fn () => get(), 1000);

$needles = [
'<div id="app" data-components="',
'Welcome to your Splade application!',
];

$missing = [];

foreach ($needles as $needle) {
if (!str_contains($home, $needle)) {
$missing[] = $needle;
echo "Not found: " . $needle . PHP_EOL;
}
}

if (empty($missing)) {
echo "OK";
exit(0);
}

exit(1);
22 changes: 16 additions & 6 deletions src/Commands/SpladeInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,21 @@ public function handle(): int
return self::SUCCESS;
}

/**
* End of line symbol.
*
* @return string
*/
private static function eol(): string
{
return windows_os() ? "\n" : PHP_EOL;
}

protected function installExceptionHandler()
{
$exceptionHandler = file_get_contents(app_path('Exceptions/Handler.php'));

$search = 'public function register()' . PHP_EOL . ' {';
$search = 'public function register()' . static::eol() . ' {';

$registerMethodAfter = Str::after($exceptionHandler, $search);

Expand All @@ -90,7 +100,7 @@ protected function installExceptionHandler()
app_path('Exceptions/Handler.php'),
str_replace(
$registerMethodAfter,
PHP_EOL . ' ' . $renderable . PHP_EOL . $registerMethodAfter,
static::eol() . ' ' . $renderable . static::eol() . $registerMethodAfter,
$exceptionHandler
)
);
Expand All @@ -105,11 +115,11 @@ protected function installRouteMiddleware()
{
$httpKernel = file_get_contents(app_path('Http/Kernel.php'));

$search = 'protected $routeMiddleware = [' . PHP_EOL;
$search = 'protected $routeMiddleware = [' . static::eol();

$routeMiddlewareAfter = Str::after($httpKernel, $search);

$routeMiddleware = "'splade' => \ProtoneMedia\Splade\Http\SpladeMiddleware::class";
$routeMiddleware = "'splade' => \ProtoneMedia\Splade\Http\SpladeMiddleware::class";

if (Str::contains($httpKernel, $routeMiddleware)) {
return;
Expand All @@ -119,7 +129,7 @@ protected function installRouteMiddleware()
app_path('Http/Kernel.php'),
str_replace(
$routeMiddlewareAfter,
' ' . $routeMiddleware . ',' . PHP_EOL . $routeMiddlewareAfter,
' ' . $routeMiddleware . ',' . static::eol() . $routeMiddlewareAfter,
$httpKernel
)
);
Expand Down Expand Up @@ -165,7 +175,7 @@ protected static function updateNodePackages(callable $callback, $dev = true)

file_put_contents(
base_path('package.json'),
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . static::eol()
);
}
}