DatabaseConnectorTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace think\test\queue;
  3. use Carbon\Carbon;
  4. use Mockery as m;
  5. use Mockery\MockInterface;
  6. use ReflectionClass;
  7. use stdClass;
  8. use think\Db;
  9. use think\queue\Connector;
  10. use think\queue\connector\Database;
  11. class DatabaseConnectorTest extends TestCase
  12. {
  13. /** @var Database|MockInterface */
  14. protected $connector;
  15. /** @var Db|MockInterface */
  16. protected $db;
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. $this->db = m::mock(Db::class);
  21. $this->connector = new Database($this->db, 'table', 'default');
  22. }
  23. public function testPushProperlyPushesJobOntoDatabase()
  24. {
  25. $this->db->shouldReceive('name')->with('table')->andReturn($query = m::mock(stdClass::class));
  26. $query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) {
  27. $this->assertEquals('default', $array['queue']);
  28. $this->assertEquals(json_encode(['job' => 'foo', 'maxTries' => null, 'timeout' => null, 'data' => ['data']]), $array['payload']);
  29. $this->assertEquals(0, $array['attempts']);
  30. $this->assertNull($array['reserved_at']);
  31. $this->assertInternalType('int', $array['available_at']);
  32. });
  33. $this->connector->push('foo', ['data']);
  34. }
  35. public function testDelayedPushProperlyPushesJobOntoDatabase()
  36. {
  37. $this->db->shouldReceive('name')->with('table')->andReturn($query = m::mock(stdClass::class));
  38. $query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) {
  39. $this->assertEquals('default', $array['queue']);
  40. $this->assertEquals(json_encode(['job' => 'foo', 'maxTries' => null, 'timeout' => null, 'data' => ['data']]), $array['payload']);
  41. $this->assertEquals(0, $array['attempts']);
  42. $this->assertNull($array['reserved_at']);
  43. $this->assertInternalType('int', $array['available_at']);
  44. });
  45. $this->connector->later(10, 'foo', ['data']);
  46. }
  47. public function testFailureToCreatePayloadFromObject()
  48. {
  49. $this->expectException('InvalidArgumentException');
  50. $job = new stdClass;
  51. $job->invalid = "\xc3\x28";
  52. $queue = $this->getMockForAbstractClass(Connector::class);
  53. $class = new ReflectionClass(Connector::class);
  54. $createPayload = $class->getMethod('createPayload');
  55. $createPayload->setAccessible(true);
  56. $createPayload->invokeArgs($queue, [
  57. $job,
  58. 'queue-name',
  59. ]);
  60. }
  61. public function testFailureToCreatePayloadFromArray()
  62. {
  63. $this->expectException('InvalidArgumentException');
  64. $queue = $this->getMockForAbstractClass(Connector::class);
  65. $class = new ReflectionClass(Connector::class);
  66. $createPayload = $class->getMethod('createPayload');
  67. $createPayload->setAccessible(true);
  68. $createPayload->invokeArgs($queue, [
  69. ["\xc3\x28"],
  70. 'queue-name',
  71. ]);
  72. }
  73. public function testBulkBatchPushesOntoDatabase()
  74. {
  75. $this->db->shouldReceive('name')->with('table')->andReturn($query = m::mock(stdClass::class));
  76. Carbon::setTestNow(
  77. $now = Carbon::now()->addSeconds()
  78. );
  79. $query->shouldReceive('insertAll')->once()->andReturnUsing(function ($records) use ($now) {
  80. $this->assertEquals([
  81. [
  82. 'queue' => 'queue',
  83. 'payload' => json_encode(['job' => 'foo', 'maxTries' => null, 'timeout' => null, 'data' => ['data']]),
  84. 'attempts' => 0,
  85. 'reserved_at' => null,
  86. 'available_at' => $now->getTimestamp(),
  87. 'created_at' => $now->getTimestamp(),
  88. ], [
  89. 'queue' => 'queue',
  90. 'payload' => json_encode(['job' => 'bar', 'maxTries' => null, 'timeout' => null, 'data' => ['data']]),
  91. 'attempts' => 0,
  92. 'reserved_at' => null,
  93. 'available_at' => $now->getTimestamp(),
  94. 'created_at' => $now->getTimestamp(),
  95. ],
  96. ], $records);
  97. });
  98. $this->connector->bulk(['foo', 'bar'], ['data'], 'queue');
  99. }
  100. }