QueueTestCaseAbstract.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue\UseCase;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. use Magento\Framework\MessageQueue\PublisherInterface;
  10. use Magento\TestFramework\MessageQueue\PublisherConsumerController;
  11. use Magento\TestFramework\MessageQueue\EnvironmentPreconditionException;
  12. use Magento\TestFramework\MessageQueue\PreconditionFailedException;
  13. /**
  14. * Base test case for message queue tests.
  15. */
  16. class QueueTestCaseAbstract extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var string[]
  20. */
  21. protected $consumers = [];
  22. /**
  23. * @var ObjectManagerInterface
  24. */
  25. protected $objectManager;
  26. /**
  27. * @var PublisherInterface
  28. */
  29. protected $publisher;
  30. /**
  31. * @var string
  32. */
  33. protected $logFilePath;
  34. /**
  35. * @var int|null
  36. */
  37. protected $maxMessages = null;
  38. /**
  39. * @var PublisherConsumerController
  40. */
  41. private $publisherConsumerController;
  42. protected function setUp()
  43. {
  44. $this->objectManager = Bootstrap::getObjectManager();
  45. $this->logFilePath = TESTS_TEMP_DIR . "/MessageQueueTestLog.txt";
  46. $this->publisherConsumerController = $this->objectManager->create(PublisherConsumerController::class, [
  47. 'consumers' => $this->consumers,
  48. 'logFilePath' => $this->logFilePath,
  49. 'maxMessages' => $this->maxMessages,
  50. 'appInitParams' => \Magento\TestFramework\Helper\Bootstrap::getInstance()->getAppInitParams()
  51. ]);
  52. try {
  53. $this->publisherConsumerController->initialize();
  54. } catch (EnvironmentPreconditionException $e) {
  55. $this->markTestSkipped($e->getMessage());
  56. } catch (PreconditionFailedException $e) {
  57. $this->fail(
  58. $e->getMessage()
  59. );
  60. }
  61. $this->publisher = $this->publisherConsumerController->getPublisher();
  62. }
  63. protected function tearDown()
  64. {
  65. $this->publisherConsumerController->stopConsumers();
  66. }
  67. /**
  68. * Wait for asynchronous handlers to log data to file.
  69. *
  70. * @param int $expectedLinesCount
  71. * @param string $logFilePath
  72. */
  73. protected function waitForAsynchronousResult($expectedLinesCount, $logFilePath)
  74. {
  75. try {
  76. //$expectedLinesCount, $logFilePath
  77. $this->publisherConsumerController->waitForAsynchronousResult([$this, 'checkLogsExists'], [
  78. $expectedLinesCount, $logFilePath
  79. ]);
  80. } catch (PreconditionFailedException $e) {
  81. $this->fail($e->getMessage());
  82. }
  83. }
  84. public function checkLogsExists($expectedLinesCount)
  85. {
  86. $actualCount = file_exists($this->logFilePath) ? count(file($this->logFilePath)) : 0;
  87. return $expectedLinesCount === $actualCount;
  88. }
  89. /**
  90. * Workaround for https://bugs.php.net/bug.php?id=72286
  91. */
  92. public static function tearDownAfterClass()
  93. {
  94. if (version_compare(phpversion(), '7') == -1) {
  95. $closeConnection = new \ReflectionMethod(\Magento\Amqp\Model\Config::class, 'closeConnection');
  96. $closeConnection->setAccessible(true);
  97. $config = Bootstrap::getObjectManager()->get(\Magento\Amqp\Model\Config::class);
  98. $closeConnection->invoke($config);
  99. }
  100. }
  101. }