BxBaseConnectionTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. use PHPUnit\Framework\Attributes\DataProvider;
  3. /**
  4. * Connection objects must not create dynamic properties: on PHP 8.2+ that is an
  5. * E_DEPRECATED notice, and with display_errors on it is printed into API responses.
  6. */
  7. class BxBaseConnectionTest extends BxDolTestCase
  8. {
  9. #[DataProvider('providerForConnectionObjects')]
  10. public function testGetElementApiCreatesNoDynamicProperty($sObject, $sModule)
  11. {
  12. if ($sModule && !BxDolModuleQuery::getInstance()->isEnabledByName($sModule))
  13. $this->markTestSkipped($sModule . ' module is not installed.');
  14. // getObjectInstance caches the object; a cached one may already hold the property.
  15. $sKey = 'BxTemplConnection!' . $sObject;
  16. $mixedCached = $GLOBALS['bxDolClasses'][$sKey] ?? null;
  17. unset($GLOBALS['bxDolClasses'][$sKey]);
  18. $aErrors = [];
  19. set_error_handler(function ($iErrno, $sError) use (&$aErrors) {
  20. $aErrors[] = $sError;
  21. return true;
  22. }, E_DEPRECATED | E_USER_DEPRECATED);
  23. try {
  24. $o = BxDolConnection::getObjectInstance($sObject);
  25. $this->assertInstanceOf(BxBaseConnection::class, $o);
  26. $o->getElementAPI(1);
  27. }
  28. finally {
  29. restore_error_handler();
  30. unset($GLOBALS['bxDolClasses'][$sKey]);
  31. if ($mixedCached !== null)
  32. $GLOBALS['bxDolClasses'][$sKey] = $mixedCached;
  33. }
  34. $this->assertSame([], $aErrors);
  35. // A class name, not the object: ReflectionClass of an object also sees dynamic properties.
  36. $this->assertTrue((new ReflectionClass(get_class($o)))->hasProperty('_bIsApi'));
  37. }
  38. static public function providerForConnectionObjects()
  39. {
  40. return [
  41. ['sys_profiles_friends', ''],
  42. ['sys_profiles_subscriptions', ''],
  43. ['sys_profiles_relations', ''],
  44. ['bx_organizations_fans', 'bx_organizations'],
  45. ['bx_groups_fans', 'bx_groups'],
  46. ];
  47. }
  48. }