477c43f9bdfc52d8ce925215f8c919e0cf8d6125
[civicrm-core.git] / tests / phpunit / api / v4 / Service / TestCreationParameterProvider.php
1 <?php
2
3 namespace api\v4\Service;
4
5 use Civi\Api4\Service\Spec\FieldSpec;
6 use Civi\Api4\Service\Spec\SpecGatherer;
7
8 class TestCreationParameterProvider {
9
10 /**
11 * @var \Civi\Api4\Service\Spec\SpecGatherer
12 */
13 protected $gatherer;
14
15 /**
16 * @param \Civi\Api4\Service\Spec\SpecGatherer $gatherer
17 */
18 public function __construct(SpecGatherer $gatherer) {
19 $this->gatherer = $gatherer;
20 }
21
22 /**
23 * @param $entity
24 *
25 * @return array
26 */
27 public function getRequired($entity) {
28 $createSpec = $this->gatherer->getSpec($entity, 'create', FALSE);
29 $requiredFields = array_merge($createSpec->getRequiredFields(), $createSpec->getConditionalRequiredFields());
30
31 if ($entity === 'Contact') {
32 $requiredFields[] = $createSpec->getFieldByName('first_name');
33 $requiredFields[] = $createSpec->getFieldByName('last_name');
34 }
35
36 $requiredParams = [];
37 foreach ($requiredFields as $requiredField) {
38 $value = $this->getRequiredValue($requiredField);
39 $requiredParams[$requiredField->getName()] = $value;
40 }
41
42 unset($requiredParams['id']);
43
44 return $requiredParams;
45 }
46
47 /**
48 * Attempt to get a value using field option, defaults, FKEntity, or a random
49 * value based on the data type.
50 *
51 * @param \Civi\Api4\Service\Spec\FieldSpec $field
52 *
53 * @return mixed
54 * @throws \Exception
55 */
56 private function getRequiredValue(FieldSpec $field) {
57
58 if ($field->getOptions()) {
59 return $this->getOption($field);
60 }
61 elseif ($field->getDefaultValue()) {
62 return $field->getDefaultValue();
63 }
64 elseif ($field->getFkEntity()) {
65 return $this->getFkID($field, $field->getFkEntity());
66 }
67 elseif (in_array($field->getName(), ['entity_id', 'contact_id'])) {
68 return $this->getFkID($field, 'Contact');
69 }
70
71 $randomValue = $this->getRandomValue($field->getDataType());
72
73 if ($randomValue) {
74 return $randomValue;
75 }
76
77 throw new \Exception('Could not provide default value');
78 }
79
80 /**
81 * @param \Civi\Api4\Service\Spec\FieldSpec $field
82 *
83 * @return mixed
84 */
85 private function getOption(FieldSpec $field) {
86 $options = $field->getOptions();
87 return array_rand($options);
88 }
89
90 /**
91 * @param \Civi\Api4\Service\Spec\FieldSpec $field
92 * @param string $fkEntity
93 *
94 * @return mixed
95 * @throws \Exception
96 */
97 private function getFkID(FieldSpec $field, $fkEntity) {
98 $params = ['checkPermissions' => FALSE];
99 // Be predictable about what type of contact we select
100 if ($fkEntity === 'Contact') {
101 $params['where'] = [['contact_type', '=', 'Individual']];
102 }
103 $entityList = civicrm_api4($fkEntity, 'get', $params);
104 if ($entityList->count() < 1) {
105 $msg = sprintf('At least one %s is required in test', $fkEntity);
106 throw new \Exception($msg);
107 }
108
109 return $entityList->last()['id'];
110 }
111
112 /**
113 * @param $dataType
114 *
115 * @return int|null|string
116 */
117 private function getRandomValue($dataType) {
118 switch ($dataType) {
119 case 'Boolean':
120 return TRUE;
121
122 case 'Integer':
123 return rand(1, 2000);
124
125 case 'String':
126 return \CRM_Utils_String::createRandom(10, implode('', range('a', 'z')));
127
128 case 'Text':
129 return \CRM_Utils_String::createRandom(100, implode('', range('a', 'z')));
130
131 case 'Money':
132 return sprintf('%d.%2d', rand(0, 2000), rand(10, 99));
133
134 case 'Date':
135 return '20100102';
136
137 case 'Timestamp':
138 return 'now';
139 }
140
141 return NULL;
142 }
143
144 }