QueueTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © 2013-2017 Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Update\Test\Unit;
  7. use Magento\Update\Queue;
  8. class QueueTest extends \PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Update\Queue\Reader
  12. */
  13. private $reader;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Update\Queue\Writer
  16. */
  17. private $writer;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Update\Queue\JobFactory
  20. */
  21. private $jobFactory;
  22. /**
  23. * @var Queue
  24. */
  25. private $queue;
  26. public function setUp()
  27. {
  28. $this->reader = $this->getMock('Magento\Update\Queue\Reader', [], [], '', false);
  29. $this->writer = $this->getMock('Magento\Update\Queue\Writer', [], [], '', false);
  30. $this->jobFactory = $this->getMock('Magento\Update\Queue\JobFactory', [], [], '', false);
  31. $this->queue = new Queue($this->reader, $this->writer, $this->jobFactory);
  32. }
  33. public function testPeek()
  34. {
  35. $this->reader->expects($this->once())
  36. ->method('read')
  37. ->willReturn('{"jobs": [{"name": "job A", "params" : []}, {"name": "job B", "params" : []}]}');
  38. $this->assertEquals(['name' => 'job A', 'params' => []], $this->queue->peek());
  39. }
  40. public function testPeekEmpty()
  41. {
  42. $this->reader->expects($this->once())
  43. ->method('read')
  44. ->willReturn('');
  45. $this->assertEquals([], $this->queue->peek());
  46. }
  47. /**
  48. * @expectedException \RuntimeException
  49. * @expectedExceptionMessage "params" field is missing for one or more jobs
  50. */
  51. public function testPeekException()
  52. {
  53. $this->reader->expects($this->once())
  54. ->method('read')
  55. ->willReturn('{"jobs": [{"name": "job A"}, {"name": "job B"}]}');
  56. $this->queue->peek();
  57. }
  58. /**
  59. * @expectedException \RuntimeException
  60. * @expectedExceptionMessage "jobs" field is missing or is not an array
  61. */
  62. public function testPeekExceptionNoJobsKey()
  63. {
  64. $this->reader->expects($this->once())
  65. ->method('read')
  66. ->willReturn('{"foo": "bar"}');
  67. $this->queue->peek();
  68. }
  69. public function testPopQueuedJob()
  70. {
  71. $this->reader->expects($this->once())
  72. ->method('read')
  73. ->willReturn('{"jobs": [{"name": "job A", "params" : []}, {"name": "job B", "params" : []}]}');
  74. $job = $this->getMockForAbstractClass('Magento\Update\Queue\AbstractJob', [], '', false);
  75. $this->jobFactory->expects($this->once())->method('create')->with('job A', [])->willReturn($job);
  76. $rawData = ['jobs' => [['name' => 'job B', 'params' => []]]];
  77. $this->writer->expects($this->once())->method('write')->with(json_encode($rawData, JSON_PRETTY_PRINT));
  78. $this->assertEquals($job, $this->queue->popQueuedJob());
  79. }
  80. public function testPopQueuedJobEmptyAfter()
  81. {
  82. $this->reader->expects($this->once())
  83. ->method('read')
  84. ->willReturn('{"jobs": [{"name": "job A", "params" : []}]}');
  85. $job = $this->getMockForAbstractClass('Magento\Update\Queue\AbstractJob', [], '', false);
  86. $this->jobFactory->expects($this->once())->method('create')->with('job A', [])->willReturn($job);
  87. $this->writer->expects($this->once())->method('write')->with('');
  88. $this->assertEquals($job, $this->queue->popQueuedJob());
  89. }
  90. /**
  91. * @expectedException \RuntimeException
  92. * @expectedExceptionMessage "params" field is missing for one or more jobs
  93. */
  94. public function testPopQueuedJobException()
  95. {
  96. $this->reader->expects($this->once())
  97. ->method('read')
  98. ->willReturn('{"jobs": [{"name": "job A"}, {"name": "job B"}]}');
  99. $this->writer->expects($this->never())->method('write');
  100. $this->queue->popQueuedJob();
  101. }
  102. /**
  103. * @expectedException \RuntimeException
  104. * @expectedExceptionMessage "jobs" field is missing or is not an array
  105. */
  106. public function testPopQueuedJobExceptionNoJobsKey()
  107. {
  108. $this->reader->expects($this->once())
  109. ->method('read')
  110. ->willReturn('{"foo": "bar"}');
  111. $this->writer->expects($this->never())->method('write');
  112. $this->queue->popQueuedJob();
  113. }
  114. public function testAddJobs()
  115. {
  116. $queue = ['jobs' => []];
  117. $this->reader->expects($this->at(0))->method('read')->willReturn('');
  118. $queue['jobs'][] = ['name' => 'job A', 'params' => []];
  119. $this->writer->expects($this->at(0))
  120. ->method('write')
  121. ->with(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
  122. $this->reader->expects($this->at(1))
  123. ->method('read')
  124. ->willReturn(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
  125. $queue['jobs'][] = ['name' => 'job B', 'params' => []];
  126. $this->writer->expects($this->at(1))
  127. ->method('write')
  128. ->with(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
  129. $this->queue->addJobs([['name' => 'job A', 'params' => []], ['name' => 'job B', 'params' => []]]);
  130. }
  131. /**
  132. * @expectedException \RuntimeException
  133. * @expectedExceptionMessage field is missing for one or more jobs
  134. */
  135. public function testAddJobsInvalidJobs()
  136. {
  137. $this->queue->addJobs([['no_name' => 'no job', 'no_params' => []]]);
  138. }
  139. }