<commit_before><?php

class CM_Jobdistribution_Job_AbstractTest extends CMTest_TestCase {

    public function testRun() {
        if (!extension_loaded('gearman')) {
            $this->markTestSkipped('Gearman Pecl Extension not installed.');
        }
        CM_Config::get()->CM_Jobdistribution_Job_Abstract->gearmanEnabled = true;

        /** @var CM_Jobdistribution_Job_Abstract $job */
        $job = $this->getMockForAbstractClass('CM_Jobdistribution_Job_Abstract', array(), '', true, true, true,
            array('_getGearmanClient', '_execute'));
        $gearmanClientMock = $this->getMock('GearmanClient', array('doNormal', 'returnCode'));
        $that = $this;
        $gearmanJobMock = $gearmanJobMock = $this->getMock('GearmanJob', array('sendFail', 'workload'));
        $gearmanClientMock->expects($this->any())->method('doNormal')->will($this->returnCallback(function ($jobName, $workload) use ($job, $gearmanClientMock, &$gearmanJobMock, $that) {
            $gearmanJobMock->expects($that->any())->method('workload')->will($that->returnValue($workload));
            $gearmanJobMock->expects($that->any())->method('sendFail')->will($that->returnCallback(function () use ($gearmanClientMock, $that) {
                $gearmanClientMock->expects($that->any())->method('returnCode')->will($that->returnValue(GEARMAN_WORK_FAIL));
            }));
            return $job->__executeGearman($gearmanJobMock);
        }));
        $job->expects($this->any())->method('_getGearmanClient')->will($this->returnValue($gearmanClientMock));
        $job->expects($this->any())->method('_execute')->will($this->returnCallback(function (CM_Params $params) {
            return array_flip($params->getAllOriginal());
        }));

        $result = $job->run(array('foo' => 'bar'));
        $this->assertSame(array('bar' => 'foo'), $result);

        // Exception thrown in worker
        $job->expects($this->any())->method('_execute')->will($this->returnCallback(function (CM_Params $params) use ($gearmanJobMock) {
            $gearmanJobMock->sendFail();
        }));
        try {
            $job->run(array('foo' => 'bar'));
            $this->fail('Job should have thrown an exception');
        } catch (CM_Exception $ex) {
            $this->assertContains('Job `' . get_class($job) . '` failed', $ex->getMessage());
        }

        CMTest_TH::clearConfig();
    }

    public function testRunGearmanDisabled() {
        CM_Config::get()->CM_Jobdistribution_Job_Abstract->gearmanEnabled = false;

        $job = $this->getMockForAbstractClass('CM_Jobdistribution_Job_Abstract', array(), '', true, true, true, array('_execute'));
        $job->expects($this->exactly(2))->method('_execute')->will($this->returnCallback(function (CM_Params $params) {
            return array_flip($params->getAllOriginal());
        }));

        /** @var CM_Jobdistribution_Job_Abstract $job */
        $result = $job->run(array('foo' => 'bar'));
        $this->assertSame(array('bar' => 'foo'), $result);

        $job->queue(array('foo' => 'bar'));

        CMTest_TH::clearConfig();
    }

    public function testRunGearmanDisabledThrows() {
        CM_Config::get()->CM_Jobdistribution_Job_Abstract->gearmanEnabled = false;

        $job = $this->getMockForAbstractClass('CM_Jobdistribution_Job_Abstract', array(), '', true, true, true, array('_execute'));
        $job->expects($this->exactly(1))->method('_execute')->will($this->returnCallback(function (CM_Params $params) {
            throw new Exception('Job failed');
        }));

        /** @var CM_Jobdistribution_Job_Abstract $job */
        try {
            $job->run(array('foo' => 'bar'));
            $this->fail('Job should have thrown an exception');
        } catch (Exception $ex) {
            $this->assertSame('Job failed', $ex->getMessage());
        }

        CMTest_TH::clearConfig();<commit_msg>Add test for Job::runMultiple<commit_after><?php

class CM_Jobdistribution_Job_AbstractTest extends CMTest_TestCase {

    protected function tearDown() {
        CMTest_TH::clearEnv();
    }

    public function testRun() {
        if (!extension_loaded('gearman')) {
            $this->markTestSkipped('Gearman Pecl Extension not installed.');
        }
        CM_Config::get()->CM_Jobdistribution_Job_Abstract->gearmanEnabled = true;

        /** @var CM_Jobdistribution_Job_Abstract $job */
        $job = $this->getMockForAbstractClass('CM_Jobdistribution_Job_Abstract', array(), '', true, true, true,
            array('_getGearmanClient', '_execute'));
        $gearmanClientMock = $this->getMock('GearmanClient', array('doNormal', 'returnCode'));
        $that = $this;
        $gearmanJobMock = $this->getMock('GearmanJob', array('sendFail', 'workload'));
        $gearmanClientMock->expects($this->any())->method('doNormal')->will($this->returnCallback(function ($jobName, $workload) use ($job, $gearmanClientMock, $gearmanJobMock, $that) {
            $gearmanJobMock->expects($that->any())->method('workload')->will($that->returnValue($workload));
            $gearmanJobMock->expects($that->any())->method('sendFail')->will($that->returnCallback(function () use ($gearmanClientMock, $that) {
                $gearmanClientMock->expects($that->any())->method('returnCode')->will($that->returnValue(GEARMAN_WORK_FAIL));
            }));
            return $job->__executeGearman($gearmanJobMock);
        }));
        $job->expects($this->any())->method('_getGearmanClient')->will($this->returnValue($gearmanClientMock));
        $job->expects($this->any())->method('_execute')->will($this->returnCallback(function (CM_Params $params) {
            return array_flip($params->getAllOriginal());
        }));

        $result = $job->run(array('foo' => 'bar'));
        $this->assertSame(array('bar' => 'foo'), $result);

        // Exception thrown in worker
        $job->expects($this->any())->method('_execute')->will($this->returnCallback(function (CM_Params $params) use ($gearmanJobMock) {
            $gearmanJobMock->sendFail();
        }));
        try {
            $job->run(array('foo' => 'bar'));
            $this->fail('Job should have thrown an exception');
        } catch (CM_Exception $ex) {
            $this->assertContains('Job `' . get_class($job) . '` failed', $ex->getMessage());
        }
    }

    public function testRunMultiple() {
        if (!extension_loaded('gearman')) {
            $this->markTestSkipped('Gearman Pecl Extension not installed.');
        }
        CM_Config::get()->CM_Jobdistribution_Job_Abstract->gearmanEnabled = true;

        $gearmanClient = $this->getMock('GearmanClient', array('addTask', 'runTasks', 'setCompleteCallback', 'setFailCallback'));
        $gearmanClient->expects($this->exactly(2))->method('addTask')->will($this->returnValue(true));
        $gearmanClient->expects($this->exactly(1))->method('runTasks')->will($this->returnValue(true));
        $gearmanClient->expects($this->exactly(1))->method('setCompleteCallback')->will($this->returnCallback(function ($completeCallback) {
            $task1 = $this->getMockBuilder('GearmanTask')->setMethods(array('data'))->getMock();
            $task1->expects($this->once())->method('data')->will($this->returnValue(array('bar1' => 'foo1')));
            $completeCallback($task1);

            $task2 = $this->getMockBuilder('GearmanTask')->setMethods(array('data'))->getMock();
            $task2->expects($this->once())->method('data')->will($this->returnValue(array('bar2' => 'foo2')));
            $completeCallback($task2);
        }));
        $gearmanClient->expects($this->exactly(1))->method('setFailCallback');
        /** @var GearmanClient $gearmanClient */

        $job = $this->getMockBuilder('CM_Jobdistribution_Job_Abstract')
            ->setMethods(array('_getGearmanClient', '_execute', '_getJobName'))->getMockForAbstractClass();
        $job->expects($this->any())->method('_getGearmanClient')->will($this->returnValue($gearmanClient));
        $job->expects($this->any())->method('_getJobName')->will($this->returnValue('myJob'));
        $job->expects($this->any())->method('_execute')->will($this->returnCallback(function (CM_Params $params) {
            return array_flip($params->getAllOriginal());
        }));
        /** @var CM_Jobdistribution_Job_Abstract $job */

        $result = $job->runMultiple(array(
            array('foo1' => 'bar1'),
            array('foo2' => 'bar2'),
        ));

        $this->assertSame(array(
            array('bar1' => 'foo1'),
            array('bar2' => 'foo2'),
        ), $result);
    }

    public function testRunGearmanDisabled() {
        CM_Config::get()->CM_Jobdistribution_Job_Abstract->gearmanEnabled = false;

        $job = $this->getMockForAbstractClass('CM_Jobdistribution_Job_Abstract', array(), '', true, true, true, array('_execute'));
        $job->expects($this->exactly(2))->method('_execute')->will($this->returnCallback(function (CM_Params $params) {
            return array_flip($params->getAllOriginal());
        }));

        /** @var CM_Jobdistribution_Job_Abstract $job */
        $result = $job->run(array('foo' => 'bar'));
        $this->assertSame(array('bar' => 'foo'), $result);

        $job->queue(array('foo' => 'bar'));
    }

    public function testRunGearmanDisabledThrows() {
        CM_Config::get()->CM_Jobdistribution_Job_Abstract->gearmanEnabled = false;

        $job = $this->getMockForAbstractClass('CM_Jobdistribution_Job_Abstract', array(), '', true, true, true, array('_execute'));
        $job->expects($this->exactly(1))->method('_execute')->will($this->returnCallback(function (CM_Params $params) {
            throw new Exception('Job failed');
        }));

        /** @var CM_Jobdistribution_Job_Abstract $job */
        try {
            $job->run(array('foo' => 'bar'));
            $this->fail('Job should have thrown an exception');
        } catch (Exception $ex) {
            $this->assertSame('Job failed', $ex->getMessage());
        }
{'fraction_non_alphanumeric': 0.1889077985784363, 'fraction_numerical': 0.002602863149464411, 'mean_word_length': 3.5040577096483316, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 230, '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': '5931959', 'n_tokens_mistral': 2957, 'n_tokens_neox': 2874, 'n_words': 387}