1
0

bootstrap.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. $sAutoloadPath = $aPathInfo['dirname'] . '/../plugins/autoload.php';
  7. if (is_readable($sAutoloadPath)) {
  8. require_once($sAutoloadPath);
  9. if (class_exists(Dotenv\Dotenv::class))
  10. Dotenv\Dotenv::createImmutable($aPathInfo['dirname'])->safeLoad();
  11. }
  12. $sHeaderPath = $aPathInfo['dirname'] . '/../inc/header.inc.php';
  13. if (!file_exists($sHeaderPath))
  14. die("Script is not installed\n");
  15. require_once($sHeaderPath);
  16. // The global _t() is declared at the bottom of BxDolLanguages.php, so it only exists once that class
  17. // file has been loaded. A web request loads it while bootstrapping; the installer-generated header
  18. // used by CI does not, and unit tests that compare against _t() strings failed there with
  19. // "Call to undefined function _t()". Load it up front, as api.php does.
  20. bx_import('BxDolLanguages');
  21. class BxDolTestCase extends \PHPUnit\Framework\TestCase
  22. {
  23. /**
  24. * @var list<callable>|null
  25. */
  26. private $_aExceptionHandlersBackup;
  27. #[Before]
  28. protected function bxSnapshotExceptionHandlers(): void
  29. {
  30. $this->_aExceptionHandlersBackup = $this->bxActiveExceptionHandlers();
  31. }
  32. #[After]
  33. protected function bxRestoreExceptionHandlers(): void
  34. {
  35. if ($this->_aExceptionHandlersBackup === null)
  36. return;
  37. foreach ($this->bxActiveExceptionHandlers() as $mixedIgnored)
  38. restore_exception_handler();
  39. foreach ($this->_aExceptionHandlersBackup as $mixedHandler)
  40. set_exception_handler($mixedHandler);
  41. $this->_aExceptionHandlersBackup = null;
  42. }
  43. /**
  44. * @return list<callable>
  45. */
  46. private function bxActiveExceptionHandlers(): array
  47. {
  48. $aHandlers = [];
  49. while (true) {
  50. $mixedHandler = set_exception_handler(static function (): void {});
  51. restore_exception_handler();
  52. if ($mixedHandler === null)
  53. break;
  54. $aHandlers[] = $mixedHandler;
  55. restore_exception_handler();
  56. }
  57. $aHandlers = array_reverse($aHandlers);
  58. foreach ($aHandlers as $mixedHandler)
  59. set_exception_handler($mixedHandler);
  60. return $aHandlers;
  61. }
  62. protected function bxRequireAntispam()
  63. {
  64. bx_import('BxDolModule');
  65. $oModule = BxDolModule::getInstance('bx_antispam');
  66. if (!$oModule)
  67. $this->markTestSkipped('bx_antispam module is not installed.');
  68. if (!BxDolDb::getInstance()->isTableExists('bx_antispam_dnsbl_rules'))
  69. $this->markTestSkipped('bx_antispam module is not installed (table bx_antispam_dnsbl_rules is missing).');
  70. return $oModule;
  71. }
  72. protected function bxRequirePosts()
  73. {
  74. bx_import('BxDolModule');
  75. $oModule = BxDolModule::getInstance('bx_posts');
  76. if (!$oModule)
  77. $this->markTestSkipped('bx_posts module is not installed.');
  78. if (!BxDolDb::getInstance()->isTableExists('bx_posts_posts'))
  79. $this->markTestSkipped('bx_posts module is not installed (table bx_posts_posts is missing).');
  80. return $oModule;
  81. }
  82. protected function bxRequirePersons()
  83. {
  84. bx_import('BxDolModule');
  85. $oModule = BxDolModule::getInstance('bx_persons');
  86. if (!$oModule)
  87. $this->markTestSkipped('bx_persons module is not installed.');
  88. if (!BxDolDb::getInstance()->isTableExists('bx_persons_data'))
  89. $this->markTestSkipped('bx_persons module is not installed (table bx_persons_data is missing).');
  90. return $oModule;
  91. }
  92. protected function bxCallProtected($o, $sMethod, ...$aArgs)
  93. {
  94. $oMethod = new ReflectionMethod($o, $sMethod);
  95. return $oMethod->invokeArgs($o, $aArgs);
  96. }
  97. protected function bxCallProtectedArgs($o, $sMethod, array $aArgs)
  98. {
  99. $oMethod = new ReflectionMethod($o, $sMethod);
  100. return $oMethod->invokeArgs($o, $aArgs);
  101. }
  102. protected function bxGetProtected($o, $sProperty, $sClass = null)
  103. {
  104. return (new ReflectionProperty($sClass ?: $o, $sProperty))->getValue($o);
  105. }
  106. protected function bxSetProtected($o, $sProperty, $mixedValue, $sClass = null)
  107. {
  108. (new ReflectionProperty($sClass ?: $o, $sProperty))->setValue($o, $mixedValue);
  109. }
  110. protected function bxModuleInstances(BxDolModule $oModule, ?array $aReplace = null): array
  111. {
  112. $oProperty = new ReflectionProperty(BxDolModule::class, '_aInstancesStorage');
  113. $aCurrent = $oProperty->getValue($oModule) ?: [];
  114. if ($aReplace !== null)
  115. $oProperty->setValue($oModule, $aReplace);
  116. return $aCurrent;
  117. }
  118. function bxMockGet ($sClass, $aModule = array(), $bDisableContructor = false)
  119. {
  120. if ($aModule)
  121. bx_import(bx_ltrim_str($sClass, $aModule['class_prefix']), $aModule);
  122. else
  123. bx_import($sClass);
  124. if ($bDisableContructor) {
  125. $GLOBALS['bxDolClasses'][$sClass] = $this->getMockBuilder($sClass)
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. } else {
  129. $GLOBALS['bxDolClasses'][$sClass] = $this->createMock($sClass);
  130. }
  131. return $GLOBALS['bxDolClasses'][$sClass];
  132. }
  133. function bxMockFree (&$o)
  134. {
  135. if (!$o)
  136. return;
  137. // PHPUnit 12 names doubles MockObject_* / TestStub_*; older versions used Mock_*.
  138. $sClassName = preg_replace('/^(?:MockObject|TestStub|Mock)_/', '', get_class($o));
  139. $sClassName = preg_replace('/_[A-Za-z0-9]+$/', '', $sClassName);
  140. unset($GLOBALS['bxDolClasses'][$sClassName]);
  141. unset($o);
  142. }
  143. }
  144. require_once dirname(__FILE__) . '/units/modules/boonex/posts/BxPostsTestCase.php';
  145. require_once dirname(__FILE__) . '/units/modules/boonex/persons/BxPersonsTestCase.php';
  146. require_once dirname(__FILE__) . '/integration/BxDolIntegrationTestCase.php';
  147. require_once dirname(__FILE__) . '/integration/account/BxDolAccountTestCase.php';