diff --git a/tests/phpunit/CRM/Myextension/2MyHeadlessTest.php b/tests/phpunit/CRM/Myextension/2MyHeadlessTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d4c71f7e0ee9bedc6c1b292ab3bbde048c1822a5 --- /dev/null +++ b/tests/phpunit/CRM/Myextension/2MyHeadlessTest.php @@ -0,0 +1,54 @@ +<?php + +use CRM_Dataprocessor_ExtensionUtil as E; +use Civi\Test\HeadlessInterface; +use Civi\Test\HookInterface; +use Civi\Test\TransactionalInterface; + +/** + * FIXME - Add test description. + * + * Tips: + * - With HookInterface, you may implement CiviCRM hooks directly in the test class. + * Simply create corresponding functions (e.g. "hook_civicrm_post(...)" or similar). + * - With TransactionalInterface, any data changes made by setUp() or test****() functions will + * rollback automatically -- as long as you don't manipulate schema or truncate tables. + * If this test needs to manipulate schema or truncate tables, then either: + * a. Do all that using setupHeadless() and Civi\Test. + * b. Disable TransactionalInterface, and handle all setup/teardown yourself. + * + * @group headless + */ +class CRM_Myextension_2MyHeadlessTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { + + public function setUpHeadless() { + // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). + // See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest + return \Civi\Test::headless() + ->installMe(__DIR__) + ->apply(); + } + + public function setUp() { + parent::setUp(); + } + + public function tearDown() { + parent::tearDown(); + } + + /** + * Example: Test that a version is returned. + */ + public function testWellFormedVersion() { + $this->assertRegExp('/^([0-9\.]|alpha|beta)*$/', \CRM_Utils_System::version()); + } + + /** + * Example: Test that we're using a fake CMS. + */ + public function testWellFormedUF() { + $this->assertEquals('UnitTests', CIVICRM_UF); + } + +} diff --git a/tests/phpunit/CRM/Myextension/CreateDataProcessorTest.php b/tests/phpunit/CRM/Myextension/CreateDataProcessorTest.php new file mode 100644 index 0000000000000000000000000000000000000000..266c639196bc2d67e862cd5da39a3f311bfeda7f --- /dev/null +++ b/tests/phpunit/CRM/Myextension/CreateDataProcessorTest.php @@ -0,0 +1,76 @@ +<?php + +use CRM_Dataprocessor_ExtensionUtil as E; +use Civi\Test\HeadlessInterface; +use Civi\Test\HookInterface; +use Civi\Test\TransactionalInterface; + +/** + * FIXME - Add test description. + * + * Tips: + * - With HookInterface, you may implement CiviCRM hooks directly in the test class. + * Simply create corresponding functions (e.g. "hook_civicrm_post(...)" or similar). + * - With TransactionalInterface, any data changes made by setUp() or test****() functions will + * rollback automatically -- as long as you don't manipulate schema or truncate tables. + * If this test needs to manipulate schema or truncate tables, then either: + * a. Do all that using setupHeadless() and Civi\Test. + * b. Disable TransactionalInterface, and handle all setup/teardown yourself. + * + * @group headless + */ +class CRM_Myextension_MyHeadlessTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { + + public function setUpHeadless() { + // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). + // See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest + return \Civi\Test::headless() + ->installMe(__DIR__) + ->apply(); + } + + public function setUp() { + parent::setUp(); + } + + public function tearDown() { + parent::tearDown(); + } + + public function testCreateDataProcessor() { + + $params['name'] = "test"; + $params['title'] = "title"; + $params['description'] = 'Creating a Test Description'; + $params['is_active'] = 1; + + // Creating a DataProcessor + civicrm_api3('DataProcessor', 'create', $params); + + // Retrieving the data processor + $result = civicrm_api3('DataProcessor', 'get'); + + // Retrieving the id of data processor + $id = $result['id']; + + $this->assertEquals('test', $result['values'][$id]['name']); + $this->assertEquals('title', $result['values'][$id]['title']); + $this->assertEquals('Creating a Test Description', $result['values'][$id]['description']); + $this->assertEquals(1, $result['values'][$id]['is_active']); + + } + /** + * Example: Test that a version is returned. + */ + public function testWellFormedVersion() { + $this->assertRegExp('/^([0-9\.]|alpha|beta)*$/', \CRM_Utils_System::version()); + } + + /** + * Example: Test that we're using a fake CMS. + */ + public function testWellFormedUF() { + $this->assertEquals('UnitTests', CIVICRM_UF); + } + +} diff --git a/tests/phpunit/bootstrap.php b/tests/phpunit/bootstrap.php new file mode 100644 index 0000000000000000000000000000000000000000..afa827e95ae842a2cc0acdcdeee69b8fb018f0f5 --- /dev/null +++ b/tests/phpunit/bootstrap.php @@ -0,0 +1,62 @@ +<?php + +ini_set('memory_limit', '2G'); +ini_set('safe_mode', 0); +eval(cv('php:boot --level=classloader', 'phpcode')); + +// Allow autoloading of PHPUnit helper classes in this extension. +$loader = new \Composer\Autoload\ClassLoader(); +$loader->add('CRM_', __DIR__); +$loader->add('Civi\\', __DIR__); +$loader->add('api_', __DIR__); +$loader->add('api\\', __DIR__); +$loader->register(); + +/** + * Call the "cv" command. + * + * @param string $cmd + * The rest of the command to send. + * @param string $decode + * Ex: 'json' or 'phpcode'. + * @return string + * Response output (if the command executed normally). + * @throws \RuntimeException + * If the command terminates abnormally. + */ +function cv($cmd, $decode = 'json') { + $cmd = 'cv ' . $cmd; + $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR); + $oldOutput = getenv('CV_OUTPUT'); + putenv("CV_OUTPUT=json"); + + // Execute `cv` in the original folder. This is a work-around for + // phpunit/codeception, which seem to manipulate PWD. + $cmd = sprintf('cd %s; %s', escapeshellarg(getenv('PWD')), $cmd); + + $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__); + putenv("CV_OUTPUT=$oldOutput"); + fclose($pipes[0]); + $result = stream_get_contents($pipes[1]); + fclose($pipes[1]); + if (proc_close($process) !== 0) { + throw new RuntimeException("Command failed ($cmd):\n$result"); + } + switch ($decode) { + case 'raw': + return $result; + + case 'phpcode': + // If the last output is /*PHPCODE*/, then we managed to complete execution. + if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") { + throw new \RuntimeException("Command failed ($cmd):\n$result"); + } + return $result; + + case 'json': + return json_decode($result, 1); + + default: + throw new RuntimeException("Bad decoder format ($decode)"); + } +}