base_test.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. * @package Base
  22. * @subpackage Tests
  23. * @version //autogentag//
  24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25. */
  26. /**
  27. * @package Base
  28. * @subpackage Tests
  29. */
  30. class ezcBaseTest extends ezcTestCase
  31. {
  32. /*
  33. * For use with the method testInvalidClass().
  34. */
  35. private $errorMessage = null;
  36. public function testConfigExceptionUnknownSetting()
  37. {
  38. try
  39. {
  40. throw new ezcBaseSettingNotFoundException( 'broken' );
  41. }
  42. catch ( ezcBaseSettingNotFoundException $e )
  43. {
  44. $this->assertEquals( "The setting 'broken' is not a valid configuration setting.", $e->getMessage() );
  45. }
  46. }
  47. public function testConfigExceptionOutOfRange1()
  48. {
  49. try
  50. {
  51. throw new ezcBaseSettingValueException( 'broken', 42 );
  52. }
  53. catch ( ezcBaseSettingValueException $e )
  54. {
  55. $this->assertEquals( "The value '42' that you were trying to assign to setting 'broken' is invalid.", $e->getMessage() );
  56. }
  57. }
  58. public function testConfigExceptionOutOfRange2()
  59. {
  60. try
  61. {
  62. throw new ezcBaseSettingValueException( 'broken', 42, "int, 40 - 48" );
  63. }
  64. catch ( ezcBaseSettingValueException $e )
  65. {
  66. $this->assertEquals( "The value '42' that you were trying to assign to setting 'broken' is invalid. Allowed values are: int, 40 - 48", $e->getMessage() );
  67. }
  68. }
  69. public function testConfigExceptionOutOfRange3()
  70. {
  71. try
  72. {
  73. throw new ezcBaseSettingValueException( 'broken', array(1, 1, 3, 4, 5), 'int' );
  74. }
  75. catch ( ezcBaseSettingValueException $e )
  76. {
  77. $this->assertEquals( "The value 'a:5:{i:0;i:1;i:1;i:1;i:2;i:3;i:3;i:4;i:4;i:5;}' that you were trying to assign to setting 'broken' is invalid. Allowed values are: int", $e->getMessage() );
  78. }
  79. }
  80. public function testFileIoException1()
  81. {
  82. try
  83. {
  84. throw new ezcBaseFileIoException( 'testfile.php', ezcBaseFileException::READ );
  85. }
  86. catch ( ezcBaseFileIoException $e )
  87. {
  88. $this->assertEquals( "An error occurred while reading from 'testfile.php'.", $e->getMessage() );
  89. }
  90. }
  91. public function testFileIoException2()
  92. {
  93. try
  94. {
  95. throw new ezcBaseFileIoException( 'testfile.php', ezcBaseFileException::WRITE );
  96. }
  97. catch ( ezcBaseFileIoException $e )
  98. {
  99. $this->assertEquals( "An error occurred while writing to 'testfile.php'.", $e->getMessage() );
  100. }
  101. }
  102. public function testFileIoException3()
  103. {
  104. try
  105. {
  106. throw new ezcBaseFileIoException( 'testfile.php', ezcBaseFileException::WRITE, "Extra extra" );
  107. }
  108. catch ( ezcBaseFileIoException $e )
  109. {
  110. $this->assertEquals( "An error occurred while writing to 'testfile.php'. (Extra extra)", $e->getMessage() );
  111. }
  112. }
  113. public function testFileNotFoundException1()
  114. {
  115. try
  116. {
  117. throw new ezcBaseFileNotFoundException( 'testfile.php' );
  118. }
  119. catch ( ezcBaseFileNotFoundException $e )
  120. {
  121. $this->assertEquals( "The file 'testfile.php' could not be found.", $e->getMessage() );
  122. }
  123. }
  124. public function testFileNotFoundException2()
  125. {
  126. try
  127. {
  128. throw new ezcBaseFileNotFoundException( 'testfile.php', 'INI' );
  129. }
  130. catch ( ezcBaseFileNotFoundException $e )
  131. {
  132. $this->assertEquals( "The INI file 'testfile.php' could not be found.", $e->getMessage() );
  133. }
  134. }
  135. public function testFileNotFoundException3()
  136. {
  137. try
  138. {
  139. throw new ezcBaseFileNotFoundException( 'testfile.php', 'INI', "Extra extra" );
  140. }
  141. catch ( ezcBaseFileNotFoundException $e )
  142. {
  143. $this->assertEquals( "The INI file 'testfile.php' could not be found. (Extra extra)", $e->getMessage() );
  144. }
  145. }
  146. public function testFilePermissionException1()
  147. {
  148. try
  149. {
  150. throw new ezcBaseFilePermissionException( 'testfile.php', ezcBaseFileException::READ );
  151. }
  152. catch ( ezcBaseFilePermissionException $e )
  153. {
  154. $this->assertEquals( "The file 'testfile.php' can not be opened for reading.", $e->getMessage() );
  155. }
  156. }
  157. public function testFilePermissionException2()
  158. {
  159. try
  160. {
  161. throw new ezcBaseFilePermissionException( 'testfile.php', ezcBaseFileException::WRITE );
  162. }
  163. catch ( ezcBaseFileException $e )
  164. {
  165. $this->assertEquals( "The file 'testfile.php' can not be opened for writing.", $e->getMessage() );
  166. }
  167. }
  168. public function testFilePermissionException3()
  169. {
  170. try
  171. {
  172. throw new ezcBaseFilePermissionException( 'testfile.php', ezcBaseFileException::EXECUTE );
  173. }
  174. catch ( ezcBaseException $e )
  175. {
  176. $this->assertEquals( "The file 'testfile.php' can not be executed.", $e->getMessage() );
  177. }
  178. }
  179. public function testFilePermissionException4()
  180. {
  181. try
  182. {
  183. throw new ezcBaseFilePermissionException( 'testfile.php', ezcBaseFilePermissionException::CHANGE, "Extra extra" );
  184. }
  185. catch ( ezcBaseException $e )
  186. {
  187. $this->assertEquals( "The permissions for 'testfile.php' can not be changed. (Extra extra)", $e->getMessage() );
  188. }
  189. }
  190. public function testFilePermissionException5()
  191. {
  192. try
  193. {
  194. throw new ezcBaseFilePermissionException( 'testfile.php', ezcBaseFilePermissionException::READ | ezcBaseFilePermissionException::WRITE, "Extra extra" );
  195. }
  196. catch ( ezcBaseException $e )
  197. {
  198. $this->assertEquals( "The file 'testfile.php' can not be opened for reading and writing. (Extra extra)", $e->getMessage() );
  199. }
  200. }
  201. public function testFilePermissionException6()
  202. {
  203. try
  204. {
  205. throw new ezcBaseFilePermissionException( 'testfile.php', ezcBaseFilePermissionException::REMOVE, "Extra extra" );
  206. }
  207. catch ( ezcBaseException $e )
  208. {
  209. $this->assertEquals( "The file 'testfile.php' can not be removed. (Extra extra)", $e->getMessage() );
  210. }
  211. }
  212. public function testPropertyNotFoundException()
  213. {
  214. try
  215. {
  216. throw new ezcBasePropertyNotFoundException( 'broken' );
  217. }
  218. catch ( ezcBasePropertyNotFoundException $e )
  219. {
  220. $this->assertEquals( "No such property name 'broken'.", $e->getMessage() );
  221. }
  222. }
  223. public function testPropertyPermissionException1()
  224. {
  225. try
  226. {
  227. throw new ezcBasePropertyPermissionException( 'broken', ezcBasePropertyPermissionException::READ );
  228. }
  229. catch ( ezcBaseException $e )
  230. {
  231. $this->assertEquals( "The property 'broken' is read-only.", $e->getMessage() );
  232. }
  233. }
  234. public function testPropertyPermissionException2()
  235. {
  236. try
  237. {
  238. throw new ezcBasePropertyPermissionException( 'broken', ezcBasePropertyPermissionException::WRITE );
  239. }
  240. catch ( ezcBaseException $e )
  241. {
  242. $this->assertEquals( "The property 'broken' is write-only.", $e->getMessage() );
  243. }
  244. }
  245. public function testBaseValue1()
  246. {
  247. try
  248. {
  249. throw new ezcBaseValueException( 'broken', array( 42 ) );
  250. }
  251. catch ( ezcBaseValueException $e )
  252. {
  253. $this->assertEquals( "The value 'a:1:{i:0;i:42;}' that you were trying to assign to setting 'broken' is invalid.", $e->getMessage() );
  254. }
  255. }
  256. public function testBaseValue2()
  257. {
  258. try
  259. {
  260. throw new ezcBaseValueException( 'broken', "string", "strings" );
  261. }
  262. catch ( ezcBaseValueException $e )
  263. {
  264. $this->assertEquals( "The value 'string' that you were trying to assign to setting 'broken' is invalid. Allowed values are: strings.", $e->getMessage() );
  265. $this->assertEquals( "The value 'string' that you were trying to assign to setting 'broken' is invalid. Allowed values are: strings.", $e->originalMessage );
  266. }
  267. }
  268. public function testExtraDirNotFoundException()
  269. {
  270. try
  271. {
  272. ezcBase::addClassRepository( 'wrongDir' );
  273. }
  274. catch ( ezcBaseFileNotFoundException $e )
  275. {
  276. $this->assertEquals( "The base directory file 'wrongDir' could not be found.", $e->getMessage() );
  277. }
  278. }
  279. public function testExtraDirBaseNotFoundException()
  280. {
  281. try
  282. {
  283. ezcBase::addClassRepository( '.', './wrongAutoloadDir' );
  284. }
  285. catch ( ezcBaseFileNotFoundException $e )
  286. {
  287. $this->assertEquals( "The autoload directory file './wrongAutoloadDir' could not be found.", $e->getMessage() );
  288. }
  289. }
  290. public function testBaseAddAndGetAutoloadDirs1()
  291. {
  292. ezcBase::addClassRepository( __DIR__ );
  293. $resultArray = ezcBase::getRepositoryDirectories();
  294. if ( count( $resultArray ) != 2 )
  295. {
  296. $this->fail( "Duplicating or missing extra autoload dirs while adding." );
  297. }
  298. if ( !isset( $resultArray['ezc'] ) )
  299. {
  300. $this->fail( "No packageDir found in result of getRepositoryDirectories()" );
  301. }
  302. if ( !isset( $resultArray[0] ) || $resultArray[0]->basePath != __DIR__ )
  303. {
  304. $this->fail( "Extra base dir '{$resultArray[0]->basePath}' is added incorrectly" );
  305. }
  306. if ( !isset( $resultArray[0] ) || $resultArray[0]->autoloadPath != __DIR__ . '/autoload' )
  307. {
  308. $this->fail( "Extra autoload dir '{$resultArray[0]->autoloadPath}' is added incorrectly" );
  309. }
  310. }
  311. // this test is sorta obsolete, but we keep it around for good measure
  312. public function testBaseAddAndGetAutoloadDirs2()
  313. {
  314. ezcBase::addClassRepository( __DIR__, __DIR__ . '/autoload' );
  315. ezcBase::addClassRepository( __DIR__ . '/test_repository', __DIR__ . '/test_repository/autoload_files' );
  316. ezcBase::addClassRepository( __DIR__ . '/test_repository', __DIR__ . '/test_repository/autoload_files' );
  317. $resultArray = ezcBase::getRepositoryDirectories();
  318. if ( count( $resultArray ) != 5 )
  319. {
  320. $this->fail( "Duplicating or missing extra autoload dirs while adding." );
  321. }
  322. if ( !isset( $resultArray['ezc'] ) )
  323. {
  324. $this->fail( "No packageDir found in result of getRepositoryDirectories()" );
  325. }
  326. if ( !isset( $resultArray[2] ) || $resultArray[2]->autoloadPath != __DIR__ . '/test_repository/autoload_files' )
  327. {
  328. $this->fail( "Extra autoload dir '{$resultArray[2]->autoloadPath}' is added incorrectly" );
  329. }
  330. self::assertEquals( true, class_exists( 'trBasetestClass', true ) );
  331. self::assertEquals( true, class_exists( 'trBasetestClass2', true ) );
  332. try
  333. {
  334. self::assertEquals( false, class_exists( 'trBasetestClass3', true ) );
  335. self::fail( 'The expected exception was not thrown.' );
  336. }
  337. catch ( ezcBaseAutoloadException $e )
  338. {
  339. self::assertStringStartsWith( "Could not find a class to file mapping for 'trBasetestClass3'. Searched for basetest_class3_autoload.php, basetest_autoload.php, autoload.php in:", $e->getMessage());
  340. }
  341. self::assertEquals( true, class_exists( 'trBasetestLongClass', true ) );
  342. try
  343. {
  344. class_exists( 'trBasetestClass4', true );
  345. self::fail( 'The expected exception was not thrown.' );
  346. }
  347. catch ( ezcBaseFileNotFoundException $e )
  348. {
  349. self::assertEquals( "The file '" . __DIR__ . "/test_repository/TestClasses/base_test_class_number_four.php' could not be found.", $e->getMessage() );
  350. }
  351. }
  352. public function testBaseAddAndGetAutoloadDirs3()
  353. {
  354. ezcBase::addClassRepository( __DIR__ . '/extra_repository', null, 'ext' );
  355. $resultArray = ezcBase::getRepositoryDirectories();
  356. self::assertEquals( true, array_key_exists( 'ezc', $resultArray ) );
  357. self::assertEquals( true, array_key_exists( 'ext', $resultArray ) );
  358. //self::assertEquals( true, class_exists( 'extTranslationTest', true ) );
  359. //self::assertEquals( true, class_exists( 'ezcTranslationTsBackend', true ) );
  360. }
  361. public function testBaseAddAndGetAutoloadDirs4()
  362. {
  363. ezcBase::addClassRepository( __DIR__ . '/test_repository', __DIR__ . '/test_repository/autoload_files', 'tr' );
  364. try
  365. {
  366. ezcBase::addClassRepository( __DIR__ . '/test_repository', __DIR__ . '/test_repository/autoload_files', 'tr' );
  367. }
  368. catch ( ezcBaseDoubleClassRepositoryPrefixException $e )
  369. {
  370. self::assertEquals( "The class repository in '" . __DIR__ . "/test_repository' (with autoload dir '" . __DIR__ . "/test_repository/autoload_files') can not be added because another class repository already uses the prefix 'tr'.", $e->getMessage() );
  371. }
  372. $resultArray = ezcBase::getRepositoryDirectories();
  373. self::assertEquals( 7, count( $resultArray ) );
  374. self::assertEquals( true, array_key_exists( 'ezc', $resultArray ) );
  375. self::assertEquals( true, array_key_exists( 'tr', $resultArray ) );
  376. self::assertEquals( __DIR__ . '/test_repository', $resultArray['tr']->basePath );
  377. self::assertEquals( __DIR__ . '/test_repository/autoload_files', $resultArray['tr']->autoloadPath );
  378. }
  379. public function testNoPrefixAutoload()
  380. {
  381. ezcBase::addClassRepository( __DIR__ . '/test_repository', __DIR__ . '/test_repository/autoload_files' );
  382. ezc_autoload( 'Objet' );
  383. if ( !class_exists( 'Objet' ) )
  384. {
  385. $this->fail( "Autoload does not handle classes with no prefix" );
  386. }
  387. }
  388. public function testNoPrefixAutoload2()
  389. {
  390. ezcBase::addClassRepository( __DIR__ . '/issue15896' );
  391. ezc_autoload( 'ab' );
  392. }
  393. public function testCheckDependencyExtension()
  394. {
  395. ezcBase::checkDependency( 'Tester', ezcBase::DEP_PHP_EXTENSION, 'standard' );
  396. }
  397. public function testCheckDependencyVersion()
  398. {
  399. ezcBase::checkDependency( 'Tester', ezcBase::DEP_PHP_VERSION, '5.1.1' );
  400. }
  401. public function testInvalidClass()
  402. {
  403. $this->setExpectedException( 'ezcBaseAutoloadException', "Could not find a class to file mapping for 'ezcNoSuchClass'. Searched for no_such_autoload.php, no_autoload.php, autoload.php in:" );
  404. self::assertEquals( false, class_exists( 'ezcNoSuchClass', true ) );
  405. }
  406. public function testDebug()
  407. {
  408. try
  409. {
  410. class_exists( 'ezcTestingOne' );
  411. self::fail( "There should have been an exception" );
  412. }
  413. catch ( ezcBaseAutoloadException $e )
  414. {
  415. }
  416. }
  417. public function testNoDebug()
  418. {
  419. try
  420. {
  421. $options = new ezcBaseAutoloadOptions;
  422. $options->debug = false;
  423. ezcBase::setOptions( $options );
  424. class_exists( 'ezcTestingOne' );
  425. }
  426. catch ( Exception $e )
  427. {
  428. self::fail( "There should not have been an exception. Found one: " . $e->getMessage() );
  429. }
  430. }
  431. public function testGetInstallationPath()
  432. {
  433. $this->markTestSkipped( 'What should behavior be in a composer installed environment?' );
  434. $path = ezcBase::getInstallationPath();
  435. $pathParts = explode( DIRECTORY_SEPARATOR, $path );
  436. self::assertEquals( array( 'trunk', '' ), array_splice( $pathParts, -2 ) );
  437. self::assertEquals( DIRECTORY_SEPARATOR, substr( $path, -1 ) );
  438. }
  439. public function testSetInvalidRunMode()
  440. {
  441. try
  442. {
  443. ezcBase::setRunMode( -3 );
  444. self::fail( "Expected exception not thrown." );
  445. }
  446. catch ( ezcBaseValueException $e )
  447. {
  448. self::assertEquals( "The value '-3' that you were trying to assign to setting 'runMode' is invalid. Allowed values are: ezcBase::MODE_PRODUCTION or ezcBase::MODE_DEVELOPMENT.", $e->getMessage() );
  449. }
  450. }
  451. public function testSetGetRunMode()
  452. {
  453. self::assertEquals( ezcBase::MODE_DEVELOPMENT, ezcBase::getRunMode() );
  454. self::assertEquals( true, ezcBase::inDevMode() );
  455. ezcBase::setRunMode( ezcBase::MODE_PRODUCTION );
  456. self::assertEquals( ezcBase::MODE_PRODUCTION, ezcBase::getRunMode() );
  457. self::assertEquals( false, ezcBase::inDevMode() );
  458. ezcBase::setRunMode( ezcBase::MODE_DEVELOPMENT );
  459. self::assertEquals( ezcBase::MODE_DEVELOPMENT, ezcBase::getRunMode() );
  460. self::assertEquals( true, ezcBase::inDevMode() );
  461. }
  462. public function testGetInstallMethod()
  463. {
  464. self::assertEquals( 'custom', ezcBase::getInstallMethod() );
  465. }
  466. public function setup()
  467. {
  468. $options = new ezcBaseAutoloadOptions;
  469. $options->debug = true;
  470. ezcBase::setOptions( $options );
  471. }
  472. public function teardown()
  473. {
  474. $options = new ezcBaseAutoloadOptions;
  475. $options->debug = true;
  476. ezcBase::setOptions( $options );
  477. }
  478. public static function suite()
  479. {
  480. return new PHPUnit_Framework_TestSuite("ezcBaseTest");
  481. }
  482. }
  483. ?>