bootstrap.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. class BxDolTestCase extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var list<callable>|null
  20. */
  21. private $_aExceptionHandlersBackup;
  22. #[Before]
  23. protected function bxSnapshotExceptionHandlers(): void
  24. {
  25. $this->_aExceptionHandlersBackup = $this->bxActiveExceptionHandlers();
  26. }
  27. #[After]
  28. protected function bxRestoreExceptionHandlers(): void
  29. {
  30. if ($this->_aExceptionHandlersBackup === null)
  31. return;
  32. foreach ($this->bxActiveExceptionHandlers() as $mixedIgnored)
  33. restore_exception_handler();
  34. foreach ($this->_aExceptionHandlersBackup as $mixedHandler)
  35. set_exception_handler($mixedHandler);
  36. $this->_aExceptionHandlersBackup = null;
  37. }
  38. /**
  39. * @return list<callable>
  40. */
  41. private function bxActiveExceptionHandlers(): array
  42. {
  43. $aHandlers = [];
  44. while (true) {
  45. $mixedHandler = set_exception_handler(static function (): void {});
  46. restore_exception_handler();
  47. if ($mixedHandler === null)
  48. break;
  49. $aHandlers[] = $mixedHandler;
  50. restore_exception_handler();
  51. }
  52. $aHandlers = array_reverse($aHandlers);
  53. foreach ($aHandlers as $mixedHandler)
  54. set_exception_handler($mixedHandler);
  55. return $aHandlers;
  56. }
  57. protected function bxRequireAntispam()
  58. {
  59. bx_import('BxDolModule');
  60. $oModule = BxDolModule::getInstance('bx_antispam');
  61. if (!$oModule)
  62. $this->markTestSkipped('bx_antispam module is not installed.');
  63. if (!BxDolDb::getInstance()->isTableExists('bx_antispam_dnsbl_rules'))
  64. $this->markTestSkipped('bx_antispam module is not installed (table bx_antispam_dnsbl_rules is missing).');
  65. return $oModule;
  66. }
  67. protected function bxRequirePosts()
  68. {
  69. bx_import('BxDolModule');
  70. $oModule = BxDolModule::getInstance('bx_posts');
  71. if (!$oModule)
  72. $this->markTestSkipped('bx_posts module is not installed.');
  73. if (!BxDolDb::getInstance()->isTableExists('bx_posts_posts'))
  74. $this->markTestSkipped('bx_posts module is not installed (table bx_posts_posts is missing).');
  75. return $oModule;
  76. }
  77. protected function bxRequirePersons()
  78. {
  79. bx_import('BxDolModule');
  80. $oModule = BxDolModule::getInstance('bx_persons');
  81. if (!$oModule)
  82. $this->markTestSkipped('bx_persons module is not installed.');
  83. if (!BxDolDb::getInstance()->isTableExists('bx_persons_data'))
  84. $this->markTestSkipped('bx_persons module is not installed (table bx_persons_data is missing).');
  85. return $oModule;
  86. }
  87. protected function bxCallProtected($o, $sMethod, ...$aArgs)
  88. {
  89. $oMethod = new ReflectionMethod($o, $sMethod);
  90. return $oMethod->invokeArgs($o, $aArgs);
  91. }
  92. protected function bxCallProtectedArgs($o, $sMethod, array $aArgs)
  93. {
  94. $oMethod = new ReflectionMethod($o, $sMethod);
  95. return $oMethod->invokeArgs($o, $aArgs);
  96. }
  97. protected function bxGetProtected($o, $sProperty, $sClass = null)
  98. {
  99. return (new ReflectionProperty($sClass ?: $o, $sProperty))->getValue($o);
  100. }
  101. protected function bxSetProtected($o, $sProperty, $mixedValue, $sClass = null)
  102. {
  103. (new ReflectionProperty($sClass ?: $o, $sProperty))->setValue($o, $mixedValue);
  104. }
  105. protected function bxModuleInstances(BxDolModule $oModule, ?array $aReplace = null): array
  106. {
  107. $oProperty = new ReflectionProperty(BxDolModule::class, '_aInstancesStorage');
  108. $aCurrent = $oProperty->getValue($oModule) ?: [];
  109. if ($aReplace !== null)
  110. $oProperty->setValue($oModule, $aReplace);
  111. return $aCurrent;
  112. }
  113. function bxMockGet ($sClass, $aModule = array(), $bDisableContructor = false)
  114. {
  115. if ($aModule)
  116. bx_import(bx_ltrim_str($sClass, $aModule['class_prefix']), $aModule);
  117. else
  118. bx_import($sClass);
  119. if ($bDisableContructor) {
  120. $GLOBALS['bxDolClasses'][$sClass] = $this->getMockBuilder($sClass)
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. } else {
  124. $GLOBALS['bxDolClasses'][$sClass] = $this->createMock($sClass);
  125. }
  126. return $GLOBALS['bxDolClasses'][$sClass];
  127. }
  128. function bxMockFree (&$o)
  129. {
  130. if (!$o)
  131. return;
  132. // PHPUnit 12 names doubles MockObject_* / TestStub_*; older versions used Mock_*.
  133. $sClassName = preg_replace('/^(?:MockObject|TestStub|Mock)_/', '', get_class($o));
  134. $sClassName = preg_replace('/_[A-Za-z0-9]+$/', '', $sClassName);
  135. unset($GLOBALS['bxDolClasses'][$sClassName]);
  136. unset($o);
  137. }
  138. }
  139. require_once dirname(__FILE__) . '/units/modules/boonex/posts/BxPostsTestCase.php';
  140. require_once dirname(__FILE__) . '/units/modules/boonex/persons/BxPersonsTestCase.php';
  141. require_once dirname(__FILE__) . '/integration/BxDolIntegrationTestCase.php';