Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Oct 2, 2024
1 parent cf88d30 commit de721ce
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
10 changes: 10 additions & 0 deletions tests/Fixtures/ModelStubWithHiddenAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace DirectoryTree\ActiveRedis\Tests\Fixtures;

use DirectoryTree\ActiveRedis\Model;

class ModelStubWithHiddenAttributes extends Model
{
protected array $hidden = ['hidden'];
}
10 changes: 10 additions & 0 deletions tests/Fixtures/ModelStubWithVisibleAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace DirectoryTree\ActiveRedis\Tests\Fixtures;

use DirectoryTree\ActiveRedis\Model;

class ModelStubWithVisibleAttributes extends Model
{
protected array $visible = ['visible'];
}
22 changes: 21 additions & 1 deletion tests/ModelCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
expect($model->json)->toBeArray();
expect($model->json['name'])->toEqual('John');
expect($model->json['age'])->toEqual(30);

expect($model->toArray()['json'])->toBe([
'name' => 'John',
'age' => 30,
]);
});

it('casts array', function () {
Expand All @@ -48,9 +53,12 @@

expect($model->array)->toBeArray();
expect($model->array)->toEqual([1, 2, 3]);
expect($model->toArray()['array'])->toBe([1, 2, 3]);
});

it('casts date', function () {
Date::setTestNow(Date::now());

$model = new ModelStubWithCasts;

$model->date = Date::now()->format('Y-m-d');
Expand All @@ -61,6 +69,7 @@
expect($model->date)->toBeInstanceOf(Carbon::class);
expect($model->date)->toEqual(Date::today());
expect($model->date)->isSameDay(Date::now());
expect($model->toArray()['date'])->toBe(Date::now()->startOfDay()->toISOString());
});

it('casts string', function () {
Expand Down Expand Up @@ -91,6 +100,7 @@
expect($model->object)->toBeInstanceOf(stdClass::class);
expect($model->object->name)->toEqual('John');
expect($model->object->age)->toEqual(30);
expect($model->toArray()['object'])->toBeInstanceOf(stdClass::class);
});

it('casts decimal', function () {
Expand All @@ -103,6 +113,7 @@

expect($model->decimal)->toBeString();
expect($model->decimal)->toEqual('123.46');
expect($model->toArray()['decimal'])->toBe('123.46');
});

it('casts timestamp', function () {
Expand All @@ -115,6 +126,7 @@

expect($model->timestamp)->toBeInt();
expect($model->timestamp)->toEqual($timestamp);
expect($model->toArray()['timestamp'])->toBe($timestamp);
});

it('casts collection', function () {
Expand All @@ -127,6 +139,7 @@

expect($model->collection)->toBeInstanceOf(Collection::class);
expect($model->collection)->toEqual(collect([1, 2, 3]));
expect($model->toArray()['collection'])->toBe([1, 2, 3]);
});

it('casts integer', function () {
Expand All @@ -139,6 +152,7 @@

expect($model->integer)->toBeInt();
expect($model->integer)->toEqual(123);
expect($model->toArray()['integer'])->toBe(123);
});

it('casts boolean', function () {
Expand All @@ -155,6 +169,7 @@

$model->boolean = '0';
expect($model->boolean)->toBeFalse();
expect($model->toArray()['boolean'])->toBeFalse();
});

it('casts float', function () {
Expand All @@ -166,7 +181,8 @@
$model->refresh();

expect($model->float)->toBeFloat();
expect($model->float)->toEqual(123.45);
expect($model->float)->toBe(123.45);
expect($model->toArray()['float'])->toBe(123.45);
});

it('casts datetime', function () {
Expand All @@ -179,6 +195,7 @@

expect($model->datetime)->toBeInstanceOf(Carbon::class);
expect($model->datetime)->toEqual(Carbon::parse('2024-09-24 15:30:00'));
expect($model->toArray()['datetime'])->toBe(Carbon::parse('2024-09-24 15:30:00')->toISOString());
});

it('casts custom_datetime', function () {
Expand All @@ -191,6 +208,7 @@

expect($model->custom_datetime)->toBeInstanceOf(Carbon::class);
expect($model->custom_datetime)->toEqual(Carbon::parse('2024-09-24 00:00:00'));
expect($model->toArray()['custom_datetime'])->toBe('2024-09-24');
});

it('casts immutable_date', function () {
Expand All @@ -203,6 +221,7 @@

expect($model->immutable_date)->toBeInstanceOf(CarbonImmutable::class);
expect($model->immutable_date)->toEqual(CarbonImmutable::parse('2024-09-24 00:00:00'));
expect($model->toArray()['immutable_date'])->toBe(CarbonImmutable::parse('2024-09-24 00:00:00')->toISOString());
});

it('casts immutable_datetime', function () {
Expand All @@ -215,4 +234,5 @@

expect($model->immutable_datetime)->toBeInstanceOf(CarbonImmutable::class);
expect($model->immutable_datetime)->toEqual(CarbonImmutable::parse('2024-09-24 15:30:00'));
expect($model->toArray()['immutable_datetime'])->toBe(CarbonImmutable::parse('2024-09-24 15:30:00')->toISOString());
});
30 changes: 30 additions & 0 deletions tests/ModelHiddenAttributesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithHiddenAttributes;
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithVisibleAttributes;

it('does not include hidden attributes when transformed into array', function () {
$model = new ModelStubWithHiddenAttributes([
'visible' => 'visible',
'hidden' => 'hidden',
]);

expect($model->toArray())->toBe(['visible' => 'visible']);

$model->setHidden(['visible']);

expect($model->toArray())->toBe(['hidden' => 'hidden']);
});

it('only includes visible attributes when transformed into array', function () {
$model = new ModelStubWithVisibleAttributes([
'visible' => 'visible',
'hidden' => 'hidden',
]);

expect($model->toArray())->toBe(['visible' => 'visible']);

$model->setVisible(['hidden']);

expect($model->toArray())->toBe(['hidden' => 'hidden']);
});

0 comments on commit de721ce

Please sign in to comment.