Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for phpunit testing. #8

Merged
merged 3 commits into from Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,2 @@
tests/results.xml
vendor
@@ -0,0 +1,10 @@
{
"require-dev": {
"drush/drush": "^8.0"
},
"autoload-dev": {
"psr-0": {
"HostingCiviTest": "tests/"
}
}
}
@@ -20,9 +20,26 @@ function provision_civicrm_tests_drush_command() {
),
);

$items['provision-civicrm-tests-run-pending'] = array(
'description' => dt('Runs provision_civicrm pending tasks'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
// Although we're a provision command, we require hostmaster to be around to
// run the tests correctly
'drupal dependencies' => array(
'hosting',
),
);

return $items;
}

/**
* Drush command to run pending tasks.
*/
function drush_provision_civicrm_tests_run_pending() {
drush_provision_civicrm_tests_run_remaining_tasks();
}

/**
* Drush command to run the provision tests.
*/
@@ -0,0 +1,18 @@
<?php

namespace HostingCiviTest\Command;

class PlatformDelete {

/**
* Helper function to remove a platform.
*/
public static function run($platform_name) {
// FIXME: normally we should use backend_invoke_foo(), but the
// hostmaster context was not successfully bootstrapped, so the
// commands aren't found.
exec('drush @hm hosting-task ' . drush_escapeshellarg("@platform_$platform_name") . ' delete');
exec('drush @hm provision-civicrm-tests-run-pending');
}

}
@@ -0,0 +1,43 @@
<?php

namespace HostingCiviTest\Command;

class PlatformInstall {

/**
* Helper function to install a platform.
*/
public static function run($platform_name, $platform_alias = NULL) {
$makes_dir = dirname(__FILE__) . '/../../../drush/provision_civicrm_tests/makes';

if (is_null($platform_alias)) {
$platform_alias = $platform_name;
}

drush_log(dt('Building platform: @platform and adding to hostmaster.', array('@platform' => $platform_alias)), 'ok');

$args = array(
$makes_dir . "/$platform_name.build",
"/var/aegir/platforms/$platform_alias"
);

drush_invoke_process('@none', 'make', $args);

$args = array(
"@platform_$platform_alias",
);

$options = array(
'root' => "/var/aegir/platforms/$platform_alias",
'context_type' => 'platform',
);

drush_invoke_process('@none', 'provision-save', $args, $options);

// FIXME: normally we should use backend_invoke_foo(), but the
// hostmaster context was not successfully bootstrapped, so the
// commands aren't found.
exec('drush @hm hosting-import ' . drush_escapeshellarg("@platform_$platform_alias"));
exec('drush @hm provision-civicrm-tests-run-pending');
}
}
@@ -0,0 +1,55 @@
<?php

namespace HostingCiviTest;

/**
* Base class for Hosting CiviCRM unit tests
*
* Those tests will run in a bootstrapped Drush environment
*
* This should be ran in separate processes, which the following
* annotation should do in 3.6 and above:
*
* This file is inspired from the UnitUnishTestCase.php file from Drush
* and from CiviCRM's phpunit tests.
*
* @runTestsInSeparateProcesses
*/
abstract class HostingCiviTestCase extends \PHPUnit_Framework_TestCase {

function __construct($name = NULL, array $data = array(), $dataName = '') {
parent::__construct($name, $data, $dataName);
}

/**
* Minimally bootstrap drush
*
* This is equivalent to the level DRUSH_BOOTSTRAP_NONE, as we
* haven't run drush_bootstrap() yet. To do anything, you'll need to
* bootstrap to some level using drush_bootstrap().
*
* @see drush_bootstrap()
*/
public static function setUpBeforeClass() {
parent::setUpBeforeClass();

require_once('vendor/drush/drush/includes/preflight.inc');
drush_preflight_prepare();

// Try to do a full Hostmaster bootstrap
drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL);

// Need to set DRUSH_COMMAND so that drush will be called and not phpunit
define('DRUSH_COMMAND', '/usr/local/bin/drush');
}

public static function tearDownAfterClass() {
parent::tearDownAfterClass();
\drush_postflight();
}

function drush_major_version() {
return DRUSH_MAJOR_VERSION;
}

}
@@ -0,0 +1,23 @@
<?php

/**
* @file
* Initialize a sandboxed environment. Starts with call hosting_civicrm_tests_init() at bottom.
*/
// This code copied from drush_bootstrap_prepare() except for 1st line.
$local_vendor_path = dirname(__DIR__) . '/vendor/autoload.php';

// Check for a local composer install or a global composer install. Vendor dirs are in different spots.
if ((!@include $local_vendor_path)) {
$msg = "Unable to load autoload.php. Drush now requires Composer in order to install its dependencies and autoload classes. Please see http://docs.drush.org/en/master/install/\n";
fwrite(STDERR, $msg);
return FALSE;
}

hosting_civicrm_tests_init();

/**
* Initialize our environment at the start of each run (i.e. suite).
*/
function hosting_civicrm_tests_init() {
}
@@ -0,0 +1,70 @@
<?php

namespace HostingCiviTest;

/**
* Tests that phpunit is working.
*/
class installTest extends HostingCiviTestCase {
/**
* @param string|null $name
*/
public function __construct($name = NULL) {
parent::__construct($name);
}

/**
* Called before the test functions will be executed.
* this function is defined in PHPUnit_TestCase and overwritten
* here
*/
public function setUp() {
Command\PlatformInstall::run('civicrm43d7');
Command\PlatformInstall::run('civicrm44d7');
Command\PlatformInstall::run('civicrm46d7');
Command\PlatformInstall::run('civicrm46d6');
Command\PlatformInstall::run('civicrm47d7');
}

/**
* Called after the test functions are executed.
* this function is defined in PHPUnit_TestCase and overwritten
* here.
*/
public function tearDown() {
Command\PlatformDelete::run('civicrm43d7');
Command\PlatformDelete::run('civicrm44d7');
Command\PlatformDelete::run('civicrm46d7');
Command\PlatformDelete::run('civicrm46d6');
Command\PlatformDelete::run('civicrm47d7');
}

/**
* Test the toString function.
*/
public function testHello() {
$result = 'hello';
$expected = 'hello';
$this->assertEquals($result, $expected);
}

/**
* Test the installation and deletion of sites with CiviCRM.
*/
public function testInstallAndDelete() {
/*
$this->installSite('civicrm43d7', 'civicrm43d7-standard');
$this->removeSite('civicrm43d7-standard');

$this->installSite('civicrm44d7', 'civicrm44d7-standard');
$this->removeSite('civicrm44d7-standard');

$this->installSite('civicrm46d7', 'civicrm46d7-standard');
$this->removeSite('civicrm46d7-standard');

$this->installSite('civicrm46d6', 'civicrm46d6', 'default');
$this->removeSite('civicrm46d6-default');
*/
}

}
@@ -0,0 +1,16 @@
<!-- Copy and rename to phpunit.xml. Customize as needed. -->
<phpunit backupGlobals="false"
backupStaticAttributes="false"
syntaxCheck="false"
bootstrap="bootstrap.inc"
colors="true">
<testsuites>
<testsuite name="Hosting CiviCRM Test Suite">
<directory>.</directory>
</testsuite>
</testsuites>

<php>
<includePath>.</includePath>
</php>
</phpunit>