From 35fa4d517234d0bf8857ad5d309fb414aef95ea3 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Thu, 26 Aug 2021 13:35:26 +0800 Subject: [PATCH] Replace unused Entity private method --- rector.php | 1 - system/Entity/Entity.php | 14 ---------- tests/system/Entity/EntityTest.php | 43 +++++++++++++++++------------- 3 files changed, 25 insertions(+), 33 deletions(-) diff --git a/rector.php b/rector.php index cbce74ea213d..552c6341cb0b 100644 --- a/rector.php +++ b/rector.php @@ -78,7 +78,6 @@ // private method called via getPrivateMethodInvoker RemoveUnusedPrivateMethodRector::class => [ - __DIR__ . '/system/Entity/Entity.php', __DIR__ . '/tests/system/Test/ReflectionHelperTest.php', ], diff --git a/system/Entity/Entity.php b/system/Entity/Entity.php index 03f6327f8142..d8c95fea6c52 100644 --- a/system/Entity/Entity.php +++ b/system/Entity/Entity.php @@ -389,20 +389,6 @@ protected function castAs($value, string $attribute, string $method = 'get') return $handlers[$type]::$method($value, $params); } - /** - * Cast as JSON - * - * @param mixed $value - * - * @throws CastException - * - * @return mixed - */ - private function castAsJson($value, bool $asArray = false) - { - return JsonCast::get($value, $asArray ? ['array'] : []); - } - /** * Support for json_encode() * diff --git a/tests/system/Entity/EntityTest.php b/tests/system/Entity/EntityTest.php index 0928279ac514..69d0546510bd 100644 --- a/tests/system/Entity/EntityTest.php +++ b/tests/system/Entity/EntityTest.php @@ -11,6 +11,7 @@ namespace CodeIgniter\Entity; +use Closure; use CodeIgniter\Entity\Exceptions\CastException; use CodeIgniter\HTTP\URI; use CodeIgniter\I18n\Time; @@ -589,54 +590,60 @@ public function testCastAsJSONErrorUTF8() public function testCastAsJSONSyntaxError() { - $entity = new Entity(); - - $method = $this->getPrivateMethodInvoker($entity, 'castAsJson'); - $this->expectException(CastException::class); $this->expectExceptionMessage('Syntax error, malformed JSON'); - $method('{ this is bad string', true); + (Closure::bind(static function (string $value) { + $entity = new Entity(); + $entity->casts['dummy'] = 'json[array]'; + + return $entity->castAs($value, 'dummy'); + }, null, Entity::class))('{ this is bad string'); } public function testCastAsJSONAnotherErrorDepth() { - $entity = new Entity(); - - $method = $this->getPrivateMethodInvoker($entity, 'castAsJson'); - $this->expectException(CastException::class); $this->expectExceptionMessage('Maximum stack depth exceeded'); $string = '{' . str_repeat('"test":{', 513) . '"test":"value"' . str_repeat('}', 513) . '}'; - $method($string, true); + (Closure::bind(static function (string $value) { + $entity = new Entity(); + $entity->casts['dummy'] = 'json[array]'; + + return $entity->castAs($value, 'dummy'); + }, null, Entity::class))($string); } public function testCastAsJSONControlCharCheck() { - $entity = new Entity(); - $method = $this->getPrivateMethodInvoker($entity, 'castAsJson'); - $this->expectException(CastException::class); $this->expectExceptionMessage('Unexpected control character found'); $string = "{\n\t\"property1\": \"The quick brown fox\njumps over the lazy dog\",\n\t\"property2\":\"value2\"\n}"; - $method($string, true); + (Closure::bind(static function (string $value) { + $entity = new Entity(); + $entity->casts['dummy'] = 'json[array]'; + + return $entity->castAs($value, 'dummy'); + }, null, Entity::class))($string); } public function testCastAsJSONStateMismatch() { - $entity = new Entity(); - $method = $this->getPrivateMethodInvoker($entity, 'castAsJson'); - $this->expectException(CastException::class); $this->expectExceptionMessage('Underflow or the modes mismatch'); $string = '[{"name":"jack","product_id":"1234"]'; - $method($string, true); + (Closure::bind(static function (string $value) { + $entity = new Entity(); + $entity->casts['dummy'] = 'json[array]'; + + return $entity->castAs($value, 'dummy'); + }, null, Entity::class))($string); } public function testCastSetter()