Commit 3b87df1f authored by jaapjansma's avatar jaapjansma

added unit tests

parent d0801dfb
......@@ -5,7 +5,7 @@ namespace Civi\ActionProvider\Action;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\ParameterBag;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Exception\InvalidParametersException;
use \Civi\ActionProvider\Exception\InvalidParameterException;
use \Civi\ActionProvider\Exception\InvalidConfigurationException;
/**
......@@ -86,7 +86,7 @@ abstract class AbstractAction {
throw new InvalidConfigurationException("Found invalid configuration for the action: ".$this->getTitle());
}
if (!$this->validateParameters($parameters)) {
throw new InvalidParametersException("Found invalid configuration for the action: ".$this->getTitle());
throw new InvalidParameterException("Found invalid configuration for the action: ".$this->getTitle());
}
$this->doAction($parameters);
......@@ -103,6 +103,9 @@ abstract class AbstractAction {
* @return bool;
*/
protected function validateConfiguration() {
if ($this->configuration === null) {
return false;
}
return SpecificationBag::validate($this->configuration, $this->getConfigurationSpecification());
}
......
<?xml version="1.0"?>
<phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" bootstrap="tests/phpunit/bootstrap.php">
<testsuites>
<testsuite name="My Test Suite">
<directory>./tests/phpunit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./</directory>
</whitelist>
</filter>
<listeners>
<listener class="Civi\Test\CiviTestListener">
<arguments/>
</listener>
</listeners>
</phpunit>
<?php
abstract class Civi_ActionProvider_Action_Action extends \PHPUnit_Framework_TestCase {
/**
* @var \Civi\ActionProvider\Provider
*/
protected $provider;
public function setup() {
parent::setup();
\Civi::reset();
$civi_container = \Civi::container();
$action_provider_container = $civi_container->get('action_provider');
$this->provider = $action_provider_container->getDefaultProvider();
}
}
<?php
require_once(__DIR__.'/Action.php');
use CRM_ActionProvider_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 Civi_ActionProvider_Action_AddToGroupTest extends \Civi_ActionProvider_Action_Action implements HeadlessInterface, TransactionalInterface {
protected $contactId;
protected $groupId;
public function setUpHeadless() {
// Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
// See: https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
return \Civi\Test::headless()
->installMe(__DIR__)
->apply();
}
public function setUp() {
$result = civicrm_api3("Contact","create",array(
'contact_type' => 'Individual',
'first_name' => 'Adele',
'last_name' => 'Jensen'
));
$this->contactId=$result['id'];
$result = civicrm_api3('Group','create',array(
'title' => "TestGroup",
'name' => "test_group",
));
$this->groupId = $result['id'];
parent::setUp();
}
public function tearDown() {
parent::tearDown();
}
/**
* First test invalid configuration, then a valid one
* and then check whether the contact is added to the group. *
*/
public function testAction() {
$action = $this->provider->getActionByName('AddToGroup');
try {
$action->execute($this->provider->createParameterBag());
} catch (Exception $e) {
$this->assertInstanceOf('\Civi\ActionProvider\Exception\InvalidConfigurationException', $e, 'Wrong exception thrown');
}
// Now set a valid configuration
$config = $this->provider->createParameterBag();
$config->setParameter('group_id', $this->groupId);
$action->setConfiguration($config);
try {
$action->execute($this->provider->createParameterBag());
} catch (Exception $e) {
$this->assertInstanceOf('\Civi\ActionProvider\Exception\InvalidParameterException', $e, 'Wrong exception thrown');
}
}
}
?>
\ No newline at end of file
<?php
use CRM_ActionProvider_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 Civi_ActionProvider_ContainerTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, TransactionalInterface {
public function setUpHeadless() {
// Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
// See: https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
return \Civi\Test::headless()
->installMe(__DIR__)
->apply();
}
public function setUp() {
parent::setUp();
}
public function tearDown() {
parent::tearDown();
}
/**
* Test whether the container is created
* and whether the default functions work as exepcted
*
*/
public function testContainer() {
\Civi::reset(); // Reset the civi container so its rebuild again
$civi_container = \Civi::container();
$action_provider_container = $civi_container->get('action_provider');
$this->assertInstanceOf('\Civi\ActionProvider\Container', $action_provider_container, 'The container is not created successfully');
$defaultProvider = $action_provider_container->getDefaultProvider();
$this->assertInstanceOf('\Civi\ActionProvider\Provider', $defaultProvider, 'Default provider is not set correctly');
$action_provider_container->addProviderWithContext('unittest', new \Civi\ActionProvider\Provider());
$contextProvider = $action_provider_container->getProviderByContext('unittest');
$this->assertNotSame($defaultProvider, $contextProvider, 'Context provider should differ from the default provider');
$this->assertInstanceOf('\Civi\ActionProvider\Provider', $contextProvider, 'Context provider is not set correctly');
}
}
<?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");
$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)");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment