From 9995e4208e8baad153f764b873fc72a29c5dddf6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 30 Sep 2023 10:36:51 +0900 Subject: [PATCH] test: update existing tests --- tests/system/Entity/EntityTest.php | 115 +++++++++++++++++------------ 1 file changed, 69 insertions(+), 46 deletions(-) diff --git a/tests/system/Entity/EntityTest.php b/tests/system/Entity/EntityTest.php index 66ee0645ac89..6863c19df199 100644 --- a/tests/system/Entity/EntityTest.php +++ b/tests/system/Entity/EntityTest.php @@ -61,7 +61,7 @@ public function testSetArrayToPropertyNamedAttributes() 2 => 3, ], ]; - $this->assertSame($expected, $entity->toRawArray()); + $this->assertSame($expected, $entity->toDatabase()); } public function testSimpleSetAndGet(): void @@ -347,23 +347,31 @@ public function testCastIntBool(): void ]; }; - $entity->setAttributes(['active' => '1']); + $entity->active = '1'; $this->assertTrue($entity->active); - $entity->setAttributes(['active' => '0']); + $entity->active = '0'; + + $this->assertFalse($entity->active); + + $entity->active = 1; + + $this->assertTrue($entity->active); + + $entity->active = 0; $this->assertFalse($entity->active); $entity->active = true; $this->assertTrue($entity->active); - $this->assertSame(['active' => 1], $entity->toRawArray()); + $this->assertSame(['active' => 1], $entity->toDatabase()); $entity->active = false; $this->assertFalse($entity->active); - $this->assertSame(['active' => 0], $entity->toRawArray()); + $this->assertSame(['active' => 0], $entity->toDatabase()); } public function testCastFloat(): void @@ -423,11 +431,12 @@ public function testCastBoolean(): void public function testCastCSV(): void { - $entity = $this->getCastEntity(); + $entity = $this->getCastEntity(); + $data = ['foo', 'bar', 'bam']; $entity->twelfth = $data; - $result = $entity->toRawArray(); + $result = $entity->toDatabase(); $this->assertIsString($result['twelfth']); $this->assertSame('foo,bar,bam', $result['twelfth']); @@ -486,8 +495,9 @@ public function testCastArray(): void $entity->seventh = ['foo' => 'bar']; - $check = $this->getPrivateProperty($entity, 'attributes')['seventh']; - $this->assertSame(serialize(['foo' => 'bar']), $check); + $result = $entity->toDatabase(); + + $this->assertSame(serialize(['foo' => 'bar']), $result['seventh']); $this->assertSame(['foo' => 'bar'], $entity->seventh); } @@ -497,11 +507,12 @@ public function testCastArrayByStringSerialize(): void $entity->seventh = 'foobar'; + $result = $entity->toDatabase(); + // Should be a serialized string now... - $check = $this->getPrivateProperty($entity, 'attributes')['seventh']; - $this->assertSame(serialize('foobar'), $check); + $this->assertSame(serialize('foobar'), $result['seventh']); - $this->assertSame(['foobar'], $entity->seventh); + $this->assertSame('foobar', $entity->seventh); } public function testCastArrayByArraySerialize(): void @@ -510,9 +521,10 @@ public function testCastArrayByArraySerialize(): void $entity->seventh = ['foo' => 'bar']; + $result = $entity->toDatabase(); + // Should be a serialized string now... - $check = $this->getPrivateProperty($entity, 'attributes')['seventh']; - $this->assertSame(serialize(['foo' => 'bar']), $check); + $this->assertSame(serialize(['foo' => 'bar']), $result['seventh']); $this->assertSame(['foo' => 'bar'], $entity->seventh); } @@ -524,9 +536,10 @@ public function testCastArrayByFill(): void $data = ['seventh' => [1, 2, 3]]; $entity->fill($data); + $result = $entity->toDatabase(); + // Check if serialiazed - $check = $this->getPrivateProperty($entity, 'attributes')['seventh']; - $this->assertSame(serialize([1, 2, 3]), $check); + $this->assertSame(serialize([1, 2, 3]), $result['seventh']); // Check if unserialized $this->assertSame([1, 2, 3], $entity->seventh); } @@ -536,9 +549,10 @@ public function testCastArrayByConstructor(): void $data = ['seventh' => [1, 2, 3]]; $entity = $this->getCastEntity($data); + $result = $entity->toDatabase(); + // Check if serialiazed - $check = $this->getPrivateProperty($entity, 'attributes')['seventh']; - $this->assertSame(serialize([1, 2, 3]), $check); + $this->assertSame(serialize([1, 2, 3]), $result['seventh']); // Check if unserialized $this->assertSame([1, 2, 3], $entity->seventh); } @@ -584,12 +598,12 @@ public function testCastAsJSON(): void $entity->tenth = ['foo' => 'bar']; + $result = $entity->toDatabase(); + // Should be a JSON-encoded string now... - $check = $this->getPrivateProperty($entity, 'attributes')['tenth']; - $this->assertSame('{"foo":"bar"}', $check); + $this->assertSame('{"foo":"bar"}', $result['tenth']); - $this->assertInstanceOf('stdClass', $entity->tenth); - $this->assertSame(['foo' => 'bar'], (array) $entity->tenth); + $this->assertSame(['foo' => 'bar'], $entity->tenth); } public function testCastAsJSONArray(): void @@ -599,9 +613,10 @@ public function testCastAsJSONArray(): void $data = ['Sun', 'Mon', 'Tue']; $entity->eleventh = $data; + $result = $entity->toDatabase(); + // Should be a JSON-encoded string now... - $check = $this->getPrivateProperty($entity, 'attributes')['eleventh']; - $this->assertSame('["Sun","Mon","Tue"]', $check); + $this->assertSame('["Sun","Mon","Tue"]', $result['eleventh']); $this->assertSame($data, $entity->eleventh); } @@ -613,9 +628,10 @@ public function testCastAsJsonByFill(): void $data = ['eleventh' => [1, 2, 3]]; $entity->fill($data); + $result = $entity->toDatabase(); + // Check if serialiazed - $check = $this->getPrivateProperty($entity, 'attributes')['eleventh']; - $this->assertSame(json_encode([1, 2, 3]), $check); + $this->assertSame(json_encode([1, 2, 3]), $result['eleventh']); // Check if unserialized $this->assertSame([1, 2, 3], $entity->eleventh); } @@ -625,9 +641,10 @@ public function testCastAsJsonByConstructor(): void $data = ['eleventh' => [1, 2, 3]]; $entity = $this->getCastEntity($data); + $result = $entity->toDatabase(); + // Check if serialiazed - $check = $this->getPrivateProperty($entity, 'attributes')['eleventh']; - $this->assertSame(json_encode([1, 2, 3]), $check); + $this->assertSame(json_encode([1, 2, 3]), $result['eleventh']); // Check if unserialized $this->assertSame([1, 2, 3], $entity->eleventh); } @@ -651,6 +668,8 @@ public function testCastAsJSONErrorDepth(): void } $current = $value; $entity->tenth = $array; + + $entity->toDatabase(); } public function testCastAsJSONErrorUTF8(): void @@ -661,6 +680,8 @@ public function testCastAsJSONErrorUTF8(): void $entity = $this->getCastEntity(); $entity->tenth = "\xB1\x31"; + + $entity->toDatabase(); } /** @@ -675,7 +696,7 @@ public function testCastAsJSONSyntaxError(): void $entity = new Entity(); $entity->casts['dummy'] = 'json[array]'; - return $entity->castAs($value, 'dummy'); + return $entity->castAs($value, 'dummy', 'fromDatabase'); }, null, Entity::class))('{ this is bad string'); } @@ -692,7 +713,7 @@ public function testCastAsJSONAnotherErrorDepth(): void $entity = new Entity(); $entity->casts['dummy'] = 'json[array]'; - return $entity->castAs($value, 'dummy'); + return $entity->castAs($value, 'dummy', 'fromDatabase'); }, null, Entity::class))($string); } @@ -709,7 +730,7 @@ public function testCastAsJSONControlCharCheck(): void $entity = new Entity(); $entity->casts['dummy'] = 'json[array]'; - return $entity->castAs($value, 'dummy'); + return $entity->castAs($value, 'dummy', 'fromDatabase'); }, null, Entity::class))($string); } @@ -726,22 +747,24 @@ public function testCastAsJSONStateMismatch(): void $entity = new Entity(); $entity->casts['dummy'] = 'json[array]'; - return $entity->castAs($value, 'dummy'); + return $entity->castAs($value, 'dummy', 'fromDatabase'); }, null, Entity::class))($string); } public function testCastSetter(): void { - $string = '321 String with numbers 123'; - $entity = $this->getCastEntity(); - $entity->first = $string; + $entity = $this->getCastEntity(); $entity->cast(false); + $string = '321 String with numbers 123'; + $entity->first = $string; $this->assertIsString($entity->first); $this->assertSame($string, $entity->first); $entity->cast(true); + $string = '321 String with numbers 123'; + $entity->first = $string; $this->assertIsInt($entity->first); $this->assertSame((int) $string, $entity->first); @@ -863,7 +886,7 @@ public function testDataMappingIssetSwapped(): void $this->assertTrue($isset); $this->assertSame('222', $entity->bar); - $result = $entity->toRawArray(); + $result = $entity->toDatabase(); $this->assertSame([ 'foo' => '222', @@ -916,11 +939,11 @@ public function testAsArrayOnlyChanged(): void ], $result); } - public function testToRawArray(): void + public function testToDatabase(): void { $entity = $this->getEntity(); - $result = $entity->toRawArray(); + $result = $entity->toDatabase(); $this->assertSame([ 'foo' => null, @@ -930,12 +953,12 @@ public function testToRawArray(): void ], $result); } - public function testToRawArrayRecursive(): void + public function testToDatabaseRecursive(): void { $entity = $this->getEntity(); $entity->entity = $this->getEntity(); - $result = $entity->toRawArray(false, true); + $result = $entity->toDatabase(false, true); $this->assertSame([ 'foo' => null, @@ -951,12 +974,12 @@ public function testToRawArrayRecursive(): void ], $result); } - public function testToRawArrayOnlyChanged(): void + public function testToDatabaseOnlyChanged(): void { $entity = $this->getEntity(); $entity->bar = 'foo'; - $result = $entity->toRawArray(true); + $result = $entity->toDatabase(true); $this->assertSame([ 'bar' => 'bar:foo', @@ -1285,16 +1308,16 @@ protected function getCastNullableEntity() return new class () extends Entity { protected $attributes = [ 'string_null' => null, - 'string_empty' => null, + 'string_empty' => '', 'integer_null' => null, - 'integer_0' => null, + 'integer_0' => 0, 'string_value_not_null' => 'value', ]; protected $_original = [ 'string_null' => null, - 'string_empty' => null, + 'string_empty' => '', 'integer_null' => null, - 'integer_0' => null, + 'integer_0' => 0, 'string_value_not_null' => 'value', ];