Skip to content

Commit

Permalink
Add tests for all castable properties
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgaal committed Dec 19, 2022
1 parent bdbea43 commit 3611020
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
19 changes: 19 additions & 0 deletions tests/Application/app/User.php
Expand Up @@ -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',
];

/**
Expand Down
Expand Up @@ -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();
});
Expand Down
25 changes: 23 additions & 2 deletions tests/Type/data/model-properties.php
Expand Up @@ -4,15 +4,36 @@

use App\User;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Support\Collection;
use function PHPStan\Testing\assertType;

/** @var User $user */
assertType('int', $user->newStyleAttribute);
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);

0 comments on commit 3611020

Please sign in to comment.