CoreTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  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 <contractfrombelow@gmail.com>
  15. * @copyright (c) Kohana Team
  16. * @license https://koseven.ga/LICENSE.md
  17. */
  18. class Kohana_CoreTest extends Unittest_TestCase
  19. {
  20. protected $old_modules = [];
  21. /**
  22. * Captures the module list as it was before this test
  23. *
  24. * @return null
  25. */
  26. // @codingStandardsIgnoreStart
  27. public function setUp()
  28. // @codingStandardsIgnoreEnd
  29. {
  30. parent::setUp();
  31. $this->old_modules = Kohana::modules();
  32. }
  33. /**
  34. * Restores the module list
  35. *
  36. * @return null
  37. */
  38. // @codingStandardsIgnoreStart
  39. public function tearDown()
  40. // @codingStandardsIgnoreEnd
  41. {
  42. Kohana::modules($this->old_modules);
  43. }
  44. /**
  45. * Provides test data for test_sanitize()
  46. *
  47. * @return array
  48. */
  49. public function provider_sanitize()
  50. {
  51. return [
  52. // $value, $result
  53. ['foo', 'foo'],
  54. ["foo\r\nbar", "foo\nbar"],
  55. ["foo\rbar", "foo\nbar"],
  56. ];
  57. }
  58. /**
  59. * Tests Kohana::santize()
  60. *
  61. * @test
  62. * @dataProvider provider_sanitize
  63. * @covers Kohana::sanitize
  64. * @param boolean $value Input for Kohana::sanitize
  65. * @param boolean $result Output for Kohana::sanitize
  66. */
  67. public function test_sanitize($value, $result)
  68. {
  69. $this->assertSame($result, Kohana::sanitize($value));
  70. }
  71. /**
  72. * Passing FALSE for the file extension should prevent appending any extension.
  73. * See issue #3214
  74. *
  75. * @test
  76. * @covers Kohana::find_file
  77. */
  78. public function test_find_file_no_extension()
  79. {
  80. // EXT is manually appened to the _file name_, not passed as the extension
  81. $path = Kohana::find_file('classes', $file = 'Kohana/Core'.EXT, FALSE);
  82. $this->assertInternalType('string', $path);
  83. $this->assertStringEndsWith($file, $path);
  84. }
  85. /**
  86. * If a file can't be found then find_file() should return FALSE if
  87. * only a single file was requested, or an empty array if multiple files
  88. * (i.e. configuration files) were requested
  89. *
  90. * @test
  91. * @covers Kohana::find_file
  92. */
  93. public function test_find_file_returns_false_or_array_on_failure()
  94. {
  95. $this->assertFalse(Kohana::find_file('configy', 'zebra'));
  96. $this->assertSame([], Kohana::find_file('configy', 'zebra', NULL, TRUE));
  97. }
  98. /**
  99. * Kohana::list_files() should return an array on success and an empty array on failure
  100. *
  101. * @test
  102. * @covers Kohana::list_files
  103. */
  104. public function test_list_files_returns_array_on_success_and_failure()
  105. {
  106. $files = Kohana::list_files('config');
  107. $this->assertInternalType('array', $files);
  108. $this->assertGreaterThan(3, count($files));
  109. $this->assertSame([], Kohana::list_files('geshmuck'));
  110. }
  111. /**
  112. * Provides test data for testCache()
  113. *
  114. * @return array
  115. */
  116. public function provider_cache()
  117. {
  118. return [
  119. // $value, $result
  120. ['foo', 'hello, world', 10],
  121. ['bar', NULL, 10],
  122. ['bar', NULL, -10],
  123. ];
  124. }
  125. /**
  126. * Tests Kohana::cache()
  127. *
  128. * @test
  129. * @dataProvider provider_cache
  130. * @covers Kohana::cache
  131. * @param boolean $key Key to cache/get for Kohana::cache
  132. * @param boolean $value Output from Kohana::cache
  133. * @param boolean $lifetime Lifetime for Kohana::cache
  134. */
  135. public function test_cache($key, $value, $lifetime)
  136. {
  137. Kohana::cache($key, $value, $lifetime);
  138. $this->assertEquals($value, Kohana::cache($key));
  139. }
  140. /**
  141. * Tests Kohana::find_file() cache is saved on shutdown.
  142. *
  143. * @test
  144. */
  145. /*public function test_find_file_cache_saved()
  146. {
  147. $old_caching = Kohana::$caching;
  148. $old_errors = Kohana::$errors;
  149. Kohana::$caching = TRUE;
  150. Kohana::$errors = FALSE;
  151. // trigger find_file() so Kohana::$_files_changed is set to TRUE
  152. Kohana::find_file('abc', 'def');
  153. // trigger shutdown so kohana write to file cache
  154. Kohana::shutdown_handler();
  155. $this->assertInternalType('array', Kohana::file_cache('Kohana::find_file()'));
  156. Kohana::$caching = $old_caching;
  157. Kohana::$errors = $old_errors;
  158. }*/
  159. /**
  160. * Provides test data for test_message()
  161. *
  162. * @return array
  163. */
  164. public function provider_message()
  165. {
  166. return [
  167. ['no_message_file', 'anything', 'default', 'default'],
  168. ['no_message_file', NULL, 'anything', []],
  169. ['kohana_core_message_tests', 'bottom_only', 'anything', 'inherited bottom message'],
  170. ['kohana_core_message_tests', 'cfs_replaced', 'anything', 'overriding cfs_replaced message'],
  171. ['kohana_core_message_tests', 'top_only', 'anything', 'top only message'],
  172. ['kohana_core_message_tests', 'missing', 'default', 'default'],
  173. ['kohana_core_message_tests', NULL, 'anything',
  174. [
  175. 'bottom_only' => 'inherited bottom message',
  176. 'cfs_replaced' => 'overriding cfs_replaced message',
  177. 'top_only' => 'top only message'
  178. ]
  179. ],
  180. ];
  181. }
  182. /**
  183. * Tests Kohana::message()
  184. *
  185. * @test
  186. * @dataProvider provider_message
  187. * @covers Kohana::message
  188. * @param string $file to pass to Kohana::message
  189. * @param string $key to pass to Kohana::message
  190. * @param string $default to pass to Kohana::message
  191. * @param string $expected Output for Kohana::message
  192. */
  193. public function test_message($file, $key, $default, $expected)
  194. {
  195. $test_path = realpath(dirname(__FILE__).'/../test_data/message_tests');
  196. Kohana::modules(['top' => "$test_path/top_module", 'bottom' => "$test_path/bottom_module"]);
  197. $this->assertEquals($expected, Kohana::message($file, $key, $default, $expected));
  198. }
  199. /**
  200. * Provides test data for test_error_handler()
  201. *
  202. * @return array
  203. */
  204. public function provider_error_handler()
  205. {
  206. return [
  207. [1, 'Foobar', 'foobar.php', __LINE__],
  208. ];
  209. }
  210. /**
  211. * Tests Kohana::error_handler()
  212. *
  213. * @test
  214. * @dataProvider provider_error_handler
  215. * @covers Kohana::error_handler
  216. * @param boolean $code Input for Kohana::sanitize
  217. * @param boolean $error Input for Kohana::sanitize
  218. * @param boolean $file Input for Kohana::sanitize
  219. * @param boolean $line Output for Kohana::sanitize
  220. */
  221. public function test_error_handler($code, $error, $file, $line)
  222. {
  223. $error_level = error_reporting();
  224. error_reporting(E_ALL);
  225. try
  226. {
  227. Kohana::error_handler($code, $error, $file, $line);
  228. }
  229. catch (Exception $e)
  230. {
  231. $this->assertEquals($code, $e->getCode());
  232. $this->assertEquals($error, $e->getMessage());
  233. }
  234. error_reporting($error_level);
  235. }
  236. /**
  237. * Provides test data for test_modules_sets_and_returns_valid_modules()
  238. *
  239. * @return array
  240. */
  241. public function provider_modules_detects_invalid_modules()
  242. {
  243. return [
  244. [['unittest' => MODPATH.'fo0bar']],
  245. [['unittest' => MODPATH.'unittest', 'fo0bar' => MODPATH.'fo0bar']],
  246. ];
  247. }
  248. /**
  249. * Tests Kohana::modules()
  250. *
  251. * @test
  252. * @dataProvider provider_modules_detects_invalid_modules
  253. * @expectedException Kohana_Exception
  254. * @param boolean $source Input for Kohana::modules
  255. *
  256. */
  257. public function test_modules_detects_invalid_modules($source)
  258. {
  259. $modules = Kohana::modules();
  260. try
  261. {
  262. Kohana::modules($source);
  263. }
  264. catch(Exception $e)
  265. {
  266. // Restore modules
  267. Kohana::modules($modules);
  268. throw $e;
  269. }
  270. // Restore modules
  271. Kohana::modules($modules);
  272. }
  273. /**
  274. * Provides test data for test_modules_sets_and_returns_valid_modules()
  275. *
  276. * @return array
  277. */
  278. public function provider_modules_sets_and_returns_valid_modules()
  279. {
  280. return [
  281. [[], []],
  282. [['module' => __DIR__], ['module' => $this->dirSeparator(__DIR__.'/')]],
  283. ];
  284. }
  285. /**
  286. * Tests Kohana::modules()
  287. *
  288. * @test
  289. * @dataProvider provider_modules_sets_and_returns_valid_modules
  290. * @param boolean $source Input for Kohana::modules
  291. * @param boolean $expected Output for Kohana::modules
  292. */
  293. public function test_modules_sets_and_returns_valid_modules($source, $expected)
  294. {
  295. $modules = Kohana::modules();
  296. try
  297. {
  298. $this->assertEquals($expected, Kohana::modules($source));
  299. }
  300. catch(Exception $e)
  301. {
  302. Kohana::modules($modules);
  303. throw $e;
  304. }
  305. Kohana::modules($modules);
  306. }
  307. /**
  308. * To make the tests as portable as possible this just tests that
  309. * you get an array of modules when you can Kohana::modules() and that
  310. * said array contains unittest
  311. *
  312. * @test
  313. * @covers Kohana::modules
  314. */
  315. public function test_modules_returns_array_of_modules()
  316. {
  317. $modules = Kohana::modules();
  318. $this->assertInternalType('array', $modules);
  319. $this->assertArrayHasKey('unittest', $modules);
  320. }
  321. /**
  322. * Tests Kohana::include_paths()
  323. *
  324. * The include paths must contain the apppath and syspath
  325. * @test
  326. * @covers Kohana::include_paths
  327. */
  328. public function test_include_paths()
  329. {
  330. $include_paths = Kohana::include_paths();
  331. $modules = Kohana::modules();
  332. $this->assertInternalType('array', $include_paths);
  333. // We must have at least 2 items in include paths (APP / SYS)
  334. $this->assertGreaterThan(2, count($include_paths));
  335. // Make sure said paths are in the include paths
  336. // And make sure they're in the correct positions
  337. $this->assertSame(APPPATH, reset($include_paths));
  338. $this->assertSame(SYSPATH, end($include_paths));
  339. foreach ($modules as $module)
  340. {
  341. $this->assertContains($module, $include_paths);
  342. }
  343. }
  344. }