Skip to content

Commit

Permalink
Sushi models can relate to non-sushi models (#98)
Browse files Browse the repository at this point in the history
* failing test - sushi to non-sushi relations

* sushi models can be related to non-sushi models
  • Loading branch information
amadeann authored Jan 21, 2024
1 parent 9e08b32 commit e7ce22a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/.idea/
/vendor/
/tests/database/database.sqlite
.phpunit.result.cache
composer.lock
/tests/cache/
.vscode/settings.json
9 changes: 9 additions & 0 deletions src/Sushi.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ public static function bootSushi()
}
}

protected function newRelatedInstance($class)
{
return tap(new $class, function ($instance) {
if (!$instance->getConnectionName()) {
$instance->setConnection($this->getConnectionResolver()->getDefaultConnection());
}
});
}

protected static function setSqliteConnection($database)
{
$config = [
Expand Down
53 changes: 53 additions & 0 deletions tests/SushiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,33 @@ function can_use_exists_validation_rule()
? $this->assertFalse(Validator::make(['bob' => 'ble'], ['bob' => 'exists:'.ModelWithNonStandardKeys::class])->passes())
: $this->assertFalse(Validator::make(['bob' => 'ble'], ['bob' => 'exists:'.ModelWithNonStandardKeys::class.'.model_with_non_standard_keys'])->passes());
}

/**
* @test
* @define-env usesSqliteConnection
* */
function sushi_models_can_relate_to_models_in_regular_sqlite_databases()
{
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
$this->artisan('migrate', ['--database' => 'testbench-sqlite'])->run();

$californiaMaki = Maki::create(['id' => 1, 'type' => 'California']);

$this->assertEquals(1, Ingredient::find(1)->maki->id);
$this->assertCount(2, $californiaMaki->ingredients);
}

protected function usesSqliteConnection($app)
{
file_put_contents(__DIR__ . '/database/database.sqlite', '');

$app['config']->set('database.default', 'testbench-sqlite');
$app['config']->set('database.connections.testbench-sqlite', [
'driver' => 'sqlite',
'database' => __DIR__ . '/database/database.sqlite',
'prefix' => '',
]);
}
}

class Foo extends Model
Expand Down Expand Up @@ -297,3 +324,29 @@ class Blank extends Model

protected $rows = [];
}

class Maki extends Model
{
protected $guarded = [];
public $timestamps = false;

public function ingredients()
{
return $this->hasMany(Ingredient::class);
}
}

class Ingredient extends Model
{
use \Sushi\Sushi;

protected $rows = [
['id' => 1, 'type' => 'salmon', 'maki_id' => 1],
['id' => 2, 'type' => 'avocado', 'maki_id' => 1],
];

public function maki()
{
return $this->belongsTo(Maki::class);
}
}
31 changes: 31 additions & 0 deletions tests/database/migrations/0000_00_00_000000_create_makis_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

class CreateMakisTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('makis', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('type');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('makis');
}
}

0 comments on commit e7ce22a

Please sign in to comment.