CreateTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\ResourceModel\Operation;
  7. /**
  8. * Unit test for Create operation.
  9. */
  10. class CreateTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\EntityManager\MetadataPool|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $metadataPool;
  16. /**
  17. * @var \Magento\Framework\EntityManager\TypeResolver|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $typeResolver;
  20. /**
  21. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $resourceConnection;
  24. /**
  25. * @var \Magento\AsynchronousOperations\Model\ResourceModel\Operation\Create
  26. */
  27. private $create;
  28. /**
  29. * Set up.
  30. *
  31. * @return void
  32. */
  33. protected function setUp()
  34. {
  35. $this->metadataPool = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
  36. ->disableOriginalConstructor()->getMock();
  37. $this->typeResolver = $this->getMockBuilder(\Magento\Framework\EntityManager\TypeResolver::class)
  38. ->disableOriginalConstructor()->getMock();
  39. $this->resourceConnection = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  40. ->disableOriginalConstructor()->getMock();
  41. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  42. $this->create = $objectManager->getObject(
  43. \Magento\AsynchronousOperations\Model\ResourceModel\Operation\Create::class,
  44. [
  45. 'metadataPool' => $this->metadataPool,
  46. 'typeResolver' => $this->typeResolver,
  47. 'resourceConnection' => $this->resourceConnection,
  48. ]
  49. );
  50. }
  51. /**
  52. * Test for execute method.
  53. *
  54. * @return void
  55. */
  56. public function testExecute()
  57. {
  58. $connectionName = 'default';
  59. $operationData = ['key1' => 'value1'];
  60. $operationTable = 'magento_operation';
  61. $operationList = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationListInterface::class)
  62. ->disableOriginalConstructor()->getMock();
  63. $this->typeResolver->expects($this->once())->method('resolve')->with($operationList)
  64. ->willReturn(\Magento\AsynchronousOperations\Api\Data\OperationListInterface::class);
  65. $metadata = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
  66. ->disableOriginalConstructor()->getMock();
  67. $this->metadataPool->expects($this->once())->method('getMetadata')
  68. ->with(\Magento\AsynchronousOperations\Api\Data\OperationListInterface::class)->willReturn($metadata);
  69. $metadata->expects($this->once())->method('getEntityConnectionName')->willReturn($connectionName);
  70. $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  71. ->disableOriginalConstructor()->getMock();
  72. $this->resourceConnection->expects($this->once())
  73. ->method('getConnection')->with($connectionName)->willReturn($connection);
  74. $connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
  75. $operation = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class)
  76. ->setMethods(['getData'])
  77. ->disableOriginalConstructor()
  78. ->getMockForAbstractClass();
  79. $operationList->expects($this->once())->method('getItems')->willReturn([$operation]);
  80. $operation->expects($this->once())->method('getData')->willReturn($operationData);
  81. $metadata->expects($this->once())->method('getEntityTable')->willReturn($operationTable);
  82. $connection->expects($this->once())->method('insertOnDuplicate')
  83. ->with($operationTable, [$operationData], ['status', 'error_code', 'result_message'])->willReturn(1);
  84. $connection->expects($this->once())->method('commit')->willReturnSelf();
  85. $this->assertEquals($operationList, $this->create->execute($operationList));
  86. }
  87. /**
  88. * Test for execute method with exception.
  89. *
  90. * @return void
  91. * @expectedException \Exception
  92. */
  93. public function testExecuteWithException()
  94. {
  95. $connectionName = 'default';
  96. $operationData = ['key1' => 'value1'];
  97. $operationTable = 'magento_operation';
  98. $operationList = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationListInterface::class)
  99. ->disableOriginalConstructor()->getMock();
  100. $this->typeResolver->expects($this->once())->method('resolve')->with($operationList)
  101. ->willReturn(\Magento\AsynchronousOperations\Api\Data\OperationListInterface::class);
  102. $metadata = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
  103. ->disableOriginalConstructor()->getMock();
  104. $this->metadataPool->expects($this->once())->method('getMetadata')
  105. ->with(\Magento\AsynchronousOperations\Api\Data\OperationListInterface::class)->willReturn($metadata);
  106. $metadata->expects($this->once())->method('getEntityConnectionName')->willReturn($connectionName);
  107. $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  108. ->disableOriginalConstructor()->getMock();
  109. $this->resourceConnection->expects($this->once())
  110. ->method('getConnection')->with($connectionName)->willReturn($connection);
  111. $connection->expects($this->once())->method('beginTransaction')->willReturnSelf();
  112. $operation = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class)
  113. ->setMethods(['getData'])
  114. ->disableOriginalConstructor()
  115. ->getMockForAbstractClass();
  116. $operationList->expects($this->once())->method('getItems')->willReturn([$operation]);
  117. $operation->expects($this->once())->method('getData')->willReturn($operationData);
  118. $metadata->expects($this->once())->method('getEntityTable')->willReturn($operationTable);
  119. $connection->expects($this->once())->method('insertOnDuplicate')
  120. ->with($operationTable, [$operationData], ['status', 'error_code', 'result_message'])
  121. ->willThrowException(new \Exception());
  122. $connection->expects($this->once())->method('rollBack')->willReturnSelf();
  123. $this->create->execute($operationList);
  124. }
  125. }