PageRenderTime 24ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/config/tests/cases/lmbIniTest.class.php

http://github.com/limb-php-framework/limb
PHP | 378 lines | 321 code | 50 blank | 7 comment | 0 complexity | 872e0b8105022e66777f01601ded81d2 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 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. lmb_env_setor('INI_TEST_UNIQUE_CONSTANT', '*constant*');
  12. class lmbIniTest extends UnitTestCase
  13. {
  14. function setUp()
  15. {
  16. lmbFs :: mkdir(lmb_var_dir() . '/tmp_ini');
  17. }
  18. function tearDown()
  19. {
  20. lmbFs :: rm(lmb_var_dir() . '/tmp_ini');
  21. }
  22. function _createIni($contents)
  23. {
  24. file_put_contents($file = lmb_var_dir() . '/tmp_ini/' . mt_rand() . '.ini', $contents);
  25. return new lmbIni($file);
  26. }
  27. function testFilePath()
  28. {
  29. $ini = new lmbIni(dirname(__FILE__) . '/ini_test.ini', false);
  30. $this->assertEqual($ini->getOriginalFile(), dirname(__FILE__) . '/ini_test.ini');
  31. }
  32. function testGet()
  33. {
  34. $ini = $this->_createIni('a=foo
  35. b=bar');
  36. $this->assertEqual($ini->get('a'), 'foo');
  37. $this->assertEqual($ini->get('b'), 'bar');
  38. }
  39. function testTrimFileContents()
  40. {
  41. $ini = $this->_createIni(
  42. '
  43. [group1]
  44. value = test1
  45. [group2]
  46. value = test2
  47. '
  48. );
  49. $this->assertEqual($ini->export(),
  50. array(
  51. 'group1' => array('value' => 'test1'),
  52. 'group2' => array('value' => 'test2'),
  53. )
  54. );
  55. }
  56. function testParseComments()
  57. {
  58. $ini = $this->_createIni(
  59. '
  60. #[group_is_commented]
  61. [group1]
  62. value1 = test1#this a commentary #too#
  63. #"this is just a commentary"
  64. value2 = test2
  65. value3 = "#" # symbols are allowed inside of ""
  66. '
  67. );
  68. $this->assertEqual($ini->export(),
  69. array(
  70. 'group1' => array(
  71. 'value1' => 'test1',
  72. 'value2' => 'test2',
  73. 'value3' => '#'),
  74. )
  75. );
  76. }
  77. function testParseStringsWithSpaces()
  78. {
  79. $ini = $this->_createIni(
  80. '
  81. [group1]
  82. value1 = this is a string with spaces indeed
  83. value2 = "this is string with spaces too
  84. '
  85. );
  86. $this->assertEqual($ini->export(),
  87. array(
  88. 'group1' => array(
  89. 'value1' => 'this is a string with spaces indeed',
  90. 'value2' => '"this is string with spaces too',
  91. ),
  92. )
  93. );
  94. }
  95. function testParseProperQuotes()
  96. {
  97. $ini = $this->_createIni(
  98. '
  99. [group1]
  100. value1 = " this is a quoted string "
  101. value2 = " this is a quoted string "too" "
  102. value3 = " this is a quoted string \'too\' "
  103. '
  104. );
  105. $this->assertEqual($ini->export(),
  106. array(
  107. 'group1' => array(
  108. 'value1' => ' this is a quoted string ',
  109. 'value2' => ' this is a quoted string "too" ',
  110. 'value3' => ' this is a quoted string \'too\' ',
  111. ),
  112. )
  113. );
  114. }
  115. function testParseGlobalValues()
  116. {
  117. $ini = $this->_createIni(
  118. '
  119. value = global_test
  120. [group1]
  121. value = test
  122. '
  123. );
  124. $this->assertEqual($ini->export(),
  125. array(
  126. 'value' => 'global_test',
  127. 'group1' => array('value' => 'test'),
  128. )
  129. );
  130. }
  131. function testParseNullElements()
  132. {
  133. $ini = $this->_createIni(
  134. '
  135. [group1]
  136. value =
  137. '
  138. );
  139. $this->assertEqual($ini->export(),
  140. array('group1' => array('value' => null))
  141. );
  142. $this->assertFalse($ini->hasOption('group1', 'value'));
  143. }
  144. function testParseArrayElements()
  145. {
  146. $ini = $this->_createIni(
  147. '
  148. [group1]
  149. value[] =
  150. value[] = 1
  151. value[] =
  152. value[] = 2
  153. '
  154. );
  155. $this->assertEqual($ini->export(),
  156. array('group1' => array('value' => array(null, 1, null, 2)))
  157. );
  158. }
  159. function testParseHashedArrayElements()
  160. {
  161. $ini = $this->_createIni(
  162. '
  163. [group1]
  164. value[apple] =
  165. value[banana] = 1
  166. value[fruit] =
  167. value["lime"] = not valid index!
  168. value[\'lime\'] = not valid index too!
  169. '
  170. );
  171. $this->assertEqual($ini->export(),
  172. array('group1' => array('value' =>
  173. array('apple' => null, 'banana' => 1, 'fruit' => null)))
  174. );
  175. }
  176. function testParseMixedArrays()
  177. {
  178. $ini = $this->_createIni(
  179. '
  180. [group1]
  181. foo[apple] = 1
  182. bar[] = 1
  183. foo[banana] = 2
  184. bar[] = 2
  185. '
  186. );
  187. $this->assertEqual($ini->export(),
  188. array('group1' => array('foo' => array('apple' => 1, 'banana' => 2),
  189. 'bar' => array(1, 2))));
  190. }
  191. function testHasChecks()
  192. {
  193. $ini = $this->_createIni(
  194. '
  195. unassigned =
  196. junk = 1
  197. [test]
  198. test = 1
  199. [test2]
  200. test3 =
  201. [empty_group]
  202. test = '
  203. );
  204. $this->assertFalse($ini->hasGroup(''));
  205. $this->assertTrue($ini->hasGroup('test'));
  206. $this->assertTrue($ini->hasGroup('test2'));
  207. $this->assertTrue($ini->hasGroup('empty_group'));
  208. $this->assertFalse($ini->hasOption(null, null));
  209. $this->assertFalse($ini->hasOption('', ''));
  210. $this->assertFalse($ini->hasOption('', 'no_such_block'));
  211. $this->assertTrue($ini->hasOption('test', 'test'));
  212. $this->assertFalse($ini->hasOption('no_such_variable', 'test3'));
  213. $this->assertTrue($ini->hasOption('unassigned'));
  214. $this->assertTrue($ini->hasOption('junk'));
  215. }
  216. function testGetOption()
  217. {
  218. $ini = $this->_createIni(
  219. '
  220. unassigned =
  221. junk = 1
  222. [test]
  223. test = 1
  224. [test2]
  225. test[] = 1
  226. test[] = 2
  227. [test3]
  228. test[wow] = 1
  229. test[hey] = 2'
  230. );
  231. $this->assertEqual($ini->getOption('unassigned'), '');
  232. $this->assertEqual($ini->getOption('junk'), 1);
  233. $this->assertEqual($ini->getOption('no_such_option'), '');
  234. $this->assertEqual($ini->getOption('test', 'no_such_group'), '');
  235. $this->assertEqual($ini->getOption('test', 'test'), 1);
  236. $var = $ini->getOption('test', 'test2');
  237. $this->assertEqual($var, array(1, 2));
  238. $var = $ini->getOption('test', 'test3');
  239. $this->assertEqual($var, array('wow' => 1, 'hey' => 2));
  240. }
  241. function testReplaceConstants()
  242. {
  243. $ini = $this->_createIni(
  244. '
  245. [{INI_TEST_UNIQUE_CONSTANT}]
  246. test = {INI_TEST_UNIQUE_CONSTANT}1
  247. '
  248. );
  249. $this->assertEqual($ini->getOption('test', '*constant*'), '*constant*1');
  250. }
  251. function testGetGroup()
  252. {
  253. $ini = $this->_createIni(
  254. '
  255. unassigned =
  256. junk = 1
  257. [test]
  258. test = 1
  259. '
  260. );
  261. $this->assertEqual($ini->getGroup('test'), array('test' => 1));
  262. $this->assertNull($ini->getGroup('no_such_group'));
  263. }
  264. function testAssignOption()
  265. {
  266. $ini = $this->_createIni(
  267. '
  268. unassigned =
  269. junk = 1
  270. [test]
  271. test = 2
  272. '
  273. );
  274. $this->assertTrue($ini->assignOption($test, 'unassigned'));
  275. $this->assertEqual($test, '');
  276. $this->assertTrue($ini->assignOption($test, 'junk'));
  277. $this->assertEqual($test, 1);
  278. $this->assertTrue($ini->assignOption($test, 'test', 'test'));
  279. $this->assertEqual($test, 2);
  280. $this->assertFalse($ini->assignOption($test, 'no_such_option', 'test'));
  281. $this->assertEqual($test, 2);
  282. }
  283. function testMergeWith()
  284. {
  285. $a = $this->_createIni(
  286. 'test = 1
  287. foo = 1
  288. val[] = 1
  289. [group-b]
  290. a = 2
  291. foo = 1
  292. arr[1] = a
  293. '
  294. );
  295. $b = $this->_createIni(
  296. 'test = 2
  297. bar = 2
  298. val[] = 2
  299. [group-b]
  300. a = 1
  301. bar = 2
  302. arr[2] = b
  303. '
  304. );
  305. $c = $a->mergeWith($b);
  306. $this->assertEqual($c->export(), array('test' => 2,
  307. 'foo' => 1,
  308. 'bar' => 2,
  309. 'val' => array(2),
  310. 'group-b' => array('a' => 1,
  311. 'bar' => 2,
  312. 'arr' => array(2 => 'b')
  313. )
  314. )
  315. );
  316. }
  317. }