bootstrap.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. protected function bxRequirePosts()
  62. {
  63. bx_import('BxDolModule');
  64. $oModule = BxDolModule::getInstance('bx_posts');
  65. if (!$oModule)
  66. $this->markTestSkipped('bx_posts module is not installed.');
  67. if (!BxDolDb::getInstance()->isTableExists('bx_posts_posts'))
  68. $this->markTestSkipped('bx_posts module is not installed (table bx_posts_posts is missing).');
  69. return $oModule;
  70. }
  71. protected function bxCallProtected($o, $sMethod, ...$aArgs)
  72. {
  73. $oMethod = new ReflectionMethod($o, $sMethod);
  74. $oMethod->setAccessible(true);
  75. return $oMethod->invokeArgs($o, $aArgs);
  76. }
  77. protected function bxCallProtectedArgs($o, $sMethod, array $aArgs)
  78. {
  79. $oMethod = new ReflectionMethod($o, $sMethod);
  80. $oMethod->setAccessible(true);
  81. return $oMethod->invokeArgs($o, $aArgs);
  82. }
  83. protected function bxSetProtected($o, $sProperty, $mixedValue, $sClass = null)
  84. {
  85. $oProperty = new ReflectionProperty($sClass ?: $o, $sProperty);
  86. $oProperty->setAccessible(true);
  87. $oProperty->setValue($o, $mixedValue);
  88. }
  89. protected function bxModuleInstances(BxDolModule $oModule, ?array $aReplace = null): array
  90. {
  91. $oProperty = new ReflectionProperty(BxDolModule::class, '_aInstancesStorage');
  92. $oProperty->setAccessible(true);
  93. $aCurrent = $oProperty->getValue($oModule) ?: [];
  94. if ($aReplace !== null)
  95. $oProperty->setValue($oModule, $aReplace);
  96. return $aCurrent;
  97. }
  98. function bxMockGet ($sClass, $aModule = array(), $bDisableContructor = false)
  99. {
  100. if ($aModule)
  101. bx_import(bx_ltrim_str($sClass, $aModule['class_prefix']), $aModule);
  102. else
  103. bx_import($sClass);
  104. if ($bDisableContructor) {
  105. $GLOBALS['bxDolClasses'][$sClass] = $this->getMockBuilder($sClass)
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. } else {
  109. $GLOBALS['bxDolClasses'][$sClass] = $this->createMock($sClass);
  110. }
  111. return $GLOBALS['bxDolClasses'][$sClass];
  112. }
  113. function bxMockFree (&$o)
  114. {
  115. if (!$o)
  116. return;
  117. // PHPUnit 12 names doubles MockObject_* / TestStub_*; older versions used Mock_*.
  118. $sClassName = preg_replace('/^(?:MockObject|TestStub|Mock)_/', '', get_class($o));
  119. $sClassName = preg_replace('/_[A-Za-z0-9]+$/', '', $sClassName);
  120. unset($GLOBALS['bxDolClasses'][$sClassName]);
  121. unset($o);
  122. }
  123. }
  124. require_once dirname(__FILE__) . '/units/modules/boonex/posts/BxPostsTestCase.php';