From be1f3a347a74660a5f519cc3604be195414e83be Mon Sep 17 00:00:00 2001
From: sarvesh21 <sarvesh211999@gmail.com>
Date: Fri, 21 Jun 2019 07:30:02 +0000
Subject: [PATCH] added test case for data processor creation

---
 .../CRM/Myextension/2MyHeadlessTest.php       | 54 +++++++++++++
 .../Myextension/CreateDataProcessorTest.php   | 76 +++++++++++++++++++
 tests/phpunit/bootstrap.php                   | 62 +++++++++++++++
 3 files changed, 192 insertions(+)
 create mode 100644 tests/phpunit/CRM/Myextension/2MyHeadlessTest.php
 create mode 100644 tests/phpunit/CRM/Myextension/CreateDataProcessorTest.php
 create mode 100644 tests/phpunit/bootstrap.php

diff --git a/tests/phpunit/CRM/Myextension/2MyHeadlessTest.php b/tests/phpunit/CRM/Myextension/2MyHeadlessTest.php
new file mode 100644
index 00000000..d4c71f7e
--- /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 00000000..266c6391
--- /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 00000000..afa827e9
--- /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)");
+  }
+}
-- 
GitLab