Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert getConnectionName change #99

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 9 additions & 7 deletions src/Sushi.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public static function bootSushi()
}
}

public static function getSlug()
{
$slug = Str::slug(str_replace('\\', '-', static::class));
return 'sushi-'.$slug;
}

protected static function setSqliteConnection($database)
{
$config = [
Expand All @@ -93,7 +99,7 @@ protected static function setSqliteConnection($database)

static::$sushiConnection = app(ConnectionFactory::class)->make($config);

app('config')->set('database.connections.'.static::class, $config);
app('config')->set('database.connections.'.static::getSlug(), $config);
}

public function migrate()
Expand Down Expand Up @@ -209,12 +215,8 @@ public function usesTimestamps()
: false;
}

public function getSushiInsertChunkSize() {
return $this->sushiInsertChunkSize ?? 100;
}

public function getConnectionName()
public function getSushiInsertChunkSize()
{
return static::class;
return $this->sushiInsertChunkSize ?? 100;
}
}
75 changes: 52 additions & 23 deletions tests/SushiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Validator;
use Orchestra\Testbench\TestCase;
Expand Down Expand Up @@ -37,7 +39,7 @@ public function tearDown(): void
}

/** @test */
function basic_usage()
public function basic_usage()
{
$this->assertEquals(3, Foo::count());
$this->assertEquals('bar', Foo::first()->foo);
Expand All @@ -48,7 +50,7 @@ function basic_usage()
}

/** @test */
function columns_with_varying_types()
public function columns_with_varying_types()
{
$row = ModelWithVaryingTypeColumns::first();
$connectionBuilder = ModelWithVaryingTypeColumns::resolveConnection()->getSchemaBuilder();
Expand All @@ -60,7 +62,7 @@ function columns_with_varying_types()
}

/** @test */
function model_with_custom_schema()
public function model_with_custom_schema()
{
ModelWithCustomSchema::count();
$connectionBuilder = ModelWithCustomSchema::resolveConnection()->getSchemaBuilder();
Expand All @@ -69,7 +71,7 @@ function model_with_custom_schema()
}

/** @test */
function models_using_the_get_rows_property_arent_cached()
public function models_using_the_get_rows_property_arent_cached()
{
Bar::$hasBeenAccessedBefore = false;
$this->assertEquals(2, Bar::count());
Expand All @@ -78,7 +80,7 @@ function models_using_the_get_rows_property_arent_cached()
}

/** @test */
function uses_in_memory_if_the_cache_directory_is_not_writeable_or_not_found()
public function uses_in_memory_if_the_cache_directory_is_not_writeable_or_not_found()
{
config(['sushi.cache-path' => $path = __DIR__ . '/non-existant-path']);

Expand All @@ -89,7 +91,7 @@ function uses_in_memory_if_the_cache_directory_is_not_writeable_or_not_found()
}

/** @test */
function caches_sqlite_file_if_storage_cache_folder_is_available()
public function caches_sqlite_file_if_storage_cache_folder_is_available()
{
Foo::count();

Expand All @@ -101,7 +103,7 @@ function caches_sqlite_file_if_storage_cache_folder_is_available()
}

/** @test */
function avoids_error_when_creating_database_concurrently()
public function avoids_error_when_creating_database_concurrently()
{
$actualFactory = app(ConnectionFactory::class);
$actualConnection = $actualFactory->make([
Expand Down Expand Up @@ -135,7 +137,7 @@ function avoids_error_when_creating_database_concurrently()
* @test
* @group skipped
* */
function uses_same_cache_between_requests()
public function uses_same_cache_between_requests()
{
$this->markTestSkipped("I can't find a good way to test this right now.");
}
Expand All @@ -144,42 +146,46 @@ function uses_same_cache_between_requests()
* @test
* @group skipped
* */
function use_same_cache_between_requests()
public function use_same_cache_between_requests()
{
$this->markTestSkipped("I can't find a good way to test this right now.");
}

/** @test */
function adds_primary_key_if_needed()
public function adds_primary_key_if_needed()
{
$this->assertEquals([5,6], ModelWithNonStandardKeys::orderBy('id')->pluck('id')->toArray());
$this->assertEquals(1, Foo::find(1)->getKey());
}


/** @test */
function it_returns_an_empty_collection()
public function it_returns_an_empty_collection()
{
$this->assertEquals(0, Blank::count());
}

/** @test */
function can_use_exists_validation_rule()
public function can_use_exists_validation_rule()
{
ModelWithNonStandardKeys::all();
Foo::all();

$this->assertTrue(Validator::make(['bob' => 'lob'], ['bob' => 'exists:'.ModelWithNonStandardKeys::class.'.model_with_non_standard_keys'])->passes());
$this->assertTrue(Validator::make(['foo' => 'bar'], ['foo' => 'exists:'.Foo::class.'.foos'])->passes());
(int) explode('.', app()->version())[0] >= 6
? $this->assertTrue(Validator::make(['foo' => 5], ['foo' => 'exists:'.ModelWithNonStandardKeys::class.',id'])->passes())
: $this->assertTrue(Validator::make(['foo' => 5], ['foo' => 'exists:'.ModelWithNonStandardKeys::class.'.model_with_non_standard_keys,id'])->passes());

$this->assertFalse(Validator::make(['id' => 4], ['id' => 'exists:'.ModelWithNonStandardKeys::class.'.model_with_non_standard_keys'])->passes());
$this->assertFalse(Validator::make(['foo' => 'bob'], ['foo' => 'exists:'.Foo::class.'.foos'])->passes());
(int) explode('.', app()->version())[0] >= 6
? $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());
$this->assertTrue(Validator::make(['bob' => 'lob'], ['bob' => 'exists:'.ModelWithNonStandardKeys::getSlug().'.model_with_non_standard_keys'])->passes());
$this->assertTrue(Validator::make(['foo' => 'bar'], ['foo' => 'exists:'.Foo::getSlug().'.foos'])->passes());
$this->assertTrue(Validator::make(['foo' => 5], ['foo' => 'exists:'.ModelWithNonStandardKeys::getSlug().'.model_with_non_standard_keys,id'])->passes());

$this->assertFalse(Validator::make(['id' => 4], ['id' => 'exists:'.ModelWithNonStandardKeys::getSlug().'.model_with_non_standard_keys'])->passes());
$this->assertFalse(Validator::make(['foo' => 'bob'], ['foo' => 'exists:'.Foo::getSlug().'.foos'])->passes());
$this->assertFalse(Validator::make(['bob' => 'ble'], ['bob' => 'exists:'.ModelWithNonStandardKeys::getSlug().'.model_with_non_standard_keys'])->passes());
}

/** @test */
public function can_trigger_through_relations()
{
$this->expectExceptionMessage("Connection refused");

Qux::find(1)->quz;
}
}

Expand Down Expand Up @@ -297,3 +303,26 @@ class Blank extends Model

protected $rows = [];
}

class Qux extends Model
{
use \Sushi\Sushi;

protected $rows = [
['id' => 1],
];

public function quz() : HasOne
{
return $this->hasOne(Quz::class);
}
}


class Quz extends Model
{
public function qux() : BelongsTo
{
return $this->belongsTo(Qux::class);
}
}