Permalink
Browse files

Add dump() to PhpReader.

  • Loading branch information...
1 parent c703a63 commit 578dac92598a585beea27840d513832bd5d5be3a @markstory markstory committed Apr 20, 2012
Showing with 53 additions and 0 deletions.
  1. +11 −0 lib/Cake/Configure/PhpReader.php
  2. +42 −0 lib/Cake/Test/Case/Configure/PhpReaderTest.php
View
11 lib/Cake/Configure/PhpReader.php
@@ -87,4 +87,15 @@ public function read($key) {
return $config;
}
+/**
+ * Converts the provided $data into a string of PHP code that can
+ * be used saved into a file and loaded later.
+ *
+ * @param array $data Data to dump.
+ * @return string String or PHP code.
+ */
+ public function dump($data) {
+ return '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';
+ }
+
}
View
42 lib/Cake/Test/Case/Configure/PhpReaderTest.php
@@ -96,4 +96,46 @@ public function testReadPluginValue() {
$this->assertTrue(isset($result['plugin_load']));
CakePlugin::unload();
}
+
+/**
+ * Test dumping data to PHP format.
+ *
+ * @return void
+ */
+ public function testDump() {
+ $reader = new PhpReader($this->path);
+ $data = array(
+ 'One' => array(
+ 'two' => 'value',
+ 'three' => array(
+ 'four' => 'value four'
+ ),
+ 'null' => null
+ ),
+ 'Asset' => array(
+ 'timestamp' => 'force'
+ ),
+ );
+ $result = $reader->dump($data);
+ $expected = <<<PHP
+<?php
+\$config = array (
+ 'One' =>
+ array (
+ 'two' => 'value',
+ 'three' =>
+ array (
+ 'four' => 'value four',
+ ),
+ 'null' => NULL,
+ ),
+ 'Asset' =>
+ array (
+ 'timestamp' => 'force',
+ ),
+);
+PHP;
+ $this->assertEquals($expected, $result);
+ }
+
}

0 comments on commit 578dac9

Please sign in to comment.