PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Configuration/tests/configuration_ini_writer_test.php

https://github.com/Yannix/zetacomponents
PHP | 513 lines | 430 code | 49 blank | 34 comment | 1 complexity | 9c750b7a4e263533ed1e8ec610883833 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  22. * @version //autogentag//
  23. * @filesource
  24. * @package Configuration
  25. * @subpackage Tests
  26. */
  27. /**
  28. * @package Configuration
  29. * @subpackage Tests
  30. */
  31. class ezcConfigurationIniWriterTest extends ezcTestCase
  32. {
  33. private $tempDir;
  34. protected function setUp()
  35. {
  36. if ( !ezcBaseFeatures::hasExtensionSupport( 'posix' ) )
  37. {
  38. $this->markTestSkipped( 'ext/posix is required for this test.' );
  39. }
  40. $this->tempDir = $this->createTempDir( 'ezcConfigurationIniWriterTest' );
  41. }
  42. protected function tearDown()
  43. {
  44. $this->removeTempDir();
  45. }
  46. public function testConfigSettingUseComments()
  47. {
  48. $backend = new ezcConfigurationIniWriter();
  49. $backend->setOptions( array ( 'useComments' => true ) );
  50. $backend->setOptions( array ( 'useComments' => false ) );
  51. }
  52. public function testConfigSettingUseCommentsWrongType()
  53. {
  54. $backend = new ezcConfigurationIniWriter();
  55. try
  56. {
  57. $backend->setOptions( array ( 'useComments' => 'tests/translations' ) );
  58. $this->fail( 'Expected exception was not thrown' );
  59. }
  60. catch ( ezcBaseSettingValueException $e )
  61. {
  62. self::assertEquals( "The value 'tests/translations' that you were trying to assign to setting 'useComments' is invalid. Allowed values are: bool", $e->getMessage() );
  63. }
  64. }
  65. public function testConfigSettingPermissions()
  66. {
  67. $backend = new ezcConfigurationIniWriter();
  68. $backend->setOptions( array ( 'permissions' => 0660 ) );
  69. }
  70. public function testConfigSettingPermissionsWrongType()
  71. {
  72. $backend = new ezcConfigurationIniWriter();
  73. try
  74. {
  75. $backend->setOptions( array ( 'permissions' => 'tests/translations' ) );
  76. $this->fail( 'Expected exception was not thrown' );
  77. }
  78. catch ( ezcBaseSettingValueException $e )
  79. {
  80. self::assertEquals( "The value 'tests/translations' that you were trying to assign to setting 'permissions' is invalid. Allowed values are: int, 0 - 0777", $e->getMessage() );
  81. }
  82. }
  83. public function testConfigSettingPermissionsOutOfRange1()
  84. {
  85. $backend = new ezcConfigurationIniWriter();
  86. try
  87. {
  88. $backend->setOptions( array ( 'permissions' => -1 ) );
  89. $this->fail( 'Expected exception was not thrown' );
  90. }
  91. catch ( ezcBaseSettingValueException $e )
  92. {
  93. self::assertEquals( "The value '-1' that you were trying to assign to setting 'permissions' is invalid. Allowed values are: int, 0 - 0777", $e->getMessage() );
  94. }
  95. }
  96. public function testConfigSettingPermissionsOutOfRange2()
  97. {
  98. $backend = new ezcConfigurationIniWriter();
  99. try
  100. {
  101. $backend->setOptions( array ( 'permissions' => 01000 ) );
  102. $this->fail( 'Expected exception was not thrown' );
  103. }
  104. catch ( ezcBaseSettingValueException $e )
  105. {
  106. self::assertEquals( "The value '512' that you were trying to assign to setting 'permissions' is invalid. Allowed values are: int, 0 - 0777", $e->getMessage() );
  107. }
  108. }
  109. public function testGetOptions()
  110. {
  111. $backend = new ezcConfigurationIniWriter();
  112. $backend->setOptions( array ( 'useComments' => true, 'permissions' => 0600 ) );
  113. $this->assertEquals( array ( 'useComments' => true, 'permissions' => 0600 ), $backend->getOptions() );
  114. }
  115. public function testConfigSettingBroken()
  116. {
  117. $backend = new ezcConfigurationIniWriter();
  118. try
  119. {
  120. $backend->setOptions( array ( 'lOcAtIOn' => 'tests/translations' ) );
  121. $this->fail( 'Expected exception was not thrown' );
  122. }
  123. catch ( ezcBaseSettingNotFoundException $e )
  124. {
  125. self::assertEquals( "The setting 'lOcAtIOn' is not a valid configuration setting.", $e->getMessage() );
  126. }
  127. }
  128. public function testInitCtor1()
  129. {
  130. $backend = new ezcConfigurationIniWriter( 'files/write_basic.ini', new ezcConfiguration() );
  131. $this->assertEquals( 'files', $backend->getLocation() );
  132. $this->assertEquals( 'write_basic', $backend->getName() );
  133. $this->assertSame( 0666, $this->readAttribute( $backend, 'permissions' ) );
  134. }
  135. public function testInitCtor2()
  136. {
  137. $backend = new ezcConfigurationIniWriter( 'files.foo/write_basic.ini', new ezcConfiguration(), 0660 );
  138. $this->assertEquals( 'files.foo', $backend->getLocation() );
  139. $this->assertEquals( 'write_basic', $backend->getName() );
  140. $this->assertSame( 0660, $this->readAttribute( $backend, 'permissions' ) );
  141. }
  142. public function testInitCtor3()
  143. {
  144. try
  145. {
  146. $backend = new ezcConfigurationIniWriter( 'files.foo/basic.f', new ezcConfiguration() );
  147. $this->fail( 'Expected exception not thrown' );
  148. }
  149. catch ( ezcConfigurationInvalidSuffixException $e )
  150. {
  151. $this->assertEquals( "The path 'files.foo/basic.f' has an invalid suffix (should be '.ini').", $e->getMessage() );
  152. }
  153. }
  154. public function testInitMethod1()
  155. {
  156. $backend = new ezcConfigurationIniWriter();
  157. $backend->init( 'files', 'write_basic', new ezcConfiguration() );
  158. $this->assertEquals( 'files', $backend->getLocation() );
  159. $this->assertEquals( 'write_basic', $backend->getName() );
  160. }
  161. public function testNoConfigObjectToSafe()
  162. {
  163. try
  164. {
  165. $backend = new ezcConfigurationIniWriter();
  166. $backend->save();
  167. $this->fail( 'Expected exception not thrown' );
  168. }
  169. catch ( ezcConfigurationNoConfigObjectException $e )
  170. {
  171. $this->assertEquals( 'There is no config object to save.', $e->getMessage() );
  172. }
  173. }
  174. public function testEmptyFile()
  175. {
  176. $test = new ezcConfiguration();
  177. $backend = new ezcConfigurationIniWriter( $this->tempDir . '/empty.ini', $test );
  178. $backend->save();
  179. $backend = new ezcConfigurationIniReader( $this->tempDir . '/empty.ini' );
  180. $return = $backend->load();
  181. $this->assertEquals( $test, $return );
  182. }
  183. public function testBrokenLocation()
  184. {
  185. $test = new ezcConfiguration();
  186. $backend = new ezcConfigurationIniWriter( 'does-definitely-not-exist/empty.ini', $test );
  187. try
  188. {
  189. @$backend->save();
  190. $this->fail( 'Expected exception not thrown' );
  191. }
  192. catch ( ezcConfigurationWriteFailedException $e )
  193. {
  194. $this->assertEquals( "The file could not be stored in 'does-definitely-not-exist/empty.ini'.", $e->getMessage() );
  195. }
  196. }
  197. public function testOneGroup()
  198. {
  199. $settings = array(
  200. 'TheOnlyGroup' => array(
  201. 'Setting1' => true,
  202. 'SettingNoComment' => 42,
  203. 'MultiRow' => false,
  204. )
  205. );
  206. $comments = array(
  207. 'TheOnlyGroup' => array(
  208. '#' => "Just one group",
  209. 'Setting1' => " This setting sucks",
  210. 'MultiRow' => " Multi\n row\n comment",
  211. )
  212. );
  213. $test = new ezcConfiguration( $settings, $comments );
  214. $backend = new ezcConfigurationIniWriter( $this->tempDir . '/one-group.ini', $test );
  215. $backend->save();
  216. $backend = new ezcConfigurationIniReader( $this->tempDir . '/one-group.ini' );
  217. $return = $backend->load();
  218. $this->assertEquals( $test, $return );
  219. }
  220. public function testOneGroupWithSetConfig()
  221. {
  222. $settings = array(
  223. 'TheOnlyGroup' => array(
  224. 'Setting1' => true,
  225. 'SettingNoComment' => 42,
  226. 'MultiRow' => false,
  227. )
  228. );
  229. $comments = array(
  230. 'TheOnlyGroup' => array(
  231. '#' => "Just one group",
  232. 'Setting1' => " This setting sucks",
  233. 'MultiRow' => " Multi\n row\n comment",
  234. )
  235. );
  236. $test = new ezcConfiguration( $settings, $comments );
  237. $backend = new ezcConfigurationIniWriter( $this->tempDir . '/one-group.ini', new ezcConfiguration() );
  238. $backend->setConfig( $test );
  239. $backend->save();
  240. $backend = new ezcConfigurationIniReader( $this->tempDir . '/one-group.ini' );
  241. $return = $backend->load();
  242. $this->assertEquals( $test, $return );
  243. }
  244. public function testOneGroupNoComments()
  245. {
  246. $settings = array(
  247. 'TheOnlyGroup' => array(
  248. 'Setting1' => true,
  249. 'SettingNoComment' => 42,
  250. 'MultiRow' => false,
  251. )
  252. );
  253. $comments = array(
  254. 'TheOnlyGroup' => array(
  255. '#' => "Just one group",
  256. 'Setting1' => " This setting sucks",
  257. 'MultiRow' => " Multi\n row\n comment",
  258. )
  259. );
  260. $test = new ezcConfiguration( $settings, $comments );
  261. $expected = new ezcConfiguration( $settings, array() );
  262. $backend = new ezcConfigurationIniWriter( $this->tempDir . '/one-group-no-comments.ini', $test );
  263. $backend->setOptions( array ( 'useComments' => false ) );
  264. $backend->save();
  265. $backend = new ezcConfigurationIniReader( $this->tempDir . '/one-group-no-comments.ini' );
  266. $return = $backend->load();
  267. $this->assertEquals( $expected, $return );
  268. }
  269. public function testTwoGroups()
  270. {
  271. $settings = array(
  272. 'NotTheOnlyGroup' => array(
  273. 'Setting1' => true,
  274. ),
  275. 'TheSecond' => array(
  276. 'Setting1' => false,
  277. ),
  278. );
  279. $comments = array(
  280. 'NotTheOnlyGroup' => array(
  281. '#' => " Not just one group",
  282. ),
  283. 'TheSecond' => array(
  284. '#' => " The second group",
  285. ),
  286. );
  287. $test = new ezcConfiguration( $settings, $comments );
  288. $backend = new ezcConfigurationIniWriter( $this->tempDir . '/two-groups.ini', $test );
  289. $backend->save();
  290. $backend = new ezcConfigurationIniReader( $this->tempDir . '/two-groups.ini' );
  291. $return = $backend->load();
  292. $this->assertEquals( $test, $return );
  293. }
  294. public function testFormats()
  295. {
  296. $settings = array(
  297. 'FormatTest' => array(
  298. 'Decimal1' => 42,
  299. 'Decimal2' => 0,
  300. 'MaxSize' => 400,
  301. 'MinSize' => 0,
  302. 'Hex1' => 0x11189196,
  303. 'Hex2' => 0x11189196,
  304. 'Hex3' => 0x11189196,
  305. 'Hex4' => 0x11189196,
  306. 'TextColor' => 66302,
  307. 'Octal1' => 01,
  308. 'Octal2' => 0458,
  309. 'Permission' => 0438,
  310. 'Float1' => 0.2,
  311. 'Float2' => .8123,
  312. 'Float3' => 42.,
  313. 'Float4' => 314e-2,
  314. 'Float5' => 3.141592654e1,
  315. 'Price' => 10.4,
  316. 'Seed' => 10e5,
  317. 'String1' => 'Blah blah blah',
  318. 'String2' => 'Derick "Tiger" Rethans',
  319. 'String3' => 'Foo \\ Bar',
  320. 'String4' => 'Foo \\',
  321. )
  322. );
  323. $comments = array(
  324. );
  325. $test = new ezcConfiguration( $settings, $comments );
  326. $backend = new ezcConfigurationIniWriter( $this->tempDir . '/formats.ini', $test );
  327. $backend->save();
  328. $backend = new ezcConfigurationIniReader( $this->tempDir . '/formats.ini' );
  329. $return = $backend->load();
  330. $this->assertEquals( $test, $return );
  331. }
  332. public function testStringFormats()
  333. {
  334. $settings = array(
  335. 'FormatTest' => array(
  336. 'Bool1' => 'false',
  337. 'Bool2' => 'true',
  338. 'Bool3' => false,
  339. 'Bool4' => true,
  340. 'Decimal1' => "42",
  341. 'Decimal2' => "0",
  342. 'MaxSize' => "400",
  343. 'MinSize' => "0",
  344. 'Hex1' => "0x11189196",
  345. 'Hex2' => "0x11189196",
  346. 'Hex3' => "0x11189196",
  347. 'Hex4' => "0x11189196",
  348. 'TextColor' => "66302",
  349. 'Octal1' => "01",
  350. 'Octal2' => "0458",
  351. 'Permission' => "0438",
  352. 'Float1' => "0.2",
  353. 'Float2' => ".8123",
  354. 'Float3' => "42.",
  355. 'Float4' => "314e-2",
  356. 'Float5' => "3.141592654e1",
  357. 'Price' => "10.4",
  358. 'Seed' => "10e5",
  359. 'String1' => 'Blah blah blah',
  360. 'String2' => 'Derick "Tiger" Rethans',
  361. 'String3' => 'Foo \\ Bar',
  362. 'String4' => 'Foo \\',
  363. )
  364. );
  365. $comments = array(
  366. );
  367. $test = new ezcConfiguration( $settings, $comments );
  368. $backend = new ezcConfigurationIniWriter( $this->tempDir . '/string-formats.ini', $test );
  369. $backend->save();
  370. $backend = new ezcConfigurationIniReader( $this->tempDir . '/string-formats.ini' );
  371. $return = $backend->load();
  372. $this->assertEquals( $test, $return );
  373. $this->assertEquals( file_get_contents( 'Configuration/tests/files/string-formats-test.ini' ), file_get_contents( $this->tempDir . '/string-formats.ini' ) );
  374. }
  375. public function test2D()
  376. {
  377. $settings = array(
  378. '2D-numbered' => array(
  379. 'Decimal' => array( 42, 0 ),
  380. 'Mixed' => array( 42, 0.812, false, "Derick \"Tiger\" Rethans" ),
  381. ),
  382. '2D-associative' => array(
  383. 'Decimal' => array( 'a' => 42, 'b' => 0 ),
  384. 'Mixed' => array( 'a' => 42, 1 => 0.812, 'b' => false, 2 => "Derick \"Tiger\" Rethans" ),
  385. ),
  386. );
  387. $comments = array(
  388. );
  389. $test = new ezcConfiguration( $settings, $comments );
  390. $backend = new ezcConfigurationIniWriter( $this->tempDir . '/multi-dim.ini', $test );
  391. $backend->save();
  392. $backend = new ezcConfigurationIniReader( $this->tempDir . '/multi-dim.ini' );
  393. $return = $backend->load();
  394. $this->assertEquals( $test, $return );
  395. }
  396. public function test3D()
  397. {
  398. $settings = array(
  399. '3D' => array(
  400. 'Decimal' => array( 42, 0 ),
  401. 'Array' => array(
  402. 'Decimal' => array( 'a' => 42, 'b' => 0 ),
  403. 'Mixed' => array( 'b' => false, 2 => "Derick \"Tiger\" Rethans" ),
  404. ),
  405. ),
  406. );
  407. $comments = array(
  408. '3D' => array(
  409. 'Decimal' => array( " One with a comment", " Second one with a comment" ),
  410. 'Array' => array(
  411. 'Mixed' => array( 2 => " One with a comment" ),
  412. ),
  413. ),
  414. );
  415. $test = new ezcConfiguration( $settings, $comments );
  416. $backend = new ezcConfigurationIniWriter( $this->tempDir . '/multi-dim2.ini', $test );
  417. $backend->save();
  418. $backend = new ezcConfigurationIniReader( $this->tempDir . '/multi-dim2.ini' );
  419. $return = $backend->load();
  420. $this->assertEquals( $test, $return );
  421. }
  422. public function testFilePermissionsDefault()
  423. {
  424. $backend = new ezcConfigurationIniWriter( $this->tempDir . '/empty.ini', new ezcConfiguration() );
  425. $oldUmask = umask( 0 );
  426. $backend->save();
  427. umask( $oldUmask );
  428. $stat = stat( $this->tempDir . '/empty.ini' );
  429. $this->assertEquals( POSIX_S_IFREG | 0666, $stat['mode'] );
  430. }
  431. public function testFilePermissions1()
  432. {
  433. $backend = new ezcConfigurationIniWriter( $this->tempDir . '/empty.ini', new ezcConfiguration() );
  434. $oldUmask = umask( 0 );
  435. $backend->setOptions( array ( 'permissions' => 0660 ) );
  436. $backend->save();
  437. umask( $oldUmask );
  438. $stat = stat( $this->tempDir . '/empty.ini' );
  439. $this->assertEquals( POSIX_S_IFREG | 0660, $stat['mode'] );
  440. }
  441. public function testFilePermissions2()
  442. {
  443. $backend = new ezcConfigurationIniWriter( $this->tempDir . '/empty.ini', new ezcConfiguration() );
  444. $oldUmask = umask( 0 );
  445. $backend->setOptions( array ( 'permissions' => 0640 ) );
  446. $backend->save();
  447. umask( $oldUmask );
  448. $stat = stat( $this->tempDir . '/empty.ini' );
  449. $this->assertEquals( POSIX_S_IFREG | 0640, $stat['mode'] );
  450. }
  451. /*
  452. public function testWriteFailure()
  453. {
  454. }
  455. */
  456. public static function suite()
  457. {
  458. return new PHPUnit_Framework_TestSuite( 'ezcConfigurationIniWriterTest' );
  459. }
  460. }
  461. ?>