Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace unused Entity private method #5029

Merged
merged 1 commit into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 25 additions & 18 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,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()
Expand Down