CoreTest.php 9.4 KB

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