PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/system/tests/kohana/CoreTest.php

https://github.com/jongwook/ksa14-kohana
PHP | 367 lines | 206 code | 33 blank | 128 comment | 1 complexity | 670b7a6015c411f107fe6fb03a7a3b84 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 Kohana
  11. * @category Tests
  12. * @author Kohana Team
  13. * @author Jeremy Bush <contractfrombelow@gmail.com>
  14. * @copyright (c) 2008-2011 Kohana Team
  15. * @license http://kohanaframework.org/license
  16. */
  17. class Kohana_CoreTest extends Unittest_TestCase
  18. {
  19. /**
  20. * Provides test data for test_sanitize()
  21. *
  22. * @return array
  23. */
  24. public function provider_sanitize()
  25. {
  26. return array(
  27. // $value, $result
  28. array('foo', 'foo'),
  29. array("foo\r\nbar", "foo\nbar"),
  30. array("foo\rbar", "foo\nbar"),
  31. array("Is your name O\'reilly?", "Is your name O'reilly?")
  32. );
  33. }
  34. /**
  35. * Tests Kohana::santize()
  36. *
  37. * @test
  38. * @dataProvider provider_sanitize
  39. * @covers Kohana::sanitize
  40. * @param boolean $value Input for Kohana::sanitize
  41. * @param boolean $result Output for Kohana::sanitize
  42. */
  43. public function test_sanitize($value, $result)
  44. {
  45. $this->setEnvironment(array('Kohana::$magic_quotes' => TRUE));
  46. $this->assertSame($result, Kohana::sanitize($value));
  47. }
  48. /**
  49. * Passing FALSE for the file extension should prevent appending any extension.
  50. * See issue #3214
  51. *
  52. * @test
  53. * @covers Kohana::find_file
  54. */
  55. public function test_find_file_no_extension()
  56. {
  57. // EXT is manually appened to the _file name_, not passed as the extension
  58. $path = Kohana::find_file('classes', $file = 'kohana/core'.EXT, FALSE);
  59. $this->assertInternalType('string', $path);
  60. $this->assertStringEndsWith($file, $path);
  61. }
  62. /**
  63. * If a file can't be found then find_file() should return FALSE if
  64. * only a single file was requested, or an empty array if multiple files
  65. * (i.e. configuration files) were requested
  66. *
  67. * @test
  68. * @covers Kohana::find_file
  69. */
  70. public function test_find_file_returns_false_or_array_on_failure()
  71. {
  72. $this->assertFalse(Kohana::find_file('configy', 'zebra'));
  73. $this->assertSame(array(), Kohana::find_file('configy', 'zebra', NULL, TRUE));
  74. }
  75. /**
  76. * Kohana::list_files() should return an array on success and an empty array on failure
  77. *
  78. * @test
  79. * @covers Kohana::list_files
  80. */
  81. public function test_list_files_returns_array_on_success_and_failure()
  82. {
  83. $files = Kohana::list_files('config');
  84. $this->assertInternalType('array', $files);
  85. $this->assertGreaterThan(3, count($files));
  86. $this->assertSame(array(), Kohana::list_files('geshmuck'));
  87. }
  88. /**
  89. * Tests Kohana::globals()
  90. *
  91. * @test
  92. * @covers Kohana::globals
  93. */
  94. public function test_globals_removes_user_def_globals()
  95. {
  96. $GLOBALS = array('hackers' => 'foobar','name' => array('','',''), '_POST' => array());
  97. Kohana::globals();
  98. $this->assertEquals(array('_POST' => array()), $GLOBALS);
  99. }
  100. /**
  101. * Provides test data for testCache()
  102. *
  103. * @return array
  104. */
  105. public function provider_cache()
  106. {
  107. return array(
  108. // $value, $result
  109. array('foo', 'hello, world', 10),
  110. array('bar', NULL, 10),
  111. array('bar', NULL, -10),
  112. );
  113. }
  114. /**
  115. * Tests Kohana::cache()
  116. *
  117. * @test
  118. * @dataProvider provider_cache
  119. * @covers Kohana::cache
  120. * @param boolean $key Key to cache/get for Kohana::cache
  121. * @param boolean $value Output from Kohana::cache
  122. * @param boolean $lifetime Lifetime for Kohana::cache
  123. */
  124. public function test_cache($key, $value, $lifetime)
  125. {
  126. Kohana::cache($key, $value, $lifetime);
  127. $this->assertEquals($value, Kohana::cache($key));
  128. }
  129. /**
  130. * Provides test data for test_message()
  131. *
  132. * @return array
  133. */
  134. public function provider_message()
  135. {
  136. return array(
  137. // $value, $result
  138. array(':field must not be empty', 'validation', 'not_empty'),
  139. array(
  140. array(
  141. 'alpha' => ':field must contain only letters',
  142. 'alpha_dash' => ':field must contain only numbers, letters and dashes',
  143. 'alpha_numeric' => ':field must contain only letters and numbers',
  144. 'color' => ':field must be a color',
  145. 'credit_card' => ':field must be a credit card number',
  146. 'date' => ':field must be a date',
  147. 'decimal' => ':field must be a decimal with :param2 places',
  148. 'digit' => ':field must be a digit',
  149. 'email' => ':field must be a email address',
  150. 'email_domain' => ':field must contain a valid email domain',
  151. 'equals' => ':field must equal :param2',
  152. 'exact_length' => ':field must be exactly :param2 characters long',
  153. 'in_array' => ':field must be one of the available options',
  154. 'ip' => ':field must be an ip address',
  155. 'matches' => ':field must be the same as :param2',
  156. 'min_length' => ':field must be at least :param2 characters long',
  157. 'max_length' => ':field must not exceed :param2 characters long',
  158. 'not_empty' => ':field must not be empty',
  159. 'numeric' => ':field must be numeric',
  160. 'phone' => ':field must be a phone number',
  161. 'range' => ':field must be within the range of :param2 to :param3',
  162. 'regex' => ':field does not match the required format',
  163. 'url' => ':field must be a url',
  164. ),
  165. 'validation', NULL,
  166. ),
  167. );
  168. }
  169. /**
  170. * Tests Kohana::message()
  171. *
  172. * @test
  173. * @dataProvider provider_message
  174. * @covers Kohana::message
  175. * @param boolean $expected Output for Kohana::message
  176. * @param boolean $file File to look in for Kohana::message
  177. * @param boolean $key Key for Kohana::message
  178. */
  179. public function test_message($expected, $file, $key)
  180. {
  181. $this->assertEquals($expected, Kohana::message($file, $key));
  182. }
  183. /**
  184. * Provides test data for test_error_handler()
  185. *
  186. * @return array
  187. */
  188. public function provider_error_handler()
  189. {
  190. return array(
  191. array(1, 'Foobar', 'foobar.php', __LINE__),
  192. );
  193. }
  194. /**
  195. * Tests Kohana::error_handler()
  196. *
  197. * @test
  198. * @dataProvider provider_error_handler
  199. * @covers Kohana::error_handler
  200. * @param boolean $code Input for Kohana::sanitize
  201. * @param boolean $error Input for Kohana::sanitize
  202. * @param boolean $file Input for Kohana::sanitize
  203. * @param boolean $line Output for Kohana::sanitize
  204. */
  205. public function test_error_handler($code, $error, $file, $line)
  206. {
  207. $error_level = error_reporting();
  208. error_reporting(E_ALL);
  209. try
  210. {
  211. Kohana::error_handler($code, $error, $file, $line);
  212. }
  213. catch (Exception $e)
  214. {
  215. $this->assertEquals($code, $e->getCode());
  216. $this->assertEquals($error, $e->getMessage());
  217. }
  218. error_reporting($error_level);
  219. }
  220. /**
  221. * Provides test data for test_modules_sets_and_returns_valid_modules()
  222. *
  223. * @return array
  224. */
  225. public function provider_modules_detects_invalid_modules()
  226. {
  227. return array(
  228. array(array('unittest' => MODPATH.'fo0bar')),
  229. array(array('unittest' => MODPATH.'unittest', 'fo0bar' => MODPATH.'fo0bar')),
  230. );
  231. }
  232. /**
  233. * Tests Kohana::modules()
  234. *
  235. * @test
  236. * @dataProvider provider_modules_detects_invalid_modules
  237. * @expectedException Kohana_Exception
  238. * @param boolean $source Input for Kohana::modules
  239. *
  240. */
  241. public function test_modules_detects_invalid_modules($source)
  242. {
  243. $modules = Kohana::modules();
  244. try
  245. {
  246. Kohana::modules($source);
  247. }
  248. catch(Exception $e)
  249. {
  250. // Restore modules
  251. Kohana::modules($modules);
  252. throw $e;
  253. }
  254. // Restore modules
  255. Kohana::modules($modules);
  256. }
  257. /**
  258. * Provides test data for test_modules_sets_and_returns_valid_modules()
  259. *
  260. * @return array
  261. */
  262. public function provider_modules_sets_and_returns_valid_modules()
  263. {
  264. return array(
  265. array(array(), array()),
  266. array(array('unittest' => MODPATH.'unittest'), array('unittest' => $this->dirSeparator(MODPATH.'unittest/'))),
  267. );
  268. }
  269. /**
  270. * Tests Kohana::modules()
  271. *
  272. * @test
  273. * @dataProvider provider_modules_sets_and_returns_valid_modules
  274. * @param boolean $source Input for Kohana::modules
  275. * @param boolean $expected Output for Kohana::modules
  276. */
  277. public function test_modules_sets_and_returns_valid_modules($source, $expected)
  278. {
  279. $modules = Kohana::modules();
  280. try
  281. {
  282. $this->assertEquals($expected, Kohana::modules($source));
  283. }
  284. catch(Exception $e)
  285. {
  286. Kohana::modules($modules);
  287. throw $e;
  288. }
  289. Kohana::modules($modules);
  290. }
  291. /**
  292. * To make the tests as portable as possible this just tests that
  293. * you get an array of modules when you can Kohana::modules() and that
  294. * said array contains unittest
  295. *
  296. * @test
  297. * @covers Kohana::modules
  298. */
  299. public function test_modules_returns_array_of_modules()
  300. {
  301. $modules = Kohana::modules();
  302. $this->assertInternalType('array', $modules);
  303. $this->assertArrayHasKey('unittest', $modules);
  304. }
  305. /**
  306. * Tests Kohana::include_paths()
  307. *
  308. * The include paths must contain the apppath and syspath
  309. * @test
  310. * @covers Kohana::include_paths
  311. */
  312. public function test_include_paths()
  313. {
  314. $include_paths = Kohana::include_paths();
  315. $modules = Kohana::modules();
  316. $this->assertInternalType('array', $include_paths);
  317. // We must have at least 2 items in include paths (APP / SYS)
  318. $this->assertGreaterThan(2, count($include_paths));
  319. // Make sure said paths are in the include paths
  320. // And make sure they're in the correct positions
  321. $this->assertSame(APPPATH, reset($include_paths));
  322. $this->assertSame(SYSPATH, end($include_paths));
  323. foreach ($modules as $module)
  324. {
  325. $this->assertContains($module, $include_paths);
  326. }
  327. }
  328. }