summaryrefslogtreecommitdiffstats
path: root/library/Director/Test/TestSuite.php
blob: 131b9741fcb7dade2347cb5b53e26e4e500e0cfd (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
<?php

namespace Icinga\Module\Director\Test;

use Icinga\Application\Icinga;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

abstract class TestSuite
{
    private $basedir;

    abstract public function run();

    public static function newTempfile()
    {
        return tempnam(sys_get_temp_dir(), 'DirectorTest-');
    }

    public function process($command, $identifier = null)
    {
        return new TestProcess($command, $identifier);
    }

    protected function filesByExtension($base, $extensions)
    {
        $files = array();

        if (! is_array($extensions)) {
            $extensions = array($extensions);
        }

        $basedir = $this->getBaseDir() . '/' . $base;
        $dir = new RecursiveDirectoryIterator($basedir);
        $iterator = new RecursiveIteratorIterator(
            $dir,
            RecursiveIteratorIterator::SELF_FIRST
        );

        foreach ($iterator as $file) {
            if (! $file->isFile()) {
                continue;
            }

            if (in_array($file->getExtension(), $extensions)) {
                $files[] = $file->getPathname();
            }
        }

        return $files;
    }

    public function getBaseDir($file = null)
    {
        if ($this->basedir === null) {
            $this->basedir = Icinga::app()
                ->getModuleManager()
                ->getModule('director')
                ->getBaseDir();
        }

        if ($file === null) {
            return $this->basedir;
        } else {
            return $this->basedir . '/' . $file;
        }
    }
}