Skip to content

Commit

Permalink
Replace unused Entity private method
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Aug 26, 2021
1 parent 4d0110f commit 4056a40
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
1 change: 0 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@

// private method called via getPrivateMethodInvoker
RemoveUnusedPrivateMethodRector::class => [
__DIR__ . '/system/Entity/Entity.php',
__DIR__ . '/tests/system/Test/ReflectionHelperTest.php',
],

Expand Down
14 changes: 0 additions & 14 deletions system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
*
Expand Down
36 changes: 25 additions & 11 deletions tests/system/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Entity;

use Closure;
use CodeIgniter\Entity\Exceptions\CastException;
use CodeIgniter\HTTP\URI;
use CodeIgniter\I18n\Time;
Expand Down Expand Up @@ -589,34 +590,43 @@ public function testCastAsJSONErrorUTF8()

public function testCastAsJSONSyntaxError()
{
$entity = new Entity();
$method = Closure::bind(static function (string $value) {
$entity = new Entity();
$entity->casts['dummy'] = 'json[array]';

$method = $this->getPrivateMethodInvoker($entity, 'castAsJson');
return $entity->castAs($value, 'dummy');
}, null, Entity::class);

$this->expectException(CastException::class);
$this->expectExceptionMessage('Syntax error, malformed JSON');

$method('{ this is bad string', true);
$method('{ this is bad string');
}

public function testCastAsJSONAnotherErrorDepth()
{
$entity = new Entity();
$method = Closure::bind(static function (string $value) {
$entity = new Entity();
$entity->casts['dummy'] = 'json[array]';

$method = $this->getPrivateMethodInvoker($entity, 'castAsJson');
return $entity->castAs($value, 'dummy');
}, null, Entity::class);

$this->expectException(CastException::class);
$this->expectExceptionMessage('Maximum stack depth exceeded');

$string = '{' . str_repeat('"test":{', 513) . '"test":"value"' . str_repeat('}', 513) . '}';

$method($string, true);
$method($string);
}

public function testCastAsJSONControlCharCheck()
{
$entity = new Entity();
$method = $this->getPrivateMethodInvoker($entity, 'castAsJson');
$method = Closure::bind(static function (string $value) {
$entity = new Entity();
$entity->casts['dummy'] = 'json[array]';

return $entity->castAs($value, 'dummy');
}, null, Entity::class);

$this->expectException(CastException::class);
$this->expectExceptionMessage('Unexpected control character found');
Expand All @@ -628,8 +638,12 @@ public function testCastAsJSONControlCharCheck()

public function testCastAsJSONStateMismatch()
{
$entity = new Entity();
$method = $this->getPrivateMethodInvoker($entity, 'castAsJson');
$method = Closure::bind(static function (string $value) {
$entity = new Entity();
$entity->casts['dummy'] = 'json[array]';

return $entity->castAs($value, 'dummy');
}, null, Entity::class);

$this->expectException(CastException::class);
$this->expectExceptionMessage('Underflow or the modes mismatch');
Expand Down

0 comments on commit 4056a40

Please sign in to comment.