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

Automatic eloquent casts #11

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
}
},
"scripts": {
"pest": "vendor/bin/pest",
"test": "vendor/bin/phpunit",
"fix-style": "vendor/bin/php-cs-fixer fix",
"check-style": "vendor/bin/php-cs-fixer fix --diff --dry-run"
},
Expand Down
3 changes: 2 additions & 1 deletion src/Bits.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public static function castUsing(array $arguments): BitsCast
{
return match (reset($arguments)) {
'sonyflake', 'sonyflakes' => new BitsCast(app(MakesSonyflakes::class)),
default => new BitsCast(app(MakesSnowflakes::class)),
'snowflake', 'snowflakes' => new BitsCast(app(MakesSnowflakes::class)),
default => new BitsCast(app(MakesBits::class)),
};
}

Expand Down
10 changes: 8 additions & 2 deletions src/Database/HasSnowflakes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

namespace Glhd\Bits\Database;

use Glhd\Bits\Snowflake;
use Glhd\Bits\Bits;
use Glhd\Bits\Contracts\MakesBits;

/**
* @mixin \Illuminate\Database\Eloquent\Model
*/
trait HasSnowflakes
{
protected function initializeHasSnowflakes(): void
{
$this->mergeCasts(array_fill_keys($this->uniqueIds(), Bits::class));

if (property_exists($this, 'usesUniqueIds')) {
$this->usesUniqueIds = true;
return;
Expand All @@ -23,7 +29,7 @@ public function uniqueIds()

public function newUniqueId()
{
return Snowflake::make()->id();
return app(MakesBits::class)->make()->id();
}

public function getIncrementing()
Expand Down
64 changes: 55 additions & 9 deletions tests/Feature/SnowflakeCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Glhd\Bits\Tests\TestCase;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Schema;
use PHPUnit\Framework\Attributes\DataProvider;

class SnowflakeCastTest extends TestCase
{
Expand All @@ -23,30 +25,57 @@ protected function setUp(): void
$table->string('label')->nullable();
$table->timestamps();
});

Schema::create('test_casts_method_models', function(Blueprint $table) {
$table->unsignedBigInteger('id')->primary();
$table->string('label')->nullable();
$table->timestamps();
});

Schema::create('test_automatic_casts_models', function(Blueprint $table) {
$table->unsignedBigInteger('id')->primary();
$table->string('label')->nullable();
$table->timestamps();
});
}

public function test_it_casts_attributes(): void

/** @param class-string<Model> $class */
#[DataProvider('modelClassProvider')]
public function test_it_casts_attributes(string $class): void
{
$model1 = TestModel::create();
$model2 = TestModel::create();
$model1 = $class::create();
$model2 = $class::create();

$this->assertInstanceOf(Snowflake::class, $model1->id);
$this->assertInstanceOf(Snowflake::class, $model2->id);

$this->assertTrue($model2->id->id() > $model1->id->id());
}

public function test_you_can_set_id_manually(): void

/** @param class-string<Model> $class */
#[DataProvider('modelClassProvider')]
public function test_you_can_set_id_manually(string $class): void
{
$model = TestModel::forceCreate(['id' => 123]);
$model = $class::forceCreate(['id' => 123]);

$this->assertEquals(123, $model->id->id());

$model = TestModel::find(123);
$model = $class::find(123);

$this->assertEquals(123, $model->id->id());

$this->assertEquals(1, TestModel::count());
$this->assertEquals(1, $class::count());
}

public static function modelClassProvider(): array
{
$with_casts_method = (int) Application::VERSION >= 11;

return array_filter([
'casts property' => [TestModel::class],
'casts method' => $with_casts_method ? [TestCastsMethodModel::class] : null,
'automatic casting' => [TestAutomaticCastsModel::class],
]);
}
}

Expand All @@ -58,3 +87,20 @@ class TestModel extends Model
'id' => Snowflake::class,
];
}

class TestCastsMethodModel extends Model
{
use HasSnowflakes;

protected function casts()
{
return [
'id' => Snowflake::class,
];
}
}

class TestAutomaticCastsModel extends Model
{
use HasSnowflakes;
}
109 changes: 109 additions & 0 deletions tests/Feature/SonyflakeCastTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Glhd\Bits\Tests\Feature;

use Glhd\Bits\Database\HasSnowflakes;
use Glhd\Bits\Sonyflake;
use Glhd\Bits\Tests\ResolvesSequencesFromMemory;
use Glhd\Bits\Tests\TestCase;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\Attributes\DefineEnvironment;
use PHPUnit\Framework\Attributes\DataProvider;

class SonyflakeCastTest extends TestCase
{
use ResolvesSequencesFromMemory;

protected function setUp(): void
{
parent::setUp();

Schema::create('test_sonyflake_models', function(Blueprint $table) {
$table->unsignedBigInteger('id')->primary();
$table->string('label')->nullable();
$table->timestamps();
});

Schema::create('test_sonyflake_casts_method_models', function(Blueprint $table) {
$table->unsignedBigInteger('id')->primary();
$table->string('label')->nullable();
$table->timestamps();
});

Schema::create('test_sonyflake_automatic_casts_models', function(Blueprint $table) {
$table->unsignedBigInteger('id')->primary();
$table->string('label')->nullable();
$table->timestamps();
});
}

/** @param class-string<Model> $class */
#[DataProvider('modelClassProvider')]
#[DefineEnvironment('usesSonyflakes')]
public function test_it_casts_attributes(string $class): void
{
$model1 = $class::create();
$model2 = $class::create();

$this->assertInstanceOf(Sonyflake::class, $model1->id);
$this->assertInstanceOf(Sonyflake::class, $model2->id);

$this->assertTrue($model2->id->id() > $model1->id->id());
}

/** @param class-string<Model> $class */
#[DataProvider('modelClassProvider')]
#[DefineEnvironment('usesSonyflakes')]
public function test_you_can_set_id_manually(string $class): void
{
$model = $class::forceCreate(['id' => 123]);

$this->assertEquals(123, $model->id->id());

$model = $class::find(123);

$this->assertEquals(123, $model->id->id());

$this->assertEquals(1, $class::count());
}

public static function modelClassProvider(): array
{
$with_casts_method = (int) Application::VERSION >= 11;

return [
'casts property' => [TestSonyflakeModel::class],
'casts method' => $with_casts_method ? [TestSonyflakeCastsMethodModel::class] : null,
'automatic casting' => [TestSonyflakeAutomaticCastsModel::class],
];
}
}

class TestSonyflakeModel extends Model
{
use HasSnowflakes;

protected $casts = [
'id' => Sonyflake::class,
];
}

class TestSonyflakeCastsMethodModel extends Model
{
use HasSnowflakes;

protected function casts()
{
return [
'id' => Sonyflake::class,
];
}
}

class TestSonyflakeAutomaticCastsModel extends Model
{
use HasSnowflakes;
}
5 changes: 5 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ protected function getApplicationTimezone($app)
{
return 'America/New_York';
}

protected function usesSonyflakes($app): void
{
$app['config']->set('bits.format', 'sonyflake');
}
}
Loading