Completed
Push — master ( dbf744...57c37c )
by Terry
01:51
created

TestRunner::getArg()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 4
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
#!/usr/bin/env php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 1.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
<?php declare(strict_types=1);
3
4
use Terah\Assert\Tester;
5
6
require_once __DIR__ . '/../vendor/autoload.php';
7
8
class TestRunner
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    public static function run()
11
    {
12
        $fileName       = (string)static::getArg(1, getcwd());
13
        $suite          = (string)static::getArg('suite', '');
14
        $test           = (string)static::getArg('test', '');
15
        $recursive      = (bool)static::getArg('recursive', true);
16
        $generate       = (string)static::getArg('generate', '');
17
        $output         = (string)static::getArg('output', '');
18
        if ( $generate )
19
        {
20
            static::generate($generate, $output);
21
        }
22
23
        static::runTests($fileName, $suite, $test, $recursive);
24
    }
25
26
    public static function generate(string $generate, string $output)
27
    {
28
        Tester::generateTest($generate, $output);
29
30
        exit(0);
31
    }
32
33
    public static function runTests(string $fileName, string $suite, string $test, bool $recursive)
34
    {
35
        $tests          = static::getTestFiles($fileName, $recursive);
36
        if ( empty($tests) )
37
        {
38
            Tester::getLogger()->error("No test files found/specified");
39
40
            exit(1);
41
        }
42
        foreach ( $tests as $fileName )
43
        {
44
            Tester::getLogger()->debug("Loading test file {$fileName}");
45
            require($fileName);
46
            Tester::run($suite, $test);
47
        }
48
49
        exit(0);
50
    }
51
52
    /**
53
     * @param string $fileName
54
     * @param bool   $recursive
55
     * @return array
56
     */
57
    protected static function getTestFiles(string $fileName='', bool $recursive=false) : array
58
    {
59
        if ( empty($fileName) )
60
        {
61
            return [];
62
        }
63
        if ( ! file_exists($fileName) )
64
        {
65
            Tester::getLogger()->error("{$fileName} does not exist; exiting");
66
67
            exit(1);
68
        }
69
        $fileName   = realpath($fileName);
70
        if ( is_dir($fileName) )
71
        {
72
            $iterator       = new \DirectoryIterator($fileName);
73
            if ( $recursive )
74
            {
75
                $iterator       = new \RecursiveDirectoryIterator($fileName);
76
                $iterator       = $recursive ? new RecursiveIteratorIterator($iterator) : $iterator;
77
            }
78
            $testFiles      = [];
79
            foreach ( $iterator as $fileInfo )
80
            {
81
                if ( preg_match('/Suite.php$/', $fileInfo->getBasename()) )
82
                {
83
                    $testFiles[] = $fileInfo->getPathname();
84
                }
85
            }
86
87
            return $testFiles;
88
        }
89
        if ( ! is_file($fileName) )
90
        {
91
            Tester::getLogger()->error("{$fileName} is not a file; exiting");
92
93
            exit(1);
94
        }
95
96
        return [$fileName];
97
    }
98
99
    /**
100
     * PARSE ARGUMENTS
101
     *
102
     * This command line option parser supports any combination of three types of options
103
     * [single character options (`-a -b` or `-ab` or `-c -d=dog` or `-cd dog`),
104
     * long options (`--foo` or `--bar=baz` or `--bar baz`)
105
     * and arguments (`arg1 arg2`)] and returns a simple array.
106
     *
107
     * [pfisher ~]$ php test.php --foo --bar=baz --spam eggs
108
     *   ["foo"]   => true
109
     *   ["bar"]   => "baz"
110
     *   ["spam"]  => "eggs"
111
     *
112
     * [pfisher ~]$ php test.php -abc foo
113
     *   ["a"]     => true
114
     *   ["b"]     => true
115
     *   ["c"]     => "foo"
116
     *
117
     * [pfisher ~]$ php test.php arg1 arg2 arg3
118
     *   [0]       => "arg1"
119
     *   [1]       => "arg2"
120
     *   [2]       => "arg3"
121
     *
122
     * [pfisher ~]$ php test.php plain-arg --foo --bar=baz --funny="spam=eggs" --also-funny=spam=eggs \
123
     * > 'plain arg 2' -abc -k=value "plain arg 3" --s="original" --s='overwrite' --s
124
     *   [0]       => "plain-arg"
125
     *   ["foo"]   => true
126
     *   ["bar"]   => "baz"
127
     *   ["funny"] => "spam=eggs"
128
     *   ["also-funny"]=> "spam=eggs"
129
     *   [1]       => "plain arg 2"
130
     *   ["a"]     => true
131
     *   ["b"]     => true
132
     *   ["c"]     => true
133
     *   ["k"]     => "value"
134
     *   [2]       => "plain arg 3"
135
     *   ["s"]     => "overwrite"
136
     *
137
     * Not supported: `-cd=dog`.
138
     *
139
     * @author              Patrick Fisher <[email protected]>
140
     * @since               August 21, 2009
141
     * @see                 https://github.com/pwfisher/CommandLine.php
142
     * @see                 http://www.php.net/manual/en/features.commandline.php
143
     *                      #81042 function arguments($argv) by technorati at gmail dot com, 12-Feb-2008
144
     *                      #78651 function getArgs($args) by B Crawford, 22-Oct-2007
145
     * @usage               $args = CommandLine::parseArgs($_SERVER['argv']);
146
     * @param array $argv
147
     * @return array
148
     */
149
    protected static function parseArgs(array $argv=[]) : array
150
    {
151
        $argv = $argv ?: ! empty($_SERVER['argv']) ? $_SERVER['argv'] : [];
152
        array_shift($argv);
153
        $out = [];
154
        for ( $i = 0, $j = count($argv); $i < $j; $i++ )
155
        {
156
            $arg = $argv[$i];
157
            // --foo --bar=baz
158
            if ( mb_substr($arg, 0, 2) === '--' )
159
            {
160
                $eqPos = mb_strpos($arg, '=');
161
                // --foo
162
                if ($eqPos === false)
163
                {
164
                    $key = mb_substr($arg, 2);
165
                    // --foo value
166
                    if ($i + 1 < $j && $argv[$i + 1][0] !== '-')
167
                    {
168
                        $value = $argv[$i + 1];
169
                        $i++;
170
                    }
171
                    else
172
                    {
173
                        $value = isset($out[$key]) ? $out[$key] : true;
174
                    }
175
                    $out[$key] = $value;
176
                }
177
                // --bar=baz
178
                else
179
                {
180
                    $key        = mb_substr($arg, 2, $eqPos - 2);
181
                    $value      = mb_substr($arg, $eqPos + 1);
182
                    $out[$key]  = $value;
183
                }
184
            }
185
            // -k=value -abc
186
            else if (mb_substr($arg, 0, 1) === '-')
187
            {
188
                // -k=value
189
                if (mb_substr($arg, 2, 1) === '=')
190
                {
191
                    $key       = mb_substr($arg, 1, 1);
192
                    $value     = mb_substr($arg, 3);
193
                    $out[$key] = $value;
194
                }
195
                // -abc
196
                else
197
                {
198
                    $chars = str_split(mb_substr($arg, 1));
199
                    $key = '';
200
                    foreach ( $chars as $char )
201
                    {
202
                        $key       = $char;
203
                        $value     = isset($out[$key]) ? $out[$key] : true;
204
                        $out[$key] = $value;
205
                    }
206
                    // -a value1 -abc value2
207
                    if ($i + 1 < $j && $argv[$i + 1][0] !== '-')
208
                    {
209
                        $out[$key] = $argv[$i + 1];
210
                        $i++;
211
                    }
212
                }
213
            }
214
            // plain-arg
215
            else
216
            {
217
                $value = $arg;
218
                $out[] = $value;
219
            }
220
        }
221
        foreach ( $out as $idx => $val )
222
        {
223
            if ( is_string($val) && strpos($val, '|') !== false )
224
            {
225
                $out[$idx] = explode('|', $val);
226
            }
227
        }
228
229
        return $out;
230
    }
231
232
    /**
233
     * @param $name
234
     * @param mixed $default
235
     * @return string
236
     */
237
    protected static function getArg($name, $default=null)
238
    {
239
        $args = static::parseArgs();
240
241
        return isset($args[$name]) && $args[$name] ? $args[$name] : $default;
242
    }
243
244
}
245
246
TestRunner::run();