bootstrap.php 5.2 KB

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