|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cone\Root\Tests\Casts; |
| 6 | + |
| 7 | +use Cone\Root\Casts\MetaValue; |
| 8 | +use Cone\Root\Tests\TestCase; |
| 9 | +use Illuminate\Database\Eloquent\Model; |
| 10 | + |
| 11 | +final class MetaValueTest extends TestCase |
| 12 | +{ |
| 13 | + protected MetaValue $cast; |
| 14 | + |
| 15 | + protected Model $model; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + $this->cast = new MetaValue(); |
| 22 | + |
| 23 | + $this->model = new class extends Model |
| 24 | + { |
| 25 | + protected $guarded = []; |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + public function test_it_returns_null_when_value_is_null(): void |
| 30 | + { |
| 31 | + $result = $this->cast->get($this->model, 'test_key', null, []); |
| 32 | + |
| 33 | + $this->assertNull($result); |
| 34 | + } |
| 35 | + |
| 36 | + public function test_it_decodes_json_value(): void |
| 37 | + { |
| 38 | + $jsonValue = json_encode(['foo' => 'bar', 'baz' => 'qux']); |
| 39 | + |
| 40 | + $result = $this->cast->get($this->model, 'test_key', $jsonValue, []); |
| 41 | + |
| 42 | + $this->assertSame(['foo' => 'bar', 'baz' => 'qux'], $result); |
| 43 | + } |
| 44 | + |
| 45 | + public function test_it_returns_string_value_when_json_decode_fails(): void |
| 46 | + { |
| 47 | + $result = $this->cast->get($this->model, 'test_key', 'plain string', []); |
| 48 | + |
| 49 | + $this->assertSame('plain string', $result); |
| 50 | + } |
| 51 | + |
| 52 | + public function test_it_returns_null_for_storage_when_value_is_null(): void |
| 53 | + { |
| 54 | + $result = $this->cast->set($this->model, 'test_key', null, []); |
| 55 | + |
| 56 | + $this->assertNull($result); |
| 57 | + } |
| 58 | + |
| 59 | + public function test_it_returns_string_value_for_storage(): void |
| 60 | + { |
| 61 | + $result = $this->cast->set($this->model, 'test_key', 'test value', []); |
| 62 | + |
| 63 | + $this->assertSame('test value', $result); |
| 64 | + } |
| 65 | + |
| 66 | + public function test_it_returns_numeric_value_as_string_for_storage(): void |
| 67 | + { |
| 68 | + $result = $this->cast->set($this->model, 'test_key', 123, []); |
| 69 | + |
| 70 | + $this->assertSame('123', $result); |
| 71 | + } |
| 72 | + |
| 73 | + public function test_it_encodes_array_to_json_for_storage(): void |
| 74 | + { |
| 75 | + $result = $this->cast->set($this->model, 'test_key', ['foo' => 'bar'], []); |
| 76 | + |
| 77 | + $this->assertSame('{"foo":"bar"}', $result); |
| 78 | + } |
| 79 | + |
| 80 | + public function test_it_converts_stringable_object_for_storage(): void |
| 81 | + { |
| 82 | + $stringable = new class |
| 83 | + { |
| 84 | + public function __toString(): string |
| 85 | + { |
| 86 | + return 'stringable value'; |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + $result = $this->cast->set($this->model, 'test_key', $stringable, []); |
| 91 | + |
| 92 | + $this->assertSame('stringable value', $result); |
| 93 | + } |
| 94 | +} |
0 commit comments