/src/EBNF/TestDirHelper.php

https://github.com/Weltraumschaf/ebnf · PHP · 83 lines · 18 code · 8 blank · 57 comment · 0 complexity · 1a99cf4d4e7637f30b23c3460943cea6 MD5 · raw file

  1. <?php
  2. /**
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * @license http://www.gnu.org/licenses/ GNU General Public License
  17. * @author Sven Strittmatter <ich@weltraumschaf.de>
  18. * @package ebnf
  19. */
  20. namespace de\weltraumschaf\ebnf;
  21. /**
  22. * Helper class for generating random temp dir names and deletes them.
  23. * Useful for unittests which can not use vfsStream files.
  24. *
  25. * This class generates on construction time a dir name prefixed with basepath
  26. * provided to the constructor, the string "ebnf_test_files_" and a random
  27. * number. This object is in that way imutable that the directory name is
  28. * not mutable. Each instance reprsents a iwn unique direcoty.
  29. *
  30. * @package ebnf
  31. */
  32. class TestDirHelper {
  33. /**
  34. * Default base dir is unix style tmp dir.
  35. */
  36. const DEFAULT_BASE_DIR = "/tmp";
  37. /**
  38. * Unique and imutable dir name.
  39. *
  40. * @var string
  41. */
  42. private $dirName;
  43. /**
  44. * Initializes the object with the unique dir name prefixed by $baseDir.
  45. *
  46. * @param string $baseDir Direcotry prefix.
  47. */
  48. public function __construct($baseDir = self::DEFAULT_BASE_DIR) {
  49. $this->dirName = $baseDir . DIRECTORY_SEPARATOR . "ebnf_test_files_" . mt_rand();
  50. }
  51. /**
  52. * Creates the directory physcally.
  53. *
  54. * @return void
  55. */
  56. public function create() {
  57. system("mkdir -p {$this->dirName}");
  58. }
  59. /**
  60. * removes the directory physcally.
  61. *
  62. * @return void
  63. */
  64. public function remove() {
  65. system("rm -rf {$this->dirName}");
  66. }
  67. /**
  68. * Retunrs the unique directory name string.
  69. *
  70. * @return string
  71. */
  72. public function get() {
  73. return $this->dirName;
  74. }
  75. }