summaryrefslogtreecommitdiff
path: root/_test/core/TestUtils.php
blob: 2750a3edf1972690913e31f75eb23ae6a802ffc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

/**
 * Helper class with some filesystem utilities.
 */
class TestUtils {

    /**
     * helper for recursive copy()
     *
     * @static
     * @param $destdir string
     * @param $source string
     */
    public static function rcopy($destdir, $source) {
        if (!is_dir($source)) {
            copy($source, $destdir.'/'.basename($source));
        } else {
            $newdestdir = $destdir.'/'.basename($source);
            if (!is_dir($newdestdir)) {
                mkdir($newdestdir);
            }

            $dh = dir($source);
            while (false !== ($entry = $dh->read())) {
                if ($entry == '.' || $entry == '..') {
                    continue;
                }
                TestUtils::rcopy($newdestdir, $source.'/'.$entry);
            }
            $dh->close();
        }
    }

    /**
     * helper for recursive rmdir()/unlink()
     *
     * @static
     * @param $target string
     */
    public static function rdelete($target) {
        if (!is_dir($target)) {
            unlink($target);
        } else {
            $dh = dir($target);
            while (false !== ($entry = $dh->read())) {
                if ($entry == '.' || $entry == '..') {
                    continue;
                }
                TestUtils::rdelete("$target/$entry");
            }
            $dh->close();
            rmdir($target);
        }
    }

    /**
     * helper to append text to a file
     *
     * @static
     * @param $file string
     * @param $text string
     */
    public static function fappend($file, $text) {
        $fh = fopen($file, 'a');
        fwrite($fh, $text);
        fclose($fh);
    }

}