diff --git a/src/Runway.php b/src/Runway.php index 053191a2..f7e08db5 100644 --- a/src/Runway.php +++ b/src/Runway.php @@ -77,6 +77,13 @@ public static function registerResource(string $model, array $config): self return new static; } + public static function clearRegisteredResources(): self + { + static::$registeredResources = []; + + return new static; + } + public static function usesRouting(): bool { return static::allResources()->filter->hasRouting()->count() >= 1; diff --git a/src/Traits/HasRunwayResource.php b/src/Traits/HasRunwayResource.php index abaafba3..9a3658e7 100644 --- a/src/Traits/HasRunwayResource.php +++ b/src/Traits/HasRunwayResource.php @@ -3,7 +3,6 @@ namespace StatamicRadPack\Runway\Traits; use Illuminate\Contracts\Database\Eloquent\Builder; -use Illuminate\Support\Facades\Schema; use Illuminate\Support\Str; use Statamic\Contracts\Data\Augmented; use Statamic\Contracts\Revisions\Revision; @@ -58,7 +57,7 @@ public function scopeRunway(Builder $query) public function scopeRunwaySearch(Builder $query, string $searchQuery) { $this->runwayResource()->blueprint()->fields()->all() - ->filter(fn (Field $field) => Schema::hasColumn($this->getTable(), $field->handle())) + ->filter(fn (Field $field) => $this->getConnection()->getSchemaBuilder()->hasColumn($this->getTable(), $field->handle())) ->reject(fn (Field $field) => $field->visibility() === 'computed') ->each(fn (Field $field) => $query->orWhere($this->getColumnForField($field->handle()), 'LIKE', '%'.$searchQuery.'%')); } diff --git a/tests/HasRunwayResourceTest.php b/tests/HasRunwayResourceTest.php new file mode 100644 index 00000000..e8e9b86a --- /dev/null +++ b/tests/HasRunwayResourceTest.php @@ -0,0 +1,51 @@ + 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + + Schema::connection('external')->create('external_posts', function ($table) { + $table->id(); + $table->string('title'); + $table->longText('body'); + $table->timestamps(); + }); + + Runway::registerResource(ExternalPost::class, []); + + ExternalPost::create([ + 'title' => 'Test External Post', + 'body' => 'This is the body of the test post.', + ]); + + ExternalPost::create([ + 'title' => 'Another Post', + 'body' => 'This is different content.', + ]); + + ExternalPost::create([ + 'title' => 'Something Else', + 'body' => 'No matching content here.', + ]); + + $results = ExternalPost::query()->runwaySearch('Test External')->get(); + + $this->assertCount(1, $results); + $this->assertEquals('Test External Post', $results->first()->title); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index cadcbcbc..fb23bff6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -8,6 +8,7 @@ use Statamic\Stache\Stores\UsersStore; use Statamic\Statamic; use Statamic\Testing\AddonTestCase; +use StatamicRadPack\Runway\Runway; use StatamicRadPack\Runway\ServiceProvider; abstract class TestCase extends AddonTestCase @@ -52,6 +53,8 @@ protected function resolveApplicationConfiguration($app) Statamic::booted(function () { Blueprint::setDirectory(__DIR__.'/__fixtures__/resources/blueprints'); }); + + Runway::clearRegisteredResources(); } protected function getPackageProviders($app): array diff --git a/tests/__fixtures__/app/Models/ExternalPost.php b/tests/__fixtures__/app/Models/ExternalPost.php new file mode 100644 index 00000000..857b8576 --- /dev/null +++ b/tests/__fixtures__/app/Models/ExternalPost.php @@ -0,0 +1,20 @@ +