test.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. Copyright (c) 2009-2014 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfree.sf.net).
  5. THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
  6. ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  7. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  8. PURPOSE.
  9. Please see the license.txt file for more information.
  10. */
  11. //! Unit test kit
  12. class Test {
  13. //@{ Reporting level
  14. const
  15. FLAG_False=0,
  16. FLAG_True=1,
  17. FLAG_Both=2;
  18. //@}
  19. protected
  20. //! Test results
  21. $data=array();
  22. /**
  23. * Return test results
  24. * @return array
  25. **/
  26. function results() {
  27. return $this->data;
  28. }
  29. /**
  30. * Evaluate condition and save test result
  31. * @return object
  32. * @param $cond bool
  33. * @param $text string
  34. **/
  35. function expect($cond,$text=NULL) {
  36. $out=(bool)$cond;
  37. if ($this->level==$out || $this->level==self::FLAG_Both) {
  38. $data=array('status'=>$out,'text'=>$text,'source'=>NULL);
  39. foreach (debug_backtrace() as $frame)
  40. if (isset($frame['file'])) {
  41. $data['source']=Base::instance()->
  42. fixslashes($frame['file']).':'.$frame['line'];
  43. break;
  44. }
  45. $this->data[]=$data;
  46. }
  47. return $this;
  48. }
  49. /**
  50. * Append message to test results
  51. * @return NULL
  52. * @param $text string
  53. **/
  54. function message($text) {
  55. $this->expect(TRUE,$text);
  56. }
  57. /**
  58. * Class constructor
  59. * @return NULL
  60. * @param $level int
  61. **/
  62. function __construct($level=self::FLAG_Both) {
  63. $this->level=$level;
  64. }
  65. }