From 88589ec249ee422d83f6557ffb04957f3b707a31 Mon Sep 17 00:00:00 2001 From: colemanw Date: Fri, 25 Aug 2023 12:26:57 -0400 Subject: [PATCH] APIv4 - Simplify entity name gathering Before: Getting entity names was hard and the conformanceTest struggled After: It's easier, as every entity class has a public getEntityName method --- Civi/Api4/Action/GetActions.php | 2 +- Civi/Api4/Generic/AbstractEntity.php | 2 +- Civi/Api4/Provider/ActionObjectProvider.php | 2 +- ext/civi_case/Civi/Api4/CiviCase.php | 2 +- .../phpunit/api/v4/Entity/ConformanceTest.php | 30 +++++-------------- 5 files changed, 12 insertions(+), 26 deletions(-) diff --git a/Civi/Api4/Action/GetActions.php b/Civi/Api4/Action/GetActions.php index 1442543308..f1419fb4c3 100644 --- a/Civi/Api4/Action/GetActions.php +++ b/Civi/Api4/Action/GetActions.php @@ -35,7 +35,7 @@ class GetActions extends BasicGetAction { $entityReflection = new \ReflectionClass($className); foreach ($entityReflection->getMethods(\ReflectionMethod::IS_STATIC | \ReflectionMethod::IS_PUBLIC) as $method) { $actionName = $method->getName(); - if ($actionName != 'permissions' && $actionName != 'getInfo' && $actionName[0] != '_') { + if (!in_array($actionName, ['permissions', 'getInfo', 'getEntityName'], TRUE) && !str_starts_with($actionName, '_')) { $this->loadAction($actionName, $method); } } diff --git a/Civi/Api4/Generic/AbstractEntity.php b/Civi/Api4/Generic/AbstractEntity.php index 083e3359e2..563dddf725 100644 --- a/Civi/Api4/Generic/AbstractEntity.php +++ b/Civi/Api4/Generic/AbstractEntity.php @@ -74,7 +74,7 @@ abstract class AbstractEntity { * * @return string */ - protected static function getEntityName() { + public static function getEntityName(): string { return self::stripNamespace(static::class); } diff --git a/Civi/Api4/Provider/ActionObjectProvider.php b/Civi/Api4/Provider/ActionObjectProvider.php index 82bb3bf5d0..91c30f9784 100644 --- a/Civi/Api4/Provider/ActionObjectProvider.php +++ b/Civi/Api4/Provider/ActionObjectProvider.php @@ -172,7 +172,7 @@ class ActionObjectProvider extends AutoService implements EventSubscriberInterfa * Scan all api directories to discover entities * @return \Civi\Api4\Generic\AbstractEntity[] */ - private function getAllApiClasses() { + public function getAllApiClasses(): array { $classNames = []; $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')], array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath') diff --git a/ext/civi_case/Civi/Api4/CiviCase.php b/ext/civi_case/Civi/Api4/CiviCase.php index 2b0005fe66..23312605e7 100644 --- a/ext/civi_case/Civi/Api4/CiviCase.php +++ b/ext/civi_case/Civi/Api4/CiviCase.php @@ -28,7 +28,7 @@ class CiviCase extends Generic\DAOEntity { * * @return string */ - protected static function getEntityName() { + public static function getEntityName(): string { return 'Case'; } diff --git a/tests/phpunit/api/v4/Entity/ConformanceTest.php b/tests/phpunit/api/v4/Entity/ConformanceTest.php index c582de01b9..75fa3b9ca3 100644 --- a/tests/phpunit/api/v4/Entity/ConformanceTest.php +++ b/tests/phpunit/api/v4/Entity/ConformanceTest.php @@ -26,6 +26,7 @@ use Civi\Api4\CustomGroup; use Civi\Api4\Entity; use api\v4\Api4TestBase; use Civi\Api4\Event\ValidateValuesEvent; +use Civi\Api4\Provider\ActionObjectProvider; use Civi\Api4\Service\Spec\CustomFieldSpec; use Civi\Api4\Service\Spec\FieldSpec; use Civi\Api4\Utils\CoreUtil; @@ -101,33 +102,18 @@ class ConformanceTest extends Api4TestBase implements HookInterface { /** * Get entities to test. * - * This is the low-tech list as generated by manual-overrides and direct inspection. - * It may be summoned at any time during PHPUnit lifecycle, but it may require - * occasional twiddling to give correct results. + * This method uses file-scanning only and doesn't include dynamic entities (e.g. from multi-record custom fields) + * But it may be summoned at any time during PHPUnit lifecycle. * * @return array */ public function getEntitiesLotech(): array { - // Core + required core extensions - $directores = ['', 'ext/search_kit/', 'ext/civigrant/', 'ext/civi_*/']; - $manual['remove'] = ['CustomValue', 'SKEntity']; - $manual['transform'] = ['CiviCase' => 'Case']; - - $scanned = []; - $baseDir = dirname(__DIR__, 5); - foreach ($directores as $directory) { - foreach ((array) glob("$baseDir/{$directory}Civi/Api4/*.php") as $name) { - $fileName = basename($name, '.php'); - $scanned[] = $manual['transform'][$fileName] ?? $fileName; - } + $provider = new ActionObjectProvider(); + $entityNames = []; + foreach ($provider->getAllApiClasses() as $className) { + $entityNames[] = $className::getEntityName(); } - - $names = array_diff( - $scanned, - $manual['remove'] - ); - - return $this->toDataProviderArray($names); + return $this->toDataProviderArray($entityNames); } /** -- 2.25.1