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: expectation.php 1532 2006-12-01 12:28:55Z xue $
7
     */
8
 
9
    /**#@+
10
     *    include other SimpleTest class files
11
     */
12
    require_once(dirname(__FILE__) . '/dumper.php');
13
    require_once(dirname(__FILE__) . '/compatibility.php');
14
    /**#@-*/
15
 
16
    /**
17
     *    Assertion that can display failure information.
18
     *    Also includes various helper methods.
19
     *    @package SimpleTest
20
     *    @subpackage UnitTester
21
     *    @abstract
22
     */
23
    class SimpleExpectation {
24
        protected $_dumper;
25
        protected $_message;
26
 
27
        /**
28
         *    Creates a dumper for displaying values and sets
29
         *    the test message.
30
         *    @param string $message    Customised message on failure.
31
         */
32
        function SimpleExpectation($message = '%s') {
33
            $this->_dumper = new SimpleDumper();
34
            $this->_message = $message;
35
        }
36
 
37
        /**
38
         *    Tests the expectation. True if correct.
39
         *    @param mixed $compare        Comparison value.
40
         *    @return boolean              True if correct.
41
         *    @access public
42
         *    @abstract
43
         */
44
        function test($compare) {
45
        }
46
 
47
        /**
48
         *    Returns a human readable test message.
49
         *    @param mixed $compare      Comparison value.
50
         *    @return string             Description of success
51
         *                               or failure.
52
         *    @access public
53
         *    @abstract
54
         */
55
        function testMessage($compare) {
56
        }
57
 
58
        /**
59
         *    Overlays the generated message onto the stored user
60
         *    message. An additional message can be interjected.
61
         *    @param mixed $compare      Comparison value.
62
         *    @return string             Description of success
63
         *                               or failure.
64
         *    @access public
65
         */
66
        function overlayMessage($compare) {
67
            return sprintf($this->_message, $this->testMessage($compare));
68
        }
69
 
70
        /**
71
         *    Accessor for the dumper.
72
         *    @return SimpleDumper    Current value dumper.
73
         *    @access protected
74
         */
75
        function &_getDumper() {
76
            return $this->_dumper;
77
        }
78
 
79
        /**
80
         *    Test to see if a value is an expectation object.
81
         *    A useful utility method.
82
         *    @param mixed $expectation    Hopefully an Epectation
83
         *                                 class.
84
         *    @return boolean              True if descended from
85
         *                                 this class.
86
         *    @access public
87
         *    @static
88
         */
89
        static function isExpectation($expectation) {
90
            return is_object($expectation) &&
91
                    SimpleTestCompatibility::isA($expectation, 'SimpleExpectation');
92
        }
93
    }
94
 
95
    /**
96
     *    Test for equality.
97
     *      @package SimpleTest
98
     *      @subpackage UnitTester
99
     */
100
    class EqualExpectation extends SimpleExpectation {
101
        protected $_value;
102
 
103
        /**
104
         *    Sets the value to compare against.
105
         *    @param mixed $value        Test value to match.
106
         *    @param string $message     Customised message on failure.
107
         *    @access public
108
         */
109
        function EqualExpectation($value, $message = '%s') {
110
            $this->SimpleExpectation($message);
111
            $this->_value = $value;
112
        }
113
 
114
        /**
115
         *    Tests the expectation. True if it matches the
116
         *    held value.
117
         *    @param mixed $compare        Comparison value.
118
         *    @return boolean              True if correct.
119
         *    @access public
120
         */
121
        function test($compare) {
122
            return (($this->_value == $compare) && ($compare == $this->_value));
123
        }
124
 
125
        /**
126
         *    Returns a human readable test message.
127
         *    @param mixed $compare      Comparison value.
128
         *    @return string             Description of success
129
         *                               or failure.
130
         *    @access public
131
         */
132
        function testMessage($compare) {
133
            if ($this->test($compare)) {
134
                return "Equal expectation [" . $this->_dumper->describeValue($this->_value) . "]";
135
            } else {
136
                return "Equal expectation fails " .
137
                        $this->_dumper->describeDifference($this->_value, $compare);
138
            }
139
        }
140
 
141
        /**
142
         *    Accessor for comparison value.
143
         *    @return mixed       Held value to compare with.
144
         *    @access protected
145
         */
146
        function _getValue() {
147
            return $this->_value;
148
        }
149
    }
150
 
151
    /**
152
     *    Test for inequality.
153
     *      @package SimpleTest
154
     *      @subpackage UnitTester
155
     */
156
    class NotEqualExpectation extends EqualExpectation {
157
 
158
        /**
159
         *    Sets the value to compare against.
160
         *    @param mixed $value       Test value to match.
161
         *    @param string $message    Customised message on failure.
162
         *    @access public
163
         */
164
        function NotEqualExpectation($value, $message = '%s') {
165
            $this->EqualExpectation($value, $message);
166
        }
167
 
168
        /**
169
         *    Tests the expectation. True if it differs from the
170
         *    held value.
171
         *    @param mixed $compare        Comparison value.
172
         *    @return boolean              True if correct.
173
         *    @access public
174
         */
175
        function test($compare) {
176
            return ! parent::test($compare);
177
        }
178
 
179
        /**
180
         *    Returns a human readable test message.
181
         *    @param mixed $compare      Comparison value.
182
         *    @return string             Description of success
183
         *                               or failure.
184
         *    @access public
185
         */
186
        function testMessage($compare) {
187
            $dumper = $this->_getDumper();
188
            if ($this->test($compare)) {
189
                return "Not equal expectation passes " .
190
                        $dumper->describeDifference($this->_getValue(), $compare);
191
            } else {
192
                return "Not equal expectation fails [" .
193
                        $dumper->describeValue($this->_getValue()) .
194
                        "] matches";
195
            }
196
        }
197
    }
198
 
199
    /**
200
     *    Test for being within a range.
201
     *      @package SimpleTest
202
     *      @subpackage UnitTester
203
     */
204
    class WithinMarginExpectation extends SimpleExpectation {
205
        protected $_upper;
206
        protected $_lower;
207
 
208
        /**
209
         *    Sets the value to compare against and the fuzziness of
210
         *    the match. Used for comparing floating point values.
211
         *    @param mixed $value        Test value to match.
212
         *    @param mixed $margin       Fuzziness of match.
213
         *    @param string $message     Customised message on failure.
214
         *    @access public
215
         */
216
        function WithinMarginExpectation($value, $margin, $message = '%s') {
217
            $this->SimpleExpectation($message);
218
            $this->_upper = $value + $margin;
219
            $this->_lower = $value - $margin;
220
        }
221
 
222
        /**
223
         *    Tests the expectation. True if it matches the
224
         *    held value.
225
         *    @param mixed $compare        Comparison value.
226
         *    @return boolean              True if correct.
227
         *    @access public
228
         */
229
        function test($compare) {
230
            return (($compare <= $this->_upper) && ($compare >= $this->_lower));
231
        }
232
 
233
        /**
234
         *    Returns a human readable test message.
235
         *    @param mixed $compare      Comparison value.
236
         *    @return string             Description of success
237
         *                               or failure.
238
         *    @access public
239
         */
240
        function testMessage($compare) {
241
            if ($this->test($compare)) {
242
                return $this->_withinMessage($compare);
243
            } else {
244
                return $this->_outsideMessage($compare);
245
            }
246
        }
247
 
248
        /**
249
         *    Creates a the message for being within the range.
250
         *    @param mixed $compare        Value being tested.
251
         *    @access private
252
         */
253
        function _withinMessage($compare) {
254
            return "Within expectation [" . $this->_dumper->describeValue($this->_lower) . "] and [" .
255
                    $this->_dumper->describeValue($this->_upper) . "]";
256
        }
257
 
258
        /**
259
         *    Creates a the message for being within the range.
260
         *    @param mixed $compare        Value being tested.
261
         *    @access private
262
         */
263
        function _outsideMessage($compare) {
264
            if ($compare > $this->_upper) {
265
                return "Outside expectation " .
266
                        $this->_dumper->describeDifference($compare, $this->_upper);
267
            } else {
268
                return "Outside expectation " .
269
                        $this->_dumper->describeDifference($compare, $this->_lower);
270
            }
271
        }
272
    }
273
 
274
    /**
275
     *    Test for being outside of a range.
276
     *      @package SimpleTest
277
     *      @subpackage UnitTester
278
     */
279
    class OutsideMarginExpectation extends WithinMarginExpectation {
280
 
281
        /**
282
         *    Sets the value to compare against and the fuzziness of
283
         *    the match. Used for comparing floating point values.
284
         *    @param mixed $value        Test value to not match.
285
         *    @param mixed $margin       Fuzziness of match.
286
         *    @param string $message     Customised message on failure.
287
         *    @access public
288
         */
289
        function OutsideMarginExpectation($value, $margin, $message = '%s') {
290
            $this->WithinMarginExpectation($value, $margin, $message);
291
        }
292
 
293
        /**
294
         *    Tests the expectation. True if it matches the
295
         *    held value.
296
         *    @param mixed $compare        Comparison value.
297
         *    @return boolean              True if correct.
298
         *    @access public
299
         */
300
        function test($compare) {
301
            return ! parent::test($compare);
302
        }
303
 
304
        /**
305
         *    Returns a human readable test message.
306
         *    @param mixed $compare      Comparison value.
307
         *    @return string             Description of success
308
         *                               or failure.
309
         *    @access public
310
         */
311
        function testMessage($compare) {
312
            if (! $this->test($compare)) {
313
                return $this->_withinMessage($compare);
314
            } else {
315
                return $this->_outsideMessage($compare);
316
            }
317
        }
318
    }
319
 
320
    /**
321
     *    Test for identity.
322
     *    @package SimpleTest
323
     *    @subpackage UnitTester
324
     */
325
    class IdenticalExpectation extends EqualExpectation {
326
 
327
        /**
328
         *    Sets the value to compare against.
329
         *    @param mixed $value       Test value to match.
330
         *    @param string $message    Customised message on failure.
331
         *    @access public
332
         */
333
        function IdenticalExpectation($value, $message = '%s') {
334
            $this->EqualExpectation($value, $message);
335
        }
336
 
337
        /**
338
         *    Tests the expectation. True if it exactly
339
         *    matches the held value.
340
         *    @param mixed $compare        Comparison value.
341
         *    @return boolean              True if correct.
342
         *    @access public
343
         */
344
        function test($compare) {
345
            return SimpleTestCompatibility::isIdentical($this->_getValue(), $compare);
346
        }
347
 
348
        /**
349
         *    Returns a human readable test message.
350
         *    @param mixed $compare      Comparison value.
351
         *    @return string             Description of success
352
         *                               or failure.
353
         *    @access public
354
         */
355
        function testMessage($compare) {
356
            $dumper = $this->_getDumper();
357
            if ($this->test($compare)) {
358
                return "Identical expectation [" . $dumper->describeValue($this->_getValue()) . "]";
359
            } else {
360
                return "Identical expectation [" . $dumper->describeValue($this->_getValue()) .
361
                        "] fails with [" .
362
                        $dumper->describeValue($compare) . "] " .
363
                        $dumper->describeDifference($this->_getValue(), $compare, TYPE_MATTERS);
364
            }
365
        }
366
    }
367
 
368
    /**
369
     *    Test for non-identity.
370
     *    @package SimpleTest
371
     *    @subpackage UnitTester
372
     */
373
    class NotIdenticalExpectation extends IdenticalExpectation {
374
 
375
        /**
376
         *    Sets the value to compare against.
377
         *    @param mixed $value        Test value to match.
378
         *    @param string $message     Customised message on failure.
379
         *    @access public
380
         */
381
        function NotIdenticalExpectation($value, $message = '%s') {
382
            $this->IdenticalExpectation($value, $message);
383
        }
384
 
385
        /**
386
         *    Tests the expectation. True if it differs from the
387
         *    held value.
388
         *    @param mixed $compare        Comparison value.
389
         *    @return boolean              True if correct.
390
         *    @access public
391
         */
392
        function test($compare) {
393
            return ! parent::test($compare);
394
        }
395
 
396
        /**
397
         *    Returns a human readable test message.
398
         *    @param mixed $compare      Comparison value.
399
         *    @return string             Description of success
400
         *                               or failure.
401
         *    @access public
402
         */
403
        function testMessage($compare) {
404
            $dumper = $this->_getDumper();
405
            if ($this->test($compare)) {
406
                return "Not identical expectation passes " .
407
                        $dumper->describeDifference($this->_getValue(), $compare, TYPE_MATTERS);
408
            } else {
409
                return "Not identical expectation [" . $dumper->describeValue($this->_getValue()) . "] matches";
410
            }
411
        }
412
    }
413
 
414
    /**
415
     *    Test for a pattern using Perl regex rules.
416
     *    @package SimpleTest
417
     *    @subpackage UnitTester
418
     */
419
    class PatternExpectation extends SimpleExpectation {
420
        protected $_pattern;
421
 
422
        /**
423
         *    Sets the value to compare against.
424
         *    @param string $pattern    Pattern to search for.
425
         *    @param string $message    Customised message on failure.
426
         *    @access public
427
         */
428
        function PatternExpectation($pattern, $message = '%s') {
429
            $this->SimpleExpectation($message);
430
            $this->_pattern = $pattern;
431
        }
432
 
433
        /**
434
         *    Accessor for the pattern.
435
         *    @return string       Perl regex as string.
436
         *    @access protected
437
         */
438
        function _getPattern() {
439
            return $this->_pattern;
440
        }
441
 
442
        /**
443
         *    Tests the expectation. True if the Perl regex
444
         *    matches the comparison value.
445
         *    @param string $compare        Comparison value.
446
         *    @return boolean               True if correct.
447
         *    @access public
448
         */
449
        function test($compare) {
450
            return (boolean)preg_match($this->_getPattern(), $compare);
451
        }
452
 
453
        /**
454
         *    Returns a human readable test message.
455
         *    @param mixed $compare      Comparison value.
456
         *    @return string             Description of success
457
         *                               or failure.
458
         *    @access public
459
         */
460
        function testMessage($compare) {
461
            if ($this->test($compare)) {
462
                return $this->_describePatternMatch($this->_getPattern(), $compare);
463
            } else {
464
                $dumper = $this->_getDumper();
465
                return "Pattern [" . $this->_getPattern() .
466
                        "] not detected in [" .
467
                        $dumper->describeValue($compare) . "]";
468
            }
469
        }
470
 
471
        /**
472
         *    Describes a pattern match including the string
473
         *    found and it's position.
474
     *    @package SimpleTest
475
     *    @subpackage UnitTester
476
         *    @param string $pattern        Regex to match against.
477
         *    @param string $subject        Subject to search.
478
         *    @access protected
479
         */
480
        function _describePatternMatch($pattern, $subject) {
481
            preg_match($pattern, $subject, $matches);
482
            $position = strpos($subject, $matches[0]);
483
            $dumper = $this->_getDumper();
484
            return "Pattern [$pattern] detected at character [$position] in [" .
485
                    $dumper->describeValue($subject) . "] as [" .
486
                    $matches[0] . "] in region [" .
487
                    $dumper->clipString($subject, 100, $position) . "]";
488
        }
489
    }
490
 
491
    /**
492
     *      @deprecated
493
     */
494
    class WantedPatternExpectation extends PatternExpectation {
495
    }
496
 
497
    /**
498
     *    Fail if a pattern is detected within the
499
     *    comparison.
500
     *      @package SimpleTest
501
     *      @subpackage UnitTester
502
     */
503
    class NoPatternExpectation extends PatternExpectation {
504
 
505
        /**
506
         *    Sets the reject pattern
507
         *    @param string $pattern    Pattern to search for.
508
         *    @param string $message    Customised message on failure.
509
         *    @access public
510
         */
511
        function NoPatternExpectation($pattern, $message = '%s') {
512
            $this->PatternExpectation($pattern, $message);
513
        }
514
 
515
        /**
516
         *    Tests the expectation. False if the Perl regex
517
         *    matches the comparison value.
518
         *    @param string $compare        Comparison value.
519
         *    @return boolean               True if correct.
520
         *    @access public
521
         */
522
        function test($compare) {
523
            return ! parent::test($compare);
524
        }
525
 
526
        /**
527
         *    Returns a human readable test message.
528
         *    @param string $compare      Comparison value.
529
         *    @return string              Description of success
530
         *                                or failure.
531
         *    @access public
532
         */
533
        function testMessage($compare) {
534
            if ($this->test($compare)) {
535
                $dumper = $this->_getDumper();
536
                return "Pattern [" . $this->_getPattern() .
537
                        "] not detected in [" .
538
                        $dumper->describeValue($compare) . "]";
539
            } else {
540
                return $this->_describePatternMatch($this->_getPattern(), $compare);
541
            }
542
        }
543
    }
544
 
545
    /**
546
     *    @package SimpleTest
547
     *    @subpackage UnitTester
548
     *      @deprecated
549
     */
550
    class UnwantedPatternExpectation extends NoPatternExpectation {
551
    }
552
 
553
    /**
554
     *    Tests either type or class name if it's an object.
555
     *      @package SimpleTest
556
     *      @subpackage UnitTester
557
     */
558
    class IsAExpectation extends SimpleExpectation {
559
        protected $_type;
560
 
561
        /**
562
         *    Sets the type to compare with.
563
         *    @param string $type       Type or class name.
564
         *    @param string $message    Customised message on failure.
565
         *    @access public
566
         */
567
        function IsAExpectation($type, $message = '%s') {
568
            $this->SimpleExpectation($message);
569
            $this->_type = $type;
570
        }
571
 
572
        /**
573
         *    Accessor for type to check against.
574
         *    @return string    Type or class name.
575
         *    @access protected
576
         */
577
        function _getType() {
578
            return $this->_type;
579
        }
580
 
581
        /**
582
         *    Tests the expectation. True if the type or
583
         *    class matches the string value.
584
         *    @param string $compare        Comparison value.
585
         *    @return boolean               True if correct.
586
         *    @access public
587
         */
588
        function test($compare) {
589
            if (is_object($compare)) {
590
                return SimpleTestCompatibility::isA($compare, $this->_type);
591
            } else {
592
                return (strtolower(gettype($compare)) == $this->_canonicalType($this->_type));
593
            }
594
        }
595
 
596
        /**
597
         *    Coerces type name into a gettype() match.
598
         *    @param string $type        User type.
599
         *    @return string             Simpler type.
600
         *    @access private
601
         */
602
        function _canonicalType($type) {
603
            $type = strtolower($type);
604
            $map = array(
605
                    'bool' => 'boolean',
606
                    'float' => 'double',
607
                    'real' => 'double',
608
                    'int' => 'integer');
609
            if (isset($map[$type])) {
610
                $type = $map[$type];
611
            }
612
            return $type;
613
        }
614
 
615
        /**
616
         *    Returns a human readable test message.
617
         *    @param mixed $compare      Comparison value.
618
         *    @return string             Description of success
619
         *                               or failure.
620
         *    @access public
621
         */
622
        function testMessage($compare) {
623
            $dumper = $this->_getDumper();
624
            return "Value [" . $dumper->describeValue($compare) .
625
                    "] should be type [" . $this->_type . "]";
626
        }
627
    }
628
 
629
    /**
630
     *    Tests either type or class name if it's an object.
631
     *    Will succeed if the type does not match.
632
     *      @package SimpleTest
633
     *      @subpackage UnitTester
634
     */
635
    class NotAExpectation extends IsAExpectation {
636
        protected $_type;
637
 
638
        /**
639
         *    Sets the type to compare with.
640
         *    @param string $type       Type or class name.
641
         *    @param string $message    Customised message on failure.
642
         *    @access public
643
         */
644
        function NotAExpectation($type, $message = '%s') {
645
            $this->IsAExpectation($type, $message);
646
        }
647
 
648
        /**
649
         *    Tests the expectation. False if the type or
650
         *    class matches the string value.
651
         *    @param string $compare        Comparison value.
652
         *    @return boolean               True if different.
653
         *    @access public
654
         */
655
        function test($compare) {
656
            return ! parent::test($compare);
657
        }
658
 
659
        /**
660
         *    Returns a human readable test message.
661
         *    @param mixed $compare      Comparison value.
662
         *    @return string             Description of success
663
         *                               or failure.
664
         *    @access public
665
         */
666
        function testMessage($compare) {
667
            $dumper = $this->_getDumper();
668
            return "Value [" . $dumper->describeValue($compare) .
669
                    "] should not be type [" . $this->_getType() . "]";
670
        }
671
    }
672
 
673
    /**
674
     *    Tests for existance of a method in an object
675
     *      @package SimpleTest
676
     *      @subpackage UnitTester
677
     */
678
    class MethodExistsExpectation extends SimpleExpectation {
679
        protected $_method;
680
 
681
        /**
682
         *    Sets the value to compare against.
683
         *    @param string $method     Method to check.
684
         *    @param string $message    Customised message on failure.
685
         *    @access public
686
         *    @return void
687
         */
688
        function MethodExistsExpectation($method, $message = '%s') {
689
            $this->SimpleExpectation($message);
690
            $this->_method = $method;
691
        }
692
 
693
        /**
694
         *    Tests the expectation. True if the method exists in the test object.
695
         *    @param string $compare        Comparison method name.
696
         *    @return boolean               True if correct.
697
         *    @access public
698
         */
699
        function test($compare) {
700
            return (boolean)(is_object($compare) && method_exists($compare, $this->_method));
701
        }
702
 
703
        /**
704
         *    Returns a human readable test message.
705
         *    @param mixed $compare      Comparison value.
706
         *    @return string             Description of success
707
         *                               or failure.
708
         *    @access public
709
         */
710
        function testMessage($compare) {
711
            $dumper = $this->_getDumper();
712
            if (! is_object($compare)) {
713
                return 'No method on non-object [' . $dumper->describeValue($compare) . ']';
714
            }
715
            $method = $this->_method;
716
            return "Object [" . $dumper->describeValue($compare) .
717
                    "] should contain method [$method]";
718
        }
719
    }
720
?>