Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Dec 14, 2021
1 parent bdc0456 commit 69565a1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\Fixtures\TestEloquentModelWithAttributeCast;

if (PHP_VERSION_ID >= 80000) {
include 'Fixtures/TestEloquentModelWithAttributeCast.php';
}

/**
* @requires PHP 8.0
*/
class DatabaseEloquentModelAttributeCastingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
Expand Down Expand Up @@ -177,70 +182,6 @@ public function testSettingRawAttributesClearsTheCastCache()
}
}

class TestEloquentModelWithAttributeCast extends Model
{
/**
* The attributes that aren't mass assignable.
*
* @var string[]
*/
protected $guarded = [];

public function uppercase(): Attribute
{
return new Attribute(
get: fn ($value) => strtoupper($value),
set: fn ($value) => strtoupper($value),
);
}

public function address(): Attribute
{
return new Attribute(
get: function ($value, $attributes) {
if (is_null($attributes['address_line_one'])) {
return;
}

return new AttributeCastAddress($attributes['address_line_one'], $attributes['address_line_two']);
},
set: function ($value) {
if (is_null($value)) {
return [
'address_line_one' => null,
'address_line_two' => null,
];
}

return ['address_line_one' => $value->lineOne, 'address_line_two' => $value->lineTwo];
},
);
}

public function options(): Attribute
{
return new Attribute(
get: fn ($value) => json_decode($value, true),
set: fn ($value) => json_encode($value),
);
}

public function birthdayAt(): Attribute
{
return new Attribute(
get: fn ($value) => Carbon::parse($value),
set: fn ($value) => $value->format('Y-m-d'),
);
}

public function password(): Attribute
{
return new Attribute(
set: fn ($value) => hash('sha256', $value)
);
}
}

class AttributeCastAddress
{
public $lineOne;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Illuminate\Tests\Integration\Database\Fixtures;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Tests\Integration\Database\AttributeCastAddress;

class TestEloquentModelWithAttributeCast extends Model
{
/**
* The attributes that aren't mass assignable.
*
* @var string[]
*/
protected $guarded = [];

public function uppercase(): Attribute
{
return new Attribute(
get: fn ($value) => strtoupper($value),
set: fn ($value) => strtoupper($value),
);
}

public function address(): Attribute
{
return new Attribute(
get: function ($value, $attributes) {
if (is_null($attributes['address_line_one'])) {
return;
}

return new AttributeCastAddress($attributes['address_line_one'], $attributes['address_line_two']);
},
set: function ($value) {
if (is_null($value)) {
return [
'address_line_one' => null,
'address_line_two' => null,
];
}

return ['address_line_one' => $value->lineOne, 'address_line_two' => $value->lineTwo];
},
);
}

public function options(): Attribute
{
return new Attribute(
get: fn ($value) => json_decode($value, true),
set: fn ($value) => json_encode($value),
);
}

public function birthdayAt(): Attribute
{
return new Attribute(
get: fn ($value) => Carbon::parse($value),
set: fn ($value) => $value->format('Y-m-d'),
);
}

public function password(): Attribute
{
return new Attribute(
set: fn ($value) => hash('sha256', $value)
);
}
}

0 comments on commit 69565a1

Please sign in to comment.