summaryrefslogtreecommitdiff
path: root/modules/simpletest/expectation.php
blob: c9386907fc41708d8af856e4d0dddfdb9e1687d7 (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
<?php
// $Id$

/**
 *    Assertion that can display failure information.
 *    Also includes various helper methods.
 *    @package SimpleTest
 *    @subpackage UnitTester
 *    @abstract
 */
class SimpleExpectation {
  var $_dumper = false;
  var $_message;

  /**
   *    Creates a dumper for displaying values and sets
   *    the test message.
   *    @param string $message    Customised message on failure.
   */
  function SimpleExpectation($message = '%s') {
    $this->_message = $message;
  }

  /**
   *    Tests the expectation. True if correct.
   *    @param mixed $compare        Comparison value.
   *    @return boolean              True if correct.
   *    @access public
   *    @abstract
   */
  function test($compare) {}

  /**
   *    Returns a human readable test message.
   *    @param mixed $compare      Comparison value.
   *    @return string             Description of success
   *                               or failure.
   *    @access public
   *    @abstract
   */
  function testMessage($compare) {}

  /**
   *    Overlays the generated message onto the stored user
   *    message. An additional message can be interjected.
   *    @param mixed $compare        Comparison value.
   *    @param SimpleDumper $dumper  For formatting the results.
   *    @return string               Description of success
   *                                 or failure.
   *    @access public
   */
  function overlayMessage($compare, $dumper) {
    $this->_dumper = $dumper;
    return sprintf($this->_message, $this->testMessage($compare));
  }

  /**
   *    Accessor for the dumper.
   *    @return SimpleDumper    Current value dumper.
   *    @access protected
   */
  function &_getDumper() {
    if (!$this->_dumper) {
      $dumper = &new SimpleDumper();
      return $dumper;
    }
    return $this->_dumper;
  }

  /**
   *    Test to see if a value is an expectation object.
   *    A useful utility method.
   *    @param mixed $expectation    Hopefully an Epectation
   *                                 class.
   *    @return boolean              True if descended from
   *                                 this class.
   *    @access public
   *    @static
   */
  function isExpectation($expectation) {
    return is_object($expectation) && is_a($expectation, 'SimpleExpectation');
  }
}

/**
 *    A wildcard expectation always matches.
 *    @package SimpleTest
 *    @subpackage MockObjects
 */
class AnythingExpectation extends SimpleExpectation {

  /**
   *    Tests the expectation. Always true.
   *    @param mixed $compare  Ignored.
   *    @return boolean        True.
   *    @access public
   */
  function test($compare) {
    return true;
  }

  /**
   *    Returns a human readable test message.
   *    @param mixed $compare      Comparison value.
   *    @return string             Description of success
   *                               or failure.
   *    @access public
   */
  function testMessage($compare) {
    $dumper = &$this->_getDumper();
    return 'Anything always matches [' . $dumper->describeValue($compare) . ']';
  }
}

/**
 *    An expectation that passes on boolean true.
 *    @package SimpleTest
 *    @subpackage MockObjects
 */
class TrueExpectation extends SimpleExpectation {

  /**
   *    Tests the expectation.
   *    @param mixed $compare  Should be true.
   *    @return boolean        True on match.
   *    @access public
   */
  function test($compare) {
    return (boolean)$compare;
  }

  /**
   *    Returns a human readable test message.
   *    @param mixed $compare      Comparison value.
   *    @return string             Description of success
   *                               or failure.
   *    @access public
   */
  function testMessage($compare) {
    $dumper = &$this->_getDumper();
    return 'Expected true, got [' . $dumper->describeValue($compare) . ']';
  }
}