Convert `contribution{,_recur}.ex.php` to `Civi/Test/ExampleData/*`
authorTim Otten <totten@civicrm.org>
Tue, 14 Sep 2021 23:42:54 +0000 (16:42 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 15 Sep 2021 23:15:14 +0000 (16:15 -0700)
1. Use revised class format
2. Use `entity/{$ENTITY}/{$EXAMPLE}` instead of `workflow/{$WORKFLOW}/{$EXAMPLE}`

Civi/Test/EntityExample.php [new file with mode: 0644]
Civi/Test/ExampleData/Contribution/Euro5990.ex.php [new file with mode: 0644]
Civi/Test/ExampleData/ContributionRecur/Euro5990.ex.php [new file with mode: 0644]
Civi/WorkflowMessage/GenericWorkflowMessage/contribution.ex.php [deleted file]
Civi/WorkflowMessage/GenericWorkflowMessage/contribution_recur.ex.php [deleted file]

diff --git a/Civi/Test/EntityExample.php b/Civi/Test/EntityExample.php
new file mode 100644 (file)
index 0000000..930f597
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+namespace Civi\Test;
+
+/**
+ * Helper class for defining entity examples.
+ *
+ * By convention, you should name this class relative to the target workflow,
+ * as in:
+ *
+ * - Entity Name: ContributionRecur
+ * - Example Data: Civi\Test\ExampleData\ContributionRecur\Euro5990
+ * - Example Name: entity/ContributionRecur/Euro5900
+ */
+abstract class EntityExample implements ExampleDataInterface {
+
+  /**
+   * @var string
+   */
+  protected $entityName;
+
+  /**
+   * @var string
+   */
+  protected $exName;
+
+  public function __construct() {
+    if (!preg_match(';^(.*)[_\\\]([a-zA-Z0-9]+)[_\\\]([a-zA-Z0-9]+)$;', static::class, $m)) {
+      throw new \RuntimeException("Failed to parse class: " . static::class);
+    }
+    $this->entityName = $m[2];
+    $this->exName = $m[3];
+  }
+
+  protected function dao(): string {
+    return \CRM_Core_DAO_AllCoreTables::getFullName($this->entityName);
+  }
+
+  protected function bao(): string {
+    return \CRM_Core_DAO_AllCoreTables::getBAOClassName($this->getDAO());
+  }
+
+}
diff --git a/Civi/Test/ExampleData/Contribution/Euro5990.ex.php b/Civi/Test/ExampleData/Contribution/Euro5990.ex.php
new file mode 100644 (file)
index 0000000..0d3237d
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+
+namespace Civi\Test\ExampleData\Contribution;
+
+class Euro5990 extends \Civi\Test\EntityExample {
+
+  public function getExamples(): iterable {
+    yield [
+      'name' => "entity/{$this->entityName}/{$this->exName}/completed",
+    ];
+  }
+
+  public function build(array &$example): void {
+    $base = [
+      'id' => 50,
+      'contact_id' => 100,
+      'financial_type_id' => 'Member dues',
+      'payment_instrument_id:label' => 'Debit Card',
+      'contribution_page_id' => 2,
+      'receive_date' => '2021-07-23 15:39:20',
+      'revenue_recognition_date' => '2021-07-23 00:00:00',
+      'thankyou_date' => '2021-07-23 15:39:20',
+      'cancel_date' => '',
+      'cancel_reason' => '',
+      'non_deductible_amount' => 5,
+      'total_amount' => 5990.99,
+      'fee_amount' => 0.99,
+      'net_amount' => 5990,
+      'tax_amount' => 60,
+      'currency' => 'EUR',
+      'source' => 'Online donation',
+      'amount_level' => 'premium purchased',
+      'contribution_recur_id' => 50,
+      'check_number' => '',
+      'campaign_id:label' => 'Outreach',
+      'creditnote_id' => '',
+      'trxn_id' => 123,
+      'invoice_id' => 'inv123',
+      'is_test' => TRUE,
+      'is_pay_later' => FALSE,
+    ];
+
+    $extras['completed'] = [
+      'contribution_status_id' => 1,
+    ];
+
+    $example['data'] = $base + $extras[basename($example['name'])];
+  }
+
+}
diff --git a/Civi/Test/ExampleData/ContributionRecur/Euro5990.ex.php b/Civi/Test/ExampleData/ContributionRecur/Euro5990.ex.php
new file mode 100644 (file)
index 0000000..e55cf2f
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+namespace Civi\Test\ExampleData\ContributionRecur;
+
+class Euro5990 extends \Civi\Test\EntityExample {
+
+  public function getExamples(): iterable {
+    yield [
+      'name' => "entity/{$this->entityName}/{$this->exName}/pending",
+    ];
+    yield [
+      'name' => "entity/{$this->entityName}/{$this->exName}/cancelled",
+    ];
+  }
+
+  public function build(array &$example): void {
+    $base = [
+      'id' => 50,
+      'contact_id' => 100,
+      'is_email_receipt' => 1,
+      'start_date' => '2021-07-23 15:39:20',
+      'end_date' => '2021-07-26 18:07:20',
+      'amount' => 5990.99,
+      'currency' => 'EUR',
+      'frequency_unit' => 'year',
+      'frequency_interval' => 2,
+      'installments' => 24,
+      'payment_instrument_id:label' => 'Debit Card',
+      'financial_type_id:label' => 'Member dues',
+      'processor_id' => 'abc_xyz',
+      'payment_processor_id' => 2,
+      'trxn_id' => 123,
+      'invoice_id' => 'inv123',
+      'sequential' => 1,
+      'failure_retry_date' => '2020-01-03',
+      'auto_renew' => 1,
+      'cycle_day' => '15',
+      'is_test' => TRUE,
+      'payment_token_id' => 4,
+    ];
+
+    $extras['pending'] = [
+      'status_id' => 2,
+      'cancel_date' => NULL,
+      'cancel_reason' => NULL,
+    ];
+    $extras['cancelled'] = [
+      'status_id' => 3,
+      'cancel_date' => '2021-08-19 09:12:45',
+      'cancel_reason' => 'Because',
+    ];
+
+    $example['data'] = $base + $extras[basename($example['name'])];
+  }
+
+}
diff --git a/Civi/WorkflowMessage/GenericWorkflowMessage/contribution.ex.php b/Civi/WorkflowMessage/GenericWorkflowMessage/contribution.ex.php
deleted file mode 100644 (file)
index 1ceb6ac..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-<?php
-return [
-  'tags' => [],
-  'data' => function(\Civi\WorkflowMessage\Examples $examples) {
-    return [
-      'modelProps' => [
-        'contribution' => [
-          'id' => 50,
-          'contact_id' => 100,
-          'financial_type_id' => 'Member dues',
-          'contribution_page_id' => 2,
-          'payment_instrument_id:label' => 'Debit Card',
-          'contribution_status_id' => 1,
-          'receive_date' => '2021-07-23 15:39:20',
-          'revenue_recognition_date' => '2021-07-23 00:00:00',
-          'thankyou_date' => '2021-07-23 15:39:20',
-          'cancel_date' => '',
-          'cancel_reason' => '',
-          'non_deductible_amount' => 5,
-          'total_amount' => 5990.99,
-          'fee_amount' => 0.99,
-          'net_amount' => 5990,
-          'tax_amount' => 60,
-          'currency' => 'EUR',
-          'source' => 'Online donation',
-          'amount_level' => 'premium purchased',
-          'contribution_recur_id' => 50,
-          'check_number' => '',
-          'campaign_id:label' => 'Outreach',
-          'creditnote_id' => '',
-          'trxn_id' => 123,
-          'invoice_id' => 'inv123',
-          'is_test' => TRUE,
-          'is_pay_later' => FALSE,
-        ],
-      ],
-    ];
-  },
-];
diff --git a/Civi/WorkflowMessage/GenericWorkflowMessage/contribution_recur.ex.php b/Civi/WorkflowMessage/GenericWorkflowMessage/contribution_recur.ex.php
deleted file mode 100644 (file)
index f680afc..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-<?php
-return [
-  'tags' => [],
-  'data' => function(\Civi\WorkflowMessage\Examples $examples) {
-    return [
-      'modelProps' => [
-        'contribution_recur' => [
-          'id' => 50,
-          'contact_id' => 100,
-          'status_id' => 3,
-          'is_email_receipt' => 1,
-          'start_date' => '2021-07-23 15:39:20',
-          'end_date' => '2021-07-26 18:07:20',
-          'cancel_date' => '2021-08-19 09:12:45',
-          'cancel_reason' => 'Because',
-          'amount' => 5990.99,
-          'currency' => 'EUR',
-          'frequency_unit' => 'year',
-          'frequency_interval' => 2,
-          'installments' => 24,
-          'payment_instrument_id:label' => 'Debit Card',
-          'financial_type_id:label' => 'Member dues',
-          'processor_id' => 'abc_xyz',
-          'payment_processor_id' => 2,
-          'trxn_id' => 123,
-          'invoice_id' => 'inv123',
-          'sequential' => 1,
-          'failure_retry_date' => '2020-01-03',
-          'auto_renew' => 1,
-          'cycle_day' => '15',
-          'is_test' => TRUE,
-          'payment_token_id' => 4,
-        ],
-      ],
-    ];
-  },
-];