| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- use PHPUnit\Framework\Attributes\After;
- use PHPUnit\Framework\Attributes\Before;
- define('BX_SKIP_INSTALL_CHECK', 1);
- $aPathInfo = pathinfo(__FILE__);
- $sAutoloadPath = $aPathInfo['dirname'] . '/../plugins/autoload.php';
- if (is_readable($sAutoloadPath)) {
- require_once($sAutoloadPath);
- if (class_exists(Dotenv\Dotenv::class))
- Dotenv\Dotenv::createImmutable($aPathInfo['dirname'])->safeLoad();
- }
- $sHeaderPath = $aPathInfo['dirname'] . '/../inc/header.inc.php';
- if (!file_exists($sHeaderPath))
- die("Script is not installed\n");
- require_once($sHeaderPath);
- // The global _t() is declared at the bottom of BxDolLanguages.php, so it only exists once that class
- // file has been loaded. A web request loads it while bootstrapping; the installer-generated header
- // used by CI does not, and unit tests that compare against _t() strings failed there with
- // "Call to undefined function _t()". Load it up front, as api.php does.
- bx_import('BxDolLanguages');
- class BxDolTestCase extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var list<callable>|null
- */
- private $_aExceptionHandlersBackup;
- #[Before]
- protected function bxSnapshotExceptionHandlers(): void
- {
- $this->_aExceptionHandlersBackup = $this->bxActiveExceptionHandlers();
- }
- #[After]
- protected function bxRestoreExceptionHandlers(): void
- {
- if ($this->_aExceptionHandlersBackup === null)
- return;
- foreach ($this->bxActiveExceptionHandlers() as $mixedIgnored)
- restore_exception_handler();
- foreach ($this->_aExceptionHandlersBackup as $mixedHandler)
- set_exception_handler($mixedHandler);
- $this->_aExceptionHandlersBackup = null;
- }
- /**
- * @return list<callable>
- */
- private function bxActiveExceptionHandlers(): array
- {
- $aHandlers = [];
- while (true) {
- $mixedHandler = set_exception_handler(static function (): void {});
- restore_exception_handler();
- if ($mixedHandler === null)
- break;
- $aHandlers[] = $mixedHandler;
- restore_exception_handler();
- }
- $aHandlers = array_reverse($aHandlers);
- foreach ($aHandlers as $mixedHandler)
- set_exception_handler($mixedHandler);
- return $aHandlers;
- }
- protected function bxRequireAntispam()
- {
- bx_import('BxDolModule');
- $oModule = BxDolModule::getInstance('bx_antispam');
- if (!$oModule)
- $this->markTestSkipped('bx_antispam module is not installed.');
- if (!BxDolDb::getInstance()->isTableExists('bx_antispam_dnsbl_rules'))
- $this->markTestSkipped('bx_antispam module is not installed (table bx_antispam_dnsbl_rules is missing).');
- return $oModule;
- }
- protected function bxRequirePosts()
- {
- bx_import('BxDolModule');
- $oModule = BxDolModule::getInstance('bx_posts');
- if (!$oModule)
- $this->markTestSkipped('bx_posts module is not installed.');
- if (!BxDolDb::getInstance()->isTableExists('bx_posts_posts'))
- $this->markTestSkipped('bx_posts module is not installed (table bx_posts_posts is missing).');
- return $oModule;
- }
- protected function bxRequirePersons()
- {
- bx_import('BxDolModule');
- $oModule = BxDolModule::getInstance('bx_persons');
- if (!$oModule)
- $this->markTestSkipped('bx_persons module is not installed.');
- if (!BxDolDb::getInstance()->isTableExists('bx_persons_data'))
- $this->markTestSkipped('bx_persons module is not installed (table bx_persons_data is missing).');
- return $oModule;
- }
- protected function bxCallProtected($o, $sMethod, ...$aArgs)
- {
- $oMethod = new ReflectionMethod($o, $sMethod);
- return $oMethod->invokeArgs($o, $aArgs);
- }
- protected function bxCallProtectedArgs($o, $sMethod, array $aArgs)
- {
- $oMethod = new ReflectionMethod($o, $sMethod);
- return $oMethod->invokeArgs($o, $aArgs);
- }
- protected function bxGetProtected($o, $sProperty, $sClass = null)
- {
- return (new ReflectionProperty($sClass ?: $o, $sProperty))->getValue($o);
- }
- protected function bxSetProtected($o, $sProperty, $mixedValue, $sClass = null)
- {
- (new ReflectionProperty($sClass ?: $o, $sProperty))->setValue($o, $mixedValue);
- }
- protected function bxModuleInstances(BxDolModule $oModule, ?array $aReplace = null): array
- {
- $oProperty = new ReflectionProperty(BxDolModule::class, '_aInstancesStorage');
- $aCurrent = $oProperty->getValue($oModule) ?: [];
- if ($aReplace !== null)
- $oProperty->setValue($oModule, $aReplace);
- return $aCurrent;
- }
- function bxMockGet ($sClass, $aModule = array(), $bDisableContructor = false)
- {
- if ($aModule)
- bx_import(bx_ltrim_str($sClass, $aModule['class_prefix']), $aModule);
- else
- bx_import($sClass);
- if ($bDisableContructor) {
- $GLOBALS['bxDolClasses'][$sClass] = $this->getMockBuilder($sClass)
- ->disableOriginalConstructor()
- ->getMock();
- } else {
- $GLOBALS['bxDolClasses'][$sClass] = $this->createMock($sClass);
- }
- return $GLOBALS['bxDolClasses'][$sClass];
- }
- function bxMockFree (&$o)
- {
- if (!$o)
- return;
- // PHPUnit 12 names doubles MockObject_* / TestStub_*; older versions used Mock_*.
- $sClassName = preg_replace('/^(?:MockObject|TestStub|Mock)_/', '', get_class($o));
- $sClassName = preg_replace('/_[A-Za-z0-9]+$/', '', $sClassName);
- unset($GLOBALS['bxDolClasses'][$sClassName]);
- unset($o);
- }
- }
- require_once dirname(__FILE__) . '/units/modules/boonex/posts/BxPostsTestCase.php';
- require_once dirname(__FILE__) . '/units/modules/boonex/persons/BxPersonsTestCase.php';
- require_once dirname(__FILE__) . '/integration/BxDolIntegrationTestCase.php';
- require_once dirname(__FILE__) . '/integration/account/BxDolAccountTestCase.php';
|