<commit_before><?php

/*
  +------------------------------------------------------------------------+
  | Phalcon Framework                                                      |
  +------------------------------------------------------------------------+
  | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com)       |
  +------------------------------------------------------------------------+
  | This source file is subject to the New BSD License that is bundled     |
  | with this package in the file docs/LICENSE.txt.                        |
  |                                                                        |
  | If you did not receive a copy of the license and are unable to         |
  | obtain it through the world-wide-web, please send an email             |
  | to <EMAIL> so we can send you a copy immediately.       |
  +------------------------------------------------------------------------+
  | Authors: <NAME> <<EMAIL>>                      |
  |          <NAME> <<EMAIL>>                         |
  +------------------------------------------------------------------------+
*/

require_once 'helpers/xcache.php';

class CacheTest extends PHPUnit_Framework_TestCase
{

	public function setUp()
	{
		date_default_timezone_set('UTC');
		$iterator = new DirectoryIterator('unit-tests/cache/');
		foreach ($iterator as $item) {
			if (!$item->isDir()) {
				unlink($item->getPathname());
			}
		}
	}

	public function testOutputFileCache()
	{

		$time = date('H:i:s');

		$frontCache = new Phalcon\Cache\Frontend\Output(array(
			'lifetime' => 2
		));

		$cache = new Phalcon\Cache\Backend\File($frontCache, array(
			'cacheDir' => 'unit-tests/cache/',
			'prefix' => 'unit'
		));

		$this->assertFalse($cache->isStarted());

		ob_start();

		//First time cache
		$content = $cache->start('testoutput');
		$this->assertTrue($cache->isStarted());

		if ($content !== null) {
			$this->assertTrue(false);
		}

		echo $time;
		$cache->save(null, null, null, true);

		$obContent = ob_get_contents();
		ob_end_clean();

		$this->assertEquals($time, $obContent);
		$this->assertTrue(file_exists('unit-tests/cache/unittestoutput'));

		//Same cache
		$content = $cache->start('testoutput');
		$this->assertTrue($cache->isStarted());

		if ($content === null) {
			$this->assertTrue(false);
		}

		$this->assertEquals($time, $obContent);

		//Refresh cache
		sleep(3);

		$time2 = date('H:i:s');

		ob_start();

		$content = $cache->start('testoutput');
		$this->assertTrue($cache->isStarted());

		if ($content !== null) {
			$this->assertTrue(false);
		}

		echo $time2;
		$cache->save(null, null, null, true);

		$obContent2 = ob_get_contents();
		ob_end_clean();

		$this->assertNotEquals($time, $obContent2);
		$this->assertEquals($time2, $obContent2);

		//Check keys
		$keys = $cache->queryKeys();
		$this->assertEquals($keys, array(
			0 => 'unittestoutput',
		));

		// $cache->exists('testoutput') is not always true because Travis CI could be slow sometimes
		//Exists?
		if ($cache->exists('testoutput')) {
			$this->assertTrue($cache->exists('testoutput'));

			//Delete cache
			$this->assertTrue($cache->delete('testoutput'));
		}

	}

	public function testDataFileCache()
	{

		$frontCache = new Phalcon\Cache\Frontend\Data();

		$cache = new Phalcon\Cache\Backend\File($frontCache, array(
			'cacheDir' => 'unit-tests/cache/'
		));

		$this->assertFalse($cache->isStarted());

		//Save
		$cache->save('test-data', "nothing interesting");

		$this->assertTrue(file_exists('unit-tests/cache/test-data'));

		//Get
		$cachedContent = $cache->get('test-data');
		$this->assertEquals($cachedContent, "nothing interesting");

		//Save
		$cache->save('test-data', "sure, nothing interesting");

		//Get
		$cachedContent = $cache->get('test-data');
		$this->assertEquals($cachedContent, "sure, nothing interesting");

		//Exists
		$this->assertTrue($cache->exists('test-data'));

		//Delete
		$this->assertTrue($cache->delete('test-data'));

	}

	private function _prepareIgbinary()
	{

		if (!extension_loaded('igbinary')) {
			$this->markTestSkipped('Warning: igbinary extension is not loaded');
			return false;
		}

		return true;
	}

	public function testIgbinaryFileCache()
	{
		if (!$this->_prepareIgbinary()) {
			return false;
		}

		$frontCache = new Phalcon\Cache\Frontend\Igbinary();

		$cache = new Phalcon\Cache\Backend\File($frontCache, array(
			'cacheDir' => 'unit-tests/cache/'
		));

		$this->assertFalse($cache->isStarted());
<commit_msg>Make sure the test won't fail under Valgrind<commit_after><?php

/*
  +------------------------------------------------------------------------+
  | Phalcon Framework                                                      |
  +------------------------------------------------------------------------+
  | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com)       |
  +------------------------------------------------------------------------+
  | This source file is subject to the New BSD License that is bundled     |
  | with this package in the file docs/LICENSE.txt.                        |
  |                                                                        |
  | If you did not receive a copy of the license and are unable to         |
  | obtain it through the world-wide-web, please send an email             |
  | to <EMAIL> so we can send you a copy immediately.       |
  +------------------------------------------------------------------------+
  | Authors: <NAME> <<EMAIL>>                      |
  |          <NAME> <<EMAIL>>                         |
  +------------------------------------------------------------------------+
*/

require_once 'helpers/xcache.php';

class CacheTest extends PHPUnit_Framework_TestCase
{

	public function setUp()
	{
		date_default_timezone_set('UTC');
		$iterator = new DirectoryIterator('unit-tests/cache/');
		foreach ($iterator as $item) {
			if (!$item->isDir()) {
				unlink($item->getPathname());
			}
		}
	}

	public function testOutputFileCache()
	{

		$time = date('H:i:s');

		$frontCache = new Phalcon\Cache\Frontend\Output(array(
			'lifetime' => 2
		));

		$cache = new Phalcon\Cache\Backend\File($frontCache, array(
			'cacheDir' => 'unit-tests/cache/',
			'prefix' => 'unit'
		));

		$this->assertFalse($cache->isStarted());

		ob_start();

		//First time cache
		$content = $cache->start('testoutput');
		$this->assertTrue($cache->isStarted());

		if ($content !== null) {
			$this->assertTrue(false);
		}

		echo $time;
		$cache->save(null, null, null, true);

		$obContent = ob_get_contents();
		ob_end_clean();

		$this->assertEquals($time, $obContent);
		$this->assertTrue(file_exists('unit-tests/cache/unittestoutput'));

		//Same cache
		$content = $cache->start('testoutput');
		$this->assertTrue($cache->isStarted());

		if ($content === null) {
			$this->assertTrue(false);
		}

		$this->assertEquals($time, $obContent);

		//Refresh cache
		sleep(3);

		$time2 = date('H:i:s');

		ob_start();

		$content = $cache->start('testoutput');
		$this->assertTrue($cache->isStarted());

		if ($content !== null) {
			$this->assertTrue(false);
		}

		echo $time2;
		$cache->save(null, null, null, true);

		$obContent2 = ob_get_contents();
		ob_end_clean();

		$this->assertNotEquals($time, $obContent2);
		$this->assertEquals($time2, $obContent2);

		//Check keys
		$keys = $cache->queryKeys();
		$this->assertEquals($keys, array(
			0 => 'unittestoutput',
		));

		// $cache->exists('testoutput') is not always true because Travis CI could be slow sometimes
		//Exists?
		if ($cache->exists('testoutput')) {
			$this->assertTrue($cache->exists('testoutput'));

			//Delete cache
			$this->assertTrue($cache->delete('testoutput'));
		}

	}

	public function testDataFileCache()
	{

		$frontCache = new Phalcon\Cache\Frontend\Data(array('lifetime' => 10));

		$cache = new Phalcon\Cache\Backend\File($frontCache, array(
			'cacheDir' => 'unit-tests/cache/',
		));

		$this->assertFalse($cache->isStarted());

		//Save
		$cache->save('test-data', "nothing interesting");

		$this->assertTrue(file_exists('unit-tests/cache/test-data'));

		//Get
		$cachedContent = $cache->get('test-data');
		$this->assertEquals($cachedContent, "nothing interesting");

		//Save
		$cache->save('test-data', "sure, nothing interesting");

		//Get
		$cachedContent = $cache->get('test-data');
		$this->assertEquals($cachedContent, "sure, nothing interesting");

		//Exists
		$this->assertTrue($cache->exists('test-data'));

		//Delete
		$this->assertTrue($cache->delete('test-data'));

	}

	private function _prepareIgbinary()
	{

		if (!extension_loaded('igbinary')) {
			$this->markTestSkipped('Warning: igbinary extension is not loaded');
			return false;
		}

		return true;
	}

	public function testIgbinaryFileCache()
	{
		if (!$this->_prepareIgbinary()) {
			return false;
		}

		$frontCache = new Phalcon\Cache\Frontend\Igbinary(array('lifetime' => 600));

		$cache = new Phalcon\Cache\Backend\File($frontCache, array(
			'cacheDir' => 'unit-tests/cache/'
		));

		$this->assertFalse($cache->isStarted());

{'fraction_non_alphanumeric': 0.2398414271555996, 'fraction_numerical': 0.004294681202510736, 'mean_word_length': 2.2692584593232543, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5969466', 'n_tokens_mistral': 3012, 'n_tokens_neox': 2673, 'n_words': 539}