summaryrefslogtreecommitdiffstats
path: root/application/third_party/test-more-php/Test-More-OO.php
blob: 7ce9f94218d53b97bffcfcaf8ef63f76477f41e5 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
/*
    Test-More-OO.php:
        A workalike of Perl's Test::More for PHP.

    Why Test-More?
        Test-More is a great way to start testing RIGHT NOW.

    Why ok and not ok?
        Test-More produces TAP compliant output.
        For more on TAP, see: http://testanything.org
        For the TAP spec, see: http://search.cpan.org/dist/TAP/TAP.pm

    Other testing libraries:
        You can replace Test-Simple with Test-More without making any changes
        to existing test code, providing access to further testing methods.
        You can also replace any other PHP Test::More workalike library out there
        with Test-More.php and it will work without making any changes to the code.

    Assertions:
        produce TAP output
        provide testing functions
        exit with error code:
            0                   all tests successful
            255                 test died or all passed but wrong # of tests run
            any other number    how many failed (including missing or extras)

    Example:
        require_once('Test-More-OO.php');
        $t = new TestMore();
        $t->plan(2);
        $t->ok(1 + 1 = 2, 'One plus one equals two');
        $t->ok( doSomethingAndReturnTrue() , 'doSomethingAndReturnTrue() successful');

    Procedural Example:
        require_once('Test-More.php');
        plan(2);
        ok(1 + 1 = 2, 'One plus one equals two');
        ok( doSomethingAndReturnTrue() , 'doSomethingAndReturnTrue() successful');

    From a browser
        If you are running Test-Simple on a web server and want slightly more web-readable
        output, call the web_output() method/function.

    Updates
        Updates will be posted to the Google code page:
        http://code.google.com/p/test-more-php/

    Bugs
        Please file bug reports via the Issues tracker at the google code page.

    Acknowledgements
        Michael G Schwern: http://search.cpan.org/~mschwern/Test-Simple/
        Chris Shiflet: http://shiflett.org/code/test-more.php

    Author
        Copyright RJ Herrick <RJHerrick@beyondlogical.net> 2009, 2010

*/

require_once('Test-Simple-OO.php');

class TestMore extends TestSimple {

/* Test-More extensions */
    private $interp;

    function pass ($name = NULL) {
        return $this->ok(TRUE, $name);
    }

    function fail ($name = NULL) {
        return $this->ok(FALSE, $name);
    }

    function _compare ($operator, $thing1, $thing2, $name = NULL) {
    // Test.php's cmp_ok function accepts coderefs, hmmm

        $result = eval("return (\$thing1 $operator \$thing2);");

        return $this->ok($result, $name);
	}

	private function dumpvar($a) {
		ob_start();
		var_dump($a);
		$ret = ob_get_clean();
		$ret = preg_replace("/^[^\n]*\n/", "", $ret);
		$ret = preg_replace("/\n$/", "", $ret);
		# replace unprintable characters with questionmarks
		$old = ini_get("mbstring.substitute_character");
		ini_set("mbstring.substitute_character", "?");
		$ret = mb_convert_encoding($ret, 'UTF-8', 'UTF-8');
		ini_set("mbstring.substitute_character", $old);
		return $ret;
	}

    function is ($thing1, $thing2, $name = NULL) {
        $pass = $this->_compare ('===',$thing1,$thing2,$name);
        if (!$pass) {
            $this->diag("         got: ".$this->dumpvar($thing1)."",
                        "    expected: ".$this->dumpvar($thing2)."");
        }
        return $pass;
    }

    function isnt ($thing1, $thing2, $name = NULL) {
        $pass = $this->_compare ('!==',$thing1,$thing2,$name);
        if (!$pass) {
            $this->diag("         got: '$thing1'",
                        "    expected: '$thing2'");
        }
        return $pass;
    }

    function like ($string, $pattern, $name = NULL) {
        $pass = preg_match($pattern, $string);

        $ok = $this->ok($pass, $name);

        if (!$ok) {
            $this->diag("                  '$string'");
            $this->diag("    doesn't match '$pattern'");
        }

        return $ok;
    }

    function unlike ($string, $pattern, $name = NULL) {
        $pass = !preg_match($pattern, $string);

        $ok = $this->ok($pass, $name);

        if (!$ok) {
            $this->diag("                  '$string'");
            $this->diag("          matches '$pattern'");
        }

        return $ok;
    }

    function cmp_ok ($thing1, $operator, $thing2, $name = NULL) {
        eval("\$pass = (\$thing1 $operator \$thing2);");

        ob_start();
        var_dump($thing1);
        $_thing1 = trim(ob_get_clean());

        ob_start();
        var_dump($thing2);
        $_thing2 = trim(ob_get_clean());

        $ok = $this->ok($pass, $name);

        if (!$ok) {
            $this->diag("         got: $_thing1");
            $this->diag("    expected: $_thing2");
        }

        return $ok;
    }

    function can_ok ($object, $methods) {
        $pass = TRUE;
        $errors = array();
        if (!is_array($methods)) $methods = array($methods);

        foreach ($methods as $method) {
            if (!method_exists($object, $method)) {
                $pass = FALSE;
                $errors[] = "    method_exists(\$object, $method) failed";
            }
        }

        $ok = $this->ok($pass, "method_exists(\$object, ...)");

        if (!$ok) {
            $this->diag($errors);
        }

        return $ok;
    }

    function isa_ok ($object, $expected_class, $object_name = 'The object') {
        $got_class = get_class($object);

        if (version_compare(phpversion(), '5', '>=')) {
            $pass = ($got_class == $expected_class);
        } else {
            $pass = ($got_class == strtolower($expected_class));
        }

        if ($pass) {
            $ok = $this->ok(TRUE, "$object_name isa $expected_class");
        } else {
            $ok = $this->ok(FALSE, "$object_name isn't a '$expected_class' it's a '$got_class'");
        }

        return $ok;
    }

    function _include_fatal_error_handler ($buffer) { 

        // Finish successfully? Carry on.
        if ($buffer === 'included OK') return '';

        $module = $this->LastModuleTested;

        // Inside ob_start, won't see the output
        $this->ok(FALSE,"include $module");

        $message = trim($buffer);
        $unrunmsg = '';

        if ( is_int($this->NumberOfTests) ) {
            $unrun = $this->NumberOfTests - (int)$this->TestsRun;
            $plural = $unrun == 1 ? '' : 's';
            $unrunmsg = "# Looks like ${unrun} planned test${plural} never ran.\n";
        }

        $gasp = $this->LastFail . "\n"
              . "#     Tried to include '$module'\n"
              . "#     $message\n"
              . $unrunmsg
              . "# Looks like 1 test aborted before it could finish due to a fatal error!\n"
              . "Bail out! Terminating prematurely due to fatal error.\n"
              ;

        return $gasp;
    }

    function _include_ok ($module,$type) {
        $path = null;
        $full_path = null;
        $retval = 999;

        // Resolve full path, nice to know although only necessary on windows
        foreach (explode(PATH_SEPARATOR,get_include_path()) as $prefix) {
            // Repeat existance test and find full path
            $full_path = realpath($prefix.DIRECTORY_SEPARATOR.$module);
            $lines = @file($full_path);
            // Stop on success
            if ($lines) {
                $path = $full_path;
                break;
            }
        }
        // Make sure, if we would include it, it's not going to choke on syntax
        $error = false;
        if ($path) {
            @exec('"'.$this->interp().'" -l '.$path, $bunk, $retval);
            if ($retval===0) {
                // Prep in case we hit error handler
                $this->Backtrace = debug_backtrace();
                $this->LastModuleTested = $module;
                ob_start(array($this,'_include_fatal_error_handler'));
                if ($type === 'include') {
                    $done = (include $module);
                } else if ($type === 'require') {
                    $done = (require $module);
                } else {
                    $this->bail("Second argument to _include_ok() must be 'require' or 'include'");
                }
                echo "included OK";
                ob_end_flush();
                if (!$done) $error = "  Unable to $type '$module'";
            } else {
                $error = "  Syntax check for '$module' failed";
            }
        } else {
            $error = "  Cannot find ${type}d file '$module'";
        }

        $pass = !$retval && $done; 
        $ok = $this->ok($pass, "$type $module" );
        if ($error) $this->diag($error);
        if ($error && $path) $this->diag("  Resolved $module as $full_path");
        return $ok;
    }

    function include_ok ($module) {
    // Test success of including file, but continue testing if possible even if unable to include

        return $this->_include_ok($module,'include');
    }


    function require_ok ($module) {
    // As include_ok() but exit gracefully if requirement missing

        $ok = $this->_include_ok($module,'require');

        // Stop testing if we fail a require test
        // Not a bail because you asked for it
        if ($ok == FALSE) {
            $this->diag("  Exiting due to missing requirement.");
            throw new RuntimeException("Missing requirement");
        }

        return $ok;
    } 

    function skip($why, $num) {

        if ($num < 0) $num = 0;

        $this->Skips += $num;
        $this->SkipReason = $why;

        return TRUE;
    }

    function eq_array ($thing1, $thing2) {
    // Deprecated comparison function provided for compatibility
    // Look only at values, order is important
        $this->diag(" ! eq_array() is a deprecated comparison function provided for compatibility. Use array_diff() or similar instead.");
        return !array_diff($thing1, $thing2);
    }

    function eq_hash ($thing1, $thing2) {
    // Deprecated comparison function provided for compatibility
    // Look at keys and values, order is NOT important
        $this->diag(" ! eq_hash() is a deprecated comparison function provided for compatibility. Use array_diff() or similar instead.");
        return !array_diff_assoc($thing1, $thing2);
    }

    function eq_set ($thing1, $thing2, $name = NULL) {
    // Deprecated comparison function provided for compatibility
    // Look only at values, duplicates are NOT important
        $this->diag(" ! eq_set() is a deprecated comparison function provided for compatibility. Use array_diff() or similar instead.");
        $a = $thing1;
        sort($a);
        $b = $thing2;
        sort($b);
        return !array_diff($a, $b);
    }

    function is_deeply ($thing1, $thing2, $name = NULL) {

        $pass = $this->_compare_deeply($thing1, $thing2, $name);

        $ok = $this->ok($pass,$name);

        if (!$ok) {
            foreach(array($thing1,$thing2) as $it){
                ob_start();
                var_dump($it);
                $dump = ob_get_clean();
                #$stringified[] = implode("\n#",explode("\n",$dump));
                $stringified[] = str_replace("\n","\n#   ",$dump);
            }
            $this->diag(" wanted:  ".$stringified[0]);
            $this->diag("    got:  ".$stringified[1]);
        }

        return $ok;
    }

    function isnt_deeply ($thing1, $thing2, $name = NULL) {

        $pass = !$this->_compare_deeply($thing1, $thing2, $name);

        $ok = $this->ok($pass,$name);

        if (!$ok) $this->diag("Structures are identical.\n");

        return $ok;
    }

    function _compare_deeply ($thing1, $thing2) {
        
        if (is_array($thing1) && is_array($thing2)) {
            if ((count($thing1) === count($thing2)) && !array_diff_key($thing1,$thing2)) {
                foreach(array_keys($thing1) as $key){
                    $pass = $this->_compare_deeply($thing1[$key],$thing2[$key]);
                    if(!$pass) {
                        return FALSE;
                    }
                }
                return TRUE;

            } else {
                return FALSE;
            }

        } else {
            return $thing1 === $thing2;
        }
    }

    function todo ($why, $howmany) {
    // Marks tests as expected to fail, then runs them anyway

        if ($howmany < 0) $howmany = 0;

        $this->Todo = $howmany;
        $this->TodoReason = $why;

        return TRUE;
    }

    function todo_skip ($why, $howmany) {
    // Marks tests as expected to fail, then skips them, as they are expected to also create fatal errors

        $this->todo($why, $howmany);
        $this->skip($why, $howmany);

        return TRUE;
    }

    function todo_start ($why) {
    // as starting a TODO block in Perl- instead of using todo() to set a number of tests, all
    // tests until todo_end are expected to fail and run anyway

        $this->TodoBlock = FALSE;
        $this->TodoReason = $why;

        return TRUE;
    }

    function todo_end () {
    // as ending a SKIP block in Perl

        $this->TodoBlock = FALSE;
        unset($this->TodoReason);

        return TRUE;
    }

    function interp ($new_interp_command=NULL) {
    // Return the command used to invoke the PHP interpreter, such as for exec()

        if ($new_interp_command == NULL && $this->interp == '') {
            // In some situations you might need to specify a php interpreter.
            if ( isset($_SERVER['PHP']) ) {
                $new_interp_command = escapeshellcmd($_SERVER['PHP']);
            } else { 
                $new_interp_command = 'php';
            }
        }
        if ($new_interp_command != $this->interp) {
            $this->interp = $new_interp_command;

            // Check that we can use the interpreter
            @exec('"'.$this->interp.'" -v', $bunk, $retval);
            if ($retval!==0) $this->bail("Unable to run PHP interpreter with '$this->interp'. Try setting the PHP environmant variable to the path of the interpreter.");
        }

        return $this->interp;
    }


}

?>