Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
    /**
3
     *	base include file for SimpleTest
4
     *	@package	SimpleTest
5
     *	@subpackage	UnitTester
6
     *	@version	$Id: scorer.php 1532 2006-12-01 12:28:55Z xue $
7
     */
8
 
9
    /**#@+*/
10
    require_once(dirname(__FILE__) . '/invoker.php');
11
    /**#@-*/
12
 
13
    /**
14
     *    Can recieve test events and display them. Display
15
     *    is achieved by making display methods available
16
     *    and visiting the incoming event.
17
	 *	  @package SimpleTest
18
	 *	  @subpackage UnitTester
19
     *    @abstract
20
     */
21
    class SimpleScorer {
22
        protected $_passes;
23
        protected $_fails;
24
        protected $_exceptions;
25
        protected $_is_dry_run;
26
 
27
        /**
28
         *    Starts the test run with no results.
29
         *    @access public
30
         */
31
        function SimpleScorer() {
32
            $this->_passes = 0;
33
            $this->_fails = 0;
34
            $this->_exceptions = 0;
35
            $this->_is_dry_run = false;
36
        }
37
 
38
        /**
39
         *    Signals that the next evaluation will be a dry
40
         *    run. That is, the structure events will be
41
         *    recorded, but no tests will be run.
42
         *    @param boolean $is_dry        Dry run if true.
43
         *    @access public
44
         */
45
        function makeDry($is_dry = true) {
46
            $this->_is_dry_run = $is_dry;
47
        }
48
 
49
        /**
50
         *    The reporter has a veto on what should be run.
51
         *    @param string $test_case_name  name of test case.
52
         *    @param string $method          Name of test method.
53
         *    @access public
54
         */
55
        function shouldInvoke($test_case_name, $method) {
56
            return ! $this->_is_dry_run;
57
        }
58
 
59
        /**
60
         *    Can wrap the invoker in preperation for running
61
         *    a test.
62
         *    @param SimpleInvoker $invoker   Individual test runner.
63
         *    @return SimpleInvoker           Wrapped test runner.
64
         *    @access public
65
         */
66
        function &createInvoker($invoker) {
67
            return $invoker;
68
        }
69
 
70
        /**
71
         *    Accessor for current status. Will be false
72
         *    if there have been any failures or exceptions.
73
         *    Used for command line tools.
74
         *    @return boolean        True if no failures.
75
         *    @access public
76
         */
77
        function getStatus() {
78
            if ($this->_exceptions + $this->_fails > 0) {
79
                return false;
80
            }
81
            return true;
82
        }
83
 
84
        /**
85
         *    Paints the start of a group test.
86
         *    @param string $test_name     Name of test or other label.
87
         *    @param integer $size         Number of test cases starting.
88
         *    @access public
89
         */
90
        function paintGroupStart($test_name, $size) {
91
        }
92
 
93
        /**
94
         *    Paints the end of a group test.
95
         *    @param string $test_name     Name of test or other label.
96
         *    @access public
97
         */
98
        function paintGroupEnd($test_name) {
99
        }
100
 
101
        /**
102
         *    Paints the start of a test case.
103
         *    @param string $test_name     Name of test or other label.
104
         *    @access public
105
         */
106
        function paintCaseStart($test_name) {
107
        }
108
 
109
        /**
110
         *    Paints the end of a test case.
111
         *    @param string $test_name     Name of test or other label.
112
         *    @access public
113
         */
114
        function paintCaseEnd($test_name) {
115
        }
116
 
117
        /**
118
         *    Paints the start of a test method.
119
         *    @param string $test_name     Name of test or other label.
120
         *    @access public
121
         */
122
        function paintMethodStart($test_name) {
123
        }
124
 
125
        /**
126
         *    Paints the end of a test method.
127
         *    @param string $test_name     Name of test or other label.
128
         *    @access public
129
         */
130
        function paintMethodEnd($test_name) {
131
        }
132
 
133
        /**
134
         *    Increments the pass count.
135
         *    @param string $message        Message is ignored.
136
         *    @access public
137
         */
138
        function paintPass($message) {
139
            $this->_passes++;
140
        }
141
 
142
        /**
143
         *    Increments the fail count.
144
         *    @param string $message        Message is ignored.
145
         *    @access public
146
         */
147
        function paintFail($message) {
148
            $this->_fails++;
149
        }
150
 
151
        /**
152
         *    Deals with PHP 4 throwing an error or PHP 5
153
         *    throwing an exception.
154
         *    @param string $message    Text of error formatted by
155
         *                              the test case.
156
         *    @access public
157
         */
158
        function paintError($message) {
159
            $this->_exceptions++;
160
        }
161
 
162
        /**
163
         *    Accessor for the number of passes so far.
164
         *    @return integer       Number of passes.
165
         *    @access public
166
         */
167
        function getPassCount() {
168
            return $this->_passes;
169
        }
170
 
171
        /**
172
         *    Accessor for the number of fails so far.
173
         *    @return integer       Number of fails.
174
         *    @access public
175
         */
176
        function getFailCount() {
177
            return $this->_fails;
178
        }
179
 
180
        /**
181
         *    Accessor for the number of untrapped errors
182
         *    so far.
183
         *    @return integer       Number of exceptions.
184
         *    @access public
185
         */
186
        function getExceptionCount() {
187
            return $this->_exceptions;
188
        }
189
 
190
        /**
191
         *    Paints a simple supplementary message.
192
         *    @param string $message        Text to display.
193
         *    @access public
194
         */
195
        function paintMessage($message) {
196
        }
197
 
198
        /**
199
         *    Paints a formatted ASCII message such as a
200
         *    variable dump.
201
         *    @param string $message        Text to display.
202
         *    @access public
203
         */
204
        function paintFormattedMessage($message) {
205
        }
206
 
207
        /**
208
         *    By default just ignores user generated events.
209
         *    @param string $type        Event type as text.
210
         *    @param mixed $payload      Message or object.
211
         *    @access public
212
         */
213
        function paintSignal($type, $payload) {
214
        }
215
    }
216
 
217
    /**
218
     *    Recipient of generated test messages that can display
219
     *    page footers and headers. Also keeps track of the
220
     *    test nesting. This is the main base class on which
221
     *    to build the finished test (page based) displays.
222
	 *	  @package SimpleTest
223
	 *	  @subpackage UnitTester
224
     */
225
    class SimpleReporter extends SimpleScorer {
226
        protected $_test_stack;
227
        protected $_size;
228
        protected $_progress;
229
 
230
        /**
231
         *    Starts the display with no results in.
232
         *    @access public
233
         */
234
        function SimpleReporter() {
235
            $this->SimpleScorer();
236
            $this->_test_stack = array();
237
            $this->_size = null;
238
            $this->_progress = 0;
239
        }
240
 
241
        /**
242
         *    Paints the start of a group test. Will also paint
243
         *    the page header and footer if this is the
244
         *    first test. Will stash the size if the first
245
         *    start.
246
         *    @param string $test_name   Name of test that is starting.
247
         *    @param integer $size       Number of test cases starting.
248
         *    @access public
249
         */
250
        function paintGroupStart($test_name, $size) {
251
            if (! isset($this->_size)) {
252
                $this->_size = $size;
253
            }
254
            if (count($this->_test_stack) == 0) {
255
                $this->paintHeader($test_name);
256
            }
257
            $this->_test_stack[] = $test_name;
258
        }
259
 
260
        /**
261
         *    Paints the end of a group test. Will paint the page
262
         *    footer if the stack of tests has unwound.
263
         *    @param string $test_name   Name of test that is ending.
264
         *    @param integer $progress   Number of test cases ending.
265
         *    @access public
266
         */
267
        function paintGroupEnd($test_name) {
268
            array_pop($this->_test_stack);
269
            if (count($this->_test_stack) == 0) {
270
                $this->paintFooter($test_name);
271
            }
272
        }
273
 
274
        /**
275
         *    Paints the start of a test case. Will also paint
276
         *    the page header and footer if this is the
277
         *    first test. Will stash the size if the first
278
         *    start.
279
         *    @param string $test_name   Name of test that is starting.
280
         *    @access public
281
         */
282
        function paintCaseStart($test_name) {
283
            if (! isset($this->_size)) {
284
                $this->_size = 1;
285
            }
286
            if (count($this->_test_stack) == 0) {
287
                $this->paintHeader($test_name);
288
            }
289
            $this->_test_stack[] = $test_name;
290
        }
291
 
292
        /**
293
         *    Paints the end of a test case. Will paint the page
294
         *    footer if the stack of tests has unwound.
295
         *    @param string $test_name   Name of test that is ending.
296
         *    @access public
297
         */
298
        function paintCaseEnd($test_name) {
299
            $this->_progress++;
300
            array_pop($this->_test_stack);
301
            if (count($this->_test_stack) == 0) {
302
                $this->paintFooter($test_name);
303
            }
304
        }
305
 
306
        /**
307
         *    Paints the start of a test method.
308
         *    @param string $test_name   Name of test that is starting.
309
         *    @access public
310
         */
311
        function paintMethodStart($test_name) {
312
            $this->_test_stack[] = $test_name;
313
        }
314
 
315
        /**
316
         *    Paints the end of a test method. Will paint the page
317
         *    footer if the stack of tests has unwound.
318
         *    @param string $test_name   Name of test that is ending.
319
         *    @access public
320
         */
321
        function paintMethodEnd($test_name) {
322
            array_pop($this->_test_stack);
323
        }
324
 
325
        /**
326
         *    Paints the test document header.
327
         *    @param string $test_name     First test top level
328
         *                                 to start.
329
         *    @access public
330
         *    @abstract
331
         */
332
        function paintHeader($test_name) {
333
        }
334
 
335
        /**
336
         *    Paints the test document footer.
337
         *    @param string $test_name        The top level test.
338
         *    @access public
339
         *    @abstract
340
         */
341
        function paintFooter($test_name) {
342
        }
343
 
344
        /**
345
         *    Accessor for internal test stack. For
346
         *    subclasses that need to see the whole test
347
         *    history for display purposes.
348
         *    @return array     List of methods in nesting order.
349
         *    @access public
350
         */
351
        function getTestList() {
352
            return $this->_test_stack;
353
        }
354
 
355
        /**
356
         *    Accessor for total test size in number
357
         *    of test cases. Null until the first
358
         *    test is started.
359
         *    @return integer   Total number of cases at start.
360
         *    @access public
361
         */
362
        function getTestCaseCount() {
363
            return $this->_size;
364
        }
365
 
366
        /**
367
         *    Accessor for the number of test cases
368
         *    completed so far.
369
         *    @return integer   Number of ended cases.
370
         *    @access public
371
         */
372
        function getTestCaseProgress() {
373
            return $this->_progress;
374
        }
375
 
376
        /**
377
         *    Static check for running in the comand line.
378
         *    @return boolean        True if CLI.
379
         *    @access public
380
         *    @static
381
         */
382
        static function inCli() {
383
            return php_sapi_name() == 'cli';
384
        }
385
    }
386
 
387
    /**
388
     *    For modifying the behaviour of the visual reporters.
389
	 *	  @package SimpleTest
390
	 *	  @subpackage UnitTester
391
     */
392
    class SimpleReporterDecorator {
393
        protected $_reporter;
394
 
395
        /**
396
         *    Mediates between teh reporter and the test case.
397
         *    @param SimpleScorer $reporter       Reporter to receive events.
398
         */
399
        function SimpleReporterDecorator($reporter) {
400
            $this->_reporter = $reporter;
401
        }
402
 
403
        /**
404
         *    Signals that the next evaluation will be a dry
405
         *    run. That is, the structure events will be
406
         *    recorded, but no tests will be run.
407
         *    @param boolean $is_dry        Dry run if true.
408
         *    @access public
409
         */
410
        function makeDry($is_dry = true) {
411
            $this->_reporter->makeDry($is_dry);
412
        }
413
 
414
        /**
415
         *    Accessor for current status. Will be false
416
         *    if there have been any failures or exceptions.
417
         *    Used for command line tools.
418
         *    @return boolean        True if no failures.
419
         *    @access public
420
         */
421
        function getStatus() {
422
            return $this->_reporter->getStatus();
423
        }
424
 
425
        /**
426
         *    The reporter has a veto on what should be run.
427
         *    @param string $test_case_name  name of test case.
428
         *    @param string $method          Name of test method.
429
         *    @return boolean                True if test should be run.
430
         *    @access public
431
         */
432
        function shouldInvoke($test_case_name, $method) {
433
            return $this->_reporter->shouldInvoke($test_case_name, $method);
434
        }
435
 
436
        /**
437
         *    Can wrap the invoker in preperation for running
438
         *    a test.
439
         *    @param SimpleInvoker $invoker   Individual test runner.
440
         *    @return SimpleInvoker           Wrapped test runner.
441
         *    @access public
442
         */
443
        function &createInvoker($invoker) {
444
            return $this->_reporter->createInvoker($invoker);
445
        }
446
 
447
        /**
448
         *    Paints the start of a group test.
449
         *    @param string $test_name     Name of test or other label.
450
         *    @param integer $size         Number of test cases starting.
451
         *    @access public
452
         */
453
        function paintGroupStart($test_name, $size) {
454
            $this->_reporter->paintGroupStart($test_name, $size);
455
        }
456
 
457
        /**
458
         *    Paints the end of a group test.
459
         *    @param string $test_name     Name of test or other label.
460
         *    @access public
461
         */
462
        function paintGroupEnd($test_name) {
463
            $this->_reporter->paintGroupEnd($test_name);
464
        }
465
 
466
        /**
467
         *    Paints the start of a test case.
468
         *    @param string $test_name     Name of test or other label.
469
         *    @access public
470
         */
471
        function paintCaseStart($test_name) {
472
            $this->_reporter->paintCaseStart($test_name);
473
        }
474
 
475
        /**
476
         *    Paints the end of a test case.
477
         *    @param string $test_name     Name of test or other label.
478
         *    @access public
479
         */
480
        function paintCaseEnd($test_name) {
481
            $this->_reporter->paintCaseEnd($test_name);
482
        }
483
 
484
        /**
485
         *    Paints the start of a test method.
486
         *    @param string $test_name     Name of test or other label.
487
         *    @access public
488
         */
489
        function paintMethodStart($test_name) {
490
            $this->_reporter->paintMethodStart($test_name);
491
        }
492
 
493
        /**
494
         *    Paints the end of a test method.
495
         *    @param string $test_name     Name of test or other label.
496
         *    @access public
497
         */
498
        function paintMethodEnd($test_name) {
499
            $this->_reporter->paintMethodEnd($test_name);
500
        }
501
 
502
        /**
503
         *    Chains to the wrapped reporter.
504
         *    @param string $message        Message is ignored.
505
         *    @access public
506
         */
507
        function paintPass($message) {
508
            $this->_reporter->paintPass($message);
509
        }
510
 
511
        /**
512
         *    Chains to the wrapped reporter.
513
         *    @param string $message        Message is ignored.
514
         *    @access public
515
         */
516
        function paintFail($message) {
517
            $this->_reporter->paintFail($message);
518
        }
519
 
520
        /**
521
         *    Chains to the wrapped reporter.
522
         *    @param string $message    Text of error formatted by
523
         *                              the test case.
524
         *    @access public
525
         */
526
        function paintError($message) {
527
            $this->_reporter->paintError($message);
528
        }
529
 
530
        /**
531
         *    Chains to the wrapped reporter.
532
         *    @param string $message        Text to display.
533
         *    @access public
534
         */
535
        function paintMessage($message) {
536
            $this->_reporter->paintMessage($message);
537
        }
538
 
539
        /**
540
         *    Chains to the wrapped reporter.
541
         *    @param string $message        Text to display.
542
         *    @access public
543
         */
544
        function paintFormattedMessage($message) {
545
            $this->_reporter->paintFormattedMessage($message);
546
        }
547
 
548
        /**
549
         *    Chains to the wrapped reporter.
550
         *    @param string $type        Event type as text.
551
         *    @param mixed $payload      Message or object.
552
         *    @return boolean            Should return false if this
553
         *                               type of signal should fail the
554
         *                               test suite.
555
         *    @access public
556
         */
557
        function paintSignal($type, $payload) {
558
            $this->_reporter->paintSignal($type, $payload);
559
        }
560
    }
561
 
562
    /**
563
     *    For sending messages to multiple reporters at
564
     *    the same time.
565
	 *	  @package SimpleTest
566
	 *	  @subpackage UnitTester
567
     */
568
    class MultipleReporter {
569
        protected $_reporters = array();
570
 
571
        /**
572
         *    Adds a reporter to the subscriber list.
573
         *    @param SimpleScorer $reporter     Reporter to receive events.
574
         *    @access public
575
         */
576
        function attachReporter($reporter) {
577
            $this->_reporters[] = $reporter;
578
        }
579
 
580
        /**
581
         *    Signals that the next evaluation will be a dry
582
         *    run. That is, the structure events will be
583
         *    recorded, but no tests will be run.
584
         *    @param boolean $is_dry        Dry run if true.
585
         *    @access public
586
         */
587
        function makeDry($is_dry = true) {
588
            for ($i = 0; $i < count($this->_reporters); $i++) {
589
                $this->_reporters[$i]->makeDry($is_dry);
590
            }
591
        }
592
 
593
        /**
594
         *    Accessor for current status. Will be false
595
         *    if there have been any failures or exceptions.
596
         *    If any reporter reports a failure, the whole
597
         *    suite fails.
598
         *    @return boolean        True if no failures.
599
         *    @access public
600
         */
601
        function getStatus() {
602
            for ($i = 0; $i < count($this->_reporters); $i++) {
603
                if (! $this->_reporters[$i]->getStatus()) {
604
                    return false;
605
                }
606
            }
607
            return true;
608
        }
609
 
610
        /**
611
         *    The reporter has a veto on what should be run.
612
         *    It requires all reporters to want to run the method.
613
         *    @param string $test_case_name  name of test case.
614
         *    @param string $method          Name of test method.
615
         *    @access public
616
         */
617
        function shouldInvoke($test_case_name, $method) {
618
            for ($i = 0; $i < count($this->_reporters); $i++) {
619
                if (! $this->_reporters[$i]->shouldInvoke($test_case_name, $method)) {
620
                    return false;
621
                }
622
            }
623
            return true;
624
        }
625
 
626
        /**
627
         *    Every reporter gets a chance to wrap the invoker.
628
         *    @param SimpleInvoker $invoker   Individual test runner.
629
         *    @return SimpleInvoker           Wrapped test runner.
630
         *    @access public
631
         */
632
        function &createInvoker($invoker) {
633
            for ($i = 0; $i < count($this->_reporters); $i++) {
634
                $invoker = $this->_reporters[$i]->createInvoker($invoker);
635
            }
636
            return $invoker;
637
        }
638
 
639
        /**
640
         *    Paints the start of a group test.
641
         *    @param string $test_name     Name of test or other label.
642
         *    @param integer $size         Number of test cases starting.
643
         *    @access public
644
         */
645
        function paintGroupStart($test_name, $size) {
646
            for ($i = 0; $i < count($this->_reporters); $i++) {
647
                $this->_reporters[$i]->paintGroupStart($test_name, $size);
648
            }
649
        }
650
 
651
        /**
652
         *    Paints the end of a group test.
653
         *    @param string $test_name     Name of test or other label.
654
         *    @access public
655
         */
656
        function paintGroupEnd($test_name) {
657
            for ($i = 0; $i < count($this->_reporters); $i++) {
658
                $this->_reporters[$i]->paintGroupEnd($test_name);
659
            }
660
        }
661
 
662
        /**
663
         *    Paints the start of a test case.
664
         *    @param string $test_name     Name of test or other label.
665
         *    @access public
666
         */
667
        function paintCaseStart($test_name) {
668
            for ($i = 0; $i < count($this->_reporters); $i++) {
669
                $this->_reporters[$i]->paintCaseStart($test_name);
670
            }
671
        }
672
 
673
        /**
674
         *    Paints the end of a test case.
675
         *    @param string $test_name     Name of test or other label.
676
         *    @access public
677
         */
678
        function paintCaseEnd($test_name) {
679
            for ($i = 0; $i < count($this->_reporters); $i++) {
680
                $this->_reporters[$i]->paintCaseEnd($test_name);
681
            }
682
        }
683
 
684
        /**
685
         *    Paints the start of a test method.
686
         *    @param string $test_name     Name of test or other label.
687
         *    @access public
688
         */
689
        function paintMethodStart($test_name) {
690
            for ($i = 0; $i < count($this->_reporters); $i++) {
691
                $this->_reporters[$i]->paintMethodStart($test_name);
692
            }
693
        }
694
 
695
        /**
696
         *    Paints the end of a test method.
697
         *    @param string $test_name     Name of test or other label.
698
         *    @access public
699
         */
700
        function paintMethodEnd($test_name) {
701
            for ($i = 0; $i < count($this->_reporters); $i++) {
702
                $this->_reporters[$i]->paintMethodEnd($test_name);
703
            }
704
        }
705
 
706
        /**
707
         *    Chains to the wrapped reporter.
708
         *    @param string $message        Message is ignored.
709
         *    @access public
710
         */
711
        function paintPass($message) {
712
            for ($i = 0; $i < count($this->_reporters); $i++) {
713
                $this->_reporters[$i]->paintPass($message);
714
            }
715
        }
716
 
717
        /**
718
         *    Chains to the wrapped reporter.
719
         *    @param string $message        Message is ignored.
720
         *    @access public
721
         */
722
        function paintFail($message) {
723
            for ($i = 0; $i < count($this->_reporters); $i++) {
724
                $this->_reporters[$i]->paintFail($message);
725
            }
726
        }
727
 
728
        /**
729
         *    Chains to the wrapped reporter.
730
         *    @param string $message    Text of error formatted by
731
         *                              the test case.
732
         *    @access public
733
         */
734
        function paintError($message) {
735
            for ($i = 0; $i < count($this->_reporters); $i++) {
736
                $this->_reporters[$i]->paintError($message);
737
            }
738
        }
739
 
740
        /**
741
         *    Chains to the wrapped reporter.
742
         *    @param string $message        Text to display.
743
         *    @access public
744
         */
745
        function paintMessage($message) {
746
            for ($i = 0; $i < count($this->_reporters); $i++) {
747
                $this->_reporters[$i]->paintMessage($message);
748
            }
749
        }
750
 
751
        /**
752
         *    Chains to the wrapped reporter.
753
         *    @param string $message        Text to display.
754
         *    @access public
755
         */
756
        function paintFormattedMessage($message) {
757
            for ($i = 0; $i < count($this->_reporters); $i++) {
758
                $this->_reporters[$i]->paintFormattedMessage($message);
759
            }
760
        }
761
 
762
        /**
763
         *    Chains to the wrapped reporter.
764
         *    @param string $type        Event type as text.
765
         *    @param mixed $payload      Message or object.
766
         *    @return boolean            Should return false if this
767
         *                               type of signal should fail the
768
         *                               test suite.
769
         *    @access public
770
         */
771
        function paintSignal($type, $payload) {
772
            for ($i = 0; $i < count($this->_reporters); $i++) {
773
                $this->_reporters[$i]->paintSignal($type, $payload);
774
            }
775
        }
776
    }
777
?>