PageRenderTime 5156ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/system/tests/kohana/CoreTest.php

https://code.google.com/p/php-blackops-rcon/
PHP | 462 lines | 237 code | 36 blank | 189 comment | 2 complexity | 105c30cd068046e1a1366225636359db MD5 | raw file
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Tests Kohana Core
  4. *
  5. * @TODO Use a virtual filesystem (see phpunit doc on mocking fs) for find_file etc.
  6. *
  7. * @group kohana
  8. * @group kohana.core
  9. *
  10. * @package Unittest
  11. * @author Kohana Team
  12. * @author Jeremy Bush <contractfrombelow@gmail.com>
  13. * @copyright (c) 2008-2010 Kohana Team
  14. * @license http://kohanaphp.com/license
  15. */
  16. class Kohana_CoreTest extends Kohana_Unittest_TestCase
  17. {
  18. /**
  19. * Provides test data for test_sanitize()
  20. *
  21. * @return array
  22. */
  23. public function provider_sanitize()
  24. {
  25. return array(
  26. // $value, $result
  27. array('foo', 'foo'),
  28. array("foo\r\nbar", "foo\nbar"),
  29. array("foo\rbar", "foo\nbar"),
  30. array("Is your name O\'reilly?", "Is your name O'reilly?")
  31. );
  32. }
  33. /**
  34. * Tests Kohana::santize()
  35. *
  36. * @test
  37. * @dataProvider provider_sanitize
  38. * @covers Kohana::sanitize
  39. * @param boolean $value Input for Kohana::sanitize
  40. * @param boolean $result Output for Kohana::sanitize
  41. */
  42. public function test_sanitize($value, $result)
  43. {
  44. $this->setEnvironment(array('Kohana::$magic_quotes' => TRUE));
  45. $this->assertSame($result, Kohana::sanitize($value));
  46. }
  47. /**
  48. * If a file can't be found then find_file() should return FALSE if
  49. * only a single file was requested, or an empty array if multiple files
  50. * (i.e. configuration files) were requested
  51. *
  52. * @test
  53. * @covers Kohana::find_file
  54. */
  55. public function test_find_file_returns_false_or_array_on_failure()
  56. {
  57. $this->assertFalse(Kohana::find_file('configy', 'zebra'));
  58. $this->assertSame(array(), Kohana::find_file('configy', 'zebra', NULL, TRUE));
  59. }
  60. /**
  61. * Kohana::list_files() should return an array on success and an empty array on failure
  62. *
  63. * @test
  64. * @covers Kohana::list_files
  65. */
  66. public function test_list_files_returns_array_on_success_and_failure()
  67. {
  68. $files = Kohana::list_files('config');
  69. $this->assertType('array', $files);
  70. $this->assertGreaterThan(3, count($files));
  71. $this->assertSame(array(), Kohana::list_files('geshmuck'));
  72. }
  73. /**
  74. * Tests Kohana::globals()
  75. *
  76. * @test
  77. * @covers Kohana::globals
  78. */
  79. public function test_globals_removes_user_def_globals()
  80. {
  81. $GLOBALS = array('hackers' => 'foobar','name' => array('','',''), '_POST' => array());
  82. Kohana::globals();
  83. $this->assertEquals(array('_POST' => array()), $GLOBALS);
  84. }
  85. /**
  86. * Provides test data for testCache()
  87. *
  88. * @return array
  89. */
  90. public function provider_cache()
  91. {
  92. return array(
  93. // $value, $result
  94. array('foo', 'hello, world', 10),
  95. array('bar', NULL, 10),
  96. array('bar', NULL, -10),
  97. );
  98. }
  99. /**
  100. * Tests Kohana::cache()
  101. *
  102. * @test
  103. * @dataProvider provider_cache
  104. * @covers Kohana::cache
  105. * @param boolean $key Key to cache/get for Kohana::cache
  106. * @param boolean $value Output from Kohana::cache
  107. * @param boolean $lifetime Lifetime for Kohana::cache
  108. */
  109. public function test_cache($key, $value, $lifetime)
  110. {
  111. Kohana::cache($key, $value, $lifetime);
  112. $this->assertEquals($value, Kohana::cache($key));
  113. }
  114. /**
  115. * Provides test data for test_message()
  116. *
  117. * @return array
  118. */
  119. public function provider_message()
  120. {
  121. return array(
  122. // $value, $result
  123. array(':field must not be empty', 'validate', 'not_empty'),
  124. array(
  125. array(
  126. 'alpha' => ':field must contain only letters',
  127. 'alpha_dash' => ':field must contain only letters and dashes',
  128. 'alpha_numeric' => ':field must contain only letters and numbers',
  129. 'color' => ':field must be a color',
  130. 'credit_card' => ':field must be a credit card number',
  131. 'date' => ':field must be a date',
  132. 'decimal' => ':field must be a decimal with :param1 places',
  133. 'digit' => ':field must be a digit',
  134. 'email' => ':field must be a email address',
  135. 'email_domain' => ':field must contain a valid email domain',
  136. 'exact_length' => ':field must be exactly :param1 characters long',
  137. 'in_array' => ':field must be one of the available options',
  138. 'ip' => ':field must be an ip address',
  139. 'matches' => ':field must be the same as :param1',
  140. 'min_length' => ':field must be at least :param1 characters long',
  141. 'max_length' => ':field must be less than :param1 characters long',
  142. 'phone' => ':field must be a phone number',
  143. 'not_empty' => ':field must not be empty',
  144. 'range' => ':field must be within the range of :param1 to :param2',
  145. 'regex' => ':field does not match the required format',
  146. 'url' => ':field must be a url',
  147. ),
  148. 'validate', NULL,
  149. ),
  150. );
  151. }
  152. /**
  153. * Tests Kohana::message()
  154. *
  155. * @test
  156. * @dataProvider provider_message
  157. * @covers Kohana::message
  158. * @param boolean $expected Output for Kohana::message
  159. * @param boolean $file File to look in for Kohana::message
  160. * @param boolean $key Key for Kohana::message
  161. */
  162. public function test_message($expected, $file, $key)
  163. {
  164. $this->assertEquals($expected, Kohana::message($file, $key));
  165. }
  166. /**
  167. * Provides test data for test_error_handler()
  168. *
  169. * @return array
  170. */
  171. public function provider_error_handler()
  172. {
  173. return array(
  174. array(1, 'Foobar', 'foobar.php', __LINE__),
  175. );
  176. }
  177. /**
  178. * Tests Kohana::error_handler()
  179. *
  180. * @test
  181. * @dataProvider provider_error_handler
  182. * @covers Kohana::error_handler
  183. * @param boolean $code Input for Kohana::sanitize
  184. * @param boolean $error Input for Kohana::sanitize
  185. * @param boolean $file Input for Kohana::sanitize
  186. * @param boolean $line Output for Kohana::sanitize
  187. */
  188. public function test_error_handler($code, $error, $file, $line)
  189. {
  190. $error_level = error_reporting();
  191. error_reporting(E_ALL);
  192. try
  193. {
  194. Kohana::error_handler($code, $error, $file, $line);
  195. }
  196. catch (Exception $e)
  197. {
  198. $this->assertEquals($code, $e->getCode());
  199. $this->assertEquals($error, $e->getMessage());
  200. }
  201. error_reporting($error_level);
  202. }
  203. /**
  204. * Provides test data for testExceptionHandler()
  205. *
  206. * @return array
  207. */
  208. public function provider_exception_handler()
  209. {
  210. return array(
  211. // $exception_type, $message, $is_cli, $expected
  212. array('Kohana_Exception', 'hello, world!', TRUE, TRUE, 'hello, world!'),
  213. array('ErrorException', 'hello, world!', TRUE, TRUE, 'hello, world!'),
  214. // #3016
  215. array('Kohana_Exception', '<hello, world!>', FALSE, TRUE, '&lt;hello, world!&gt;'),
  216. );
  217. }
  218. /**
  219. * Tests Kohana::exception_handler()
  220. *
  221. * @test
  222. * @dataProvider provider_exception_handler
  223. * @covers Kohana::exception_handler
  224. * @param boolean $exception_type Exception type to throw
  225. * @param boolean $message Message to pass to exception
  226. * @param boolean $is_cli Use cli mode?
  227. * @param boolean $expected Output for Kohana::exception_handler
  228. * @param string $expexcted_message What to look for in the output string
  229. */
  230. public function teste_exception_handler($exception_type, $message, $is_cli, $expected, $expected_message)
  231. {
  232. try
  233. {
  234. Kohana::$is_cli = $is_cli;
  235. throw new $exception_type($message);
  236. }
  237. catch (Exception $e)
  238. {
  239. ob_start();
  240. $this->assertEquals($expected, Kohana::exception_handler($e));
  241. $view = ob_get_contents();
  242. ob_clean();
  243. $this->assertContains($expected_message, $view);
  244. }
  245. Kohana::$is_cli = TRUE;
  246. }
  247. /**
  248. * Provides test data for test_debug()
  249. *
  250. * @return array
  251. */
  252. public function provider_debug()
  253. {
  254. return array(
  255. // $exception_type, $message, $is_cli, $expected
  256. array(array('foobar'), "<pre class=\"debug\"><small>array</small><span>(1)</span> <span>(\n 0 => <small>string</small><span>(6)</span> \"foobar\"\n)</span></pre>"),
  257. );
  258. }
  259. /**
  260. * Tests Kohana::debug()
  261. *
  262. * @test
  263. * @dataProvider provider_debug
  264. * @covers Kohana::debug
  265. * @param boolean $thing The thing to debug
  266. * @param boolean $expected Output for Kohana::debug
  267. */
  268. public function testdebug($thing, $expected)
  269. {
  270. $this->assertEquals($expected, Kohana::debug($thing));
  271. }
  272. /**
  273. * Provides test data for testDebugPath()
  274. *
  275. * @return array
  276. */
  277. public function provider_debug_path()
  278. {
  279. return array(
  280. array(
  281. Kohana::find_file('classes', 'kohana'),
  282. 'SYSPATH'.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR.'kohana.php'
  283. ),
  284. array(
  285. Kohana::find_file('classes', $this->dirSeparator('kohana/unittest/runner')),
  286. $this->dirSeparator('MODPATH/unittest/classes/kohana/unittest/runner.php')
  287. ),
  288. );
  289. }
  290. /**
  291. * Tests Kohana::debug_path()
  292. *
  293. * @test
  294. * @dataProvider provider_debug_path
  295. * @covers Kohana::debug_path
  296. * @param boolean $path Input for Kohana::debug_path
  297. * @param boolean $expected Output for Kohana::debug_path
  298. */
  299. public function testDebugPath($path, $expected)
  300. {
  301. $this->assertEquals($expected, Kohana::debug_path($path));
  302. }
  303. /**
  304. * Provides test data for test_modules_sets_and_returns_valid_modules()
  305. *
  306. * @return array
  307. */
  308. public function provider_modules_sets_and_returns_valid_modules()
  309. {
  310. return array(
  311. array(array(), array()),
  312. array(array('unittest' => MODPATH.'fo0bar'), array()),
  313. array(array('unittest' => MODPATH.'unittest'), array('unittest' => $this->dirSeparator(MODPATH.'unittest/'))),
  314. );
  315. }
  316. /**
  317. * Tests Kohana::modules()
  318. *
  319. * @test
  320. * @dataProvider provider_modules_sets_and_returns_valid_modules
  321. * @covers Kohana::modules
  322. * @param boolean $source Input for Kohana::modules
  323. * @param boolean $expected Output for Kohana::modules
  324. */
  325. public function test_modules_sets_and_returns_valid_modules($source, $expected)
  326. {
  327. $modules = Kohana::modules();
  328. $this->assertEquals($expected, Kohana::modules($source));
  329. Kohana::modules($modules);
  330. }
  331. /**
  332. * To make the tests as portable as possible this just tests that
  333. * you get an array of modules when you can Kohana::modules() and that
  334. * said array contains unittest
  335. *
  336. * @test
  337. * @covers Kohana::modules
  338. */
  339. public function test_modules_returns_array_of_modules()
  340. {
  341. $modules = Kohana::modules();
  342. $this->assertType('array', $modules);
  343. $this->assertArrayHasKey('unittest', $modules);
  344. }
  345. /**
  346. * Tests Kohana::include_paths()
  347. *
  348. * The include paths must contain the apppath and syspath
  349. * @test
  350. * @covers Kohana::include_paths
  351. */
  352. public function test_include_paths()
  353. {
  354. $include_paths = Kohana::include_paths();
  355. $modules = Kohana::modules();
  356. $this->assertType('array', $include_paths);
  357. // We must have at least 2 items in include paths (APP / SYS)
  358. $this->assertGreaterThan(2, count($include_paths));
  359. // Make sure said paths are in the include paths
  360. // And make sure they're in the correct positions
  361. $this->assertSame(APPPATH, reset($include_paths));
  362. $this->assertSame(SYSPATH, end($include_paths));
  363. foreach($modules as $module)
  364. {
  365. $this->assertContains($module, $include_paths);
  366. }
  367. }
  368. /**
  369. * Provides test data for test_exception_text()
  370. *
  371. * @return array
  372. */
  373. public function provider_exception_text()
  374. {
  375. return array(
  376. array(new Kohana_Exception('foobar'), $this->dirSeparator('Kohana_Exception [ 0 ]: foobar ~ SYSPATH/tests/kohana/CoreTest.php [ '.__LINE__.' ]')),
  377. );
  378. }
  379. /**
  380. * Tests Kohana::exception_text()
  381. *
  382. * @test
  383. * @dataProvider provider_exception_text
  384. * @covers Kohana::exception_text
  385. * @param object $exception exception to test
  386. * @param string $expected expected output
  387. */
  388. public function test_exception_text($exception, $expected)
  389. {
  390. $this->assertEquals($expected, Kohana::exception_text($exception));
  391. }
  392. /**
  393. * Provides test data for test_dump()
  394. *
  395. * @return array
  396. */
  397. public function provider_dump()
  398. {
  399. return array(
  400. array('foobar', 128, '<small>string</small><span>(6)</span> "foobar"'),
  401. array('foobar', 2, '<small>string</small><span>(6)</span> "fo&nbsp;&hellip;"'),
  402. array(NULL, 128, '<small>NULL</small>'),
  403. array(TRUE, 128, '<small>bool</small> TRUE'),
  404. array(array('foobar'), 128, "<small>array</small><span>(1)</span> <span>(\n 0 => <small>string</small><span>(6)</span> \"foobar\"\n)</span>"),
  405. array(new StdClass, 128, "<small>object</small> <span>stdClass(0)</span> <code>{\n}</code>"),
  406. array("fo\x6F\xFF\x00bar\x8F\xC2\xB110", 128, '<small>string</small><span>(10)</span> "foobarÂ?10"'),
  407. );
  408. }
  409. /**
  410. * Tests Kohana::dump()
  411. *
  412. * @test
  413. * @dataProvider provider_dump
  414. * @covers Kohana::dump
  415. * @covers Kohana::_dump
  416. * @param object $exception exception to test
  417. * @param string $expected expected output
  418. */
  419. public function test_dump($input, $length, $expected)
  420. {
  421. $this->assertEquals($expected, Kohana::dump($input, $length));
  422. }
  423. }