test.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. Copyright (c) 2009-2017 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. //! Unit test kit
  16. class Test {
  17. //@{ Reporting level
  18. const
  19. FLAG_False=0,
  20. FLAG_True=1,
  21. FLAG_Both=2;
  22. //@}
  23. protected
  24. //! Test results
  25. $data=[],
  26. //! Success indicator
  27. $passed=TRUE;
  28. /**
  29. * Return test results
  30. * @return array
  31. **/
  32. function results() {
  33. return $this->data;
  34. }
  35. /**
  36. * Return FALSE if at least one test case fails
  37. * @return bool
  38. **/
  39. function passed() {
  40. return $this->passed;
  41. }
  42. /**
  43. * Evaluate condition and save test result
  44. * @return object
  45. * @param $cond bool
  46. * @param $text string
  47. **/
  48. function expect($cond,$text=NULL) {
  49. $out=(bool)$cond;
  50. if ($this->level==$out || $this->level==self::FLAG_Both) {
  51. $data=['status'=>$out,'text'=>$text,'source'=>NULL];
  52. foreach (debug_backtrace() as $frame)
  53. if (isset($frame['file'])) {
  54. $data['source']=Base::instance()->
  55. fixslashes($frame['file']).':'.$frame['line'];
  56. break;
  57. }
  58. $this->data[]=$data;
  59. }
  60. if (!$out && $this->passed)
  61. $this->passed=FALSE;
  62. return $this;
  63. }
  64. /**
  65. * Append message to test results
  66. * @return NULL
  67. * @param $text string
  68. **/
  69. function message($text) {
  70. $this->expect(TRUE,$text);
  71. }
  72. /**
  73. * Class constructor
  74. * @return NULL
  75. * @param $level int
  76. **/
  77. function __construct($level=self::FLAG_Both) {
  78. $this->level=$level;
  79. }
  80. }