Skip to content

Commit 69565a1

Browse files
committed
Fix tests
1 parent bdc0456 commit 69565a1

File tree

2 files changed

+80
-67
lines changed

2 files changed

+80
-67
lines changed

tests/Integration/Database/DatabaseEloquentModelAttributeCastingTest.php

Lines changed: 8 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace Illuminate\Tests\Integration\Database;
44

5-
use Illuminate\Database\Eloquent\Casts\Attribute;
6-
use Illuminate\Database\Eloquent\Model;
75
use Illuminate\Database\Schema\Blueprint;
8-
use Illuminate\Support\Carbon;
96
use Illuminate\Support\Facades\Schema;
7+
use Illuminate\Tests\Integration\Database\Fixtures\TestEloquentModelWithAttributeCast;
108

9+
if (PHP_VERSION_ID >= 80000) {
10+
include 'Fixtures/TestEloquentModelWithAttributeCast.php';
11+
}
12+
13+
/**
14+
* @requires PHP 8.0
15+
*/
1116
class DatabaseEloquentModelAttributeCastingTest extends DatabaseTestCase
1217
{
1318
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
@@ -177,70 +182,6 @@ public function testSettingRawAttributesClearsTheCastCache()
177182
}
178183
}
179184

180-
class TestEloquentModelWithAttributeCast extends Model
181-
{
182-
/**
183-
* The attributes that aren't mass assignable.
184-
*
185-
* @var string[]
186-
*/
187-
protected $guarded = [];
188-
189-
public function uppercase(): Attribute
190-
{
191-
return new Attribute(
192-
get: fn ($value) => strtoupper($value),
193-
set: fn ($value) => strtoupper($value),
194-
);
195-
}
196-
197-
public function address(): Attribute
198-
{
199-
return new Attribute(
200-
get: function ($value, $attributes) {
201-
if (is_null($attributes['address_line_one'])) {
202-
return;
203-
}
204-
205-
return new AttributeCastAddress($attributes['address_line_one'], $attributes['address_line_two']);
206-
},
207-
set: function ($value) {
208-
if (is_null($value)) {
209-
return [
210-
'address_line_one' => null,
211-
'address_line_two' => null,
212-
];
213-
}
214-
215-
return ['address_line_one' => $value->lineOne, 'address_line_two' => $value->lineTwo];
216-
},
217-
);
218-
}
219-
220-
public function options(): Attribute
221-
{
222-
return new Attribute(
223-
get: fn ($value) => json_decode($value, true),
224-
set: fn ($value) => json_encode($value),
225-
);
226-
}
227-
228-
public function birthdayAt(): Attribute
229-
{
230-
return new Attribute(
231-
get: fn ($value) => Carbon::parse($value),
232-
set: fn ($value) => $value->format('Y-m-d'),
233-
);
234-
}
235-
236-
public function password(): Attribute
237-
{
238-
return new Attribute(
239-
set: fn ($value) => hash('sha256', $value)
240-
);
241-
}
242-
}
243-
244185
class AttributeCastAddress
245186
{
246187
public $lineOne;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Database\Fixtures;
4+
5+
use Illuminate\Database\Eloquent\Casts\Attribute;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Carbon;
8+
use Illuminate\Tests\Integration\Database\AttributeCastAddress;
9+
10+
class TestEloquentModelWithAttributeCast extends Model
11+
{
12+
/**
13+
* The attributes that aren't mass assignable.
14+
*
15+
* @var string[]
16+
*/
17+
protected $guarded = [];
18+
19+
public function uppercase(): Attribute
20+
{
21+
return new Attribute(
22+
get: fn ($value) => strtoupper($value),
23+
set: fn ($value) => strtoupper($value),
24+
);
25+
}
26+
27+
public function address(): Attribute
28+
{
29+
return new Attribute(
30+
get: function ($value, $attributes) {
31+
if (is_null($attributes['address_line_one'])) {
32+
return;
33+
}
34+
35+
return new AttributeCastAddress($attributes['address_line_one'], $attributes['address_line_two']);
36+
},
37+
set: function ($value) {
38+
if (is_null($value)) {
39+
return [
40+
'address_line_one' => null,
41+
'address_line_two' => null,
42+
];
43+
}
44+
45+
return ['address_line_one' => $value->lineOne, 'address_line_two' => $value->lineTwo];
46+
},
47+
);
48+
}
49+
50+
public function options(): Attribute
51+
{
52+
return new Attribute(
53+
get: fn ($value) => json_decode($value, true),
54+
set: fn ($value) => json_encode($value),
55+
);
56+
}
57+
58+
public function birthdayAt(): Attribute
59+
{
60+
return new Attribute(
61+
get: fn ($value) => Carbon::parse($value),
62+
set: fn ($value) => $value->format('Y-m-d'),
63+
);
64+
}
65+
66+
public function password(): Attribute
67+
{
68+
return new Attribute(
69+
set: fn ($value) => hash('sha256', $value)
70+
);
71+
}
72+
}

0 commit comments

Comments
 (0)