/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
tests/Database/DatabaseConcernsHasAttributesTest.php
120 строк
3 KB
Jason McCreary
[13.x] Mockery cleanup (#61117)
10 авг 2026, 20:23
Не верифицирован
10 авг 2026, 20:23
2ee40ee
Код
Авторство
О чём код?
<?php namespace Illuminate\Tests\Database; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Concerns\HasAttributes; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Mockery; use PHPUnit\Framework\TestCase; class DatabaseConcernsHasAttributesTest extends TestCase { public function testWithoutConstructor() { $instance = new HasAttributesWithoutConstructor(); $attributes = $instance->getMutatedAttributes(); $this->assertEquals(['some_attribute'], $attributes); } public function testWithConstructorArguments() { $instance = new HasAttributesWithConstructorArguments(null); $attributes = $instance->getMutatedAttributes(); $this->assertEquals(['some_attribute'], $attributes); } public function testRelationsToArray() { $mock = Mockery::mock(HasAttributesWithoutConstructor::class) ->makePartial() ->shouldAllowMockingProtectedMethods() ->expects('getArrayableRelations')->andReturn([ 'arrayable_relation' => new Collection(['foo' => 'bar']), 'invalid_relation' => 'invalid', 'null_relation' => null, ]) ->getMock(); $this->assertEquals([ 'arrayable_relation' => ['foo' => 'bar'], 'null_relation' => null, ], $mock->relationsToArray()); } public function testCastingEmptyStringToArrayDoesNotError() { $instance = new HasAttributesWithArrayCast(); $this->assertEquals(['foo' => null], $instance->attributesToArray()); $this->assertSame(json_last_error(), JSON_ERROR_NONE); } public function testUnsettingCachedAttribute() { $instance = new HasCacheableAttributeWithAccessor(); $this->assertSame('foo', $instance->getAttribute('cacheableProperty')); $this->assertTrue($instance->cachedAttributeIsset('cacheableProperty')); unset($instance->cacheableProperty); $this->assertFalse($instance->cachedAttributeIsset('cacheableProperty')); } } class HasAttributesWithoutConstructor { use HasAttributes; public function someAttribute(): Attribute { return new Attribute(function () { }); } } class HasAttributesWithConstructorArguments extends HasAttributesWithoutConstructor { public function __construct($someValue) { } } class HasAttributesWithArrayCast { use HasAttributes; public function getArrayableAttributes(): array { return ['foo' => '']; } public function getCasts(): array { return ['foo' => 'array']; } public function usesTimestamps(): bool { return false; } } /** * @property string $cacheableProperty */ class HasCacheableAttributeWithAccessor extends Model { public function cacheableProperty(): Attribute { return Attribute::make( get: fn () => 'foo' )->shouldCache(); } public function cachedAttributeIsset($attribute): bool { return isset($this->attributeCastCache[$attribute]); } }