diff options
Diffstat (limited to 'www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits')
4 files changed, 159 insertions, 0 deletions
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/OptionCleanupTrait.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/OptionCleanupTrait.php new file mode 100644 index 00000000..06f43235 --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/OptionCleanupTrait.php @@ -0,0 +1,24 @@ +<?php + +namespace Civi\Test\Api4\Traits; + +trait OptionCleanupTrait { + + protected $optionGroupMaxId; + protected $optionValueMaxId; + + public function setUp() { + $this->optionGroupMaxId = \CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_option_group'); + $this->optionValueMaxId = \CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_option_value'); + } + + public function tearDown() { + if ($this->optionValueMaxId) { + \CRM_Core_DAO::executeQuery('DELETE FROM civicrm_option_value WHERE id > ' . $this->optionValueMaxId); + } + if ($this->optionGroupMaxId) { + \CRM_Core_DAO::executeQuery('DELETE FROM civicrm_option_group WHERE id > ' . $this->optionGroupMaxId); + } + } + +} diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/QueryCounterTrait.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/QueryCounterTrait.php new file mode 100644 index 00000000..c7e10f1b --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/QueryCounterTrait.php @@ -0,0 +1,43 @@ +<?php + +namespace Civi\Test\Api4\Traits; + +use \CRM_Utils_Array as ArrayHelper; + +trait QueryCounterTrait { + + /** + * @var int + */ + protected $startCount = 0; + + /** + * Start the query counter + */ + protected function beginQueryCount() { + $this->startCount = $this->getCurrentGlobalQueryCount(); + } + + /** + * @return int + * The number of queries since the counter was started + */ + protected function getQueryCount() { + return $this->getCurrentGlobalQueryCount() - $this->startCount; + } + + /** + * @return int + * @throws \Exception + */ + private function getCurrentGlobalQueryCount() { + global $_DB_DATAOBJECT; + + if (!$_DB_DATAOBJECT) { + throw new \Exception('Database object not set so cannot count queries'); + } + + return ArrayHelper::value('RESULTSEQ', $_DB_DATAOBJECT, 0); + } + +} diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TableDropperTrait.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TableDropperTrait.php new file mode 100644 index 00000000..6e543473 --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TableDropperTrait.php @@ -0,0 +1,23 @@ +<?php + +namespace Civi\Test\Api4\Traits; + +trait TableDropperTrait { + /** + * @param $prefix + */ + protected function dropByPrefix($prefix) { + $sql = "SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' ) " . + "AS statement FROM information_schema.tables " . + "WHERE table_name LIKE '%s%%' AND table_schema = DATABASE();"; + $sql = sprintf($sql, $prefix); + $dropTableQuery = \CRM_Core_DAO::executeQuery($sql); + $dropTableQuery->fetch(); + $dropTableQuery = $dropTableQuery->statement; + + if ($dropTableQuery) { + \CRM_Core_DAO::executeQuery($dropTableQuery); + } + } + +} diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TestDataLoaderTrait.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TestDataLoaderTrait.php new file mode 100644 index 00000000..1db22090 --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/TestDataLoaderTrait.php @@ -0,0 +1,69 @@ +<?php + +namespace Civi\Test\Api4\Traits; + +/** + * This probably should be a separate class + */ +trait TestDataLoaderTrait { + + /** + * @var array + * References to entities used for loading test data + */ + protected $references; + + /** + * Creates entities from a JSON data set + * + * @param $path + */ + protected function loadDataSet($path) { + if (!file_exists($path)) { + $path = __DIR__ . '/../DataSets/' . $path . '.json'; + } + + $dataSet = json_decode(file_get_contents($path), TRUE); + foreach ($dataSet as $entityName => $entities) { + foreach ($entities as $entityValues) { + + $entityValues = $this->replaceReferences($entityValues); + + $params = ['values' => $entityValues, 'checkPermissions' => FALSE]; + $result = civicrm_api4($entityName, 'create', $params); + if (isset($entityValues['@ref'])) { + $this->references[$entityValues['@ref']] = $result->first(); + } + } + } + } + + /** + * @param $name + * + * @return null|mixed + */ + protected function getReference($name) { + return isset($this->references[$name]) ? $this->references[$name] : NULL; + } + + /** + * @param array $entityValues + * + * @return array + */ + private function replaceReferences($entityValues) { + foreach ($entityValues as $name => $value) { + if (is_array($value)) { + $entityValues[$name] = $this->replaceReferences($value); + } + elseif (substr($value, 0, 4) === '@ref') { + $referenceName = substr($value, 5); + list ($reference, $property) = explode('.', $referenceName); + $entityValues[$name] = $this->references[$reference][$property]; + } + } + return $entityValues; + } + +} |