|
| 1 | +<?php |
| 2 | + |
| 3 | +class CastsValueObjectTest extends TestCase |
| 4 | +{ |
| 5 | + public function testInstantiateModel() |
| 6 | + { |
| 7 | + $user = new UserModel(); |
| 8 | + $this->assertInstanceOf(UserModel::class, $user); |
| 9 | + } |
| 10 | + |
| 11 | + public function testInstantiateValueObject() |
| 12 | + { |
| 13 | + $email = new EmailValueObject( '[email protected]'); |
| 14 | + $this->assertInstanceOf(EmailValueObject::class, $email); |
| 15 | + } |
| 16 | + |
| 17 | + public function testAttributeInObjectsHashIsCastToValueObject() |
| 18 | + { |
| 19 | + $user = new UserModel(); |
| 20 | + $user-> setInternalAttributes([ 'email' => $email = '[email protected]']); |
| 21 | + |
| 22 | + $this->assertInstanceOf(EmailValueObject::class, $user->email); |
| 23 | + $this->assertEquals($email, $user->email->toScalar()); |
| 24 | + } |
| 25 | + |
| 26 | + public function testAttributeInObjectsHashCanBeSetWithScalarValue() |
| 27 | + { |
| 28 | + $user = new UserModel(); |
| 29 | + |
| 30 | + $user-> email = $email = '[email protected]'; |
| 31 | + |
| 32 | + $this->assertInstanceOf(EmailValueObject::class, $user->email); |
| 33 | + $this->assertEquals($email, $user->email->toScalar()); |
| 34 | + } |
| 35 | + |
| 36 | + public function testAttributeInObjectsHashCanBeSetWithValueObject() |
| 37 | + { |
| 38 | + $user = new UserModel(); |
| 39 | + |
| 40 | + $user-> email = $email = new EmailValueObject( '[email protected]'); |
| 41 | + $this->assertInstanceOf(EmailValueObject::class, $user->email); |
| 42 | + $this->assertEquals($email->toScalar(), $user->email->toScalar()); |
| 43 | + } |
| 44 | + |
| 45 | + public function testCastedValueObjectRemainsTheSameInstance() |
| 46 | + { |
| 47 | + $user = new UserModel(); |
| 48 | + |
| 49 | + $user-> email = $instance1 = new EmailValueObject( '[email protected]'); |
| 50 | + $instance2 = $user->email; |
| 51 | + |
| 52 | + $this->assertTrue($instance1 === $instance2); |
| 53 | + |
| 54 | + $instance3 = $user->email; |
| 55 | + $this->assertTrue($instance1 === $instance3); |
| 56 | + $this->assertTrue($instance2 === $instance3); |
| 57 | + } |
| 58 | + |
| 59 | + public function testAttributeNotInObjectsHashRemainsUnaffected() |
| 60 | + { |
| 61 | + $user = new UserModel(); |
| 62 | + $user->name = $name = 'John Doe'; |
| 63 | + |
| 64 | + $this->assertTrue(is_string($user->name)); |
| 65 | + $this->assertEquals($name, $user->name); |
| 66 | + } |
| 67 | + |
| 68 | + public function testCastableAttributeWithSetMutator() |
| 69 | + { |
| 70 | + $user = new UserModel(); |
| 71 | + $user-> uppercaseEmail = '[email protected]'; |
| 72 | + |
| 73 | + $this->assertInstanceOf(EmailValueObject::class, $user->uppercaseEmail); |
| 74 | + $this->assertEquals($user->getInternalAttributes()['uppercaseEmail'], $user->uppercaseEmail->toScalar()); |
| 75 | + } |
| 76 | + |
| 77 | + public function testCastableAttributeWithGetMutator() |
| 78 | + { |
| 79 | + $user = new UserModel(); |
| 80 | + $user-> mutatedEmail = $original = '[email protected]'; |
| 81 | + |
| 82 | + $this->assertEquals($user->getInternalAttributes()['mutatedEmail'], $original); |
| 83 | + $this->assertInstanceOf(EmailValueObject::class, $user->mutatedEmail); |
| 84 | + $this->assertEquals($user->getMutatedEmailAttribute($original), $user->mutatedEmail->toScalar()); |
| 85 | + } |
| 86 | + |
| 87 | + public function testValueObjectCacheIsInvalidatedWhenSettingScalar() |
| 88 | + { |
| 89 | + $user = new UserModel(); |
| 90 | + |
| 91 | + $user-> email = $email = '[email protected]'; |
| 92 | + |
| 93 | + $this->assertInstanceOf(EmailValueObject::class, $user->email); |
| 94 | + $this->assertEquals($email, $user->email->toScalar()); |
| 95 | + |
| 96 | + $user-> email = $email = '[email protected]'; |
| 97 | + |
| 98 | + $this->assertInstanceOf(EmailValueObject::class, $user->email); |
| 99 | + $this->assertEquals($email, $user->email->toScalar()); |
| 100 | + } |
| 101 | +} |
0 commit comments