<commit_before><?php

/**
 * @testCase
 */

namespace stekycz\Cronner\tests;

use Exception;
use Nette\Utils\DateTime;
use stdClass;
use stekycz\Cronner\Cronner;
use stekycz\Cronner\Tasks\Task;
use stekycz\Cronner\tests\objects\TestExceptionObject;
use stekycz\Cronner\tests\objects\TestObject;
use Tester\Assert;



require_once(__DIR__ . "/bootstrap.php");
require_once(__DIR__ . "/objects/TestObject.php");
require_once(__DIR__ . "/objects/TestExceptionObject.php");

/**
 * @author <NAME> <<EMAIL>>
 */
class CronnerTest extends \TestCase
{

	/**
	 * @var \stekycz\Cronner\Cronner
	 */
	private $cronner;

	/**
	 * @var \stekycz\Cronner\ITimestampStorage
	 */
	private $timestampStorage;



	protected function setUp()
	{
		parent::setUp();
		$this->timestampStorage = $this->mockista->create(
			'\stekycz\Cronner\ITimestampStorage',
			array('setTaskName', 'saveRunTime', 'loadLastRunTime')
		);
		$this->timestampStorage->expects('loadLastRunTime')
			->andReturn(new DateTime('2013-02-04 08:00:00'));

		$criticalSection = $this->mockista->create(
			'\stekycz\Cronner\CriticalSection',
			array("enter", "leave", "isEntered")
		);
		$criticalSection->expects("enter")
			->andReturn(TRUE);
		$criticalSection->expects("leave")
			->andReturn(TRUE);
		$criticalSection->expects("isEntered")
			->andReturn(FALSE);

		$this->cronner = new Cronner($this->timestampStorage, $criticalSection);
		$this->cronner->onTaskBegin = array();
		$this->cronner->onTaskFinished = array();
		$this->cronner->onTaskError = array();
	}



	/**
	 * @dataProvider dataProviderSetMaxExecutionTime
	 */
	public function testCanSetMaxExecutionTime($expected, $value)
	{
		$this->cronner->setMaxExecutionTime($value);
		Assert::same($expected, $this->cronner->getMaxExecutionTime());
	}



	public function dataProviderSetMaxExecutionTime()
	{
		return array(
			array(1234, 1234),
			array(1234, '1234'),
			array(1234, 1234.5),
			array(1234, '1234.5'),
			array(NULL, NULL),
		);
	}



	/**
	 * @dataProvider dataProviderSetMaxExecutionTimeError
	 * @throws \stekycz\Cronner\InvalidArgumentException
	 */
	public function testThrowsExceptionOnWrongTypeOfMaxExecutionTime($value)
	{
		$this->cronner->setMaxExecutionTime($value);
	}



	public function dataProviderSetMaxExecutionTimeError()
	{
		return array(
			array(-1234),
			array('-1234'),
			array(0),
			array('0'),
			array(-2.5),
			array('-2.5'),
			array(0.0),
			array('0.0'),
			array('nejaky blabol'),
			array(TRUE),
			array(FALSE),
			array(new stdClass()),
		);
	}



	public function testAcceptsTasksObjectWithTaskMethods()
	{
		$tasks = $this->mockista->create('\stdClass');

		$this->cronner->addTasks($tasks);
		Assert::equal(1, $this->cronner->countTaskObjects());
	}



	/**
	 * @throws \stekycz\Cronner\InvalidArgumentException
	 */
	public function testThrowsExceptionOnDuplicateTasksObjectAddition()
	{
		$tasks = $this->mockista->create('\stdClass');

		$this->cronner->addTasks($tasks);
		$this->cronner->addTasks($tasks);
	}



	public function testProcessesAllAddedTasks()
	{
		$now = new DateTime('2013-02-04 09:30:00');
		$tasks = new TestObject();<commit_msg>Fixed not necessary stdClass mocking<commit_after><?php

/**
 * @testCase
 */

namespace stekycz\Cronner\tests;

use Exception;
use Nette\Utils\DateTime;
use stdClass;
use stekycz\Cronner\Cronner;
use stekycz\Cronner\Tasks\Task;
use stekycz\Cronner\tests\objects\TestExceptionObject;
use stekycz\Cronner\tests\objects\TestObject;
use Tester\Assert;



require_once(__DIR__ . "/bootstrap.php");
require_once(__DIR__ . "/objects/TestObject.php");
require_once(__DIR__ . "/objects/TestExceptionObject.php");

/**
 * @author <NAME> <<EMAIL>>
 */
class CronnerTest extends \TestCase
{

	/**
	 * @var \stekycz\Cronner\Cronner
	 */
	private $cronner;

	/**
	 * @var \stekycz\Cronner\ITimestampStorage
	 */
	private $timestampStorage;



	protected function setUp()
	{
		parent::setUp();
		$this->timestampStorage = $this->mockista->create(
			'\stekycz\Cronner\ITimestampStorage',
			array('setTaskName', 'saveRunTime', 'loadLastRunTime')
		);
		$this->timestampStorage->expects('loadLastRunTime')
			->andReturn(new DateTime('2013-02-04 08:00:00'));

		$criticalSection = $this->mockista->create(
			'\stekycz\Cronner\CriticalSection',
			array("enter", "leave", "isEntered")
		);
		$criticalSection->expects("enter")
			->andReturn(TRUE);
		$criticalSection->expects("leave")
			->andReturn(TRUE);
		$criticalSection->expects("isEntered")
			->andReturn(FALSE);

		$this->cronner = new Cronner($this->timestampStorage, $criticalSection);
		$this->cronner->onTaskBegin = array();
		$this->cronner->onTaskFinished = array();
		$this->cronner->onTaskError = array();
	}



	/**
	 * @dataProvider dataProviderSetMaxExecutionTime
	 */
	public function testCanSetMaxExecutionTime($expected, $value)
	{
		$this->cronner->setMaxExecutionTime($value);
		Assert::same($expected, $this->cronner->getMaxExecutionTime());
	}



	public function dataProviderSetMaxExecutionTime()
	{
		return array(
			array(1234, 1234),
			array(1234, '1234'),
			array(1234, 1234.5),
			array(1234, '1234.5'),
			array(NULL, NULL),
		);
	}



	/**
	 * @dataProvider dataProviderSetMaxExecutionTimeError
	 * @throws \stekycz\Cronner\InvalidArgumentException
	 */
	public function testThrowsExceptionOnWrongTypeOfMaxExecutionTime($value)
	{
		$this->cronner->setMaxExecutionTime($value);
	}



	public function dataProviderSetMaxExecutionTimeError()
	{
		return array(
			array(-1234),
			array('-1234'),
			array(0),
			array('0'),
			array(-2.5),
			array('-2.5'),
			array(0.0),
			array('0.0'),
			array('nejaky blabol'),
			array(TRUE),
			array(FALSE),
			array(new stdClass()),
		);
	}



	public function testAcceptsTasksObjectWithTaskMethods()
	{
		$this->cronner->addTasks(new \stdClass());
		Assert::equal(1, $this->cronner->countTaskObjects());
	}



	/**
	 * @throws \stekycz\Cronner\InvalidArgumentException
	 */
	public function testThrowsExceptionOnDuplicateTasksObjectAddition()
	{
		$tasks = new \stdClass();
		$this->cronner->addTasks($tasks);
		$this->cronner->addTasks($tasks);
	}



	public function testProcessesAllAddedTasks()
	{
		$now = new DateTime('2013-02-04 09:30:00');
		$tasks = new TestObject();
{'fraction_non_alphanumeric': 0.16613110539845757, 'fraction_numerical': 0.02602827763496144, 'mean_word_length': 4.669398907103825, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 85, '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': '7636723', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2136, 'n_words': 313}