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
;
31 class UnitTestCase
extends \PHPUnit\Framework\TestCase
implements HeadlessInterface
, TransactionalInterface
{
33 use TestDataLoaderTrait
;
36 * @see CiviUnitTestCase
40 * @param string $dataName
42 public function __construct($name = NULL, array $data = [], $dataName = '') {
43 parent
::__construct($name, $data, $dataName);
44 error_reporting(E_ALL
& ~E_NOTICE
);
47 public function setUpHeadless() {
48 return \Civi\Test
::headless()->apply();
52 * Tears down the fixture, for example, closes a network connection.
54 * This method is called after a test is executed.
56 public function tearDown() {
61 * Quick clean by emptying tables created for the test.
63 * @param array $params
65 public function cleanup($params) {
67 'tablesToTruncate' => [],
69 \CRM_Core_DAO
::executeQuery("SET FOREIGN_KEY_CHECKS = 0;");
70 foreach ($params['tablesToTruncate'] as $table) {
71 \Civi
::log()->info('truncating: ' . $table);
72 $sql = "TRUNCATE TABLE $table";
73 \CRM_Core_DAO
::executeQuery($sql);
75 \CRM_Core_DAO
::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
79 * Quick record counter
81 * @param string $table_name
82 * @returns int record count
84 public function getRowCount($table_name) {
85 $sql = "SELECT count(id) FROM $table_name";
86 return (int) \CRM_Core_DAO
::singleValueQuery($sql);
90 * Create sample entities (using V3 for now).
92 * @param array $params
93 * (type, seq, overrides, count)
95 * (either single, or array of array if count >1)
96 * @throws \CiviCRM_API3_Exception
99 public static function createEntity($params) {
106 for ($i = 0; $i < $params['count']; $i++
) {
108 $data = self
::sample($params);
109 $api_params = ['sequential' => 1] +
$data['sample_params'];
110 $result = civicrm_api3($data['entity'], 'create', $api_params);
111 if ($result['is_error']) {
112 throw new \
Exception("creating $data[entity] failed");
114 $entity = $result['values'][0];
115 if (!($entity['id'] > 0)) {
116 throw new \
Exception("created entity is malformed");
118 $entities[] = $entity;
120 return $params['count'] == 1 ?
$entity : $entities;
124 * Helper function for creating sample entities.
126 * Depending on the supplied sequence integer, plucks values from the dummy data.
127 * Constructs a foreign entity when an ID is required but isn't supplied in the overrides.
129 * Inspired by CiviUnitTestCase::
130 * @todo - extract this function to own class and share with CiviUnitTestCase?
131 * @param array $params
132 * - type: string roughly matching entity type
133 * - seq: (optional) int sequence number for the values of this type
134 * - overrides: (optional) array of fill in parameters
137 * - entity: string API entity type (usually the type supplied except for contact subtypes)
138 * - sample_params: array API sample_params properties of sample entity
140 public static function sample($params) {
145 $type = $params['type'];
146 // sample data - if field is array then chosed based on `seq`
148 if (in_array($type, ['Individual', 'Organization', 'Household'])) {
149 $sample_params['contact_type'] = $type;
155 // use the seq to pluck a set of params out
156 foreach (self
::sampleData($type) as $key => $value) {
157 if (is_array($value)) {
158 $sample_params[$key] = $value[$params['seq'] %
count($value)];
161 $sample_params[$key] = $value;
164 if ($type == 'Individual') {
165 $sample_params['email'] = strtolower(
166 $sample_params['first_name'] . '_' . $sample_params['last_name'] . '@civicrm.org'
168 $sample_params['prefix_id'] = 3;
169 $sample_params['suffix_id'] = 3;
171 if (!count($sample_params)) {
172 throw new \
Exception("unknown sample type: $type");
174 $sample_params = $params['overrides'] +
$sample_params;
175 // make foreign enitiies if they haven't been supplied
176 foreach ($sample_params as $key => $value) {
177 if (substr($value, 0, 6) === 'dummy.') {
178 $foreign_entity = self
::createEntity([
179 'type' => substr($value, 6),
180 'seq' => $params['seq'],
182 $sample_params[$key] = $foreign_entity['id'];
185 return compact("entity", "sample_params");
189 * Provider of sample data.
192 * Array values represent a set of allowable items.
193 * Strings in the form "dummy.Entity" require creating a foreign entity first.
195 public static function sampleData($type) {
198 // The number of values in each list need to be coprime numbers to not have duplicates
199 'first_name' => ['Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'],
200 'middle_name' => ['J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'],
201 'last_name' => ['Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'],
202 'contact_type' => 'Individual',
205 'organization_name' => [
206 'Unit Test Organization',
214 'household_name' => ['Unit Test household'],
217 'title' => 'Annual CiviCRM meet',
218 'summary' => 'If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now',
219 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
220 'event_type_id' => 1,
222 'start_date' => 20081021,
223 'end_date' => 20081023,
224 'is_online_registration' => 1,
225 'registration_start_date' => 20080601,
226 'registration_end_date' => 20081015,
227 'max_participants' => 100,
228 'event_full_text' => 'Sorry! We are already full',
231 'is_show_location' => 0,
234 'event_id' => 'dummy.Event',
235 'contact_id' => 'dummy.Individual',
238 'register_date' => 20070219,
239 'source' => 'Wimbeldon',
240 'event_level' => 'Payment',
243 'contact_id' => 'dummy.Individual',
244 // donation, 2 = member, 3 = campaign contribution, 4=event
245 'financial_type_id' => 1,
246 'total_amount' => 7.3,
249 //'activity_type_id' => 1,
250 'subject' => 'unit testing',
251 'source_contact_id' => 'dummy.Individual',
254 if ($type == 'Contact') {
255 $type = 'Individual';