Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/6878-undefined-index-errors-in-memory-adapter-on…
Browse files Browse the repository at this point in the history
…-undefined-namespaces'

Close zendframework/zendframework#6878
  • Loading branch information
Ocramius committed Nov 22, 2014
2 parents 869b243 + e2a5343 commit 66ae533
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
54 changes: 22 additions & 32 deletions src/Storage/Adapter/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,12 @@ public function clearByPrefix($prefix)
public function setTags($key, array $tags)
{
$ns = $this->getOptions()->getNamespace();
if (!$this->data[$ns]) {
if (!isset($this->data[$ns][$key])) {
return false;
}

$data = & $this->data[$ns];
if (isset($data[$key])) {
$data[$key]['tags'] = $tags;
return true;
}

return false;
$this->data[$ns][$key]['tags'] = $tags;
return true;
}

/**
Expand All @@ -249,16 +244,11 @@ public function setTags($key, array $tags)
public function getTags($key)
{
$ns = $this->getOptions()->getNamespace();
if (!$this->data[$ns]) {
return false;
}

$data = & $this->data[$ns];
if (!isset($data[$key])) {
if (!isset($this->data[$ns][$key])) {
return false;
}

return isset($data[$key]['tags']) ? $data[$key]['tags'] : array();
return isset($this->data[$ns][$key]['tags']) ? $this->data[$ns][$key]['tags'] : array();
}

/**
Expand All @@ -274,7 +264,7 @@ public function getTags($key)
public function clearByTags(array $tags, $disjunction = false)
{
$ns = $this->getOptions()->getNamespace();
if (!$this->data[$ns]) {
if (!isset($this->data[$ns])) {
return true;
}

Expand Down Expand Up @@ -654,16 +644,16 @@ protected function internalRemoveItem(& $normalizedKey)
*/
protected function internalIncrementItem(& $normalizedKey, & $value)
{
$ns = $this->getOptions()->getNamespace();
$data = & $this->data[$ns];
if (isset($data[$normalizedKey])) {
$data[$normalizedKey][0]+= $value;
$data[$normalizedKey][1] = microtime(true);
$newValue = $data[$normalizedKey][0];
$ns = $this->getOptions()->getNamespace();
if (isset($this->data[$ns][$normalizedKey])) {
$data = & $this->data[$ns][$normalizedKey];
$data[0]+= $value;
$data[1] = microtime(true);
$newValue = $data[0];
} else {
// initial value
$newValue = $value;
$data[$normalizedKey] = array($newValue, microtime(true));
$newValue = $value;
$this->data[$ns][$normalizedKey] = array($newValue, microtime(true));
}

return $newValue;
Expand All @@ -679,16 +669,16 @@ protected function internalIncrementItem(& $normalizedKey, & $value)
*/
protected function internalDecrementItem(& $normalizedKey, & $value)
{
$ns = $this->getOptions()->getNamespace();
$data = & $this->data[$ns];
if (isset($data[$normalizedKey])) {
$data[$normalizedKey][0]-= $value;
$data[$normalizedKey][1] = microtime(true);
$newValue = $data[$normalizedKey][0];
$ns = $this->getOptions()->getNamespace();
if (isset($this->data[$ns][$normalizedKey])) {
$data = & $this->data[$ns][$normalizedKey];
$data[0]-= $value;
$data[1] = microtime(true);
$newValue = $data[0];
} else {
// initial value
$newValue = -$value;
$data[$normalizedKey] = array($newValue, microtime(true));
$newValue = -$value;
$this->data[$ns][$normalizedKey] = array($newValue, microtime(true));
}

return $newValue;
Expand Down
15 changes: 15 additions & 0 deletions test/Storage/Adapter/CommonAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ public function testTaggable()
$this->markTestSkipped("Storage doesn't implement TaggableInterface");
}

// store 3 items and register the current default namespace
$this->assertSame(array(), $this->_storage->setItems(array(
'key1' => 'value1',
'key2' => 'value2',
Expand Down Expand Up @@ -1092,6 +1093,20 @@ public function testTaggable()
$this->assertFalse($this->_storage->hasItem('key3'));
}

/**
* @group 6878
*/
public function testTaggableFunctionsOnEmptyStorage()
{
if (!($this->_storage instanceof TaggableInterface)) {
$this->markTestSkipped("Storage doesn't implement TaggableInterface");
}

$this->assertFalse($this->_storage->setTags('unknown', array('no')));
$this->assertFalse($this->_storage->getTags('unknown'));
$this->assertTrue($this->_storage->clearByTags(array('unknown')));
}

public function testGetTotalSpace()
{
if (!($this->_storage instanceof TotalSpaceCapableInterface)) {
Expand Down

0 comments on commit 66ae533

Please sign in to comment.