PidConsumerManagerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\MessageQueue\Test\Unit\Model\Cron\ConsumersRunner;
  7. use Magento\MessageQueue\Model\Cron\ConsumersRunner\PidConsumerManager;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\Filesystem\Directory\WriteInterface;
  10. use Magento\Framework\Filesystem\Directory\ReadInterface;
  11. use Magento\Framework\Filesystem;
  12. use \PHPUnit_Framework_MockObject_MockObject as MockObject;
  13. class PidConsumerManagerTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var Filesystem|MockObject
  17. */
  18. private $filesystemMock;
  19. /**
  20. * @var PidConsumerManager
  21. */
  22. private $pidConsumerManager;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function setUp()
  27. {
  28. require_once __DIR__ . '/../../../_files/pid_consumer_functions_mocks.php';
  29. $this->filesystemMock = $this->getMockBuilder(Filesystem::class)
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $this->pidConsumerManager = new PidConsumerManager($this->filesystemMock);
  33. }
  34. /**
  35. * @param bool $fileExists
  36. * @param int|null $pid
  37. * @param bool $expectedResult
  38. * @dataProvider isRunDataProvider
  39. */
  40. public function testIsRun($fileExists, $pid, $expectedResult)
  41. {
  42. $pidFilePath = 'somepath/consumerName.pid';
  43. /** @var ReadInterface|MockObject $directoryMock */
  44. $directoryMock = $this->getMockBuilder(ReadInterface::class)
  45. ->getMockForAbstractClass();
  46. $directoryMock->expects($this->once())
  47. ->method('isExist')
  48. ->willReturn($fileExists);
  49. $directoryMock->expects($this->any())
  50. ->method('readFile')
  51. ->with($pidFilePath)
  52. ->willReturn($pid);
  53. $this->filesystemMock->expects($this->once())
  54. ->method('getDirectoryRead')
  55. ->with(DirectoryList::VAR_DIR)
  56. ->willReturn($directoryMock);
  57. $this->assertSame($expectedResult, $this->pidConsumerManager->isRun($pidFilePath));
  58. }
  59. /**
  60. * @return array
  61. */
  62. public function isRunDataProvider()
  63. {
  64. return [
  65. ['fileExists' => false, 'pid' => null, false],
  66. ['fileExists' => false, 'pid' => 11111, false],
  67. ['fileExists' => true, 'pid' => 11111, true],
  68. ['fileExists' => true, 'pid' => 77777, false],
  69. ];
  70. }
  71. public function testSavePid()
  72. {
  73. $pidFilePath = '/var/somePath/pidfile.pid';
  74. /** @var WriteInterface|MockObject $writeMock */
  75. $writeMock = $this->getMockBuilder(WriteInterface::class)
  76. ->getMockForAbstractClass();
  77. $this->filesystemMock->expects($this->once())
  78. ->method('getDirectoryWrite')
  79. ->with(DirectoryList::VAR_DIR)
  80. ->willReturn($writeMock);
  81. $writeMock->expects($this->once())
  82. ->method('writeFile')
  83. ->with(
  84. $pidFilePath,
  85. function_exists('posix_getpid') ? posix_getpid() : getmypid(),
  86. 'w'
  87. );
  88. $this->pidConsumerManager->savePid($pidFilePath);
  89. }
  90. }