diff options
Diffstat (limited to 'www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/QueryCounterTrait.php')
-rw-r--r-- | www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Traits/QueryCounterTrait.php | 43 |
1 files changed, 43 insertions, 0 deletions
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); + } + +} |