OperationManagementTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AsynchronousOperations\Test\Unit\Model;
  7. /**
  8. * Class OperationManagementTest
  9. */
  10. class OperationManagementTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\AsynchronousOperations\Model\OperationManagement
  14. */
  15. private $model;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $entityManagerMock;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $operationFactoryMock;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $operationMock;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $loggerMock;
  32. protected function setUp()
  33. {
  34. $this->entityManagerMock = $this->createMock(\Magento\Framework\EntityManager\EntityManager::class);
  35. $this->metadataPoolMock = $this->createMock(\Magento\Framework\EntityManager\MetadataPool::class);
  36. $this->operationFactoryMock = $this->createPartialMock(
  37. \Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory::class,
  38. ['create']
  39. );
  40. $this->operationMock =
  41. $this->createMock(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class);
  42. $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  43. $this->model = new \Magento\AsynchronousOperations\Model\OperationManagement(
  44. $this->entityManagerMock,
  45. $this->operationFactoryMock,
  46. $this->loggerMock
  47. );
  48. }
  49. public function testChangeOperationStatus()
  50. {
  51. $operationId = 1;
  52. $status = 1;
  53. $message = 'Message';
  54. $data = 'data';
  55. $errorCode = 101;
  56. $this->operationFactoryMock->expects($this->once())->method('create')->willReturn($this->operationMock);
  57. $this->entityManagerMock->expects($this->once())->method('load')->with($this->operationMock, $operationId);
  58. $this->operationMock->expects($this->once())->method('setStatus')->with($status)->willReturnSelf();
  59. $this->operationMock->expects($this->once())->method('setResultMessage')->with($message)->willReturnSelf();
  60. $this->operationMock->expects($this->once())->method('setSerializedData')->with($data)->willReturnSelf();
  61. $this->operationMock->expects($this->once())->method('setErrorCode')->with($errorCode)->willReturnSelf();
  62. $this->entityManagerMock->expects($this->once())->method('save')->with($this->operationMock);
  63. $this->assertTrue($this->model->changeOperationStatus($operationId, $status, $errorCode, $message, $data));
  64. }
  65. public function testChangeOperationStatusIfExceptionWasThrown()
  66. {
  67. $operationId = 1;
  68. $status = 1;
  69. $message = 'Message';
  70. $data = 'data';
  71. $errorCode = 101;
  72. $this->operationFactoryMock->expects($this->once())->method('create')->willReturn($this->operationMock);
  73. $this->entityManagerMock->expects($this->once())->method('load')->with($this->operationMock, $operationId);
  74. $this->operationMock->expects($this->once())->method('setStatus')->with($status)->willReturnSelf();
  75. $this->operationMock->expects($this->once())->method('setResultMessage')->with($message)->willReturnSelf();
  76. $this->operationMock->expects($this->once())->method('setSerializedData')->with($data)->willReturnSelf();
  77. $this->operationMock->expects($this->once())->method('setErrorCode')->with($errorCode)->willReturnSelf();
  78. $this->entityManagerMock->expects($this->once())->method('save')->willThrowException(new \Exception());
  79. $this->loggerMock->expects($this->once())->method('critical');
  80. $this->assertFalse($this->model->changeOperationStatus($operationId, $status, $errorCode, $message, $data));
  81. }
  82. }