-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from mp3000mp/serialize
Added "serialization" support
- Loading branch information
Showing
9 changed files
with
269 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Noodlehaus\Parser; | ||
|
||
use Noodlehaus\Exception\ParseException; | ||
|
||
/** | ||
* Class Serialize | ||
* | ||
* @package Config | ||
*/ | ||
class Serialize implements ParserInterface | ||
{ | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function parseFile($filename) | ||
{ | ||
$data = file_get_contents($filename); | ||
|
||
return (array) $this->parse($data, $filename); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function parseString($config) | ||
{ | ||
return (array) $this->parse($config); | ||
} | ||
|
||
|
||
/** | ||
* Completes parsing of JSON data | ||
* | ||
* @param string $data | ||
* @param string $filename | ||
* @return array|null | ||
* | ||
* @throws ParseException If there is an error parsing the serialized data | ||
*/ | ||
protected function parse($data = null, $filename = null) | ||
{ | ||
$serializedData = @unserialize($data); | ||
if($serializedData === false){ | ||
|
||
throw new ParseException(error_get_last()); | ||
} | ||
|
||
return $serializedData; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSupportedExtensions() | ||
{ | ||
return ['txt']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Noodlehaus\Writer; | ||
|
||
/** | ||
* Class Serialize | ||
* | ||
* @package Config | ||
*/ | ||
class Serialize extends AbstractWriter | ||
{ | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function toString($config, $pretty = true) | ||
{ | ||
return serialize($config); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSupportedExtensions() | ||
{ | ||
return ['txt']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
namespace Noodlehaus\Parser\Test; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Noodlehaus\Parser\Serialize; | ||
|
||
/** | ||
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-04-21 at 22:37:22. | ||
*/ | ||
class SerializeTest extends TestCase | ||
{ | ||
/** | ||
* @var Serialize | ||
*/ | ||
protected $serialize; | ||
|
||
/** | ||
* Sets up the fixture, for example, opens a network connection. | ||
* This method is called before a test is executed. | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->serialize = new Serialize(); | ||
} | ||
|
||
/** | ||
* Tears down the fixture, for example, closes a network connection. | ||
* This method is called after a test is executed. | ||
*/ | ||
protected function tearDown() | ||
{ | ||
} | ||
|
||
/** | ||
* @covers Noodlehaus\Parser\Serialize::getSupportedExtensions() | ||
*/ | ||
public function testGetSupportedExtensions() | ||
{ | ||
$expected = ['txt']; | ||
$actual = $this->serialize->getSupportedExtensions(); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* @covers Noodlehaus\Parser\Serialize::parseFile() | ||
* @covers Noodlehaus\Parser\Serialize::parse() | ||
* @expectedException Noodlehaus\Exception\ParseException | ||
* @expectedExceptionMessage unserialize(): Error at offset 57 of 58 bytes | ||
*/ | ||
public function testLoadInvalidSerialize() | ||
{ | ||
$this->serialize->parseFile(__DIR__ . '/../mocks/fail/error.txt'); | ||
} | ||
|
||
/** | ||
* @covers Noodlehaus\Parser\Serialize::parseFile() | ||
* @covers Noodlehaus\Parser\Serialize::parseString() | ||
* @covers Noodlehaus\Parser\Serialize::parse() | ||
*/ | ||
public function testLoadSerialize() | ||
{ | ||
$file = $this->serialize->parseFile(__DIR__ . '/../mocks/pass/config.txt'); | ||
$string = $this->serialize->parseString(file_get_contents(__DIR__ . '/../mocks/pass/config.txt')); | ||
|
||
$this->assertEquals('localhost', $file['host']); | ||
$this->assertEquals('80', $file['port']); | ||
|
||
$this->assertEquals('localhost', $string['host']); | ||
$this->assertEquals('80', $string['port']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Noodlehaus\Writer\Test; | ||
|
||
use Noodlehaus\Writer\Serialize; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SerializeTest extends TestCase | ||
{ | ||
/** | ||
* @var Serialize | ||
*/ | ||
protected $writer; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $temp_file; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $data; | ||
|
||
/** | ||
* Sets up the fixture, for example, opens a network connection. | ||
* This method is called before a test is executed. | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->writer = new Serialize(); | ||
$this->temp_file = tempnam(sys_get_temp_dir(), 'config.txt'); | ||
$this->data = [ | ||
'application' => [ | ||
'name' => 'configuration', | ||
'secret' => 's3cr3t', | ||
], | ||
'host' => 'localhost', | ||
'port' => 80, | ||
'servers' => [ | ||
'host1', | ||
'host2', | ||
'host3', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Tears down the fixture, for example, closes a network connection. | ||
* This method is called after a test is executed. | ||
*/ | ||
protected function tearDown() | ||
{ | ||
unlink($this->temp_file); | ||
} | ||
|
||
/** | ||
* @covers Noodlehaus\Writer\Serialize::getSupportedExtensions() | ||
*/ | ||
public function testGetSupportedExtensions() | ||
{ | ||
$expected = ['txt']; | ||
$actual = $this->writer->getSupportedExtensions(); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* @covers Noodlehaus\Writer\Serialize::toString() | ||
*/ | ||
public function testSerialize() | ||
{ | ||
$actual = $this->writer->toString($this->data, false); | ||
$expected = 'a:4:{s:11:"application";a:2:{s:4:"name";s:13:"configuration";s:6:"secret";s:6:"s3cr3t";}s:4:"host";s:9:"localhost";s:4:"port";i:80;s:7:"servers";a:3:{i:0;s:5:"host1";i:1;s:5:"host2";i:2;s:5:"host3";}}'; | ||
|
||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* @covers Noodlehaus\Writer\Serialize::toString() | ||
* @covers Noodlehaus\Writer\Serialize::toFile() | ||
*/ | ||
public function testWriteSerialize() | ||
{ | ||
$this->writer->toFile($this->data, $this->temp_file); | ||
|
||
$this->assertFileExists($this->temp_file); | ||
$this->assertFileEquals($this->temp_file, __DIR__.'/../mocks/pass/config4.txt'); | ||
} | ||
|
||
/** | ||
* @covers Noodlehaus\Writer\Serialize::toString() | ||
* @covers Noodlehaus\Writer\Serialize::toFile() | ||
* @expectedException Noodlehaus\Exception\WriteException | ||
* @expectedExceptionMessage There was an error writing the file | ||
*/ | ||
public function testUnwritableFile() | ||
{ | ||
chmod($this->temp_file, 0444); | ||
|
||
$this->writer->toFile($this->data, $this->temp_file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a:2:{s:11:"application";a:1:{s:4:"host";s:9:"localhost";}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a:4:{s:4:"host";s:9:"localhost";s:4:"port";i:80;s:7:"servers";a:3:{i:0;s:5:"host1";i:1;s:5:"host2";i:2;s:5:"host3";}s:11:"application";a:2:{s:4:"name";s:13:"configuration";s:6:"secret";s:6:"s3cr3t";}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a:4:{s:11:"application";a:2:{s:4:"name";s:13:"configuration";s:6:"secret";s:6:"s3cr3t";}s:4:"host";s:9:"localhost";s:4:"port";i:80;s:7:"servers";a:3:{i:0;s:5:"host1";i:1;s:5:"host2";i:2;s:5:"host3";}} |