summaryrefslogtreecommitdiff
path: root/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TestDataLoaderTrait.php
diff options
context:
space:
mode:
authorYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
committerYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
commitfc7369835258467bf97eb64f184b93691f9a9fd5 (patch)
treedaabd60089d2dd76d9f5fb416b005fbe159c799d /www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TestDataLoaderTrait.php
first commit
Diffstat (limited to 'www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TestDataLoaderTrait.php')
-rw-r--r--www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TestDataLoaderTrait.php69
1 files changed, 69 insertions, 0 deletions
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TestDataLoaderTrait.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TestDataLoaderTrait.php
new file mode 100644
index 00000000..1db22090
--- /dev/null
+++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TestDataLoaderTrait.php
@@ -0,0 +1,69 @@
+<?php
+
+namespace Civi\Test\Api4\Traits;
+
+/**
+ * This probably should be a separate class
+ */
+trait TestDataLoaderTrait {
+
+ /**
+ * @var array
+ * References to entities used for loading test data
+ */
+ protected $references;
+
+ /**
+ * Creates entities from a JSON data set
+ *
+ * @param $path
+ */
+ protected function loadDataSet($path) {
+ if (!file_exists($path)) {
+ $path = __DIR__ . '/../DataSets/' . $path . '.json';
+ }
+
+ $dataSet = json_decode(file_get_contents($path), TRUE);
+ foreach ($dataSet as $entityName => $entities) {
+ foreach ($entities as $entityValues) {
+
+ $entityValues = $this->replaceReferences($entityValues);
+
+ $params = ['values' => $entityValues, 'checkPermissions' => FALSE];
+ $result = civicrm_api4($entityName, 'create', $params);
+ if (isset($entityValues['@ref'])) {
+ $this->references[$entityValues['@ref']] = $result->first();
+ }
+ }
+ }
+ }
+
+ /**
+ * @param $name
+ *
+ * @return null|mixed
+ */
+ protected function getReference($name) {
+ return isset($this->references[$name]) ? $this->references[$name] : NULL;
+ }
+
+ /**
+ * @param array $entityValues
+ *
+ * @return array
+ */
+ private function replaceReferences($entityValues) {
+ foreach ($entityValues as $name => $value) {
+ if (is_array($value)) {
+ $entityValues[$name] = $this->replaceReferences($value);
+ }
+ elseif (substr($value, 0, 4) === '@ref') {
+ $referenceName = substr($value, 5);
+ list ($reference, $property) = explode('.', $referenceName);
+ $entityValues[$name] = $this->references[$reference][$property];
+ }
+ }
+ return $entityValues;
+ }
+
+}