Skip to content

Commit b7c91bd

Browse files
committed
move quartz to pkg
0 parents  commit b7c91bd

5 files changed

+673
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
namespace Quartz\Tests\App;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use Quartz\App\CheckMasterProcessSubscriber;
6+
use Quartz\Core\SchedulerException;
7+
use Quartz\Events\Event;
8+
use Quartz\Events\TickEvent;
9+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10+
11+
class CheckMasterProcessSubscriberTest extends TestCase
12+
{
13+
public function testShouldImplementEventSubscriberInterface()
14+
{
15+
$this->assertInstanceOf(EventSubscriberInterface::class, new CheckMasterProcessSubscriber());
16+
}
17+
18+
public function testShouldReturnExpectedSubscribedEvents()
19+
{
20+
$expectedEvents = [
21+
Event::SCHEDULER_TICK => 'checkMasterProcessor',
22+
];
23+
24+
$this->assertSame($expectedEvents, CheckMasterProcessSubscriber::getSubscribedEvents());
25+
}
26+
27+
public function testShouldThrowExceptionIfMasterProcessPidEnvIsNotSet()
28+
{
29+
$this->expectException(SchedulerException::class);
30+
$this->expectExceptionMessage('The extension rely on MASTER_PROCESS_PID env var set but it is not set.');
31+
32+
$s = new CheckMasterProcessSubscriber();
33+
$s->checkMasterProcessor(new TickEvent());
34+
}
35+
36+
public function testShouldSetInterruptedIfMasterProcessIsNotRunning()
37+
{
38+
putenv('MASTER_PROCESS_PID=-12345');
39+
40+
$s = new CheckMasterProcessSubscriber();
41+
$s->checkMasterProcessor($event = new TickEvent());
42+
43+
$this->assertTrue($event->isInterrupted());
44+
}
45+
46+
public function testShouldNotSetInterruptedIfMasterProcessIsRunning()
47+
{
48+
putenv('MASTER_PROCESS_PID='.posix_getpid());
49+
50+
$s = new CheckMasterProcessSubscriber();
51+
$s->checkMasterProcessor($event = new TickEvent());
52+
53+
$this->assertFalse($event->isInterrupted());
54+
}
55+
}

Tests/App/EnqueueResponseJobTest.php

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
namespace Quartz\Tests\App;
3+
4+
use Enqueue\Client\ProducerV2Interface;
5+
use PHPUnit\Framework\TestCase;
6+
use Quartz\App\EnqueueResponseJob;
7+
use Quartz\Core\Job;
8+
use Quartz\Core\JobExecutionContext;
9+
use Quartz\Core\Scheduler;
10+
use Quartz\JobDetail\JobDetail;
11+
use Quartz\Triggers\SimpleTrigger;
12+
13+
class EnqueueResponseJobTest extends TestCase
14+
{
15+
public function testShouldImplementJobInterface()
16+
{
17+
$this->assertInstanceOf(Job::class, new EnqueueResponseJob($this->createProducerMock()));
18+
}
19+
20+
public function testShouldUnscheduleTrigger()
21+
{
22+
$producer = $this->createProducerMock();
23+
$producer
24+
->expects($this->never())
25+
->method('sendEvent')
26+
;
27+
$producer
28+
->expects($this->never())
29+
->method('sendCommand')
30+
;
31+
32+
$job = new EnqueueResponseJob($producer);
33+
34+
$context = new JobExecutionContext($this->createMock(Scheduler::class), new SimpleTrigger(), new JobDetail());
35+
36+
$job->execute($context);
37+
38+
$this->assertSame('There is no enqueue topic or command', $context->getTrigger()->getErrorMessage());
39+
$this->assertTrue($context->isUnscheduleFiringTrigger());
40+
}
41+
42+
public function testShouldSendEvent()
43+
{
44+
$producer = $this->createProducerMock();
45+
$producer
46+
->expects($this->once())
47+
->method('sendEvent')
48+
->with('the-topic', ['topic' => 'the-topic'])
49+
;
50+
51+
$job = new EnqueueResponseJob($producer);
52+
$trigger = new SimpleTrigger();
53+
$trigger->setJobDataMap(['topic' => 'the-topic']);
54+
55+
$context = new JobExecutionContext($this->createMock(Scheduler::class), $trigger, new JobDetail());
56+
57+
$job->execute($context);
58+
59+
$this->assertFalse($context->isUnscheduleFiringTrigger());
60+
}
61+
62+
public function testShouldSendCommand()
63+
{
64+
$producer = $this->createProducerMock();
65+
$producer
66+
->expects($this->once())
67+
->method('sendCommand')
68+
->with('the-command', ['command' => 'the-command'])
69+
;
70+
71+
$job = new EnqueueResponseJob($producer);
72+
$trigger = new SimpleTrigger();
73+
$trigger->setJobDataMap(['command' => 'the-command']);
74+
75+
$context = new JobExecutionContext($this->createMock(Scheduler::class), $trigger, new JobDetail());
76+
77+
$job->execute($context);
78+
79+
$this->assertFalse($context->isUnscheduleFiringTrigger());
80+
}
81+
82+
/**
83+
* @return \PHPUnit_Framework_MockObject_MockObject|ProducerV2Interface
84+
*/
85+
private function createProducerMock()
86+
{
87+
return $this->createMock(ProducerV2Interface::class);
88+
}
89+
}
+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
namespace Quartz\Tests\App;
3+
4+
use Enqueue\Consumption\Result;
5+
use Enqueue\Null\NullMessage;
6+
use Enqueue\Psr\PsrContext;
7+
use Enqueue\Psr\PsrProcessor;
8+
use Quartz\App\RemoteSchedulerProcessor;
9+
use Quartz\App\RpcProtocol;
10+
use Quartz\Core\Scheduler;
11+
use Quartz\JobDetail\JobDetail;
12+
use Quartz\Triggers\SimpleTrigger;
13+
14+
class RemoteSchedulerProcessorTest extends \PHPUnit_Framework_TestCase
15+
{
16+
public function testShouldImpleentPsrProcessorInterface()
17+
{
18+
$processor = new RemoteSchedulerProcessor($this->createSchedulerMock(), $this->createRpcProtocolMock());
19+
20+
$this->assertInstanceOf(PsrProcessor::class, $processor);
21+
}
22+
23+
public function testShouldInvokeSchedulerMethodAndReturnResponse()
24+
{
25+
$message = new NullMessage();
26+
$message->setBody('"request"');
27+
28+
$trigger = new SimpleTrigger();
29+
$job = new JobDetail();
30+
31+
$proto = $this->createRpcProtocolMock();
32+
$proto
33+
->expects($this->once())
34+
->method('decodeRequest')
35+
->with('request')
36+
->willReturn(['method' => 'scheduleJob', 'args' => [$trigger, $job]])
37+
;
38+
$proto
39+
->expects($this->once())
40+
->method('encodeValue')
41+
->with('scheduler-result')
42+
->willReturn('result')
43+
;
44+
45+
$scheduler = $this->createSchedulerMock();
46+
$scheduler
47+
->expects($this->once())
48+
->method('scheduleJob')
49+
->with($this->identicalTo($trigger), $this->identicalTo($job))
50+
->willReturn('scheduler-result')
51+
;
52+
53+
$context = $this->createPsrContextMock();
54+
$context
55+
->expects($this->once())
56+
->method('createMessage')
57+
->willReturn(new NullMessage('result'))
58+
;
59+
60+
$processor = new RemoteSchedulerProcessor($scheduler, $proto);
61+
$result = $processor->process($message, $context);
62+
63+
$this->assertInstanceOf(Result::class, $result);
64+
$this->assertNotNull($result->getReply());
65+
$this->assertSame('result', $result->getReply()->getBody());
66+
}
67+
68+
public function testOnExceptionShouldEncodeExceptionAndReturn()
69+
{
70+
$message = new NullMessage();
71+
$message->setBody('"request"');
72+
73+
$trigger = new SimpleTrigger();
74+
$job = new JobDetail();
75+
76+
$ex = new \Exception();
77+
78+
$proto = $this->createRpcProtocolMock();
79+
$proto
80+
->expects($this->once())
81+
->method('decodeRequest')
82+
->with('request')
83+
->willReturn(['method' => 'scheduleJob', 'args' => [$trigger, $job]])
84+
;
85+
$proto
86+
->expects($this->once())
87+
->method('encodeValue')
88+
->with($this->identicalTo($ex))
89+
->willReturn('result')
90+
;
91+
92+
$scheduler = $this->createSchedulerMock();
93+
$scheduler
94+
->expects($this->once())
95+
->method('scheduleJob')
96+
->with($this->identicalTo($trigger), $this->identicalTo($job))
97+
->willThrowException($ex)
98+
;
99+
100+
$context = $this->createPsrContextMock();
101+
$context
102+
->expects($this->once())
103+
->method('createMessage')
104+
->willReturn(new NullMessage('result'))
105+
;
106+
107+
$processor = new RemoteSchedulerProcessor($scheduler, $proto);
108+
$result = $processor->process($message, $context);
109+
110+
$this->assertInstanceOf(Result::class, $result);
111+
$this->assertNotNull($result->getReply());
112+
$this->assertSame('result', $result->getReply()->getBody());
113+
}
114+
115+
/**
116+
* @return \PHPUnit_Framework_MockObject_MockObject|PsrContext
117+
*/
118+
private function createPsrContextMock()
119+
{
120+
return $this->createMock(PsrContext::class);
121+
}
122+
123+
/**
124+
* @return \PHPUnit_Framework_MockObject_MockObject|RpcProtocol
125+
*/
126+
private function createRpcProtocolMock()
127+
{
128+
return $this->createMock(RpcProtocol::class);
129+
}
130+
131+
/**
132+
* @return \PHPUnit_Framework_MockObject_MockObject|Scheduler
133+
*/
134+
private function createSchedulerMock()
135+
{
136+
return $this->createMock(Scheduler::class);
137+
}
138+
}

Tests/App/RemoteSchedulerTest.php

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
namespace Quartz\Tests\App;
3+
4+
use Enqueue\Client\ProducerV2Interface;
5+
use Enqueue\Null\NullMessage;
6+
use Enqueue\Rpc\Promise;
7+
use Enqueue\Util\JSON;
8+
use PHPUnit\Framework\TestCase;
9+
use Quartz\App\RemoteScheduler;
10+
use Quartz\App\RpcProtocol;
11+
use Quartz\Core\SchedulerException;
12+
use Quartz\JobDetail\JobDetail;
13+
use Quartz\Triggers\SimpleTrigger;
14+
15+
class RemoteSchedulerTest extends TestCase
16+
{
17+
public function testShouldDoRemoteCall()
18+
{
19+
$trigger = new SimpleTrigger();
20+
$job = new JobDetail();
21+
22+
$request = 'request';
23+
$response = 'response';
24+
25+
$responseMessage = new NullMessage(JSON::encode(['key' => 'value']));
26+
27+
$promise = $this->createMock(Promise::class);
28+
$promise
29+
->expects($this->once())
30+
->method('receive')
31+
->willReturn($responseMessage)
32+
;
33+
34+
$producer = $this->createMock(ProducerV2Interface::class);
35+
$producer
36+
->expects($this->once())
37+
->method('sendCommand')
38+
->with(RemoteScheduler::COMMAND, $request)
39+
->willReturn($promise)
40+
;
41+
42+
$rpcProto = $this->createMock(RpcProtocol::class);
43+
$rpcProto
44+
->expects($this->once())
45+
->method('encodeRequest')
46+
->with('scheduleJob', [$trigger, $job])
47+
->willReturn($request)
48+
;
49+
$rpcProto
50+
->expects($this->once())
51+
->method('decodeValue')
52+
->with(['key' => 'value'])
53+
->willReturn($response)
54+
;
55+
56+
$scheduler = new RemoteScheduler($producer, $rpcProto);
57+
58+
$result = $scheduler->scheduleJob($trigger, $job);
59+
60+
$this->assertSame($response, $result);
61+
}
62+
63+
public function testShouldThrowExceptionIfExceptionReceived()
64+
{
65+
$trigger = new SimpleTrigger();
66+
$job = new JobDetail();
67+
68+
$responseMessage = new NullMessage();
69+
70+
$e = new SchedulerException('message');
71+
72+
$promise = $this->createMock(Promise::class);
73+
$promise
74+
->expects($this->once())
75+
->method('receive')
76+
->willReturn($responseMessage)
77+
;
78+
79+
$producer = $this->createMock(ProducerV2Interface::class);
80+
$producer
81+
->expects($this->once())
82+
->method('sendCommand')
83+
->willReturn($promise)
84+
;
85+
86+
$rpcProto = $this->createMock(RpcProtocol::class);
87+
$rpcProto
88+
->expects($this->once())
89+
->method('decodeValue')
90+
->willReturn($e)
91+
;
92+
93+
$scheduler = new RemoteScheduler($producer, $rpcProto);
94+
95+
$this->expectException(SchedulerException::class);
96+
$this->expectExceptionMessage('message');
97+
98+
$scheduler->scheduleJob($trigger, $job);
99+
}
100+
}

0 commit comments

Comments
 (0)