2011-03-27 07:42:46 +00:00
|
|
|
<?php
|
2018-05-25 07:26:54 +00:00
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
namespace Resque\Test;
|
|
|
|
|
2011-03-27 07:42:46 +00:00
|
|
|
/**
|
2021-02-18 23:23:32 +00:00
|
|
|
* \Resque\Event tests.
|
2011-03-27 07:42:46 +00:00
|
|
|
*
|
2018-05-25 07:26:54 +00:00
|
|
|
* @package Resque/Tests
|
2021-02-18 23:23:32 +00:00
|
|
|
* @author Daniel Mason <daniel@m2.nz>
|
2018-05-25 07:26:54 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php
|
2011-03-27 07:42:46 +00:00
|
|
|
*/
|
2018-05-25 07:26:54 +00:00
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
class EventTest extends TestCase
|
2011-03-27 07:42:46 +00:00
|
|
|
{
|
2018-05-29 20:25:02 +00:00
|
|
|
private $callbacksHit = [];
|
2018-05-25 08:11:17 +00:00
|
|
|
private $worker;
|
2018-05-25 07:26:54 +00:00
|
|
|
|
2020-04-10 21:24:18 +00:00
|
|
|
public function setUp(): void
|
2018-05-25 07:26:54 +00:00
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
TestJob::$called = false;
|
2018-05-25 07:26:54 +00:00
|
|
|
|
|
|
|
// Register a worker to test with
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->worker = new \Resque\Worker('jobs');
|
|
|
|
$this->worker->setLogger(new \Resque\Log());
|
2018-05-25 07:26:54 +00:00
|
|
|
$this->worker->registerWorker();
|
|
|
|
}
|
|
|
|
|
2020-04-10 21:24:18 +00:00
|
|
|
public function tearDown(): void
|
2018-05-25 07:26:54 +00:00
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Event::clearListeners();
|
2018-05-29 20:25:02 +00:00
|
|
|
$this->callbacksHit = [];
|
2018-05-25 07:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getEventTestJob()
|
|
|
|
{
|
2018-05-29 20:25:02 +00:00
|
|
|
$payload = [
|
2021-02-18 23:23:32 +00:00
|
|
|
'class' => '\Resque\Test\TestJob',
|
2018-05-29 20:25:02 +00:00
|
|
|
'args' => [
|
|
|
|
['somevar'],
|
|
|
|
],
|
|
|
|
];
|
2021-02-18 23:23:32 +00:00
|
|
|
$job = new \Resque\Job\Job('jobs', $payload);
|
2018-05-25 07:26:54 +00:00
|
|
|
$job->worker = $this->worker;
|
|
|
|
return $job;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function eventCallbackProvider()
|
|
|
|
{
|
2018-05-29 20:25:02 +00:00
|
|
|
return [
|
|
|
|
['beforePerform', 'beforePerformEventCallback'],
|
|
|
|
['afterPerform', 'afterPerformEventCallback'],
|
|
|
|
['afterFork', 'afterForkEventCallback'],
|
|
|
|
];
|
2018-05-25 07:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider eventCallbackProvider
|
2018-05-29 20:25:02 +00:00
|
|
|
* @param $event
|
|
|
|
* @param $callback
|
2018-05-25 07:26:54 +00:00
|
|
|
*/
|
|
|
|
public function testEventCallbacksFire($event, $callback)
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Event::listen($event, [$this, $callback]);
|
2018-05-25 07:26:54 +00:00
|
|
|
|
|
|
|
$job = $this->getEventTestJob();
|
|
|
|
$this->worker->perform($job);
|
|
|
|
$this->worker->work(0);
|
|
|
|
|
|
|
|
$this->assertContains($callback, $this->callbacksHit, $event . ' callback (' . $callback . ') was not called');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBeforeForkEventCallbackFires()
|
|
|
|
{
|
|
|
|
$event = 'beforeFork';
|
|
|
|
$callback = 'beforeForkEventCallback';
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Event::listen($event, [$this, $callback]);
|
|
|
|
\Resque\Resque::enqueue('jobs', '\Resque\Test\TestJob', [
|
2018-05-25 07:26:54 +00:00
|
|
|
'somevar'
|
2018-05-29 20:25:02 +00:00
|
|
|
]);
|
|
|
|
$this->getEventTestJob();
|
2018-05-25 07:26:54 +00:00
|
|
|
$this->worker->work(0);
|
|
|
|
$this->assertContains($callback, $this->callbacksHit, $event . ' callback (' . $callback . ') was not called');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBeforeEnqueueEventCallbackFires()
|
|
|
|
{
|
|
|
|
$event = 'beforeEnqueue';
|
|
|
|
$callback = 'beforeEnqueueEventCallback';
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Event::listen($event, [$this, $callback]);
|
|
|
|
\Resque\Resque::enqueue('jobs', '\Resque\Test\TestJob', [
|
2018-05-25 07:26:54 +00:00
|
|
|
'somevar'
|
2018-05-29 20:25:02 +00:00
|
|
|
]);
|
2018-05-25 07:26:54 +00:00
|
|
|
$this->assertContains($callback, $this->callbacksHit, $event . ' callback (' . $callback . ') was not called');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBeforePerformEventCanStopWork()
|
|
|
|
{
|
|
|
|
$callback = 'beforePerformEventDontPerformCallback';
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Event::listen('beforePerform', [$this, $callback]);
|
2018-05-25 07:26:54 +00:00
|
|
|
|
|
|
|
$job = $this->getEventTestJob();
|
|
|
|
|
|
|
|
$this->assertFalse($job->perform());
|
|
|
|
$this->assertContains($callback, $this->callbacksHit, $callback . ' callback was not called');
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertFalse(TestJob::$called, 'Job was still performed though Resque_Job_DontPerform was thrown');
|
2018-05-25 07:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBeforeEnqueueEventStopsJobCreation()
|
|
|
|
{
|
|
|
|
$callback = 'beforeEnqueueEventDontCreateCallback';
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Event::listen('beforeEnqueue', [$this, $callback]);
|
|
|
|
\Resque\Event::listen('afterEnqueue', [$this, 'afterEnqueueEventCallback']);
|
2018-05-25 07:26:54 +00:00
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
$result = \Resque\Resque::enqueue('jobs', '\Resque\Test\TestClass');
|
2018-05-25 07:26:54 +00:00
|
|
|
$this->assertContains($callback, $this->callbacksHit, $callback . ' callback was not called');
|
|
|
|
$this->assertNotContains('afterEnqueueEventCallback', $this->callbacksHit, 'afterEnqueue was still called, even though it should not have been');
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAfterEnqueueEventCallbackFires()
|
|
|
|
{
|
|
|
|
$callback = 'afterEnqueueEventCallback';
|
|
|
|
$event = 'afterEnqueue';
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Event::listen($event, [$this, $callback]);
|
|
|
|
\Resque\Resque::enqueue('jobs', '\Resque\Test\TestJob', [
|
2018-05-25 07:26:54 +00:00
|
|
|
'somevar'
|
2018-05-29 20:25:02 +00:00
|
|
|
]);
|
2018-05-25 07:26:54 +00:00
|
|
|
$this->assertContains($callback, $this->callbacksHit, $event . ' callback (' . $callback . ') was not called');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStopListeningRemovesListener()
|
|
|
|
{
|
|
|
|
$callback = 'beforePerformEventCallback';
|
|
|
|
$event = 'beforePerform';
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Event::listen($event, [$this, $callback]);
|
|
|
|
\Resque\Event::stopListening($event, [$this, $callback]);
|
2018-05-25 07:26:54 +00:00
|
|
|
|
|
|
|
$job = $this->getEventTestJob();
|
|
|
|
$this->worker->perform($job);
|
|
|
|
$this->worker->work(0);
|
|
|
|
|
|
|
|
$this->assertNotContains($callback, $this->callbacksHit,
|
|
|
|
$event . ' callback (' . $callback . ') was called though Resque_Event::stopListening was called'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-29 20:25:02 +00:00
|
|
|
public function beforePerformEventDontPerformCallback()
|
2018-05-25 07:26:54 +00:00
|
|
|
{
|
|
|
|
$this->callbacksHit[] = __FUNCTION__;
|
2021-02-18 23:23:32 +00:00
|
|
|
throw new \Resque\Job\DontPerform();
|
2018-05-25 07:26:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 20:25:02 +00:00
|
|
|
public function beforeEnqueueEventDontCreateCallback()
|
2018-05-25 07:26:54 +00:00
|
|
|
{
|
|
|
|
$this->callbacksHit[] = __FUNCTION__;
|
2021-02-18 23:23:32 +00:00
|
|
|
throw new \Resque\Job\DontCreate();
|
2018-05-25 07:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function assertValidEventCallback($function, $job)
|
|
|
|
{
|
|
|
|
$this->callbacksHit[] = $function;
|
2021-02-18 23:23:32 +00:00
|
|
|
if (!$job instanceof \Resque\Job\Job) {
|
|
|
|
$this->fail('Callback job argument is not an instance of \Resque\Job\Job');
|
2018-05-25 07:26:54 +00:00
|
|
|
}
|
|
|
|
$args = $job->getArguments();
|
|
|
|
$this->assertEquals($args[0], 'somevar');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function afterEnqueueEventCallback($class, $args)
|
|
|
|
{
|
|
|
|
$this->callbacksHit[] = __FUNCTION__;
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertEquals('\Resque\Test\TestJob', $class);
|
2018-05-29 20:25:02 +00:00
|
|
|
$this->assertEquals([
|
2018-05-25 07:26:54 +00:00
|
|
|
'somevar',
|
2018-05-29 20:25:02 +00:00
|
|
|
], $args);
|
2018-05-25 07:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function beforeEnqueueEventCallback($job)
|
|
|
|
{
|
|
|
|
$this->callbacksHit[] = __FUNCTION__;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function beforePerformEventCallback($job)
|
|
|
|
{
|
|
|
|
$this->assertValidEventCallback(__FUNCTION__, $job);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function afterPerformEventCallback($job)
|
|
|
|
{
|
|
|
|
$this->assertValidEventCallback(__FUNCTION__, $job);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function beforeForkEventCallback($job)
|
|
|
|
{
|
|
|
|
$this->assertValidEventCallback(__FUNCTION__, $job);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function afterForkEventCallback($job)
|
|
|
|
{
|
|
|
|
$this->assertValidEventCallback(__FUNCTION__, $job);
|
|
|
|
}
|
2012-04-05 01:27:08 +00:00
|
|
|
}
|