CoreTest.php 9.5 KB

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