Skip to content

Commit

Permalink
Convert the $expire argument to a valid value. Handle cases were $dat…
Browse files Browse the repository at this point in the history
…a is an object, or neither an object or array.
  • Loading branch information
weitzman committed Dec 7, 2013
1 parent 92e2733 commit 5a4a4aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
22 changes: 21 additions & 1 deletion commands/core/cache.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ function drush_cache_command_set($cid = NULL, $data = '', $bin = NULL, $expire =
$data = stream_get_contents(STDIN);
}

// Convert the "expire" argument to a valid value for Drupal's cache_set().
if ($expire == 'CACHE_TEMPORARY') {
$expire = CACHE_TEMPORARY;
}
if ($expire == 'CACHE_PERMANENT') {
$expire = CACHE_PERMANENT;
}

// Now, we parse the object.
switch (drush_get_option('format', 'string')) {
case 'json':
Expand All @@ -164,7 +172,19 @@ function drush_cache_command_set($cid = NULL, $data = '', $bin = NULL, $expire =
}

if (drush_get_option('cache-get')) {
$data = $data->data;
// $data might be an object.
if (is_object($data) && $data->data) {
$data = $data->data;
}
// But $data returned from `drush cache-get --format=json` will be an array.
elseif (is_array($data) && isset($data['data'])) {
$data = $data['data'];
}
else {
// If $data is neither object nor array and cache-get was specified, then
// there is a problem.
return drush_set_error('CACHE_INVALID_FORMAT', dt("'cache-get' was specified as an option, but the data is neither an object or an array."));
}
}

cache_set($cid, $data, $bin, $expire);
Expand Down
10 changes: 10 additions & 0 deletions tests/cacheCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public function testCacheGetSetClear() {
$data = $this->getOutputFromJSON('data');
$this->assertEquals($expected, $data);

// Test cache-set using all arguments and many options.
$expected = array('key' => 'value');
$input = array('data'=> $expected);
$stdin = json_encode($input);
$exec = sprintf('echo %s | %s cache-set %s %s my_cache_id - cache CACHE_PERMANENT --format=json --cache-get 2>/dev/null', self::escapeshellarg($stdin), UNISH_DRUSH, "--root=" . self::escapeshellarg($options['root']), '--uri=' . $options['uri']);
$return = $this->execute($exec);
$this->drush('cache-get', array('my_cache_id', 'cache'), $options + array('format' => 'json'));
$data = $this->getOutputFromJSON('data');
$this->assertEquals((object)$expected, $data);

// Test cache-clear all.
$this->drush('cache-clear', array('all'), $options);
$this->drush('cache-get', array('cache-test-cid'), $options + array('format' => 'json'), NULL, NULL, self::EXIT_ERROR);
Expand Down

0 comments on commit 5a4a4aa

Please sign in to comment.