From 361102044b36424f89a100b270d1e32855bb3bca Mon Sep 17 00:00:00 2001 From: Erik Gaal Date: Mon, 19 Dec 2022 12:36:35 +0100 Subject: [PATCH] Add tests for all castable properties --- tests/Application/app/User.php | 19 ++++++++++++++ .../2020_01_30_000000_create_users_table.php | 21 ++++++++++++++++ tests/Type/data/model-properties.php | 25 +++++++++++++++++-- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/tests/Application/app/User.php b/tests/Application/app/User.php index 483082798..cbedd5a1f 100644 --- a/tests/Application/app/User.php +++ b/tests/Application/app/User.php @@ -54,6 +54,25 @@ class User extends Authenticatable 'properties' => AsCollection::class, 'favorites' => Favorites::class, 'secret' => Hash::class.':sha256', + + 'int' => 'int', + 'integer' => 'integer', + 'real' => 'real', + 'float' => 'float', + 'double' => 'double', + 'decimal' => 'decimal', + 'string' => 'string', + 'bool' => 'bool', + 'boolean' => 'boolean', + 'object' => 'object', + 'array' => 'array', + 'json' => 'json', + 'collection' => 'collection', + 'date' => 'date', + 'datetime' => 'datetime', + 'immutable_date' => 'immutable_date', + 'immutable_datetime' => 'immutable_datetime', + 'timestamp' => 'timestamp', ]; /** diff --git a/tests/Application/database/migrations/2020_01_30_000000_create_users_table.php b/tests/Application/database/migrations/2020_01_30_000000_create_users_table.php index 6af5657bd..8ddf53577 100644 --- a/tests/Application/database/migrations/2020_01_30_000000_create_users_table.php +++ b/tests/Application/database/migrations/2020_01_30_000000_create_users_table.php @@ -31,6 +31,27 @@ public function up(): void $table->boolean('blocked'); $table->unknownColumnType('unknown_column'); $table->rememberToken(); + + // Testing property casts + $table->integer('int'); + $table->integer('integer'); + $table->float('real'); + $table->float('float'); + $table->double('double'); + $table->decimal('decimal'); + $table->string('string'); + $table->boolean('bool'); + $table->boolean('boolean'); + $table->json('object'); + $table->json('array'); + $table->json('json'); + $table->json('collection'); + $table->date('date'); + $table->dateTime('datetime'); + $table->date('immutable_date'); + $table->dateTime('immutable_datetime'); + $table->timestamp('timestamp'); + $table->timestamps(); $table->softDeletes(); }); diff --git a/tests/Type/data/model-properties.php b/tests/Type/data/model-properties.php index a05ec748e..2d28a26e8 100644 --- a/tests/Type/data/model-properties.php +++ b/tests/Type/data/model-properties.php @@ -4,6 +4,8 @@ use App\User; use Carbon\Carbon; +use Carbon\CarbonImmutable; +use Illuminate\Support\Collection; use function PHPStan\Testing\assertType; /** @var User $user */ @@ -11,8 +13,27 @@ assertType('int', $user->stringButInt); assertType('string', $user->email); assertType('array', $user->allowed_ips); -assertType('string', $user->floatButRoundedDecimalString); -assertType(Carbon::class, $user->email_verified_at); +assertType('numeric-string', $user->floatButRoundedDecimalString); + +// Model Casts +assertType('int', $user->int); +assertType('int', $user->integer); +assertType('float', $user->real); +assertType('float', $user->float); +assertType('float', $user->double); +assertType('numeric-string', $user->decimal); +assertType('string', $user->string); +assertType('bool', $user->bool); +assertType('bool', $user->boolean); +assertType('stdClass', $user->object); +assertType('array', $user->array); +assertType('array', $user->json); +assertType(Collection::class, $user->collection); +assertType(Carbon::class, $user->date); +assertType(Carbon::class, $user->datetime); +assertType(CarbonImmutable::class, $user->immutable_date); +assertType(CarbonImmutable::class, $user->immutable_datetime); +assertType('int', $user->timestamp); // CastsAttributes assertType('App\ValueObjects\Favorites', $user->favorites);