bootstrap.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. use PHPUnit\Framework\Attributes\After;
  3. use PHPUnit\Framework\Attributes\Before;
  4. define('BX_SKIP_INSTALL_CHECK', 1);
  5. $aPathInfo = pathinfo(__FILE__);
  6. $sHeaderPath = $aPathInfo['dirname'] . '/../inc/header.inc.php';
  7. if (!file_exists($sHeaderPath))
  8. die("Script is not installed\n");
  9. require_once($sHeaderPath);
  10. class BxDolTestCase extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var list<callable>|null
  14. */
  15. private $_aExceptionHandlersBackup;
  16. #[Before]
  17. protected function bxSnapshotExceptionHandlers(): void
  18. {
  19. $this->_aExceptionHandlersBackup = $this->bxActiveExceptionHandlers();
  20. }
  21. #[After]
  22. protected function bxRestoreExceptionHandlers(): void
  23. {
  24. if ($this->_aExceptionHandlersBackup === null)
  25. return;
  26. foreach ($this->bxActiveExceptionHandlers() as $mixedIgnored)
  27. restore_exception_handler();
  28. foreach ($this->_aExceptionHandlersBackup as $mixedHandler)
  29. set_exception_handler($mixedHandler);
  30. $this->_aExceptionHandlersBackup = null;
  31. }
  32. /**
  33. * @return list<callable>
  34. */
  35. private function bxActiveExceptionHandlers(): array
  36. {
  37. $aHandlers = [];
  38. while (true) {
  39. $mixedHandler = set_exception_handler(static function (): void {});
  40. restore_exception_handler();
  41. if ($mixedHandler === null)
  42. break;
  43. $aHandlers[] = $mixedHandler;
  44. restore_exception_handler();
  45. }
  46. $aHandlers = array_reverse($aHandlers);
  47. foreach ($aHandlers as $mixedHandler)
  48. set_exception_handler($mixedHandler);
  49. return $aHandlers;
  50. }
  51. protected function bxRequireAntispam()
  52. {
  53. bx_import('BxDolModule');
  54. $oModule = BxDolModule::getInstance('bx_antispam');
  55. if (!$oModule)
  56. $this->markTestSkipped('bx_antispam module is not installed.');
  57. if (!BxDolDb::getInstance()->isTableExists('bx_antispam_dnsbl_rules'))
  58. $this->markTestSkipped('bx_antispam module is not installed (table bx_antispam_dnsbl_rules is missing).');
  59. return $oModule;
  60. }
  61. function bxMockGet ($sClass, $aModule = array(), $bDisableContructor = false)
  62. {
  63. if ($aModule)
  64. bx_import(bx_ltrim_str($sClass, $aModule['class_prefix']), $aModule);
  65. else
  66. bx_import($sClass);
  67. if ($bDisableContructor) {
  68. $GLOBALS['bxDolClasses'][$sClass] = $this->getMockBuilder($sClass)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. } else {
  72. $GLOBALS['bxDolClasses'][$sClass] = $this->createMock($sClass);
  73. }
  74. return $GLOBALS['bxDolClasses'][$sClass];
  75. }
  76. function bxMockFree (&$o)
  77. {
  78. if (!$o)
  79. return;
  80. // PHPUnit 12 names doubles MockObject_* / TestStub_*; older versions used Mock_*.
  81. $sClassName = preg_replace('/^(?:MockObject|TestStub|Mock)_/', '', get_class($o));
  82. $sClassName = preg_replace('/_[A-Za-z0-9]+$/', '', $sClassName);
  83. unset($GLOBALS['bxDolClasses'][$sClassName]);
  84. unset($o);
  85. }
  86. }