GearmanExecuteTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Gearman Bundle for Symfony2
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * Feel free to edit as you please, and have fun.
  9. *
  10. * @author Marc Morera <yuhu@mmoreram.com>
  11. */
  12. namespace Mmoreram\GearmanBundle\Tests\Service;
  13. use Mmoreram\GearmanBundle\Service\GearmanExecute;
  14. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  15. use Mmoreram\GearmanBundle\GearmanEvents;
  16. /**
  17. * Tests GearmanExecute class
  18. */
  19. class GearmanExecuteTest extends WebTestCase
  20. {
  21. /**
  22. * Test service can be instanced through container
  23. */
  24. public function testGearmanExecuteLoadFromContainer()
  25. {
  26. static::$kernel = static::createKernel();
  27. static::$kernel->boot();
  28. $this->assertInstanceOf(
  29. '\Mmoreram\GearmanBundle\Service\GearmanExecute',
  30. static::$kernel
  31. ->getContainer()
  32. ->get('gearman.execute')
  33. );
  34. }
  35. public function testDispatchingEventsOnJob()
  36. {
  37. // Worker mock
  38. $worker = $this->getMockBuilder('\GearmanWorker')
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $worker->method('addServer')->willReturn(true);
  42. // Wrapper mock
  43. $workers = array(
  44. 0 => array(
  45. 'className' => "Mmoreram\\GearmanBundle\\Tests\\Service\\Mocks\\SingleCleanFile",
  46. 'fileName' => dirname(__FILE__) . '/Mocks/SingleCleanFile.php',
  47. 'callableName' => null,
  48. 'description' => "test",
  49. 'service' => false,
  50. 'servers' => array(),
  51. 'iterations' => 1,
  52. 'jobs' => array(
  53. 0 => array(
  54. 'callableName' => "test",
  55. 'methodName' => "test",
  56. 'realCallableName' => "test",
  57. 'jobPrefix' => NULL,
  58. 'realCallableNameNoPrefix' => "test",
  59. 'description' => "test",
  60. 'iterations' => 1,
  61. 'servers' => array(),
  62. 'defaultMethod' => "doBackground"
  63. )
  64. )
  65. )
  66. );
  67. $wrapper = $this->getMockBuilder('Mmoreram\GearmanBundle\Service\GearmanCacheWrapper')
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $wrapper->method('getWorkers')
  71. ->willReturn($workers);
  72. // Prepare a dispatcher to listen to tested events
  73. $startingFlag = false;
  74. $executedFlag = false;
  75. $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
  76. $dispatcher->addListener(GearmanEvents::GEARMAN_WORK_STARTING, function() use (&$startingFlag){
  77. $startingFlag = true;
  78. });
  79. $dispatcher->addListener(GearmanEvents::GEARMAN_WORK_EXECUTED, function() use (&$executedFlag){
  80. $executedFlag = true;
  81. });
  82. // Create the service under test
  83. $service = new GearmanExecute($wrapper, array());
  84. $service->setEventDispatcher($dispatcher);
  85. // We need a job object, this part could be improved
  86. $object = new \Mmoreram\GearmanBundle\Tests\Service\Mocks\SingleCleanFile();
  87. // Finalize worker mock by making it call our job object
  88. // This is normally handled by Gearman, but for test purpose we must simulate it
  89. $worker->method('work')->will($this->returnCallback(function() use ($service, $object){
  90. $service->handleJob(new \GearmanJob(), array(
  91. 'job_object_instance' => $object,
  92. 'job_method' => 'myMethod',
  93. 'jobs' => array()
  94. ));
  95. return true;
  96. }));
  97. // Execute a job :)
  98. $service->executeJob('test', $worker);
  99. // Do we have the events ?
  100. $this->assertTrue($startingFlag);
  101. $this->assertTrue($executedFlag);
  102. }
  103. }