Skip to content

Commit

Permalink
Queueをラップしてkernelパラメータを渡せるように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
polidog committed Mar 3, 2015
1 parent 9c78944 commit ddd055b
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
namespace Tavii\SQSJobQueueBundle;

use Aws\Sqs\SqsClient;
use Tavii\SQSJobQueue\Job\JobInterface;
use Tavii\SQSJobQueue\Message\MessageInterface;
use Tavii\SQSJobQueue\Queue\Queue as BaseQueue;
use Tavii\SQSJobQueue\Queue\QueueInterface;

class Queue implements QueueInterface
{

/**
* Aamazon SQS Client
* @var SqsClient
*/
private $baseQueue;


/**
* @param SqsClient $client
*/
public function __construct(BaseQueue $baseQueue, array $kernelOptions)
{
$this->baseQueue = $baseQueue;
$this->kernelOptions = $kernelOptions;
}

/**
* {@inheritdoc}
*/
public function pull($name)
{
$message = $this->baseQueue->pull($name);
$job = $message->getJob();
if ($job instanceof ContainerAwareJob) {
$job->setKernelOptions($this->kernelOptions);
}
return $message;
}

/**
* {@inheritdoc}
*/
public function push(JobInterface $job)
{
return $this->baseQueue->push($job);
}

/**
* {@inheritdoc}
*/
public function delete(MessageInterface $message)
{
if ($this->baseQueue->delete($message)) {
$job = $message->getJob();
unset($job);
return true;
}
return false;
}


}
59 changes: 59 additions & 0 deletions Tests/QueueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
namespace Tavii\SQSJobQueueBundle;

use Phake;
use Tavii\SQSJobQueue\Job\booelan;
use Tavii\SQSJobQueue\Message\Message;

class QueueTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function pullする際にKernelOptionを渡すことができる()
{
$name = "test_queue";
$kernelOptions = array(
'kernel.root_dir' => '.',
'kernel.environment' => 'test',
'kernel.debug' => true,
);

$job = Phake::mock('Tavii\SQSJobQueueBundle\ContainerAwareJob');
$message = Phake::mock('Tavii\SQSJobQueue\Message\Message');
$baseQueue = Phake::mock('Tavii\SQSJobQueue\Queue\Queue');

Phake::when($baseQueue)->pull($name)
->thenReturn($message);
Phake::when($message)->getJob()
->thenReturn($job);

$queue = new Queue($baseQueue, $kernelOptions);
$queue->pull($name);

Phake::verify($baseQueue)->pull($name);
Phake::verify($message)->getJob();
Phake::verify($job)->setKernelOptions($kernelOptions);

}
}

class DummyContainerAwareJob extends ContainerAwareJob
{
/**
* @return booelan
*/
public function run()
{

}

/**
* @return string
*/
public function getName()
{
return 'dummy_container_aware_job';
}

}

0 comments on commit ddd055b

Please sign in to comment.