assertions.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * @package tests
  4. * @subpackage assertions
  5. */
  6. # ============================================================================ #
  7. # ASSERTIONS #
  8. # ============================================================================ #
  9. /**
  10. * assert_true
  11. *
  12. * @param string $value
  13. * @param string $message
  14. * @return boolean
  15. */
  16. function assert_true($value, $message = '<1> should be TRUE')
  17. {
  18. test_run_assertion();
  19. return assert('$value === TRUE; //'.$message);
  20. }
  21. function assert_false($value, $message = '<1> should be FALSE')
  22. {
  23. test_run_assertion();
  24. return assert('$value === FALSE; //'.$message);
  25. }
  26. function assert_null($value, $message = '<1> should be NULL')
  27. {
  28. test_run_assertion();
  29. return assert('$value === NULL; //'.$message);
  30. }
  31. function assert_not_null($value, $message = '<1> should not be NULL')
  32. {
  33. test_run_assertion();
  34. return assert('$value !== NULL; //'.$message);
  35. }
  36. function assert_empty($value, $message = '<1> should be empty')
  37. {
  38. test_run_assertion();
  39. return assert('empty($value); //'.$message);
  40. }
  41. function assert_not_empty($value, $message = '<1> should not be empty')
  42. {
  43. test_run_assertion();
  44. return assert('!empty($value); //'.$message);
  45. }
  46. function assert_equal($expected, $value, $message = '<1> should be equal to <2>')
  47. {
  48. test_run_assertion();
  49. return assert('$expected == $value; //'.$message);
  50. }
  51. function assert_not_equal($expected, $value, $message = '<1> should not equal to <2>')
  52. {
  53. test_run_assertion();
  54. return assert('$expected != $value; //'.$message);
  55. }
  56. function assert_identical($expected, $value, $message = '<1> should be identical to <2>')
  57. {
  58. test_run_assertion();
  59. return assert('$expected === $value; //'.$message);
  60. }
  61. function assert_not_identical($expected, $value, $message = '<1> should not be identical to <2>')
  62. {
  63. test_run_assertion();
  64. return assert('$expected !== $value; //'.$message);
  65. }
  66. function assert_match($pattern, $string, $message = '<2> expected to match regular expression <1>') {
  67. test_run_assertion();
  68. return assert('preg_match($pattern, $string); //'.$message);
  69. }
  70. function assert_no_match($pattern, $string, $message = '<2> expected to not match regular expression <1>') {
  71. test_run_assertion();
  72. return assert('!preg_match($pattern, $string); //'.$message);
  73. }
  74. function assert_type($type, $value, $message = '<1> is not of type <2>') {
  75. test_run_assertion();
  76. $predicate = 'is_' . strtolower(is_string($type) ? $type : gettype($type));
  77. return assert('$predicate($value); //'.$message);
  78. }
  79. function assert_instance_of($class, $object, $message = '<2> is not an instance of class <1>') {
  80. test_run_assertion();
  81. return assert('$object instanceof $class; //'.$message);
  82. }
  83. function assert_length_of($value, $length, $message = '<1> expected to be of length <2>') {
  84. test_run_assertion();
  85. $count = is_string($value) ? 'strlen' : 'count';
  86. return assert('$count($value) == $length; //'.$message);
  87. }
  88. function assert_trigger_error($callable, $args = array(), $message = '<1> should trigger an error') {
  89. test_run_assertion();
  90. $trigger_errors = count($GLOBALS["limonade"]["test_errors"]);
  91. set_error_handler("test_error_handler");
  92. $result = call_user_func_array($callable, $args);
  93. restore_error_handler();
  94. return assert('$trigger_errors < count($GLOBALS["limonade"]["test_errors"]); //'.$message);
  95. }
  96. # TODO add web browser assertions assert_http_get, assert_http_response... as in SimpleTest (http://www.simpletest.org/en/web_tester_documentation.html)
  97. function assert_header($response, $expected_name, $expected_value = null, $message = "expected header '%s' to be equal to '%s' but received '%s: %s'")
  98. {
  99. test_run_assertion();
  100. # see assert_header in http://github.com/fnando/voodoo-test/blob/f3b0994ef138a6ba94d5e7cef6c1fb1720797a86/lib/assertions.php
  101. $headers = preg_split("/^\s*$/ms", $response);
  102. //var_dump($headers);
  103. $headers = preg_replace("/\s*$/sm", "", $headers[0]);
  104. //var_dump($headers);
  105. $regex_header = str_replace("/", "\\/", $expected_name);
  106. $regex_header = str_replace(".", "\\.", $regex_header);
  107. $header = $expected_name;
  108. # from http://www.faqs.org/rfcs/rfc2616
  109. # Field names are case-insensitive
  110. if ($expected_value) {
  111. $regex = "/^{$regex_header}:(.*?)$/ism";
  112. $header .= ": {$expected_value}";
  113. } else {
  114. $regex = "/^{$regex_header}(:.*?)?$/ism";
  115. }
  116. $has_header = preg_match($regex, $headers, $matches);
  117. $sent_header = trim((string)$matches[1]);
  118. if(empty($sent_header))
  119. {
  120. if(is_null($expected_value))
  121. {
  122. $message = "expected header '%s' but header has not been sent";
  123. }
  124. else
  125. {
  126. $message = "expected header '%s' to be equal to '%s' but header has not been sent";
  127. }
  128. $message = sprintf($message, $expected_name, $expected_value);
  129. return assert("false; //".$message);
  130. }
  131. else if($expected_value)
  132. {
  133. $message = sprintf($message, $expected_name, $expected_value, $expected_name, $sent_header);
  134. return assert('$expected_value && $sent_header == $expected_value; //'.$message);
  135. }
  136. return assert("true; //");
  137. }
  138. function assert_status($response, $expected_status, $message = "expected status code to be equal to '%s' but received '%s'")
  139. {
  140. $lines = explode('\n', trim($response));
  141. if (preg_match('/HTTP\/(\d+\.\d+)\s+(\d+)/i', $lines[0], $matches))
  142. {
  143. $status = $matches[2];
  144. return assert('$expected_status == $status; //'.sprintf($message, $expected_status, $status));
  145. }
  146. return assert("false; //no status code returned in this response string");
  147. }