-
Notifications
You must be signed in to change notification settings - Fork 19
Removal of Eval and amended unit test #5
Changes from all commits
1382736
ae3b77c
7fbd696
82f2879
082af84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,131 +6,90 @@ | |
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Serializer\Adapter; | ||
|
||
use Zend\Serializer; | ||
use ZendTest\Serializer\TestAsset\Dummy; | ||
use Zend\Json\Encoder; | ||
|
||
/** | ||
* @group Zend_Serializer | ||
* @covers Zend\Serializer\Adapter\PhpCode | ||
*/ | ||
class PhpCodeTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Serializer\Adapter\PhpCode | ||
*/ | ||
/** @var Serializer\Adapter\PhpCode */ | ||
private $adapter; | ||
|
||
public function setUp() | ||
{ | ||
$this->adapter = new Serializer\Adapter\PhpCode(); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
$this->adapter = null; | ||
} | ||
|
||
public function testSerializeString() | ||
{ | ||
$value = 'test'; | ||
$expected = "'test'"; | ||
|
||
$data = $this->adapter->serialize($value); | ||
$this->assertEquals($expected, $data); | ||
} | ||
|
||
public function testSerializeFalse() | ||
{ | ||
$value = false; | ||
$expected = 'false'; | ||
|
||
$data = $this->adapter->serialize($value); | ||
$this->assertEquals($expected, $data); | ||
} | ||
|
||
public function testSerializeNull() | ||
{ | ||
$value = null; | ||
$expected = 'NULL'; | ||
|
||
$data = $this->adapter->serialize($value); | ||
$this->assertEquals($expected, $data); | ||
} | ||
|
||
public function testSerializeNumeric() | ||
{ | ||
$value = 100.12345; | ||
$expected = '100.12345'; | ||
|
||
$data = $this->adapter->serialize($value); | ||
$this->assertEquals($expected, $data); | ||
} | ||
|
||
/** | ||
* Test when serializing a PHP object it matches the | ||
* encode process | ||
* | ||
* Unserialize on PHP objects occur on Zend\Json\Encoder::encode | ||
*/ | ||
public function testSerializeObject() | ||
{ | ||
$value = new \stdClass(); | ||
$expected = "stdClass::__set_state(array(\n))"; | ||
$object = new Dummy(); | ||
$data = $this->adapter->serialize($object); | ||
|
||
$data = $this->adapter->serialize($value); | ||
$this->assertEquals($expected, $data); | ||
$this->assertEquals(Encoder::encode($object), $data); | ||
} | ||
|
||
public function testUnserializeString() | ||
/** | ||
* Test when unserializing a PHP object it matches | ||
* the the same instance of original class | ||
* | ||
* Unserialize on PHP objects occur on Zend\Json\Decoder::decode | ||
*/ | ||
public function testUnserializeObject() | ||
{ | ||
$value = "'test'"; | ||
$expected = 'test'; | ||
$expected = new Dummy(); | ||
$serialized = $this->adapter->serialize($expected); | ||
|
||
$data = $this->adapter->unserialize($value); | ||
$this->assertEquals($expected, $data); | ||
} | ||
|
||
public function testUnserializeFalse() | ||
{ | ||
$value = 'false'; | ||
$expected = false; | ||
$data = $this->adapter->unserialize($serialized); | ||
|
||
$data = $this->adapter->unserialize($value); | ||
$this->assertEquals($expected, $data); | ||
$this->assertInstanceOf(get_class($expected), $data); | ||
} | ||
|
||
public function testUnserializeNull() | ||
{ | ||
$value = 'NULL'; | ||
$expected = null; | ||
|
||
$data = $this->adapter->unserialize($value); | ||
$this->assertEquals($expected, $data); | ||
} | ||
|
||
public function testUnserializeNumeric() | ||
/** | ||
* @dataProvider serializedValuesProvider | ||
*/ | ||
public function testSerialize($unserialized, $serialized) | ||
{ | ||
$value = '100'; | ||
$expected = 100; | ||
|
||
$data = $this->adapter->unserialize($value); | ||
$this->assertEquals($expected, $data); | ||
$this->assertEquals($serialized, $this->adapter->serialize($unserialized)); | ||
} | ||
|
||
/* TODO: PHP Fatal error: Call to undefined method stdClass::__set_state() | ||
public function testUnserializeObject() | ||
/** | ||
* @dataProvider serializedValuesProvider | ||
*/ | ||
public function testUnserialize($unserialized, $serialized) | ||
{ | ||
$value = "stdClass::__set_state(array(\n))"; | ||
$expected = new stdClass(); | ||
|
||
$data = $this->adapter->unserialize($value); | ||
$this->assertEquals($expected, $data); | ||
$this->assertEquals($unserialized, $this->adapter->unserialize($serialized)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test should work without changes. Otherwise this PR introduces a BC Break. You'll find why JSON Encoder is not the solution. The adapter emulate php native serialize/unserialize without call the native implementation. JSON Encoded adapter is this https://github.com/zendframework/zend-serializer/blob/master/src/Adapter/Json.php There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we keep this test and make the changes suggested, from what I see the 'unserialize' will still fail. Mainly due to __set_status: "PHP Fatal error: Call to undefined method stdClass::__set_state()". The testUnserializeObject is skipped because of this. This was the main reason why I decided to re-work this adapter altogether. What are your thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the original issue is not resolved. This adapter has as objective serialize and unserialize using the PHP string format and not the JSON format. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we change the format, then this test wouldn't work unless we change the expected value. |
||
*/ | ||
|
||
public function testUnserializeInvalid() | ||
public function serializedValuesProvider() | ||
{ | ||
if (version_compare(PHP_VERSION, '7', 'ge')) { | ||
$this->markTestSkipped('Cannot catch parse errors in PHP 7+'); | ||
} | ||
$value = 'not a serialized string'; | ||
|
||
$this->setExpectedException('Zend\Serializer\Exception\RuntimeException', 'syntax error'); | ||
$this->adapter->unserialize($value); | ||
return [ | ||
// Description => [unserialized, serialized] | ||
'String' => ['test', serialize('test')], | ||
'true' => [true, serialize(true)], | ||
'false' => [false, serialize(false)], | ||
'null' => [null, serialize(null)], | ||
'int' => [1, serialize(1)], | ||
'float' => [1.2, serialize(1.2)], | ||
|
||
// Boolean as string | ||
'"true"' => ['true', serialize('true')], | ||
'"false"' => ['false', serialize('false')], | ||
'"null"' => ['null', serialize('null')], | ||
'"1"' => ['1', serialize('1')], | ||
'"1.2"' => ['1.2', serialize('1.2')], | ||
|
||
'PHP Code with tags' => ['<?php echo "test"; ?>', serialize('<?php echo "test"; ?>')] | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,8 @@ | |
|
||
namespace ZendTest\Serializer\TestAsset; | ||
|
||
class Dummy | ||
{} | ||
class Dummy { | ||
|
||
public $test = '1'; | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import the final class instead the only the namespace. This is the only class imported.