[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/config/tests/cases/ -> lmbIniTest.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/config/src/lmbIni.class.php');
  10  lmb_require('limb/fs/src/lmbFs.class.php');
  11  
  12  define('INI_TEST_UNIQUE_CONSTANT', '*constant*');
  13  
  14  class lmbIniTest extends UnitTestCase
  15  {
  16    function setUp()
  17    {
  18      lmbFs :: mkdir(LIMB_VAR_DIR . '/tmp_ini');
  19    }
  20  
  21    function tearDown()
  22    {
  23      lmbFs :: rm(LIMB_VAR_DIR . '/tmp_ini');
  24    }
  25  
  26    function _createIni($contents)
  27    {
  28      file_put_contents($file = LIMB_VAR_DIR . '/tmp_ini/' . mt_rand() . '.ini', $contents);
  29      return new lmbIni($file);
  30    }
  31  
  32    function testFilePath()
  33    {
  34      $ini = new lmbIni(dirname(__FILE__) . '/ini_test.ini', false);
  35      $this->assertEqual($ini->getOriginalFile(), dirname(__FILE__) . '/ini_test.ini');
  36    }
  37  
  38    function testGet()
  39    {
  40      $ini = $this->_createIni('a=foo
  41                                b=bar');
  42  
  43      $this->assertEqual($ini->get('a'), 'foo');
  44      $this->assertEqual($ini->get('b'), 'bar');
  45    }
  46  
  47    function testTrimFileContents()
  48    {
  49      $ini = $this->_createIni(
  50        '
  51          [group1]
  52           value = test1
  53        [group2]
  54                value = test2
  55        '
  56      );
  57  
  58      $this->assertEqual($ini->export(),
  59        array(
  60          'group1' => array('value' => 'test1'),
  61          'group2' => array('value' => 'test2'),
  62        )
  63      );
  64    }
  65  
  66    function testParseComments()
  67    {
  68      $ini = $this->_createIni(
  69        '
  70        #[group_is_commented]

  71        [group1]
  72         value1 = test1#this a commentary #too#
  73         #"this is just a commentary"

  74         value2 = test2
  75         value3 = "#" # symbols are allowed inside of ""
  76        '
  77      );
  78  
  79      $this->assertEqual($ini->export(),
  80        array(
  81          'group1' => array(
  82            'value1' => 'test1',
  83            'value2' => 'test2',
  84            'value3' => '#'),
  85        )
  86      );
  87    }
  88  
  89    function testParseStringsWithSpaces()
  90    {
  91      $ini = $this->_createIni(
  92        '
  93        [group1]
  94         value1 = this is a string with spaces            indeed
  95         value2 =       "this is string with spaces too
  96        '
  97      );
  98  
  99      $this->assertEqual($ini->export(),
 100        array(
 101          'group1' => array(
 102            'value1' => 'this is a string with spaces            indeed',
 103            'value2' => '"this is string with spaces too',
 104            ),
 105        )
 106      );
 107    }
 108  
 109    function testParseProperQuotes()
 110    {
 111      $ini = $this->_createIni(
 112        '
 113        [group1]
 114         value1 = "  this is a quoted string  "
 115         value2 = "  this is a quoted string "too"  "
 116         value3 = "  this is a quoted string \'too\'  "
 117        '
 118      );
 119  
 120      $this->assertEqual($ini->export(),
 121        array(
 122          'group1' => array(
 123            'value1' => '  this is a quoted string  ',
 124            'value2' => '  this is a quoted string "too"  ',
 125            'value3' => '  this is a quoted string \'too\'  ',
 126            ),
 127        )
 128      );
 129    }
 130  
 131    function testParseGlobalValues()
 132    {
 133      $ini = $this->_createIni(
 134        '
 135        value = global_test
 136        [group1]
 137         value = test
 138        '
 139      );
 140  
 141      $this->assertEqual($ini->export(),
 142        array(
 143          'value' => 'global_test',
 144          'group1' => array('value' => 'test'),
 145        )
 146      );
 147    }
 148  
 149    function testParseNullElements()
 150    {
 151      $ini = $this->_createIni(
 152        '
 153        [group1]
 154         value =
 155        '
 156      );
 157  
 158      $this->assertEqual($ini->export(),
 159        array('group1' => array('value' => null))
 160      );
 161  
 162      $this->assertFalse($ini->hasOption('group1', 'value'));
 163    }
 164  
 165    function testParseArrayElements()
 166    {
 167      $ini = $this->_createIni(
 168        '
 169        [group1]
 170         value[] =
 171         value[] = 1
 172         value[] =
 173         value[] = 2
 174        '
 175      );
 176  
 177      $this->assertEqual($ini->export(),
 178        array('group1' => array('value' => array(null, 1, null, 2)))
 179      );
 180    }
 181  
 182    function testParseHashedArrayElements()
 183    {
 184      $ini = $this->_createIni(
 185        '
 186        [group1]
 187         value[apple] =
 188         value[banana] = 1
 189         value[fruit] =
 190         value["lime"] = not valid index!
 191         value[\'lime\'] = not valid index too!
 192        '
 193      );
 194  
 195      $this->assertEqual($ini->export(),
 196        array('group1' => array('value' =>
 197                   array('apple' => null, 'banana' => 1, 'fruit' => null)))
 198      );
 199    }
 200  
 201    function testParseMixedArrays()
 202    {
 203      $ini = $this->_createIni(
 204        '
 205        [group1]
 206  
 207         foo[apple] = 1
 208         bar[] = 1
 209         foo[banana] = 2
 210         bar[] = 2
 211        '
 212      );
 213  
 214      $this->assertEqual($ini->export(),
 215        array('group1' => array('foo' => array('apple' => 1, 'banana' => 2),
 216                                'bar' => array(1, 2))));
 217    }
 218  
 219    function testHasChecks()
 220    {
 221      $ini = $this->_createIni(
 222        '
 223          unassigned =
 224          junk = 1
 225  
 226          [test]
 227          test = 1
 228  
 229          [test2]
 230          test3 =
 231  
 232          [empty_group]
 233          test = '
 234      );
 235  
 236      $this->assertFalse($ini->hasGroup(''));
 237      $this->assertTrue($ini->hasGroup('test'));
 238      $this->assertTrue($ini->hasGroup('test2'));
 239      $this->assertTrue($ini->hasGroup('empty_group'));
 240  
 241      $this->assertFalse($ini->hasOption(null, null));
 242      $this->assertFalse($ini->hasOption('', ''));
 243      $this->assertFalse($ini->hasOption('', 'no_such_block'));
 244      $this->assertTrue($ini->hasOption('test', 'test'));
 245      $this->assertFalse($ini->hasOption('no_such_variable', 'test3'));
 246      $this->assertTrue($ini->hasOption('unassigned'));
 247      $this->assertTrue($ini->hasOption('junk'));
 248    }
 249  
 250    function testGetOption()
 251    {
 252      $ini = $this->_createIni(
 253        '
 254          unassigned =
 255          junk = 1
 256  
 257          [test]
 258          test = 1
 259  
 260          [test2]
 261          test[] = 1
 262          test[] = 2
 263  
 264          [test3]
 265          test[wow] = 1
 266          test[hey] = 2'
 267      );
 268  
 269      $this->assertEqual($ini->getOption('unassigned'), '');
 270      $this->assertEqual($ini->getOption('junk'), 1);
 271  
 272      $this->assertEqual($ini->getOption('no_such_option'), '');
 273  
 274      $this->assertEqual($ini->getOption('test', 'no_such_group'), '');
 275  
 276      $this->assertEqual($ini->getOption('test', 'test'), 1);
 277  
 278      $var = $ini->getOption('test', 'test2');
 279      $this->assertEqual($var, array(1, 2));
 280  
 281      $var = $ini->getOption('test', 'test3');
 282      $this->assertEqual($var, array('wow' => 1, 'hey' => 2));
 283    }
 284  
 285    function testReplaceConstants()
 286    {
 287      $ini = $this->_createIni(
 288        '
 289          [{INI_TEST_UNIQUE_CONSTANT}]
 290          test = {INI_TEST_UNIQUE_CONSTANT}1
 291        '
 292      );
 293  
 294      $this->assertEqual($ini->getOption('test', '*constant*'), '*constant*1');
 295    }
 296  
 297    function testGetGroup()
 298    {
 299      $ini = $this->_createIni(
 300        '
 301          unassigned =
 302          junk = 1
 303  
 304          [test]
 305          test = 1
 306        '
 307      );
 308  
 309      $this->assertEqual($ini->getGroup('test'), array('test' => 1));
 310      $this->assertNull($ini->getGroup('no_such_group'));
 311    }
 312  
 313    function testAssignOption()
 314    {
 315      $ini = $this->_createIni(
 316        '
 317          unassigned =
 318          junk = 1
 319  
 320          [test]
 321          test = 2
 322        '
 323      );
 324  
 325      $this->assertTrue($ini->assignOption($test, 'unassigned'));
 326      $this->assertEqual($test, '');
 327  
 328      $this->assertTrue($ini->assignOption($test, 'junk'));
 329      $this->assertEqual($test, 1);
 330  
 331      $this->assertTrue($ini->assignOption($test, 'test', 'test'));
 332      $this->assertEqual($test, 2);
 333  
 334      $this->assertFalse($ini->assignOption($test, 'no_such_option', 'test'));
 335      $this->assertEqual($test, 2);
 336    }
 337  
 338    function testMergeWith()
 339    {
 340      $a = $this->_createIni(
 341        'test = 1
 342         foo = 1
 343         val[] = 1
 344  
 345         [group-b]
 346         a = 2
 347         foo = 1
 348         arr[1] = a
 349        '
 350      );
 351  
 352      $b = $this->_createIni(
 353        'test = 2
 354         bar = 2
 355         val[] = 2
 356  
 357         [group-b]
 358         a = 1
 359         bar = 2
 360         arr[2] = b
 361        '
 362      );
 363  
 364      $c = $a->mergeWith($b);
 365      $this->assertEqual($c->export(), array('test' => 2,
 366                                             'foo' => 1,
 367                                             'bar' => 2,
 368                                             'val' => array(2),
 369                                             'group-b' => array('a' => 1,
 370                                                                'bar' => 2,
 371                                                                'arr' => array(2 => 'b')
 372                                                                 )
 373                                              )
 374                            );
 375    }
 376  }
 377  
 378  ?>


Generated: Fri Dec 5 04:05:07 2008 Cross-referenced by PHPXref 0.7