<commit_before><?php
/**
 * Lithium: the most rad php framework
 *
 * @copyright     Copyright 2013, Union of RAD (http://union-of-rad.org)
 * @license       http://opensource.org/licenses/bsd-license.php The BSD License
 */

namespace lithium\tests\cases\storage\cache\adapter;

use lithium\storage\cache\adapter\XCache;

class XCacheTest extends \lithium\test\Unit {

	/**
	 * Skip the test if XCache extension is unavailable.
	 *
	 * @return void
	 */
	public function skip() {
		$extensionExists = (extension_loaded('xcache') && (ini_get('xcache.var_size') !== 0));
		$message = 'The XCache extension is not installed or not configured for userspace caching.';
		$this->skipIf(!$extensionExists, $message);
	}

	/**
	 * Clear the userspace cache
	 *
	 * @return void
	 */
	public function setUp() {
		for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++) {
			if (xcache_clear_cache(XC_TYPE_VAR, $i) === false) {
				return false;
			}
		}
		$this->XCache = new XCache();
	}

	public function tearDown() {
		unset($this->XCache);
	}

	public function testEnabled() {
		$xcache = $this->XCache;
		$this->assertTrue($xcache::enabled());
	}

	public function testSimpleWrite() {
		$key = 'key';
		$data = 'value';
		$keys = array($key => $data);
		$expiry = '+5 seconds';
		$time = strtotime($expiry);

		$closure = $this->XCache->write($keys, $expiry);
		$this->assertInternalType('callable', $closure);

		$params = compact('keys', 'expiry');
		$result = $closure($this->XCache, $params);
		$this->assertTrue($result);

		$expected = $data;
		$result = xcache_get($key);
		$this->assertEqual($expected, $result);

		$result = xcache_unset($key);
		$this->assertTrue($result);

		$key = 'another_key';
		$data = 'more_data';
		$keys = array($key => $data);
		$expiry = '+1 minute';
		$time = strtotime($expiry);

		$closure = $this->XCache->write($keys, $expiry);
		$this->assertInternalType('callable', $closure);

		$params = compact('keys', 'expiry');
		$result = $closure($this->XCache, $params);
		$this->assertTrue($result);

		$expected = $data;
		$result = xcache_get($key);
		$this->assertEqual($expected, $result);

		$result = xcache_unset($key);
		$this->assertTrue($result);
	}

	public function testWriteMulti() {
		$expiry = '+1 minute';
		$keys = array(
			'key1' => 'data1',
			'key2' => 'data2',
			'key3' => 'data3'
		);
		$closure = $this->XCache->write($keys, $expiry);
		$result = $closure($this->XCache, compact('keys', 'expiry'));
		$this->assertTrue($result);

		foreach ($keys as $key => $data) {
			$expected = $data;
			$result = xcache_get($key);
			$this->assertEqual($expected, $result);

			xcache_unset($key);
		}
	}

	public function testWriteExpiryDefault() {
		$xCache = new XCache(array('expiry' => '+5 seconds'));
		$key = 'default_key';
		$data = 'value';
		$keys = array($key => $data);
		$time = strtotime('+5 seconds');

		$closure = $xCache->write($keys);
		$this->assertInternalType('callable', $closure);

		$params = compact('keys');
		$result = $closure($xCache, $params);
		$this->assertTrue($result);

		$expected = $data;
		$result = xcache_get($key);
		$this->assertEqual($expected, $result);

		$result = xcache_unset($key);
		$this->assertTrue($result);
	}

	public function testWriteNoExpiry() {
		$adapter = new XCache(array('expiry' => null));

		$keys = array('key1' => 'data1');
		$expiry = null;
		$closure = $adapter->write($keys, $expiry);
		$closure($adapter, compact('keys', 'expiry'));

		$result = xcache_isset('key1');
		$this->assertTrue($result);
	}

	public function testWriteExpiryExpires() {
		$keys = array('key1' => 'data1');
		$expiry = '+5 seconds';
		$closure = $this->XCache->write($keys, $expiry);
		$closure($this->XCache, compact('keys', 'expiry'));

		$result = xcache_isset('key1');
		$this->assertTrue($result);

		xcache_unset('key1');

		$keys = array('key1' => 'data1');
		$expiry = '+1 second';
		$closure = $this->XCache->write($keys, $expiry);
		$closure($this->XCache, compact('keys', 'expiry'));

		sleep(2);

		$result = xcache_isset('key1');
		$this->assertFalse($result);
	}

	public function testWriteExpiryTtl() {
		$keys = array('key1' => 'data1');
		$expiry = 5;
		$closure = $this->XCache->write($keys, $expiry);
		$closure($this->XCache, compact('keys', 'expiry'));

		$result = xcache_isset('key1');
		$this->assertTrue($result);

		xcache_unset('key1');

		$keys = array('key1' => 'data1');
		$expiry = 1;
		$closure = $this->XCache->write($keys, $expiry);
		$closure($this->XCache, compact('keys', 'expiry'));

		sleep(2);

		$result = xcache_isset('key1');
		$this->assertFalse($result);
	}

	public function testSimpleRead() {
		$key = 'read_key';
		$data = 'read data';
		$keys = array($key);
		$time = strtotime('+1 minute');

		$result = xcache_set($key, $data, 60);
		$this->assertTrue($result);

		$closure = $this->XCache->read($keys);
		$this->assertInternalType('callable', $closure);
<commit_msg>Fixing XCache tests for expiration; adding docblocks.<commit_after><?php
/**
 * Lithium: the most rad php framework
 *
 * @copyright     Copyright 2013, Union of RAD (http://union-of-rad.org)
 * @license       http://opensource.org/licenses/bsd-license.php The BSD License
 */

namespace lithium\tests\cases\storage\cache\adapter;

use lithium\storage\cache\adapter\XCache;

class XCacheTest extends \lithium\test\Unit {

	/**
	 * Skip the test if XCache extension is unavailable.
	 *
	 * @return void
	 */
	public function skip() {
		$extensionExists = (extension_loaded('xcache') && (ini_get('xcache.var_size') !== 0));
		$message = 'The XCache extension is not installed or not configured for userspace caching.';
		$this->skipIf(!$extensionExists, $message);
	}

	/**
	 * Clear the userspace cache
	 *
	 * @return void
	 */
	public function setUp() {
		for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++) {
			if (xcache_clear_cache(XC_TYPE_VAR, $i) === false) {
				return false;
			}
		}
		$this->XCache = new XCache();
	}

	public function tearDown() {
		unset($this->XCache);
	}

	public function testEnabled() {
		$xcache = $this->XCache;
		$this->assertTrue($xcache::enabled());
	}

	public function testSimpleWrite() {
		$key = 'key';
		$data = 'value';
		$keys = array($key => $data);
		$expiry = '+5 seconds';
		$time = strtotime($expiry);

		$closure = $this->XCache->write($keys, $expiry);
		$this->assertInternalType('callable', $closure);

		$params = compact('keys', 'expiry');
		$result = $closure($this->XCache, $params);
		$this->assertTrue($result);

		$expected = $data;
		$result = xcache_get($key);
		$this->assertEqual($expected, $result);

		$result = xcache_unset($key);
		$this->assertTrue($result);

		$key = 'another_key';
		$data = 'more_data';
		$keys = array($key => $data);
		$expiry = '+1 minute';
		$time = strtotime($expiry);

		$closure = $this->XCache->write($keys, $expiry);
		$this->assertInternalType('callable', $closure);

		$params = compact('keys', 'expiry');
		$result = $closure($this->XCache, $params);
		$this->assertTrue($result);

		$expected = $data;
		$result = xcache_get($key);
		$this->assertEqual($expected, $result);

		$result = xcache_unset($key);
		$this->assertTrue($result);
	}

	public function testWriteMulti() {
		$expiry = '+1 minute';
		$keys = array(
			'key1' => 'data1',
			'key2' => 'data2',
			'key3' => 'data3'
		);
		$closure = $this->XCache->write($keys, $expiry);
		$result = $closure($this->XCache, compact('keys', 'expiry'));
		$this->assertTrue($result);

		foreach ($keys as $key => $data) {
			$expected = $data;
			$result = xcache_get($key);
			$this->assertEqual($expected, $result);

			xcache_unset($key);
		}
	}

	public function testWriteExpiryDefault() {
		$xCache = new XCache(array('expiry' => '+5 seconds'));
		$key = 'default_key';
		$data = 'value';
		$keys = array($key => $data);
		$time = strtotime('+5 seconds');

		$closure = $xCache->write($keys);
		$this->assertInternalType('callable', $closure);

		$params = compact('keys');
		$result = $closure($xCache, $params);
		$this->assertTrue($result);

		$expected = $data;
		$result = xcache_get($key);
		$this->assertEqual($expected, $result);

		$result = xcache_unset($key);
		$this->assertTrue($result);
	}

	public function testWriteNoExpiry() {
		$adapter = new XCache(array('expiry' => null));

		$keys = array('key1' => 'data1');
		$expiry = null;
		$closure = $adapter->write($keys, $expiry);
		$closure($adapter, compact('keys', 'expiry'));

		$result = xcache_isset('key1');
		$this->assertTrue($result);
	}

	/**
	 * Tests that an item can be written to the cache using
	 * `strtotime` syntax.
	 *
	 * Note that because of the nature of XCache we cannot test if an item
	 * correctly expires. Expiration checks are done by XCache only on each
	 * _page request_.
	 */
	public function testWriteExpiryExpires() {
		$keys = array('key1' => 'data1');
		$expiry = '+5 seconds';
		$closure = $this->XCache->write($keys, $expiry);
		$closure($this->XCache, compact('keys', 'expiry'));

		$result = xcache_isset('key1');
		$this->assertTrue($result);

		xcache_unset('key1');
	}

	/**
	 * Tests that an item can be written to the cache using
	 * TTL syntax.
	 *
	 * Note that because of the nature of XCache we cannot test if an item
	 * correctly expires. Expiration checks are done by XCache only on each
	 * _page request_.
	 */
	public function testWriteExpiryTtl() {
		$keys = array('key1' => 'data1');
		$expiry = 5;
		$closure = $this->XCache->write($keys, $expiry);
		$closure($this->XCache, compact('keys', 'expiry'));

		$result = xcache_isset('key1');
		$this->assertTrue($result);

		xcache_unset('key1');

		$keys = array('key1' => 'data1');
		$expiry = 1;
		$closure = $this->XCache->write($keys, $expiry);
		$closure($this->XCache, compact('keys', 'expiry'));
	}

	public function testSimpleRead() {
		$key = 'read_key';
		$data = 'read data';
		$keys = array($key);
		$time = strtotime('+1 minute');

		$result = xcache_set($key, $data, 60);
		$this->assertTrue($result);

		$closure = $this->XCache->read($keys);
		$this->assertInternalType('callable', $closure);

{'fraction_non_alphanumeric': 0.19829212590606693, 'fraction_numerical': 0.007844305431436799, 'mean_word_length': 4.317845828933474, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3947857', 'n_tokens_mistral': 3938, 'n_tokens_neox': 3507, 'n_words': 760}