Skip to content

Commit

Permalink
Merge pull request #338 from colemanw/noApi3
Browse files Browse the repository at this point in the history
AddEntityCommand - Stop generating APIv3
  • Loading branch information
totten authored Sep 26, 2024
2 parents dbbdf01 + 5ec2b47 commit 969f2f8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 46 deletions.
23 changes: 7 additions & 16 deletions scripts/make-snapshots.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ while [ -n "$1" ]; do
--version) SNAPSHOT_VER="$1" ; shift ; ;;
--test|-t) RUN_TEST=1 ; ;;
--keep|-k) KEEP=1 ; ;;
empty|qf|entity3|entity34|kitchensink|svc) SCENARIOS="$SCENARIOS $OPT" ; ;;
empty|qf|entity4|kitchensink|svc) SCENARIOS="$SCENARIOS $OPT" ; ;;
*) echo "Unrecognized option: $OPT" 1>&2 ; exit 1 ;;
esac
done

if [ -z "$SCENARIOS" ]; then
SCENARIOS="empty qf entity3 entity34 kitchensink svc"
SCENARIOS="empty qf entity4 kitchensink svc"
fi

################################################
Expand All @@ -47,11 +47,11 @@ function show_help() {
echo " --keep: Keep the temporary work folder"
echo
echo "Scenarios:"
echo " empty qf entity3 entity34 kitchensink svc"
echo " empty qf entity4 kitchensink svc"
echo
echo "Example:"
echo " CIVIX_WORKSPACE=\$CIVIBUILD_HOME/dmaster/web/sites/all/modules/civicrm/ext/civixtest bash $0 --src"
echo " CIVIX_WORKSPACE=\$CIVIBUILD_HOME/dmaster/web/sites/all/modules/civicrm/ext/civixtest bash $0 --src --keep entity34"
echo " CIVIX_WORKSPACE=\$CIVIBUILD_HOME/dmaster/web/sites/all/modules/civicrm/ext/civixtest bash $0 --src --keep entity4"
}

function clean_workspace() {
Expand Down Expand Up @@ -82,26 +82,17 @@ function build_snapshot() {
$CIVIX $VERBOSITY generate:form MyForm civicrm/my-form
;;

entity3)
entity4)
$CIVIX $VERBOSITY generate:upgrader
$CIVIX $VERBOSITY generate:entity MyEntityThree -A3
# $CIVIX $VERBOSITY generate:entity MyEntityThree
;;

entity34)
$CIVIX $VERBOSITY generate:upgrader
$CIVIX $VERBOSITY generate:entity MyEntityThreeFour -A3,4
$CIVIX $VERBOSITY generate:entity MyEntityFour
;;

kitchensink)
$CIVIX $VERBOSITY generate:api MyEntity Myaction
$CIVIX $VERBOSITY generate:api MyEntity myaction2
$CIVIX $VERBOSITY generate:case-type MyLabel MyName
# $CIVIX $VERBOSITY generate:custom-xml -f --data="FIXME" --uf="FIXME"
$CIVIX $VERBOSITY generate:entity MyEntityFour -A4
$CIVIX $VERBOSITY generate:entity MyEntityThree -A3
# $CIVIX $VERBOSITY generate:entity MyEntityThree
$CIVIX $VERBOSITY generate:entity MyEntityThreeFour -A3,4
$CIVIX $VERBOSITY generate:entity MyEntityFour
$CIVIX $VERBOSITY generate:form MyForm civicrm/my-form
$CIVIX $VERBOSITY generate:form My_StuffyForm civicrm/my-stuffy-form
$CIVIX $VERBOSITY generate:page MyPage civicrm/my-page
Expand Down
35 changes: 5 additions & 30 deletions src/CRM/CivixBundle/Command/AddEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Exception;

class AddEntityCommand extends AbstractCommand {
const API_VERSION = 3;
const API_VERSION = 4;

protected function configure() {
parent::configure();
Expand All @@ -27,7 +27,6 @@ protected function configure() {
->setDescription('Add a new API/BAO/GenCode entity to a CiviCRM Module-Extension (*EXPERIMENTAL*)')
->addArgument('<EntityName>', InputArgument::REQUIRED, 'The brief, unique name of the entity")')
->addOption('table-name', NULL, InputOption::VALUE_OPTIONAL, 'The SQL table name. (see usage)')
->addOption('api-version', 'A', InputOption::VALUE_REQUIRED, 'Comma-separated list of versions (3,4)', '4')
->setHelp('Add a new API/BAO/GenCode entity to a CiviCRM Module-Extension.
This command is experimental. Developer discretion is advised.
Expand All @@ -50,10 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {

$this->assertCurrentFormat();

$apiVersions = explode(',', $input->getOption('api-version'));
if (!empty(array_diff($apiVersions, ['3', '4']))) {
throw new Exception("In --api-versions, found unrecognized versions. Expected: '3' and/or '4'");
}
$apiVersions = [self::API_VERSION];

Civix::generator()->addUpgrader('if-forced');

Expand All @@ -71,27 +67,18 @@ protected function execute(InputInterface $input, OutputInterface $output) {

$ctx['entityNameCamel'] = ucfirst($input->getArgument('<EntityName>'));
$ctx['tableName'] = $input->getOption('table-name') ? $input->getOption('table-name') : Naming::createTableName($input->getArgument('<EntityName>'));
if (function_exists('_civicrm_api_get_entity_name_from_camel')) {
$ctx['apiFunctionPrefix'] = 'civicrm_api' . self::API_VERSION . '_' . _civicrm_api_get_entity_name_from_camel($ctx['entityNameCamel']) . '_';
}
else {
throw new Exception("Failed to determine proper API function name. Perhaps the API internals have changed?");
}

$mixins = new Mixins($info, $basedir->string('mixin'), ['[email protected]']);
$mixins->save($ctx, $output);
$info->save($ctx, $output);

$ctx['apiFile'] = $basedir->string('api', 'v3', $ctx['entityNameCamel'] . '.php');
$ctx['api4File'] = $basedir->string('Civi', 'Api4', $ctx['entityNameCamel'] . '.php');
$ctx['daoClassName'] = strtr($ctx['namespace'], '/', '_') . '_DAO_' . $input->getArgument('<EntityName>');
$ctx['daoClassFile'] = $basedir->string(strtr($ctx['daoClassName'], '_', '/') . '.php');
$ctx['baoClassName'] = strtr($ctx['namespace'], '/', '_') . '_BAO_' . $input->getArgument('<EntityName>');
$ctx['baoClassFile'] = $basedir->string(strtr($ctx['baoClassName'], '_', '/') . '.php');
$ctx['entityTypeFile'] = $basedir->string('schema', $input->getArgument('<EntityName>') . '.entityType.php');
$ctx['extensionName'] = $info->getExtensionName();
$ctx['testApi3ClassName'] = 'api_v3_' . $ctx['entityNameCamel'] . 'Test';
$ctx['testApi3ClassFile'] = $basedir->string('tests', 'phpunit', strtr($ctx['testApi3ClassName'], '_', '/') . '.php');

$ext = new Collection();
$ext->builders['dirs'] = new Dirs([
Expand All @@ -100,13 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$ext->builders['dirs']->save($ctx, $output);

$hasPhpUnit = FALSE;
if (in_array('3', $apiVersions)) {
$ext->builders['dirs']->addPath(dirname($ctx['apiFile']));
$ext->builders['api.php'] = new Template('entity-api.php.php', $ctx['apiFile'], FALSE, Civix::templating());
$ext->builders['dirs']->addPath(dirname($ctx['testApi3ClassFile']));
$ext->builders['test.php'] = new Template('entity-api3-test.php.php', $ctx['testApi3ClassFile'], FALSE, Civix::templating());
$hasPhpUnit = TRUE;
}

if (in_array('4', $apiVersions)) {
$ext->builders['dirs']->addPath(dirname($ctx['api4File']));
$ext->builders['api4.php'] = new Template('entity-api4.php.php', $ctx['api4File'], FALSE, Civix::templating());
Expand All @@ -133,14 +114,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {

Civix::generator()->updateModuleCivixPhp();

if (count($apiVersions) >= 2) {
$output->writeln('<comment>Generated API skeletons for APIv3 and APIv4.</comment>');
}
elseif ($apiVersions == ['3']) {
$output->writeln('<comment>Generated API skeletons for APIv3. To generate APIv4, specify <info>--api-version=4</info></comment>');
}
elseif ($apiVersions == ['4']) {
$output->writeln('<comment>Generated API skeletons for APIv4. To generate APIv3, specify <info>--api-version=3</info></comment>');
if ($apiVersions == [4]) {
$output->writeln('<comment>Generated API skeletons for APIv4.</comment>');
}

$output->writeln('<comment>Note: no changes have been made to the database. You can update the database by uninstalling and re-enabling the extension.</comment>');
Expand Down
17 changes: 17 additions & 0 deletions tests/e2e/SnapshotUpgradeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* - 'qf': An extension with some QuickForm pages/forms.
* - 'entity3': An extension with an entity supporting APIv3.
* - 'entity34': An extension with an entity supporting APIv3 and APIv4.
* - 'entity4': An extension with an entity supporting APIv4.
* - 'kitchensink': An extension with a bunch of random things. (Varies based on the CIVIX_VERSION.)
* - 'svc': An extension with a service-object.
* - (NOTE: For a more detailed sketch of each scenario, see `tests/make-snapshots.sh`.)
Expand Down Expand Up @@ -143,6 +144,22 @@ public function checkSnapshot_entity34(): void {
$this->assertTrue(in_array('FK to Contact', $descriptions4), "$entity.contact_id should have APIv4 description. Actual metadata response was: ");
}

/**
* The "*-entity4.zip" snapshots include an entity ("MyEntityFour") with APIv4 support.
* This also appears in some kitchen-sink builds.
*/
public function checkSnapshot_entity4(): void {
$this->runsIf($this->isScenario('entity4') || $this->isKitchenSinkWith('xml/schema/CRM/Civixsnapshot/MyEntityFour.xml'));

$entity = 'MyEntityFour';

$getFields4 = PH::runOK("cv api4 $entity.getFields --out=json");
$parsed4 = json_decode($getFields4->getOutput(), TRUE);
$descriptions4 = array_column($parsed4, 'description');
$this->assertTrue(in_array("Unique $entity ID", $descriptions4), "$entity.id should have APIv4 description. Actual metadata response was: ");
$this->assertTrue(in_array('FK to Contact', $descriptions4), "$entity.contact_id should have APIv4 description. Actual metadata response was: ");
}

/**
* The "*-qf.zip" snapshots include a traditional page+form (eg "civicrm/my-page").
* This also appears in some kitchen-sink builds.
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 969f2f8

Please sign in to comment.