Skip to content
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
3 changes: 3 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Services\Surreal\SurrealDocumentStore;
use App\Services\Surreal\SurrealHttpClient;
use App\Services\Surreal\SurrealRuntimeManager;
use App\Support\Native\NativeRuntimePersistence;
use Illuminate\Database\DatabaseManager;
use Illuminate\Queue\QueueManager;
use Illuminate\Session\DatabaseSessionHandler;
Expand All @@ -26,6 +27,8 @@ public function register(): void

public function boot(): void
{
$this->app->make(NativeRuntimePersistence::class)->configure();

$this->app->extend('migration.repository', function ($repository, $app): SurrealMigrationRepository {
$migrations = $app['config']['database.migrations'];
$table = is_array($migrations) ? ($migrations['table'] ?? 'migrations') : $migrations;
Expand Down
3 changes: 0 additions & 3 deletions app/Providers/NativeAppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

namespace App\Providers;

use App\Support\Native\NativeRuntimePersistence;
use Native\Desktop\Contracts\ProvidesPhpIni;
use Native\Desktop\Facades\Window;

class NativeAppServiceProvider implements ProvidesPhpIni
{
public function boot(): void
{
app(NativeRuntimePersistence::class)->configure();

Window::open()
->url(route('home'))
->title(config('app.name'))
Expand Down
49 changes: 27 additions & 22 deletions tests/Feature/NativeRuntimePersistenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use App\Services\Surreal\SurrealConnection;
use App\Services\Surreal\SurrealHttpClient;
use App\Services\Surreal\SurrealRuntimeManager;
use App\Support\Native\NativeRuntimePersistence;
use Illuminate\Cache\FileStore;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache;
Expand All @@ -22,13 +21,22 @@

$storagePath = storage_path('app/surrealdb/native-runtime-test-'.Str::uuid());
$originalConfig = snapshotNativeRuntimeConfig();
$originalNativeRunning = env('NATIVEPHP_RUNNING');
$originalNativeStoragePath = env('NATIVEPHP_STORAGE_PATH');
$originalNativeDatabasePath = env('NATIVEPHP_DATABASE_PATH');

File::deleteDirectory($storagePath);
File::ensureDirectoryExists(dirname($storagePath));

try {
$server = retryStartingNativeRuntimeSurrealServer($client, $storagePath);

setNativeRuntimeEnvironment('NATIVEPHP_RUNNING', 'true');
setNativeRuntimeEnvironment('NATIVEPHP_STORAGE_PATH', storage_path('framework/testing/native-runtime-'.Str::uuid()));
setNativeRuntimeEnvironment('NATIVEPHP_DATABASE_PATH', database_path('native-runtime-test.sqlite'));

$this->refreshApplication();

config()->set('surreal.host', '127.0.0.1');
config()->set('surreal.port', $server['port']);
config()->set('surreal.endpoint', $server['endpoint']);
Expand All @@ -40,28 +48,8 @@
config()->set('surreal.storage_path', $storagePath);
config()->set('surreal.runtime', 'local');
config()->set('surreal.autostart', false);

config()->set('nativephp-internal.running', true);
config()->set('database.default', 'nativephp');
config()->set('database.migrations.connection', 'nativephp');
config()->set('session.driver', 'file');
config()->set('session.connection', null);
config()->set('session.table', 'sessions');
config()->set('session.cookie', 'native-runtime-surreal');
config()->set('cache.default', 'database');
config()->set('cache.limiter', 'file');
config()->set('cache.stores.database.connection', 'nativephp');
config()->set('cache.stores.database.lock_connection', 'nativephp');
config()->set('cache.stores.surreal.connection', 'nativephp');
config()->set('cache.stores.surreal.lock_connection', 'nativephp');
config()->set('queue.default', 'database');
config()->set('queue.failed.database', 'nativephp');
config()->set('queue.batching.database', 'nativephp');
config()->set('queue.connections.database.connection', 'nativephp');
config()->set('queue.connections.surreal.connection', 'nativephp');
config()->set('ai.caching.embeddings.store', 'database');

app(NativeRuntimePersistence::class)->configure();

resetNativeRuntimePersistenceState();

Expand All @@ -73,7 +61,6 @@
->and(config('queue.default'))->toBe('surreal')
->and(config('queue.failed.database'))->toBe('surreal')
->and(config('queue.batching.database'))->toBe('surreal')
->and(config('ai.caching.embeddings.store'))->toBe('surreal')
->and(cache()->driver(config('cache.limiter'))->getStore())->toBeInstanceOf(FileStore::class);

expect(Artisan::call('migrate', [
Expand Down Expand Up @@ -120,6 +107,10 @@
->and(DB::connection('surreal')->table('sessions')->count())->toBeGreaterThan(0);
} finally {
restoreNativeRuntimeConfig($originalConfig);
setNativeRuntimeEnvironment('NATIVEPHP_RUNNING', $originalNativeRunning);
setNativeRuntimeEnvironment('NATIVEPHP_STORAGE_PATH', $originalNativeStoragePath);
setNativeRuntimeEnvironment('NATIVEPHP_DATABASE_PATH', $originalNativeDatabasePath);
$this->refreshApplication();
resetNativeRuntimePersistenceState();

if (isset($server['process'])) {
Expand Down Expand Up @@ -170,6 +161,20 @@ function restoreNativeRuntimeConfig(array $snapshot): void
}
}

function setNativeRuntimeEnvironment(string $key, string|false|null $value): void
{
if ($value === false || $value === null) {
putenv($key);
unset($_ENV[$key], $_SERVER[$key]);

return;
}

putenv(sprintf('%s=%s', $key, $value));
$_ENV[$key] = $value;
$_SERVER[$key] = $value;
}

function resetNativeRuntimePersistenceState(): void
{
app()->forgetInstance(SurrealConnection::class);
Expand Down
Loading