Skip to content

Commit

Permalink
Merge f0785ec into af9da0b
Browse files Browse the repository at this point in the history
  • Loading branch information
crynobone committed Nov 8, 2023
2 parents af9da0b + f0785ec commit 469dbe6
Show file tree
Hide file tree
Showing 20 changed files with 548 additions and 51 deletions.
26 changes: 23 additions & 3 deletions bin/sync
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env php
<?php

use Illuminate\Support\Str;

$workingPath = getcwd();

require __DIR__.'/../vendor/autoload.php';
Expand Down Expand Up @@ -36,13 +38,31 @@ $files->move("{$workingPath}/laravel/database/migrations/2014_10_12_000000_creat
$files->move("{$workingPath}/laravel/database/migrations/2014_10_12_100000_create_password_resets_table.php", "{$workingPath}/laravel/migrations/2014_10_12_100000_testbench_create_password_resets_table.php");
$files->move("{$workingPath}/laravel/database/migrations/2019_08_19_000000_create_failed_jobs_table.php", "{$workingPath}/laravel/migrations/2019_08_19_000000_testbench_create_failed_jobs_table.php");

foreach ([
Illuminate\Support\Collection::make([
'cache/0000_00_00_000000_testbench_create_cache_table' => 'Cache/Console/stubs/cache.stub',
'notifications/0000_00_00_000000_testbench_create_notifications_table' => 'Notifications/Console/stubs/notifications.stub',
'queue/0000_00_00_000000_testbench_create_jobs_table' => 'Queue/Console/stubs/jobs.stub',
'queue/0000_00_00_000000_testbench_create_job_batches_table' => 'Queue/Console/stubs/batches.stub',
// 'queue/0000_00_00_000000_testbench_create_failed_jobs_table' => 'Queue/Console/stubs/failed_jobs.stub',
'session/0000_00_00_000000_testbench_create_sessions_table' => 'Session/Console/stubs/database.stub',
])->transform(fn ($file) => "{$workingPath}/vendor/laravel/framework/src/Illuminate/{$file}")
->each(function ($from, $to) use ($files, $workingPath) {
$files->copy($from, "{$workingPath}/laravel/migrations/{$to}.php");
})->keys()
->push(...[
'2014_10_12_000000_testbench_create_users_table',
'2014_10_12_100000_testbench_create_password_resets_table',
'2019_08_19_000000_testbench_create_failed_jobs_table',
] as $migration) {
])->each(function ($migration) use ($files, $workingPath) {
$files->replaceInFile('class Create', 'class TestbenchCreate', "{$workingPath}/laravel/migrations/{$migration}.php");
}
})->filter(fn ($migration) => str_starts_with($migration, 'queue'))
->mapWithKeys(fn ($migration) => match ($migration) {
'queue/0000_00_00_000000_testbench_create_jobs_table' => [$migration => 'jobs'],
'queue/0000_00_00_000000_testbench_create_job_batches_table' => [$migration => 'job_batches'],
// 'queue/0000_00_00_000000_testbench_create_failed_jobs_table' => [$migration => 'failed_jobs'],
})->each(function ($table, $migration) use ($files, $workingPath) {
$files->replaceInFile(['{{tableClassName}}', '{{table}}'], [Str::studly($table), $table], "{$workingPath}/laravel/migrations/{$migration}.php");
});

transform([
"getcwd()" => "__DIR__.'/public'",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class TestbenchCreateCacheTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cache', function (Blueprint $table) {
$table->string('key')->primary();
$table->mediumText('value');
$table->integer('expiration');
});

Schema::create('cache_locks', function (Blueprint $table) {
$table->string('key')->primary();
$table->string('owner');
$table->integer('expiration');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('cache');
Schema::dropIfExists('cache_locks');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class TestbenchCreateNotificationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notifications');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class TestbenchCreateJobBatchesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('job_batches', function (Blueprint $table) {
$table->string('id')->primary();
$table->string('name');
$table->integer('total_jobs');
$table->integer('pending_jobs');
$table->integer('failed_jobs');
$table->text('failed_job_ids');
$table->mediumText('options')->nullable();
$table->integer('cancelled_at')->nullable();
$table->integer('created_at');
$table->integer('finished_at')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('job_batches');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class TestbenchCreateJobsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedTinyInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('jobs');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class TestbenchCreateSessionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->primary();
$table->foreignId('user_id')->nullable()->index();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->text('payload');
$table->integer('last_activity')->index();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sessions');
}
}
15 changes: 15 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
parameters:
ignoreErrors:
-
message: "#^Access to an undefined property Spatie\\\\Ray\\\\Settings\\\\Settings\\:\\:\\$send_duplicate_queries_to_ray\\.$#"
count: 1
path: src/Bootstrap/ConfigureRay.php

-
message: "#^Access to an undefined property Spatie\\\\Ray\\\\Settings\\\\Settings\\:\\:\\$send_queries_to_ray\\.$#"
count: 1
path: src/Bootstrap/ConfigureRay.php

-
message: "#^Access to an undefined property Spatie\\\\Ray\\\\Settings\\\\Settings\\:\\:\\$send_slow_queries_to_ray\\.$#"
count: 1
path: src/Bootstrap/ConfigureRay.php

-
message: "#^Call to an undefined method Illuminate\\\\Contracts\\\\Foundation\\\\Application\\:\\:environmentFile\\(\\)\\.$#"
count: 1
Expand Down
26 changes: 26 additions & 0 deletions src/Attributes/WithMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Orchestra\Testbench\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class WithMigration
{
/**
* The target types.
*
* @var array
*/
public $types = [];

/**
* Construct a new attribute.
*
* @param array<int, string> $types
*/
public function __construct(...$types)
{
$this->types = array_merge(['laravel'], $types);
}
}
6 changes: 4 additions & 2 deletions src/Concerns/HandlesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ protected function parseTestMethodAttributes($app, string $attribute, ?Closure $
->filter(static function ($attributes, string $key) use ($attribute) {
return $key === $attribute && ! empty($attributes);
})->flatten()
->filter(function ($instance) {
return \is_string($instance->method) && method_exists($this, $instance->method);
->when(\is_null($callback), function ($attributes) {
return $attributes->filter(function ($instance) {
return \is_string($instance->method) && method_exists($this, $instance->method);
});
})->each($callback ?? function ($instance) use ($app) {
$this->{$instance->method}($app);
});
Expand Down
17 changes: 17 additions & 0 deletions src/Concerns/HandlesDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

use Closure;
use Illuminate\Database\Events\DatabaseRefreshed;
use Illuminate\Support\Collection;
use Orchestra\Testbench\Attributes\DefineDatabase;
use Orchestra\Testbench\Attributes\WithMigration;

use function Orchestra\Testbench\after_resolving;
use function Orchestra\Testbench\laravel_migration_path;

trait HandlesDatabases
{
Expand Down Expand Up @@ -40,6 +45,18 @@ protected function setUpDatabaseRequirements(Closure $callback): void

if (static::usesTestingConcern(HandlesAttributes::class)) {
$this->parseTestMethodAttributes($this->app, DefineDatabase::class);

$this->parseTestMethodAttributes($this->app, WithMigration::class, function (WithMigration $attribute) {
after_resolving($this->app, 'migrator', static function ($migrator, $app) use ($attribute) {
/** @var \Illuminate\Database\Migrations\Migrator $migrator */
Collection::make($attribute->types)
->transform(static function ($type) {
return laravel_migration_path($type !== 'laravel' ? $type : null);
})->each(static function ($migration) use ($migrator) {
$migrator->path($migration);
});
});
});
}

$callback();
Expand Down
8 changes: 6 additions & 2 deletions src/Concerns/InteractsWithMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use InvalidArgumentException;
use Orchestra\Testbench\Database\MigrateProcessor;

use function Orchestra\Testbench\laravel_migration_path;

trait InteractsWithMigrations
{
/**
Expand Down Expand Up @@ -67,7 +69,8 @@ protected function loadLaravelMigrations($database = []): void

$this->beforeApplicationDestroyed(function () use ($database) {
$options = $this->resolveLaravelMigrationsOptions($database);
$options['--path'] = 'migrations';
$options['--path'] = laravel_migration_path();
$options['--realpath'] = true;

(new MigrateProcessor($this, $options))->rollback();
});
Expand All @@ -82,7 +85,8 @@ protected function loadLaravelMigrations($database = []): void
protected function loadLaravelMigrationsWithoutRollback($database = []): void
{
$options = $this->resolveLaravelMigrationsOptions($database);
$options['--path'] = 'migrations';
$options['--path'] = laravel_migration_path();
$options['--realpath'] = true;

(new MigrateProcessor($this, $options))->up();

Expand Down
22 changes: 11 additions & 11 deletions src/Concerns/WithLaravelMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Foundation\Testing\RefreshDatabase;

use function Orchestra\Testbench\after_resolving;
use function Orchestra\Testbench\laravel_migration_path;

trait WithLaravelMigrations
{
Expand All @@ -20,18 +21,17 @@ protected function setUpWithLaravelMigrations(): void
/** @var bool $loadLaravelMigrations */
$loadLaravelMigrations = static::cachedConfigurationForWorkbench()->getWorkbenchAttributes()['install'] ?? false;

if (! ($loadLaravelMigrations && static::usesTestingConcern(WithWorkbench::class))) {
if (! is_dir($this->app->basePath('migrations'))) {
return;
}
if (! ($loadLaravelMigrations && is_dir(laravel_migration_path()))) {
return;
}

if (! static::usesTestingConcern(RefreshDatabase::class)) {
$this->loadLaravelMigrations();
} else {
after_resolving($this->app, 'migrator', static function ($migrator, $app) {
$migrator->path($app->basePath('migrations'));
});
}
if (! static::usesTestingConcern(RefreshDatabase::class)) {
$this->loadLaravelMigrations();
} else {
after_resolving($this->app, 'migrator', static function ($migrator, $app) {
/** @var \Illuminate\Database\Migrations\Migrator $migrator */
$migrator->path(laravel_migration_path());
});
}
}
}

0 comments on commit 469dbe6

Please sign in to comment.