Skip to content

Commit

Permalink
self::sites is new static variable in test cases.
Browse files Browse the repository at this point in the history
Get to it with a new $this->getSites() method.

This enabled me to refactor configTest to use separate test methods without having to reinstall Drupal for every one. Other tests will get similar mimprovement soon.
  • Loading branch information
weitzman committed Apr 24, 2014
1 parent aab054e commit 5b4dddf
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 25 deletions.
4 changes: 0 additions & 4 deletions tests/Unish/CommandUnishTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ abstract class CommandUnishTestCase extends UnishTestCase {
// Unix exit codes.
const EXIT_SUCCESS = 0;
const EXIT_ERROR = 1;
/**
* An array of Drupal sites that are setup in the drush-sandbox.
*/
var $sites;

/*
* Array of code coverage data collected during a single test.
Expand Down
20 changes: 18 additions & 2 deletions tests/Unish/UnishTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

abstract class UnishTestCase extends \PHPUnit_Framework_TestCase {

/**
* A list of Drupal sites that have been recently installed.
*
* @var array
*/
private static $sites = array();

function __construct($name = NULL, array $data = array(), $dataName = '') {
$this->_output = FALSE;
parent::__construct($name, $data, $dataName);
Expand Down Expand Up @@ -49,6 +56,7 @@ public static function tearDownAfterClass() {
if (file_exists(UNISH_SANDBOX)) {
unish_file_delete_recursive(UNISH_SANDBOX, TRUE);
}
self::$sites = array();
}

/**
Expand Down Expand Up @@ -221,10 +229,18 @@ function webroot() {
return UNISH_SANDBOX . '/web';
}

function getSites() {
return self::$sites;
}

function directory_cache($subdir = '') {
return getenv('CACHE_PREFIX') . '/' . $subdir;
}

/**
* @param $env
* @return string
*/
function db_url($env) {
return substr(UNISH_DB_URL, 0, 6) == 'sqlite' ? "sqlite://sites/$env/files/unish.sqlite" : UNISH_DB_URL . '/unish_' . $env;
}
Expand Down Expand Up @@ -273,14 +289,14 @@ function setUpDrupal($num_sites = 1, $install = FALSE, $version_string = UNISH_D

// Stash details about each site.
foreach ($sites_subdirs as $subdir) {
$this->sites[$subdir] = array(
self::$sites[$subdir] = array(
'db_url' => $this->db_url($subdir),
);
// Make an alias for the site
$alias_definition = array($subdir => array('root' => $root, 'uri' => $subdir));
file_put_contents(UNISH_SANDBOX . '/etc/drush/' . $subdir . '.alias.drushrc.php', $this->unish_file_aliases($alias_definition));
}
return $this->sites;
return self::$sites;
}

function fetchInstallDrupal($env = 'dev', $install = FALSE, $version_string = UNISH_DRUPAL_MAJOR_VERSION, $profile = NULL) {
Expand Down
2 changes: 1 addition & 1 deletion tests/cacheCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testCacheGetSetClear() {
$options = array(
'yes' => NULL,
'root' => $this->webroot(),
'uri' => key($this->sites),
'uri' => key($this->getSites()),
);

// Test the cache get command.
Expand Down
30 changes: 21 additions & 9 deletions tests/configTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@

/**
* Tests for Configuration Management commands for D8+.
*
* @group commands
*/
class ConfigCase extends CommandUnishTestCase {

function testConfig() {
function setUp() {
if (UNISH_DRUPAL_MAJOR_VERSION < 8) {
$this->markTestSkipped('Config only available on D8+.');
}

// Remove the '.x' once there is a stable release.
$sites = $this->setUpDrupal(1, TRUE, '8.x');
$options = array(
'yes' => NULL,
'root' => $this->webroot(),
'uri' => key($sites),
);
if (!$this->getSites()) {
// Remove the '.x' once there is a stable release.
$this->setUpDrupal(1, TRUE, '8.x');
}
}

function testConfigGetSet() {
$options = $this->options();
$this->drush('config-set', array('system.site', 'name', 'config_test'), $options);
$this->drush('config-get', array('system.site', 'name'), $options);
$this->assertEquals("'system.site:name': config_test\n", $this->getOutput(), 'Config was successfully set and get.');
}

function testConfigList() {
$options = $this->options();
$this->drush('config-list', array(), $options);
$result = $this->getOutputAsList();
$this->assertNotEmpty($result, 'An array of config names was returned.');
Expand All @@ -38,7 +40,10 @@ function testConfig() {
$this->drush('config-list', array('system'), $options + array('format' => 'json'));
$result = $this->getOutputFromJSON();
$this->assertNotEmpty($result, 'Valid, non-empty JSON output was returned.');
}

function testConfigExportImport() {
$options = $this->options();
// Get path to staging dir.
$this->drush('core-status', array(), $options + array('format' => 'json'));
$staging = $this->webroot() . '/' . $this->getOutputFromJSON('config-staging');
Expand All @@ -56,6 +61,13 @@ function testConfig() {
$this->drush('config-get', array('system.site', 'page'), $options + array('format' => 'json'));
$page = $this->getOutputFromJSON('system.site:page');
$this->assertContains('unish', $page->front, 'Config was successfully imported.');
}

function options() {
return array(
'yes' => NULL,
'root' => $this->webroot(),
'uri' => key($this->getSites()),
);
}
}
2 changes: 1 addition & 1 deletion tests/contextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class contextCase extends CommandUnishTestCase {

function setUpPaths() {
$this->log("webroot: ".$this->webroot()."\n");
$this->env = key($this->sites);
$this->env = key($this->getSites());
$this->site = $this->webroot() . '/sites/' . $this->env;
$this->home = UNISH_SANDBOX . '/home';
$this->paths = array(
Expand Down
12 changes: 6 additions & 6 deletions tests/coreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class coreCase extends CommandUnishTestCase {
function testRsyncPercentFiles() {
$this->setUpDrupal(1, TRUE);
$root = $this->webroot();
$site = key($this->sites);
$site = key($this->getSites());
$options = array(
'root' => $root,
'uri' => key($this->sites),
'uri' => key($this->getSites()),
'simulate' => NULL,
'include-conf' => NULL,
'include-vcs' => NULL,
Expand All @@ -39,10 +39,10 @@ function testRsyncPercentFiles() {
function testPercentFilesOptimization() {
$this->setUpDrupal(1, TRUE);
$root = $this->webroot();
$site = key($this->sites);
$site = key($this->getSites());
$options = array(
'root' => $root,
'uri' => key($this->sites),
'uri' => key($this->getSites()),
'simulate' => NULL,
'include-conf' => NULL,
'include-vcs' => NULL,
Expand Down Expand Up @@ -90,7 +90,7 @@ function testDrupalDirectory() {
$sitewide = $this->drupalSitewideDirectory();
$options = array(
'root' => $root,
'uri' => key($this->sites),
'uri' => key($this->getSites()),
'yes' => NULL,
'skip' => NULL,
'cache' => NULL,
Expand Down Expand Up @@ -128,7 +128,7 @@ function testCoreRequirements() {
$root = $this->webroot();
$options = array(
'root' => $root,
'uri' => key($this->sites),
'uri' => key($this->getSites()),
'pipe' => NULL,
'ignore' => 'cron,http requests,update_core', // no network access when running in tests, so ignore these
'strict' => 0, // invoke from script: do not verify options
Expand Down
2 changes: 1 addition & 1 deletion tests/pmUpdateCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function testUpdateCode() {

$options = array(
'root' => $this->webroot(),
'uri' => key($this->sites), // Have to access class property since $sites in in setUp().
'uri' => key($this->getSites()),
'yes' => NULL,
'backup-dir' => UNISH_SANDBOX . '/backups',
'cache' => NULL,
Expand Down
2 changes: 1 addition & 1 deletion tests/sqlDumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function testSqlDump() {
return;
}

$this->sites = $this->setUpDrupal(1, TRUE);
$this->setUpDrupal(1, TRUE);
$root = $this->webroot();
$uri = 'dev';
$full_dump_file_path = UNISH_SANDBOX . DIRECTORY_SEPARATOR . 'full_db.sql';
Expand Down

0 comments on commit 5b4dddf

Please sign in to comment.