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

Commit

Permalink
Merge branch 'cache_tests_cleanup' of https://github.com/marc-mabe/zf2
Browse files Browse the repository at this point in the history
…into hotfix/cache-tests

Conflicts:
	tests/Zend/Locale/Data/CldrTest.php
  • Loading branch information
weierophinney committed Mar 12, 2012
2 parents ae4cf36 + e397d30 commit 1841b47
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 68 deletions.
13 changes: 2 additions & 11 deletions src/PatternFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public static function factory($patternName, $options = array())
public static function getBroker()
{
if (static::$broker === null) {
static::$broker = static::getDefaultBroker();
static::$broker = new PatternBroker();
static::$broker->setRegisterPluginsOnLoad(false);
}

return static::$broker;
Expand All @@ -108,14 +109,4 @@ public static function resetBroker()
{
static::$broker = null;
}

/**
* Get internal pattern broker
*
* @return PatternBroker
*/
protected static function getDefaultBroker()
{
return new PatternBroker();
}
}
6 changes: 6 additions & 0 deletions src/Storage/Adapter/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,12 @@ public function getCapacity(array $options = array())
protected function hasFreeCapacity()
{
$total = $this->getOptions()->getMemoryLimit();

// check memory limit disabled
if ($total <= 0) {
return true;
}

$free = $total - (float) memory_get_usage(true);
return ($free > 0);
}
Expand Down
9 changes: 5 additions & 4 deletions src/Storage/Adapter/MemoryOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ class MemoryOptions extends AdapterOptions
/**
* Set memory limit
*
* If the used memory of PHP exceeds this limit an OutOfCapacityException
* will be thrown.
* - Bytes of less or equal 0 will disable the memory limit
* - If the used memory of PHP exceeds this limit an OutOfCapacityException
* will be thrown.
*
* @param int $bytes
* @return MemoryOptions
Expand All @@ -72,8 +73,8 @@ public function getMemoryLimit()
if ($memoryLimit >= 0) {
$this->memoryLimit = floor($memoryLimit / 2);
} else {
// use a hard memory limit of 32M if php memory limit is disabled
$this->memoryLimit = 33554432;
// disable memory limit
$this->memoryLimit = 0;
}
}

Expand Down
72 changes: 28 additions & 44 deletions src/StorageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ public static function factory($cfg)

// instantiate the adapter
if (!isset($cfg['adapter'])) {
throw new Exception\InvalidArgumentException(
'Missing "adapter"'
);
} elseif (is_array($cfg['adapter'])) {
throw new Exception\InvalidArgumentException('Missing "adapter"');
}
$adapterName = $cfg['adapter'];
$adapterOptions = null;
if (is_array($cfg['adapter'])) {
if (!isset($cfg['adapter']['name'])) {
throw new Exception\InvalidArgumentException(
'Missing "adapter.name"'
);
throw new Exception\InvalidArgumentException('Missing "adapter.name"');
}

$name = $cfg['adapter']['name'];
$options = isset($cfg['adapter']['options'])
? $cfg['adapter']['options'] : array();
$adapter = static::adapterFactory($name, $options);
} else {
$adapter = static::adapterFactory($cfg['adapter']);
$adapterName = $cfg['adapter']['name'];
$adapterOptions = isset($cfg['adapter']['options']) ? $cfg['adapter']['options'] : null;
}
if ($adapterOptions && isset($cfg['options'])) {
$adapterOptions = array_merge($adapterOptions, $cfg['options']);
}

$adapter = static::adapterFactory((string)$adapterName, $adapterOptions);

// add plugins
if (isset($cfg['plugins'])) {
Expand All @@ -99,50 +99,33 @@ public static function factory($cfg)

foreach ($cfg['plugins'] as $k => $v) {
if (is_string($k)) {
$name = $k;
if (!is_array($v)) {
throw new Exception\InvalidArgumentException(
"'plugins.{$k}' needs to be an array"
);
if (!is_array($v)) {
throw new Exception\InvalidArgumentException(
"'plugins.{$k}' needs to be an array"
);
}
$options = $v;
$pluginName = $k;
$pluginOptions = $v;
} elseif (is_array($v)) {
if (!isset($v['name'])) {
throw new Exception\InvalidArgumentException("Invalid plugins[{$k}] or missing plugins[{$k}].name");
}
$name = (string) $v['name'];
$pluginName = (string) $v['name'];
if (isset($v['options'])) {
$options = $v['options'];
$pluginOptions = $v['options'];
} else {
$options = array();
$pluginOptions = array();
}
} else {
$name = $v;
$options = array();
$pluginName = $v;
$pluginOptions = array();
}

$plugin = static::pluginFactory($name, $options);
$plugin = static::pluginFactory($pluginName, $pluginOptions);
$adapter->addPlugin($plugin);
}
}

// set adapter or plugin options
if (isset($cfg['options'])) {
if (!is_array($cfg['options'])
&& !$cfg['options'] instanceof Traversable
) {
throw new Exception\InvalidArgumentException(
'Options needs to be an array or Traversable object'
);
}

// Options at the top-level should be *merged* with existing options
$options = $adapter->getOptions();
foreach ($cfg['options'] as $key => $value) {
$options->$key = $value;
}
}

return $adapter;
}

Expand Down Expand Up @@ -202,7 +185,7 @@ public static function setAdapterBroker(Broker $broker)
*/
public static function resetAdapterBroker()
{
static::$adapterBroker = new Storage\AdapterBroker();
static::$adapterBroker = null;
}

/**
Expand All @@ -227,6 +210,7 @@ public static function pluginFactory($pluginName, $options = array())
}

$plugin->setOptions($options);

return $plugin;
}

Expand Down Expand Up @@ -262,6 +246,6 @@ public static function setPluginBroker(Broker $broker)
*/
public static function resetPluginBroker()
{
static::$pluginBroker = new Storage\PluginBroker();
static::$pluginBroker = null;
}
}
11 changes: 11 additions & 0 deletions test/PatternFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,15 @@ public function testChangeBroker()
$this->assertSame($broker, Cache\PatternFactory::getBroker());
}

public function testFactory()
{
$pattern1 = Cache\PatternFactory::factory('capture');
$this->assertInstanceOf('Zend\Cache\Pattern\CaptureCache', $pattern1);

$pattern2 = Cache\PatternFactory::factory('capture');
$this->assertInstanceOf('Zend\Cache\Pattern\CaptureCache', $pattern2);

$this->assertNotSame($pattern1, $pattern2);
}

}
4 changes: 4 additions & 0 deletions test/Storage/Adapter/ApcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class ApcTest extends CommonAdapterTest

public function setUp()
{
if (!defined('TESTS_ZEND_CACHE_APC_ENABLED') || !TESTS_ZEND_CACHE_APC_ENABLED) {
$this->markTestSkipped("Skipped by TestConfiguration (TESTS_ZEND_CACHE_APC_ENABLED)");
}

if (version_compare('3.1.6', phpversion('apc')) > 0) {
try {
new Cache\Storage\Adapter\Apc();
Expand Down
14 changes: 12 additions & 2 deletions test/Storage/Adapter/MemcachedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,21 @@ class MemcachedTest extends CommonAdapterTest

public function setUp()
{
if (!defined('TESTS_ZEND_CACHE_MEMCACHED_ENABLED') || !TESTS_ZEND_CACHE_MEMCACHED_ENABLED) {
$this->markTestSkipped("Skipped by TestConfiguration (TESTS_ZEND_CACHE_MEMCACHED_ENABLED)");
}

if (!extension_loaded('memcached')) {
$this->markTestSkipped("Memcached extension is not loaded");
}

$this->_options = new Cache\Storage\Adapter\MemcachedOptions();
if (defined('TESTS_ZEND_CACHE_MEMCACHED_HOST') && defined('TESTS_ZEND_CACHE_MEMCACHED_PORT')) {
$this->_options->addServer(TESTS_ZEND_CACHE_MEMCACHED_HOST, TESTS_ZEND_CACHE_MEMCACHED_PORT);
} elseif (defined('TESTS_ZEND_CACHE_MEMCACHED_HOST')) {
$this->_options->addServer(TESTS_ZEND_CACHE_MEMCACHED_HOST);
}

$this->_storage = new Cache\Storage\Adapter\Memcached($this->_options);

parent::setUp();
Expand Down Expand Up @@ -93,8 +103,8 @@ public function tearDown()
{
if (!empty($this->_storage)) {
$this->_storage->clear();
}
}

parent::tearDown();
}
}
6 changes: 5 additions & 1 deletion test/Storage/Adapter/WinCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ class WinCacheTest extends CommonAdapterTest

public function setUp()
{
if (!defined('TESTS_ZEND_CACHE_WINCACHE_ENABLED') || !TESTS_ZEND_CACHE_WINCACHE_ENABLED) {
$this->markTestSkipped("Skipped by TestConfiguration (TESTS_ZEND_CACHE_WINCACHE_ENABLED)");
}

if (!extension_loaded('wincache')) {
$this->markTestSkipped("WinCache extension is not loaded");
}

$enabled = ini_get('wincache.ucenabled');
if (PHP_SAPI == 'cli') {
$enabled = $enabled && (bool) ini_get('wincache.enablecli');
Expand Down
4 changes: 4 additions & 0 deletions test/Storage/Adapter/ZendServerDiskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class ZendServerDiskTest extends CommonAdapterTest

public function setUp()
{
if (!defined('TESTS_ZEND_CACHE_ZEND_SERVER_ENABLED') || !TESTS_ZEND_CACHE_ZEND_SERVER_ENABLED) {
$this->markTestSkipped("Skipped by TestConfiguration (TESTS_ZEND_CACHE_ZEND_SERVER_ENABLED)");
}

if (!function_exists('zend_disk_cache_store') || PHP_SAPI == 'cli') {
try {
new Cache\Storage\Adapter\ZendServerDisk();
Expand Down
4 changes: 4 additions & 0 deletions test/Storage/Adapter/ZendServerShmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class ZendServerShmTest extends CommonAdapterTest

public function setUp()
{
if (!defined('TESTS_ZEND_CACHE_ZEND_SERVER_ENABLED') || !TESTS_ZEND_CACHE_ZEND_SERVER_ENABLED) {
$this->markTestSkipped("Skipped by TestConfiguration (TESTS_ZEND_CACHE_ZEND_SERVER_ENABLED)");
}

if (strtolower(PHP_SAPI) == 'cli') {
$this->markTestSkipped('Zend Server SHM does not work in CLI environment');
return;
Expand Down
25 changes: 19 additions & 6 deletions test/StorageFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function setUp()

public function tearDown()
{
Cache\StorageFactory::resetAdapterBroker();
Cache\StorageFactory::resetPluginBroker();
}

public function testDefaultAdapterBroker()
Expand All @@ -59,8 +61,13 @@ public function testChangeAdapterBroker()

public function testAdapterFactory()
{
$cache = Cache\StorageFactory::adapterFactory('Memory');
$this->assertInstanceOf('Zend\Cache\Storage\Adapter\Memory', $cache);
$adapter1 = Cache\StorageFactory::adapterFactory('Memory');
$this->assertInstanceOf('Zend\Cache\Storage\Adapter\Memory', $adapter1);

$adapter2 = Cache\StorageFactory::adapterFactory('Memory');
$this->assertInstanceOf('Zend\Cache\Storage\Adapter\Memory', $adapter2);

$this->assertNotSame($adapter1, $adapter2);
}

public function testDefaultPluginBroker()
Expand All @@ -78,8 +85,13 @@ public function testChangePluginBroker()

public function testPluginFactory()
{
$plugin = Cache\StorageFactory::pluginFactory('Serializer');
$this->assertInstanceOf('Zend\Cache\Storage\Plugin\Serializer', $plugin);
$plugin1 = Cache\StorageFactory::pluginFactory('Serializer');
$this->assertInstanceOf('Zend\Cache\Storage\Plugin\Serializer', $plugin1);

$plugin2 = Cache\StorageFactory::pluginFactory('Serializer');
$this->assertInstanceOf('Zend\Cache\Storage\Plugin\Serializer', $plugin2);

$this->assertNotSame($plugin1, $plugin2);
}

public function testFactoryAdapterAsString()
Expand Down Expand Up @@ -114,8 +126,9 @@ public function testFactoryWithPlugins()
$this->assertInstanceOf('Zend\Cache\Storage\Adapter\Memory', $cache);

// test plugin structure
foreach ($cache->getPlugins() as $i => $plugin) {
$this->assertInstanceOf('Zend\Cache\Storage\Plugin\\' . $plugins[$i], $plugin);
$i = 0;
foreach ($cache->getPlugins() as $plugin) {
$this->assertInstanceOf('Zend\Cache\Storage\Plugin\\' . $plugins[$i++], $plugin);
}
}

Expand Down

0 comments on commit 1841b47

Please sign in to comment.