HelpersTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Testing Unittest Module Helper Class
  4. * Very Important as other Unittests depend on that
  5. *
  6. * @package KO7/Unittest
  7. * @group koseven
  8. * @group koseven.unittest
  9. * @category Test
  10. *
  11. * Koseven Team
  12. * @copyright (c) since 2018 Koseven Team
  13. * @license https://koseven.dev/LICENSE
  14. */
  15. class HelpersTest extends Unittest_TestCase {
  16. /**
  17. * Replaces all / in string with directory Separator
  18. */
  19. public function test_dir_separator() : void
  20. {
  21. $expected = 'test'.DIRECTORY_SEPARATOR.'dir';
  22. self::assertSame($expected, KO7_Unittest_Helpers::dir_separator('test/dir'));
  23. }
  24. /**
  25. * Tests cleaning cache dir which should happen recursivley.
  26. * Following structure will be created to test that:
  27. *
  28. * - Folder
  29. * .hidden-file
  30. * - Folder2
  31. * - file2
  32. * - file
  33. */
  34. public function test_clean_cache_dir() : void
  35. {
  36. // Kohana Cache directory
  37. $cacheDir = KO7::$cache_dir;
  38. // Create Test Directories and Files
  39. $folder = $cacheDir.DIRECTORY_SEPARATOR.'Folder';
  40. $folder2 = $folder.DIRECTORY_SEPARATOR.'Folder2';
  41. $file = $folder.DIRECTORY_SEPARATOR.'.hidden-file';
  42. $file2 = $cacheDir.DIRECTORY_SEPARATOR.'file';
  43. $file3 = $folder2.DIRECTORY_SEPARATOR.'file2';
  44. if ( ! mkdir($folder) || ! mkdir($folder2) || ! touch($file) || ! touch($file2) || ! touch($file3)) {
  45. self::fail('Could not create Test Files. Please make sure your cache dir is writable');
  46. }
  47. KO7_Unittest_Helpers::clean_cache_dir();
  48. $files = scandir($cacheDir, 0);
  49. // Only files left should now be: '.','..','.gitignore'
  50. self::assertCount(3, $files);
  51. }
  52. /**
  53. * Test setting configuration options via set environment
  54. * @throws KO7_Exception
  55. * @throws ReflectionException
  56. */
  57. public function test_set_environment_config_options()
  58. {
  59. // Init
  60. $config = [
  61. 'cache.default' => 'test'
  62. ];
  63. $helpers = new KO7_Unittest_Helpers();
  64. // Set new environment and check if set correctly
  65. $helpers->set_environment($config);
  66. self::assertSame('test', KO7::$config->load('cache')->get('default'));
  67. // Restore old environment and check if restored
  68. $helpers->restore_environment();
  69. self::assertNotSame('test', KO7::$config->load('cache')->get('default'));
  70. }
  71. }