JobClassTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * Method reflection class
  26. */
  27. private $methodReflectionClass;
  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->methodReflectionClass = $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 first combination
  77. */
  78. public function testCombination1()
  79. {
  80. $this
  81. ->methodReflectionClass
  82. ->expects($this->once())
  83. ->method('getName')
  84. ->will($this->returnValue($this->methodName));
  85. $this->jobAnnotation->name = 'myOtherMethodName';
  86. $this->jobAnnotation->description = 'This is my own description';
  87. $this->jobAnnotation->iterations = 200;
  88. $this->jobAnnotation->defaultMethod = 'doHighBackground';
  89. $this->jobAnnotation->servers = array(
  90. array(
  91. 'host' => '10.0.0.2',
  92. 'port' => '80',
  93. ),
  94. );
  95. $jobClass = new JobClass($this->jobAnnotation, $this->methodReflectionClass, $this->callableNameClass, $this->servers, $this->defaultSettings);
  96. $this->assertEquals($jobClass->toArray(), array(
  97. 'callableName' => $this->jobAnnotation->name,
  98. 'methodName' => $this->methodName,
  99. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->jobAnnotation->name),
  100. 'description' => $this->jobAnnotation->description,
  101. 'iterations' => $this->jobAnnotation->iterations,
  102. 'servers' => $this->jobAnnotation->servers,
  103. 'defaultMethod' => $this->jobAnnotation->defaultMethod,
  104. ));
  105. }
  106. /**
  107. * Testing second combination
  108. */
  109. public function testCombination2()
  110. {
  111. $this
  112. ->methodReflectionClass
  113. ->expects($this->exactly(2))
  114. ->method('getName')
  115. ->will($this->returnValue($this->methodName));
  116. $jobClass = new JobClass($this->jobAnnotation, $this->methodReflectionClass, $this->callableNameClass, $this->servers, $this->defaultSettings);
  117. $this->assertEquals($jobClass->toArray(), array(
  118. 'callableName' => $this->methodName,
  119. 'methodName' => $this->methodName,
  120. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  121. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  122. 'iterations' => $this->defaultSettings['iterations'],
  123. 'servers' => $this->servers,
  124. 'defaultMethod' => $this->defaultSettings['method'],
  125. ));
  126. }
  127. /**
  128. * Testing specific server scenario
  129. */
  130. public function testCombinationServers()
  131. {
  132. $this
  133. ->methodReflectionClass
  134. ->expects($this->exactly(2))
  135. ->method('getName')
  136. ->will($this->returnValue($this->methodName));
  137. $this->jobAnnotation->servers = array(
  138. 'host' => '10.0.0.2',
  139. 'port' => '80',
  140. );
  141. $jobClass = new JobClass($this->jobAnnotation, $this->methodReflectionClass, $this->callableNameClass, $this->servers, $this->defaultSettings);
  142. $this->assertEquals($jobClass->toArray(), array(
  143. 'callableName' => $this->methodName,
  144. 'methodName' => $this->methodName,
  145. 'realCallableName' => str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
  146. 'description' => $jobClass::DEFAULT_DESCRIPTION,
  147. 'iterations' => $this->defaultSettings['iterations'],
  148. 'servers' => array($this->jobAnnotation->servers),
  149. 'defaultMethod' => $this->defaultSettings['method'],
  150. ));
  151. }
  152. }