JobClassTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * RSQueueBundle for Symfony2
  4. *
  5. * Marc Morera 2013
  6. */
  7. namespace Mmoreram\GearmanBundle\Tests\Module;
  8. use Mmoreram\GearmanBundle\Module\JobClass;
  9. use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
  10. use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
  11. /**
  12. * Tests JobClassTest class
  13. */
  14. class JobClassTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @var Job
  18. *
  19. * Job annotation driver
  20. */
  21. private $jobAnnotation;
  22. /**
  23. * @var \ReflectionClass
  24. *
  25. * Reflection Method
  26. */
  27. private $reflectionMethod;
  28. /**
  29. * @var string
  30. *
  31. * Callable name
  32. */
  33. private $callableNameClass = 'MyClassCallablaName';
  34. /**
  35. * @var string
  36. *
  37. * Class name
  38. */
  39. private $methodName = 'myMethod';
  40. /**
  41. * @var array
  42. *
  43. * Servers list
  44. */
  45. private $servers = array(
  46. array(
  47. 'host' => '192.168.1.1',
  48. 'port' => '8080',
  49. ),
  50. );
  51. /**
  52. * @var array
  53. *
  54. * Default settings
  55. */
  56. private $defaultSettings = array(
  57. 'method' => 'doHigh',
  58. 'iterations' => 100,
  59. );
  60. /**
  61. * Setup
  62. */
  63. public function setUp()
  64. {
  65. $this->reflectionMethod = $this->getMockBuilder('\ReflectionMethod')
  66. ->disableOriginalConstructor()
  67. ->setMethods(array(
  68. 'getName',
  69. ))
  70. ->getMock();
  71. $this->jobAnnotation = $this->getMockBuilder('\Mmoreram\GearmanBundle\Driver\Gearman\Job')
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. }
  75. /**
  76. * Testing scenario with all Job annotations filled
  77. *
  78. * All settings given in annotations should be considered to configure Job
  79. *
  80. * Also testing server definition in JobAnnotation as an array of arrays ( multi server )
  81. */
  82. public function testJobAnnotationsDefined()
  83. {
  84. $this
  85. ->reflectionMethod
  86. ->expects($this->once())
  87. ->method('getName')
  88. ->will($this->returnValue($this->methodName));
  89. $this->jobAnnotation->name = 'myOtherMethodName';
  90. $this->jobAnnotation->description = 'This is my own description';
  91. $this->jobAnnotation->iterations = 200;
  92. $this->jobAnnotation->defaultMethod = 'doHighBackground';
  93. $this->jobAnnotation->servers = array(
  94. array(
  95. 'host' => '10.0.0.2',
  96. 'port' => '80',
  97. ),
  98. );
  99. $jobClass = new JobClass($this->jobAnnotation, $this->reflectionMethod, $this->callableNameClass, $this->servers, $this->defaultSettings);
  100. $this->assertEquals($jobClass->toArray(), array(
  101. 'callableName' => $this->jobAnnotation->name,
  102. 'methodName' => $this->methodName,
  103. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->jobAnnotation->name),
  104. 'description' => $this->jobAnnotation->description,
  105. 'iterations' => $this->jobAnnotation->iterations,
  106. 'servers' => $this->jobAnnotation->servers,
  107. 'defaultMethod' => $this->jobAnnotation->defaultMethod,
  108. ));
  109. }
  110. /**
  111. * Testing scenario with any Job annotation filled
  112. *
  113. * All settings set as default should be considered to configure Job
  114. *
  115. * Also testing empty server definition in JobAnnotation
  116. */
  117. public function testJonAnnotationsEmpty()
  118. {
  119. $this
  120. ->reflectionMethod
  121. ->expects($this->exactly(2))
  122. ->method('getName')
  123. ->will($this->returnValue($this->methodName));
  124. $jobClass = new JobClass($this->jobAnnotation, $this->reflectionMethod, $this->callableNameClass, $this->servers, $this->defaultSettings);
  125. $this->assertEquals($jobClass->toArray(), array(
  126. 'callableName' => $this->methodName,
  127. 'methodName' => $this->methodName,
  128. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  129. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  130. 'iterations' => $this->defaultSettings['iterations'],
  131. 'servers' => $this->servers,
  132. 'defaultMethod' => $this->defaultSettings['method'],
  133. ));
  134. }
  135. /**
  136. * Testing specific server scenario configured in Job annotations as a simple server
  137. */
  138. public function testCombinationServers()
  139. {
  140. $this
  141. ->reflectionMethod
  142. ->expects($this->exactly(2))
  143. ->method('getName')
  144. ->will($this->returnValue($this->methodName));
  145. $this->jobAnnotation->servers = array(
  146. 'host' => '10.0.0.2',
  147. 'port' => '80',
  148. );
  149. $jobClass = new JobClass($this->jobAnnotation, $this->reflectionMethod, $this->callableNameClass, $this->servers, $this->defaultSettings);
  150. $this->assertEquals($jobClass->toArray(), array(
  151. 'callableName' => $this->methodName,
  152. 'methodName' => $this->methodName,
  153. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  154. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  155. 'iterations' => $this->defaultSettings['iterations'],
  156. 'servers' => array($this->jobAnnotation->servers),
  157. 'defaultMethod' => $this->defaultSettings['method'],
  158. ));
  159. }
  160. }