4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Traits
;
23 * This probably should be a separate class
25 trait TestDataLoaderTrait
{
29 * References to entities used for loading test data
31 protected $references;
34 * Creates entities from a JSON data set
38 protected function loadDataSet($path) {
39 if (!file_exists($path)) {
40 $path = __DIR__
. '/../DataSets/' . $path . '.json';
43 $dataSet = json_decode(file_get_contents($path), TRUE);
44 foreach ($dataSet as $entityName => $entities) {
45 foreach ($entities as $entityValues) {
47 $entityValues = $this->replaceReferences($entityValues);
49 $params = ['values' => $entityValues, 'checkPermissions' => FALSE];
50 $result = civicrm_api4($entityName, 'create', $params);
51 if (isset($entityValues['@ref'])) {
52 $this->references
[$entityValues['@ref']] = $result->first();
63 protected function getReference($name) {
64 return $this->references
[$name] ??
NULL;
68 * @param array $entityValues
72 private function replaceReferences($entityValues) {
73 foreach ($entityValues as $name => $value) {
74 if (is_array($value)) {
75 $entityValues[$name] = $this->replaceReferences($value);
77 elseif (substr($value, 0, 4) === '@ref') {
78 $referenceName = substr($value, 5);
79 list ($reference, $property) = explode('.', $referenceName);
80 $entityValues[$name] = $this->references
[$reference][$property];