From 0d703d45d14387b861fda2cf2400c99e6670293c Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 7 Feb 2016 19:46:10 -0800 Subject: [PATCH] CRM-17860 - CiviTester - Rename `builder()` to `headless()`. Add `ext()` and `extDir()`. --- tests/phpunit/CiviTest/CiviTester.php | 92 +++++++++++++++++++++------ tests/phpunit/CiviTest/bootstrap.php | 2 +- 2 files changed, 75 insertions(+), 19 deletions(-) diff --git a/tests/phpunit/CiviTest/CiviTester.php b/tests/phpunit/CiviTest/CiviTester.php index d5266a7495..fbf3e00a1f 100644 --- a/tests/phpunit/CiviTest/CiviTester.php +++ b/tests/phpunit/CiviTest/CiviTester.php @@ -63,26 +63,27 @@ class CiviTester { * @return \CiviTesterBuilder * * @code - * CiviTester::builder()->apply(); - * CiviTester::builder()->sqlFile('ex.sql')->apply(); + * CiviTester::headless()->apply(); + * CiviTester::headless()->sqlFile('ex.sql')->apply(); * @endCode */ - public static function builder() { - if (!isset(self::$singletons['builder'])) { - $civiRoot = dirname(dirname(dirname(dirname(__FILE__)))); - self::$singletons['builder'] = new CiviTesterBuilder('CiviTesterSchema'); - self::$singletons['builder'] - ->callback(function ($ctx) { - $dbName = CiviTester::dsn('database'); - echo "Installing {$dbName} schema\n"; - CiviTester::schema()->dropAll(); - }, 'msg-drop') - ->sqlFile($civiRoot . "/sql/civicrm.mysql") - ->callback(function ($ctx) { - CiviTester::data()->populate(); - }, 'populate'); - } - return self::$singletons['builder']; + public static function headless() { + $civiRoot = dirname(dirname(dirname(dirname(__FILE__)))); + $builder = new CiviTesterBuilder('CiviTesterSchema'); + $builder + ->callback(function ($ctx) { + if (CIVICRM_UF !== 'UnitTests') { + throw new \RuntimeException("CiviTester::headless() requires CIVICRM_UF=UnitTests"); + } + $dbName = CiviTester::dsn('database'); + echo "Installing {$dbName} schema\n"; + CiviTester::schema()->dropAll(); + }, 'msg-drop') + ->sqlFile($civiRoot . "/sql/civicrm.mysql") + ->callback(function ($ctx) { + CiviTester::data()->populate(); + }, 'populate'); + return $builder; } /** @@ -354,6 +355,34 @@ class CiviTesterBuilder { return $this->addStep(new CiviTesterSqlFileStep($file)); } + /** + * Require an extension (based on its name). + * + * @param string $name + * @return \CiviTesterBuilder + */ + public function ext($name) { + return $this->addStep(new CiviTesterExtensionStep($name)); + } + + /** + * Require an extension (based on its directory). + * + * @param $dir + * @return \CiviTesterBuilder + * @throws \CRM_Extension_Exception_ParseException + */ + public function extDir($dir) { + while ($dir && dirname($dir) !== $dir && !file_exists("$dir/info.xml")) { + $dir = dirname($dir); + } + if (file_exists("$dir/info.xml")) { + $info = CRM_Extension_Info::loadFromFile("$dir/info.xml"); + $name = $info->key; + } + return $this->addStep(new CiviTesterExtensionStep($name)); + } + protected function assertValid() { foreach ($this->steps as $step) { if (!$step->isValid()) { @@ -540,3 +569,30 @@ class CiviTesterCallbackStep implements CiviTesterStep { } } + +class CiviTesterExtensionStep implements CiviTesterStep { + private $name; + + /** + * CiviTesterExtensionStep constructor. + * @param $name + */ + public function __construct($name) { + $this->name = $name; + } + + public function getSig() { + return 'ext:' . $this->name; + } + + public function isValid() { + return is_string($this->name); + } + + public function run($ctx) { + CRM_Extension_System::singleton()->getManager()->install(array( + $this->name, + )); + } + +} diff --git a/tests/phpunit/CiviTest/bootstrap.php b/tests/phpunit/CiviTest/bootstrap.php index 330c742259..e351917383 100644 --- a/tests/phpunit/CiviTest/bootstrap.php +++ b/tests/phpunit/CiviTest/bootstrap.php @@ -19,7 +19,7 @@ define('CIVICRM_TEST', 1); eval(cv('php:boot --level=settings', 'phpcode')); if (CIVICRM_UF === 'UnitTests') { - CiviTester::builder()->apply(); + CiviTester::headless()->apply(); } // ------------------------------------------------------------------------------ -- 2.25.1