1<?php
2
3/**
4 * Helper class with some filesystem utilities.
5 */
6class TestUtils {
7
8    /**
9     * converts path to unix-like on windows OS
10     * @param string $path UNIX-like path to be converted
11     * @return string
12     */
13    public static function w2u($path) {
14        return isWindows() ? str_replace('\\', '/', $path) : $path;
15    }
16
17    /**
18     * helper for recursive copy()
19     *
20     * @static
21     * @param $destdir string
22     * @param $source string
23     */
24    public static function rcopy($destdir, $source) {
25        if (!is_dir($source)) {
26            copy($source, $destdir.'/'.basename($source));
27        } else {
28            $newdestdir = $destdir.'/'.basename($source);
29            if (!is_dir($newdestdir)) {
30                mkdir($newdestdir);
31            }
32
33            $dh = dir($source);
34            while (false !== ($entry = $dh->read())) {
35                if ($entry == '.' || $entry == '..') {
36                    continue;
37                }
38                TestUtils::rcopy($newdestdir, $source.'/'.$entry);
39            }
40            $dh->close();
41        }
42    }
43
44    /**
45     * helper for recursive rmdir()/unlink()
46     *
47     * @static
48     * @param $target string
49     */
50    public static function rdelete($target) {
51        if (!is_dir($target)) {
52            unlink($target);
53        } else {
54            $dh = dir($target);
55            while (false !== ($entry = $dh->read())) {
56                if ($entry == '.' || $entry == '..') {
57                    continue;
58                }
59                TestUtils::rdelete("$target/$entry");
60            }
61            $dh->close();
62            rmdir($target);
63        }
64    }
65
66    /**
67     * helper to append text to a file
68     *
69     * @static
70     * @param $file string
71     * @param $text string
72     */
73    public static function fappend($file, $text) {
74        $fh = fopen($file, 'a');
75        fwrite($fh, $text);
76        fclose($fh);
77    }
78
79}
80