fix invalid value set/add

This commit is contained in:
Dennis Eichhorn 2019-12-07 23:07:35 +01:00
parent 71cc73397d
commit dbfadd4cc3
5 changed files with 75 additions and 4 deletions

View File

@ -76,6 +76,14 @@ class MemCached extends ConnectionAbstract
*/ */
public function set($key, $value, int $expire = -1) : void public function set($key, $value, int $expire = -1) : void
{ {
if ($this->status !== CacheStatus::OK) {
return;
}
if (!(\is_scalar($value) || $value instanceof \JsonSerializable || $value instanceof \Serializable)) {
throw new \InvalidArgumentException();
}
$this->con->set($key, $value, \max($expire, 0)); $this->con->set($key, $value, \max($expire, 0));
} }
@ -88,6 +96,10 @@ class MemCached extends ConnectionAbstract
return false; return false;
} }
if (!(\is_scalar($value) || $value instanceof \JsonSerializable || $value instanceof \Serializable)) {
throw new \InvalidArgumentException();
}
return $this->con->add($key, $value, \max($expire, 0)); return $this->con->add($key, $value, \max($expire, 0));
} }

View File

@ -91,6 +91,14 @@ class RedisCache extends ConnectionAbstract
*/ */
public function set($key, $value, int $expire = -1) : void public function set($key, $value, int $expire = -1) : void
{ {
if ($this->status !== CacheStatus::OK) {
return;
}
if (!(\is_scalar($value) || $value instanceof \JsonSerializable || $value instanceof \Serializable)) {
throw new \InvalidArgumentException();
}
if ($expire > 0) { if ($expire > 0) {
$this->con->set($key, $value, $expire); $this->con->set($key, $value, $expire);
} }
@ -107,6 +115,10 @@ class RedisCache extends ConnectionAbstract
return false; return false;
} }
if (!(\is_scalar($value) || $value instanceof \JsonSerializable || $value instanceof \Serializable)) {
throw new \InvalidArgumentException();
}
if ($expire > 0) { if ($expire > 0) {
return $this->con->setNx($key, $value, $expire); return $this->con->setNx($key, $value, $expire);
} }

View File

@ -340,14 +340,26 @@ class FileCacheTest extends \PHPUnit\Framework\TestCase
} }
/** /**
* @testdox A invalid data type will throw an InvalidArgumentException * @testdox Adding a invalid data type will throw an InvalidArgumentException
* @covers phpOMS\DataStorage\Cache\Connection\FileCache<extended> * @covers phpOMS\DataStorage\Cache\Connection\FileCache<extended>
* @group framework * @group framework
*/ */
public function testInvalidDataType() : void public function testInvalidDataTypeAdd() : void
{ {
self::expectException(\InvalidArgumentException::class); self::expectException(\InvalidArgumentException::class);
$this->cache->add('invalid', $this->cache); $this->cache->add('invalid', $this->cache);
} }
/**
* @testdox Setting a invalid data type will throw an InvalidArgumentException
* @covers phpOMS\DataStorage\Cache\Connection\FileCache<extended>
* @group framework
*/
public function testInvalidDataTypeSet() : void
{
self::expectException(\InvalidArgumentException::class);
$this->cache->set('invalid', $this->cache);
}
} }

View File

@ -265,17 +265,29 @@ class MemCachedTest extends \PHPUnit\Framework\TestCase
} }
/** /**
* @testdox A invalid data type will throw an InvalidArgumentException * @testdox Adding a invalid data type will throw an InvalidArgumentException
* @covers phpOMS\DataStorage\Cache\Connection\MemCached<extended> * @covers phpOMS\DataStorage\Cache\Connection\MemCached<extended>
* @group framework * @group framework
*/ */
public function testInvalidDataType() : void public function testInvalidDataTypeAdd() : void
{ {
self::expectException(\InvalidArgumentException::class); self::expectException(\InvalidArgumentException::class);
$this->cache->add('invalid', $this->cache); $this->cache->add('invalid', $this->cache);
} }
/**
* @testdox Setting a invalid data type will throw an InvalidArgumentException
* @covers phpOMS\DataStorage\Cache\Connection\MemCached<extended>
* @group framework
*/
public function testInvalidDataTypeSet() : void
{
self::expectException(\InvalidArgumentException::class);
$this->cache->set('invalid', $this->cache);
}
/** /**
* @testdox A invalid host throws a InvalidConnectionConfigException * @testdox A invalid host throws a InvalidConnectionConfigException
* @covers phpOMS\DataStorage\Cache\Connection\MemCached<extended> * @covers phpOMS\DataStorage\Cache\Connection\MemCached<extended>

View File

@ -329,6 +329,29 @@ class RedisCacheTest extends \PHPUnit\Framework\TestCase
self::assertEquals([], $this->cache->stats()); self::assertEquals([], $this->cache->stats());
} }
/**
* @testdox Adding a invalid data type will throw an InvalidArgumentException
* @covers phpOMS\DataStorage\Cache\Connection\MemCached<extended>
* @group framework
*/
public function testInvalidDataTypeAdd() : void
{
self::expectException(\InvalidArgumentException::class);
$this->cache->add('invalid', $this->cache);
}
/**
* @testdox Setting a invalid data type will throw an InvalidArgumentException
* @covers phpOMS\DataStorage\Cache\Connection\MemCached<extended>
* @group framework
*/
public function testInvalidDataTypeSet() : void
{
self::expectException(\InvalidArgumentException::class);
$this->cache->set('invalid', $this->cache);
}
/** /**
* @testdox A invalid host throws a InvalidConnectionConfigException * @testdox A invalid host throws a InvalidConnectionConfigException