5 use api\v
4\Traits\TestDataLoaderTrait
;
6 use Civi\Test\HeadlessInterface
;
7 use Civi\Test\TransactionalInterface
;
12 class UnitTestCase
extends \PHPUnit\Framework\TestCase
implements HeadlessInterface
, TransactionalInterface
{
14 use TestDataLoaderTrait
;
17 * @see CiviUnitTestCase
21 * @param string $dataName
23 public function __construct($name = NULL, array $data = [], $dataName = '') {
24 parent
::__construct($name, $data, $dataName);
25 error_reporting(E_ALL
& ~E_NOTICE
);
28 public function setUpHeadless() {
29 return \Civi\Test
::headless()->apply();
33 * Tears down the fixture, for example, closes a network connection.
35 * This method is called after a test is executed.
37 public function tearDown() {
42 * Quick clean by emptying tables created for the test.
44 * @param array $params
46 public function cleanup($params) {
48 'tablesToTruncate' => [],
50 \CRM_Core_DAO
::executeQuery("SET FOREIGN_KEY_CHECKS = 0;");
51 foreach ($params['tablesToTruncate'] as $table) {
52 \Civi
::log()->info('truncating: ' . $table);
53 $sql = "TRUNCATE TABLE $table";
54 \CRM_Core_DAO
::executeQuery($sql);
56 \CRM_Core_DAO
::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
60 * Quick record counter
62 * @param string $table_name
63 * @returns int record count
65 public function getRowCount($table_name) {
66 $sql = "SELECT count(id) FROM $table_name";
67 return (int) \CRM_Core_DAO
::singleValueQuery($sql);
71 * Create sample entities (using V3 for now).
73 * @param array $params
74 * (type, seq, overrides, count)
76 * (either single, or array of array if count >1)
77 * @throws \CiviCRM_API3_Exception
80 public static function createEntity($params) {
87 for ($i = 0; $i < $params['count']; $i++
) {
89 $data = self
::sample($params);
90 $api_params = ['sequential' => 1] +
$data['sample_params'];
91 $result = civicrm_api3($data['entity'], 'create', $api_params);
92 if ($result['is_error']) {
93 throw new \
Exception("creating $data[entity] failed");
95 $entity = $result['values'][0];
96 if (!($entity['id'] > 0)) {
97 throw new \
Exception("created entity is malformed");
99 $entities[] = $entity;
101 return $params['count'] == 1 ?
$entity : $entities;
105 * Helper function for creating sample entities.
107 * Depending on the supplied sequence integer, plucks values from the dummy data.
108 * Constructs a foreign entity when an ID is required but isn't supplied in the overrides.
110 * Inspired by CiviUnitTestCase::
111 * @todo - extract this function to own class and share with CiviUnitTestCase?
112 * @param array $params
113 * - type: string roughly matching entity type
114 * - seq: (optional) int sequence number for the values of this type
115 * - overrides: (optional) array of fill in parameters
118 * - entity: string API entity type (usually the type supplied except for contact subtypes)
119 * - sample_params: array API sample_params properties of sample entity
121 public static function sample($params) {
126 $type = $params['type'];
127 // sample data - if field is array then chosed based on `seq`
129 if (in_array($type, ['Individual', 'Organization', 'Household'])) {
130 $sample_params['contact_type'] = $type;
136 // use the seq to pluck a set of params out
137 foreach (self
::sampleData($type) as $key => $value) {
138 if (is_array($value)) {
139 $sample_params[$key] = $value[$params['seq'] %
count($value)];
142 $sample_params[$key] = $value;
145 if ($type == 'Individual') {
146 $sample_params['email'] = strtolower(
147 $sample_params['first_name'] . '_' . $sample_params['last_name'] . '@civicrm.org'
149 $sample_params['prefix_id'] = 3;
150 $sample_params['suffix_id'] = 3;
152 if (!count($sample_params)) {
153 throw new \
Exception("unknown sample type: $type");
155 $sample_params = $params['overrides'] +
$sample_params;
156 // make foreign enitiies if they haven't been supplied
157 foreach ($sample_params as $key => $value) {
158 if (substr($value, 0, 6) === 'dummy.') {
159 $foreign_entity = self
::createEntity([
160 'type' => substr($value, 6),
161 'seq' => $params['seq'],
163 $sample_params[$key] = $foreign_entity['id'];
166 return compact("entity", "sample_params");
170 * Provider of sample data.
173 * Array values represent a set of allowable items.
174 * Strings in the form "dummy.Entity" require creating a foreign entity first.
176 public static function sampleData($type) {
179 // The number of values in each list need to be coprime numbers to not have duplicates
180 'first_name' => ['Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'],
181 'middle_name' => ['J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'],
182 'last_name' => ['Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'],
183 'contact_type' => 'Individual',
186 'organization_name' => [
187 'Unit Test Organization',
195 'household_name' => ['Unit Test household'],
198 'title' => 'Annual CiviCRM meet',
199 'summary' => 'If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now',
200 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
201 'event_type_id' => 1,
203 'start_date' => 20081021,
204 'end_date' => 20081023,
205 'is_online_registration' => 1,
206 'registration_start_date' => 20080601,
207 'registration_end_date' => 20081015,
208 'max_participants' => 100,
209 'event_full_text' => 'Sorry! We are already full',
212 'is_show_location' => 0,
215 'event_id' => 'dummy.Event',
216 'contact_id' => 'dummy.Individual',
219 'register_date' => 20070219,
220 'source' => 'Wimbeldon',
221 'event_level' => 'Payment',
224 'contact_id' => 'dummy.Individual',
225 // donation, 2 = member, 3 = campaign contribution, 4=event
226 'financial_type_id' => 1,
227 'total_amount' => 7.3,
230 //'activity_type_id' => 1,
231 'subject' => 'unit testing',
232 'source_contact_id' => 'dummy.Individual',
235 if ($type == 'Contact') {
236 $type = 'Individual';