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
24 use api\v
4\Traits\TestDataLoaderTrait
;
25 use Civi\Test\HeadlessInterface
;
26 use Civi\Test\TransactionalInterface
;
28 require_once 'api/Exception.php';
33 class UnitTestCase
extends \PHPUnit\Framework\TestCase
implements HeadlessInterface
, TransactionalInterface
{
35 use TestDataLoaderTrait
;
38 * @see CiviUnitTestCase
42 * @param string $dataName
44 public function __construct($name = NULL, array $data = [], $dataName = '') {
45 parent
::__construct($name, $data, $dataName);
46 error_reporting(E_ALL
& ~E_NOTICE
);
49 public function setUpHeadless() {
50 return \Civi\Test
::headless()->apply();
54 * Tears down the fixture, for example, closes a network connection.
56 * This method is called after a test is executed.
58 public function tearDown() {
63 * Quick clean by emptying tables created for the test.
65 * @param array $params
67 public function cleanup($params) {
69 'tablesToTruncate' => [],
71 \CRM_Core_DAO
::executeQuery("SET FOREIGN_KEY_CHECKS = 0;");
72 foreach ($params['tablesToTruncate'] as $table) {
73 \Civi
::log()->info('truncating: ' . $table);
74 $sql = "TRUNCATE TABLE $table";
75 \CRM_Core_DAO
::executeQuery($sql);
77 \CRM_Core_DAO
::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
81 * Quick record counter
83 * @param string $table_name
84 * @returns int record count
86 public function getRowCount($table_name) {
87 $sql = "SELECT count(id) FROM $table_name";
88 return (int) \CRM_Core_DAO
::singleValueQuery($sql);
92 * Create sample entities (using V3 for now).
94 * @param array $params
95 * (type, seq, overrides, count)
97 * (either single, or array of array if count >1)
98 * @throws \CiviCRM_API3_Exception
101 public static function createEntity($params) {
108 for ($i = 0; $i < $params['count']; $i++
) {
110 $data = self
::sample($params);
111 $api_params = ['sequential' => 1] +
$data['sample_params'];
112 $result = civicrm_api3($data['entity'], 'create', $api_params);
113 if ($result['is_error']) {
114 throw new \
Exception("creating $data[entity] failed");
116 $entity = $result['values'][0];
117 if (!($entity['id'] > 0)) {
118 throw new \
Exception("created entity is malformed");
120 $entities[] = $entity;
122 return $params['count'] == 1 ?
$entity : $entities;
126 * Helper function for creating sample entities.
128 * Depending on the supplied sequence integer, plucks values from the dummy data.
129 * Constructs a foreign entity when an ID is required but isn't supplied in the overrides.
131 * Inspired by CiviUnitTestCase::
132 * @todo - extract this function to own class and share with CiviUnitTestCase?
133 * @param array $params
134 * - type: string roughly matching entity type
135 * - seq: (optional) int sequence number for the values of this type
136 * - overrides: (optional) array of fill in parameters
139 * - entity: string API entity type (usually the type supplied except for contact subtypes)
140 * - sample_params: array API sample_params properties of sample entity
142 public static function sample($params) {
147 $type = $params['type'];
148 // sample data - if field is array then chosed based on `seq`
150 if (in_array($type, ['Individual', 'Organization', 'Household'])) {
151 $sample_params['contact_type'] = $type;
157 // use the seq to pluck a set of params out
158 foreach (self
::sampleData($type) as $key => $value) {
159 if (is_array($value)) {
160 $sample_params[$key] = $value[$params['seq'] %
count($value)];
163 $sample_params[$key] = $value;
166 if ($type == 'Individual') {
167 $sample_params['email'] = strtolower(
168 $sample_params['first_name'] . '_' . $sample_params['last_name'] . '@civicrm.org'
170 $sample_params['prefix_id'] = 3;
171 $sample_params['suffix_id'] = 3;
173 if (!count($sample_params)) {
174 throw new \
Exception("unknown sample type: $type");
176 $sample_params = $params['overrides'] +
$sample_params;
177 // make foreign enitiies if they haven't been supplied
178 foreach ($sample_params as $key => $value) {
179 if (substr($value, 0, 6) === 'dummy.') {
180 $foreign_entity = self
::createEntity([
181 'type' => substr($value, 6),
182 'seq' => $params['seq'],
184 $sample_params[$key] = $foreign_entity['id'];
187 return compact("entity", "sample_params");
191 * Provider of sample data.
194 * Array values represent a set of allowable items.
195 * Strings in the form "dummy.Entity" require creating a foreign entity first.
197 public static function sampleData($type) {
200 // The number of values in each list need to be coprime numbers to not have duplicates
201 'first_name' => ['Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'],
202 'middle_name' => ['J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'],
203 'last_name' => ['Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'],
204 'contact_type' => 'Individual',
207 'organization_name' => [
208 'Unit Test Organization',
216 'household_name' => ['Unit Test household'],
219 'title' => 'Annual CiviCRM meet',
220 'summary' => 'If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now',
221 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
222 'event_type_id' => 1,
224 'start_date' => 20081021,
225 'end_date' => 20081023,
226 'is_online_registration' => 1,
227 'registration_start_date' => 20080601,
228 'registration_end_date' => 20081015,
229 'max_participants' => 100,
230 'event_full_text' => 'Sorry! We are already full',
233 'is_show_location' => 0,
236 'event_id' => 'dummy.Event',
237 'contact_id' => 'dummy.Individual',
240 'register_date' => 20070219,
241 'source' => 'Wimbeldon',
242 'event_level' => 'Payment',
245 'contact_id' => 'dummy.Individual',
246 // donation, 2 = member, 3 = campaign contribution, 4=event
247 'financial_type_id' => 1,
248 'total_amount' => 7.3,
251 //'activity_type_id' => 1,
252 'subject' => 'unit testing',
253 'source_contact_id' => 'dummy.Individual',
256 if ($type == 'Contact') {
257 $type = 'Individual';