PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Configuration/tests/configuration_array_writer_test.php

https://github.com/Yannix/zetacomponents
PHP | 484 lines | 423 code | 32 blank | 29 comment | 1 complexity | 87b8421b88d08175479f02bf3f0e41f7 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 ezcConfigurationArrayWriterTest 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( 'ezcConfigurationArrayWriterTest' );
  41. }
  42. protected function tearDown()
  43. {
  44. $this->removeTempDir();
  45. }
  46. public function testConfigSettingUseComments()
  47. {
  48. $backend = new ezcConfigurationArrayWriter();
  49. $backend->setOptions( array ( 'useComments' => true ) );
  50. $backend->setOptions( array ( 'useComments' => false ) );
  51. }
  52. public function testConfigSettingUseCommentsWrongType()
  53. {
  54. $backend = new ezcConfigurationArrayWriter();
  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 ezcConfigurationArrayWriter();
  68. $backend->setOptions( array ( 'permissions' => 0660 ) );
  69. }
  70. public function testConfigSettingPermissionsWrongType()
  71. {
  72. $backend = new ezcConfigurationArrayWriter();
  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 ezcConfigurationArrayWriter();
  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 ezcConfigurationArrayWriter();
  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 ezcConfigurationArrayWriter();
  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 ezcConfigurationArrayWriter();
  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 ezcConfigurationArrayWriter( 'files/write_basic.php', 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 ezcConfigurationArrayWriter( 'files.foo/write_basic.php', 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 ezcConfigurationArrayWriter( '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 '.php').", $e->getMessage() );
  152. }
  153. }
  154. public function testInitMethod1()
  155. {
  156. $backend = new ezcConfigurationArrayWriter();
  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 ezcConfigurationArrayWriter();
  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 ezcConfigurationArrayWriter( $this->tempDir . '/empty.php', $test );
  178. $backend->save();
  179. $backend = new ezcConfigurationArrayReader( $this->tempDir . '/empty.php' );
  180. $return = $backend->load();
  181. $this->assertEquals( $test, $return );
  182. }
  183. public function testOneGroup()
  184. {
  185. $settings = array(
  186. 'TheOnlyGroup' => array(
  187. 'Setting1' => true,
  188. 'SettingNoComment' => 42,
  189. 'MultiRow' => false,
  190. )
  191. );
  192. $comments = array(
  193. 'TheOnlyGroup' => array(
  194. '#' => "Just one group",
  195. 'Setting1' => " This setting sucks",
  196. 'MultiRow' => " Multi\n row\n comment",
  197. )
  198. );
  199. $test = new ezcConfiguration( $settings, $comments );
  200. $backend = new ezcConfigurationArrayWriter( $this->tempDir . '/one-group.php', $test );
  201. $backend->save();
  202. $backend = new ezcConfigurationArrayReader( $this->tempDir . '/one-group.php' );
  203. $return = $backend->load();
  204. $this->assertEquals( $test, $return );
  205. }
  206. public function testOneGroupWithSetConfig()
  207. {
  208. $settings = array(
  209. 'TheOnlyGroup' => array(
  210. 'Setting1' => true,
  211. 'SettingNoComment' => 42,
  212. 'MultiRow' => false,
  213. )
  214. );
  215. $comments = array(
  216. 'TheOnlyGroup' => array(
  217. '#' => "Just one group",
  218. 'Setting1' => " This setting sucks",
  219. 'MultiRow' => " Multi\n row\n comment",
  220. )
  221. );
  222. $test = new ezcConfiguration( $settings, $comments );
  223. $backend = new ezcConfigurationArrayWriter( $this->tempDir . '/one-group.php', new ezcConfiguration() );
  224. $backend->setConfig( $test );
  225. $backend->save();
  226. $backend = new ezcConfigurationArrayReader( $this->tempDir . '/one-group.php' );
  227. $return = $backend->load();
  228. $this->assertEquals( $test, $return );
  229. }
  230. public function testOneGroupNoComments()
  231. {
  232. $settings = array(
  233. 'TheOnlyGroup' => array(
  234. 'Setting1' => true,
  235. 'SettingNoComment' => 42,
  236. 'MultiRow' => false,
  237. )
  238. );
  239. $comments = array(
  240. 'TheOnlyGroup' => array(
  241. '#' => "Just one group",
  242. 'Setting1' => " This setting sucks",
  243. 'MultiRow' => " Multi\n row\n comment",
  244. )
  245. );
  246. $test = new ezcConfiguration( $settings, $comments );
  247. $expected = new ezcConfiguration( $settings, array() );
  248. $backend = new ezcConfigurationArrayWriter( $this->tempDir . '/one-group-no-comments.php', $test );
  249. $backend->setOptions( array ( 'useComments' => false ) );
  250. $backend->save();
  251. $backend = new ezcConfigurationArrayReader( $this->tempDir . '/one-group-no-comments.php' );
  252. $return = $backend->load();
  253. $this->assertEquals( $expected, $return );
  254. }
  255. public function testTwoGroups()
  256. {
  257. $settings = array(
  258. 'NotTheOnlyGroup' => array(
  259. 'Setting1' => true,
  260. ),
  261. 'TheSecond' => array(
  262. 'Setting1' => false,
  263. ),
  264. );
  265. $comments = array(
  266. 'NotTheOnlyGroup' => array(
  267. '#' => " Not just one group",
  268. ),
  269. 'TheSecond' => array(
  270. '#' => " The second group",
  271. ),
  272. );
  273. $test = new ezcConfiguration( $settings, $comments );
  274. $backend = new ezcConfigurationArrayWriter( $this->tempDir . '/two-groups.php', $test );
  275. $backend->save();
  276. $backend = new ezcConfigurationArrayReader( $this->tempDir . '/two-groups.php' );
  277. $return = $backend->load();
  278. $this->assertEquals( $test, $return );
  279. }
  280. public function testFormats()
  281. {
  282. $settings = array(
  283. 'FormatTest' => array(
  284. 'Decimal1' => 42,
  285. 'Decimal2' => 0,
  286. 'MaxSize' => 400,
  287. 'MinSize' => 0,
  288. 'Hex1' => 11189196,
  289. 'Hex2' => 11189196,
  290. 'Hex3' => 11189196,
  291. 'Hex4' => 11189196,
  292. 'TextColor' => 66302,
  293. 'Octal1' => 1,
  294. 'Octal2' => 458,
  295. 'Permission' => 438,
  296. 'Float1' => 0.2,
  297. 'Float2' => .8123,
  298. 'Float3' => 314e-2,
  299. 'Float4' => 3.141592654e1,
  300. 'Price' => 10.4,
  301. 'Seed' => 10e5,
  302. 'String1' => 'Blah blah blah',
  303. 'String2' => 'Derick "Tiger" Rethans',
  304. 'String3' => 'Foo \\ Bar',
  305. 'String4' => 'Foo \\',
  306. )
  307. );
  308. $comments = array(
  309. );
  310. $test = new ezcConfiguration( $settings, $comments );
  311. $backend = new ezcConfigurationArrayWriter( $this->tempDir . '/formats.php', $test );
  312. $backend->save();
  313. $backend = new ezcConfigurationArrayReader( $this->tempDir . '/formats.php' );
  314. $return = $backend->load();
  315. $this->assertEquals( $test, $return );
  316. }
  317. public function test2D()
  318. {
  319. $settings = array(
  320. '2D-numbered' => array(
  321. 'Decimal' => array( 42, 0 ),
  322. 'Mixed' => array( 42, 0.812, false, "Derick \"Tiger\" Rethans" ),
  323. ),
  324. '2D-associative' => array(
  325. 'Decimal' => array( 'a' => 42, 'b' => 0 ),
  326. 'Mixed' => array( 'a' => 42, 1 => 0.812, 'b' => false, 2 => "Derick \"Tiger\" Rethans" ),
  327. ),
  328. );
  329. $comments = array(
  330. );
  331. $test = new ezcConfiguration( $settings, $comments );
  332. $backend = new ezcConfigurationArrayWriter( $this->tempDir . '/multi-dim.php', $test );
  333. $backend->save();
  334. $backend = new ezcConfigurationArrayReader( $this->tempDir . '/multi-dim.php' );
  335. $return = $backend->load();
  336. $this->assertEquals( $test, $return );
  337. }
  338. public function test3D()
  339. {
  340. $settings = array(
  341. '3D' => array(
  342. 'Decimal' => array( 42, 0 ),
  343. 'Array' => array(
  344. 'Decimal' => array( 'a' => 42, 'b' => 0 ),
  345. 'Mixed' => array( 'b' => false, 2 => "Derick \"Tiger\" Rethans" ),
  346. ),
  347. ),
  348. );
  349. $comments = array(
  350. '3D' => array(
  351. 'Decimal' => array( " One with a comment", " Second one with a comment" ),
  352. 'Array' => array(
  353. 'Mixed' => array( 2 => " One with a comment" ),
  354. ),
  355. ),
  356. );
  357. $test = new ezcConfiguration( $settings, $comments );
  358. $backend = new ezcConfigurationArrayWriter( $this->tempDir . '/multi-dim2.php', $test );
  359. $backend->save();
  360. $backend = new ezcConfigurationArrayReader( $this->tempDir . '/multi-dim2.php' );
  361. $return = $backend->load();
  362. $this->assertEquals( $test, $return );
  363. }
  364. public function testFilePermissionsDefault()
  365. {
  366. $backend = new ezcConfigurationArrayWriter( $this->tempDir . '/empty.php', new ezcConfiguration() );
  367. $oldUmask = umask( 0 );
  368. $backend->save();
  369. umask( $oldUmask );
  370. $stat = stat( $this->tempDir . '/empty.php' );
  371. $this->assertEquals( POSIX_S_IFREG | 0666, $stat['mode'] );
  372. }
  373. public function testFilePermissions1()
  374. {
  375. $backend = new ezcConfigurationArrayWriter( $this->tempDir . '/empty.php', new ezcConfiguration() );
  376. $oldUmask = umask( 0 );
  377. $backend->setOptions( array ( 'permissions' => 0660 ) );
  378. $backend->save();
  379. umask( $oldUmask );
  380. $stat = stat( $this->tempDir . '/empty.php' );
  381. $this->assertEquals( POSIX_S_IFREG | 0660, $stat['mode'] );
  382. }
  383. public function testFilePermissions2()
  384. {
  385. $backend = new ezcConfigurationArrayWriter( $this->tempDir . '/empty.php', new ezcConfiguration() );
  386. $oldUmask = umask( 0 );
  387. $backend->setOptions( array ( 'permissions' => 0640 ) );
  388. $backend->save();
  389. umask( $oldUmask );
  390. $stat = stat( $this->tempDir . '/empty.php' );
  391. $this->assertEquals( POSIX_S_IFREG | 0640, $stat['mode'] );
  392. }
  393. public function testValidationNonStrict()
  394. {
  395. $settings = array(
  396. '3D' => array(
  397. 'Decimal' => array( 42, 0 ),
  398. 'Array' => array(
  399. 'Decimal' => array( 'a' => 42, 'b' => 0 ),
  400. 'Mixed' => array( 'b' => false, 2 => "Derick \"Tiger\" Rethans" ),
  401. ),
  402. ),
  403. );
  404. $comments = array(
  405. '3D' => array(
  406. 'Decimal' => array( " One with a comment", " Second one with a comment" ),
  407. 'Array' => array(
  408. 'Mixed' => array( 2 => " One with a comment" ),
  409. ),
  410. ),
  411. );
  412. $test = new ezcConfiguration( $settings, $comments );
  413. $path = $this->tempDir . '/multi-dim2.php';
  414. $backend = new ezcConfigurationArrayWriter( $path, $test );
  415. $backend->save();
  416. $backend = new ezcConfigurationArrayReader( $path );
  417. $return = $backend->validate( false );
  418. $expected = new ezcConfigurationValidationResult( $backend->getLocation(), $backend->getName(), $path );
  419. $expected->isValid = true;
  420. $this->assertEquals( $expected, $return );
  421. }
  422. /*
  423. public function testWriteFailure()
  424. {
  425. }
  426. */
  427. public static function suite()
  428. {
  429. return new PHPUnit_Framework_TestSuite( 'ezcConfigurationArrayWriterTest' );
  430. }
  431. }
  432. ?>