pictcode / lib / Cake / Test / Case / Configure / PhpReaderTest.php @ 001918d1
履歴 | 表示 | アノテート | ダウンロード (4.287 KB)
| 1 | 
      <?php
     | 
  
|---|---|
| 2 | 
      /**
     | 
  
| 3 | 
       * PhpConfigReaderTest
     | 
  
| 4 | 
       *
     | 
  
| 5 | 
       * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
     | 
  
| 6 | 
       * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
     | 
  
| 7 | 
       *
     | 
  
| 8 | 
       * Licensed under The MIT License
     | 
  
| 9 | 
       * For full copyright and license information, please see the LICENSE.txt
     | 
  
| 10 | 
       * Redistributions of files must retain the above copyright notice
     | 
  
| 11 | 
       *
     | 
  
| 12 | 
       * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
     | 
  
| 13 | 
       * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
     | 
  
| 14 | 
       * @package       Cake.Test.Case.Configure
     | 
  
| 15 | 
       * @since         CakePHP(tm) v 2.0
     | 
  
| 16 | 
       * @license       http://www.opensource.org/licenses/mit-license.php MIT License
     | 
  
| 17 | 
       */
     | 
  
| 18 | 
       | 
  
| 19 | 
      App::uses('PhpReader', 'Configure');  | 
  
| 20 | 
       | 
  
| 21 | 
      /**
     | 
  
| 22 | 
       * Class PhpReaderTest
     | 
  
| 23 | 
       *
     | 
  
| 24 | 
       * @package       Cake.Test.Case.Configure
     | 
  
| 25 | 
       */
     | 
  
| 26 | 
      class PhpReaderTest extends CakeTestCase {  | 
  
| 27 | 
       | 
  
| 28 | 
      /**
     | 
  
| 29 | 
       * Test data to serialize and unserialize.
     | 
  
| 30 | 
       *
     | 
  
| 31 | 
       * @var array
     | 
  
| 32 | 
       */
     | 
  
| 33 | 
      public $testData = array(  | 
  
| 34 | 
      'One' => array(  | 
  
| 35 | 
      'two' => 'value',  | 
  
| 36 | 
      'three' => array(  | 
  
| 37 | 
      'four' => 'value four'  | 
  
| 38 | 
      ),  | 
  
| 39 | 
      'is_null' => null,  | 
  
| 40 | 
      'bool_false' => false,  | 
  
| 41 | 
      'bool_true' => true,  | 
  
| 42 | 
      ),  | 
  
| 43 | 
      'Asset' => array(  | 
  
| 44 | 
      'timestamp' => 'force'  | 
  
| 45 | 
      ),  | 
  
| 46 | 
      );  | 
  
| 47 | 
       | 
  
| 48 | 
      /**
     | 
  
| 49 | 
       * Setup.
     | 
  
| 50 | 
       *
     | 
  
| 51 | 
       * @return void
     | 
  
| 52 | 
       */
     | 
  
| 53 | 
      public function setUp() {  | 
  
| 54 | 
                      parent::setUp();
     | 
  
| 55 | 
      $this->path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;  | 
  
| 56 | 
      }  | 
  
| 57 | 
       | 
  
| 58 | 
      /**
     | 
  
| 59 | 
       * Test reading files.
     | 
  
| 60 | 
       *
     | 
  
| 61 | 
       * @return void
     | 
  
| 62 | 
       */
     | 
  
| 63 | 
      public function testRead() {  | 
  
| 64 | 
      $reader = new PhpReader($this->path);  | 
  
| 65 | 
      $values = $reader->read('var_test');  | 
  
| 66 | 
      $this->assertEquals('value', $values['Read']);  | 
  
| 67 | 
      $this->assertEquals('buried', $values['Deep']['Deeper']['Deepest']);  | 
  
| 68 | 
       | 
  
| 69 | 
      $values = $reader->read('var_test.php');  | 
  
| 70 | 
      $this->assertEquals('value', $values['Read']);  | 
  
| 71 | 
      }  | 
  
| 72 | 
       | 
  
| 73 | 
      /**
     | 
  
| 74 | 
       * Test an exception is thrown by reading files that exist without .php extension.
     | 
  
| 75 | 
       *
     | 
  
| 76 | 
       * @expectedException ConfigureException
     | 
  
| 77 | 
       * @return void
     | 
  
| 78 | 
       */
     | 
  
| 79 | 
      public function testReadWithExistentFileWithoutExtension() {  | 
  
| 80 | 
      $reader = new PhpReader($this->path);  | 
  
| 81 | 
      $reader->read('no_php_extension');  | 
  
| 82 | 
      }  | 
  
| 83 | 
       | 
  
| 84 | 
      /**
     | 
  
| 85 | 
       * Test an exception is thrown by reading files that don't exist.
     | 
  
| 86 | 
       *
     | 
  
| 87 | 
       * @expectedException ConfigureException
     | 
  
| 88 | 
       * @return void
     | 
  
| 89 | 
       */
     | 
  
| 90 | 
      public function testReadWithNonExistentFile() {  | 
  
| 91 | 
      $reader = new PhpReader($this->path);  | 
  
| 92 | 
      $reader->read('fake_values');  | 
  
| 93 | 
      }  | 
  
| 94 | 
       | 
  
| 95 | 
      /**
     | 
  
| 96 | 
       * Test reading an empty file.
     | 
  
| 97 | 
       *
     | 
  
| 98 | 
       * @expectedException ConfigureException
     | 
  
| 99 | 
       * @return void
     | 
  
| 100 | 
       */
     | 
  
| 101 | 
      public function testReadEmptyFile() {  | 
  
| 102 | 
      $reader = new PhpReader($this->path);  | 
  
| 103 | 
      $reader->read('empty');  | 
  
| 104 | 
      }  | 
  
| 105 | 
       | 
  
| 106 | 
      /**
     | 
  
| 107 | 
       * Test reading keys with ../ doesn't work.
     | 
  
| 108 | 
       *
     | 
  
| 109 | 
       * @expectedException ConfigureException
     | 
  
| 110 | 
       * @return void
     | 
  
| 111 | 
       */
     | 
  
| 112 | 
      public function testReadWithDots() {  | 
  
| 113 | 
      $reader = new PhpReader($this->path);  | 
  
| 114 | 
      $reader->read('../empty');  | 
  
| 115 | 
      }  | 
  
| 116 | 
       | 
  
| 117 | 
      /**
     | 
  
| 118 | 
       * Test reading from plugins.
     | 
  
| 119 | 
       *
     | 
  
| 120 | 
       * @return void
     | 
  
| 121 | 
       */
     | 
  
| 122 | 
      public function testReadPluginValue() {  | 
  
| 123 | 
      App::build(array(  | 
  
| 124 | 
      'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)  | 
  
| 125 | 
      ), App::RESET);  | 
  
| 126 | 
      CakePlugin::load('TestPlugin');  | 
  
| 127 | 
      $reader = new PhpReader($this->path);  | 
  
| 128 | 
      $result = $reader->read('TestPlugin.load');  | 
  
| 129 | 
      $this->assertTrue(isset($result['plugin_load']));  | 
  
| 130 | 
       | 
  
| 131 | 
      $result = $reader->read('TestPlugin.load.php');  | 
  
| 132 | 
      $this->assertTrue(isset($result['plugin_load']));  | 
  
| 133 | 
                      CakePlugin::unload();
     | 
  
| 134 | 
      }  | 
  
| 135 | 
       | 
  
| 136 | 
      /**
     | 
  
| 137 | 
       * Test dumping data to PHP format.
     | 
  
| 138 | 
       *
     | 
  
| 139 | 
       * @return void
     | 
  
| 140 | 
       */
     | 
  
| 141 | 
      public function testDump() {  | 
  
| 142 | 
      $reader = new PhpReader(TMP);  | 
  
| 143 | 
      $result = $reader->dump('test.php', $this->testData);  | 
  
| 144 | 
      $this->assertTrue($result > 0);  | 
  
| 145 | 
      $expected = <<<PHP  | 
  
| 146 | 
      <?php
     | 
  
| 147 | 
      \$config = array (
     | 
  
| 148 | 
        'One' => 
     | 
  
| 149 | 
        array (
     | 
  
| 150 | 
          'two' => 'value',
     | 
  
| 151 | 
          'three' => 
     | 
  
| 152 | 
          array (
     | 
  
| 153 | 
            'four' => 'value four',
     | 
  
| 154 | 
          ),
     | 
  
| 155 | 
          'is_null' => NULL,
     | 
  
| 156 | 
          'bool_false' => false,
     | 
  
| 157 | 
          'bool_true' => true,
     | 
  
| 158 | 
        ),
     | 
  
| 159 | 
        'Asset' => 
     | 
  
| 160 | 
        array (
     | 
  
| 161 | 
          'timestamp' => 'force',
     | 
  
| 162 | 
        ),
     | 
  
| 163 | 
      );
     | 
  
| 164 | 
      PHP;
     | 
  
| 165 | 
      $file = TMP . 'test.php';  | 
  
| 166 | 
      $contents = file_get_contents($file);  | 
  
| 167 | 
       | 
  
| 168 | 
      unlink($file);  | 
  
| 169 | 
      $this->assertTextEquals($expected, $contents);  | 
  
| 170 | 
       | 
  
| 171 | 
      $result = $reader->dump('test', $this->testData);  | 
  
| 172 | 
      $this->assertTrue($result > 0);  | 
  
| 173 | 
       | 
  
| 174 | 
      $contents = file_get_contents($file);  | 
  
| 175 | 
      $this->assertTextEquals($expected, $contents);  | 
  
| 176 | 
      unlink($file);  | 
  
| 177 | 
      }  | 
  
| 178 | 
       | 
  
| 179 | 
      /**
     | 
  
| 180 | 
       * Test that dump() makes files read() can read.
     | 
  
| 181 | 
       *
     | 
  
| 182 | 
       * @return void
     | 
  
| 183 | 
       */
     | 
  
| 184 | 
      public function testDumpRead() {  | 
  
| 185 | 
      $reader = new PhpReader(TMP);  | 
  
| 186 | 
      $reader->dump('test.php', $this->testData);  | 
  
| 187 | 
      $result = $reader->read('test.php');  | 
  
| 188 | 
      unlink(TMP . 'test.php');  | 
  
| 189 | 
       | 
  
| 190 | 
      $this->assertEquals($this->testData, $result);  | 
  
| 191 | 
      }  | 
  
| 192 | 
       | 
  
| 193 | 
      }  |