test.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. Copyright (c) 2009-2019 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. //! Reporting level
  29. $level;
  30. /**
  31. * Return test results
  32. * @return array
  33. **/
  34. function results() {
  35. return $this->data;
  36. }
  37. /**
  38. * Return FALSE if at least one test case fails
  39. * @return bool
  40. **/
  41. function passed() {
  42. return $this->passed;
  43. }
  44. /**
  45. * Evaluate condition and save test result
  46. * @return object
  47. * @param $cond bool
  48. * @param $text string
  49. **/
  50. function expect($cond,$text=NULL) {
  51. $out=(bool)$cond;
  52. if ($this->level==$out || $this->level==self::FLAG_Both) {
  53. $data=['status'=>$out,'text'=>$text,'source'=>NULL];
  54. foreach (debug_backtrace() as $frame)
  55. if (isset($frame['file'])) {
  56. $data['source']=Base::instance()->
  57. fixslashes($frame['file']).':'.$frame['line'];
  58. break;
  59. }
  60. $this->data[]=$data;
  61. }
  62. if (!$out && $this->passed)
  63. $this->passed=FALSE;
  64. return $this;
  65. }
  66. /**
  67. * Append message to test results
  68. * @return NULL
  69. * @param $text string
  70. **/
  71. function message($text) {
  72. $this->expect(TRUE,$text);
  73. }
  74. /**
  75. * Class constructor
  76. * @return NULL
  77. * @param $level int
  78. **/
  79. function __construct($level=self::FLAG_Both) {
  80. $this->level=$level;
  81. }
  82. }