JobClassTest.php 5.9 KB

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