UpdateTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\EntityManager\Test\Unit\Operation;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\DataObject;
  9. use Magento\Framework\DB\Adapter\AdapterInterface;
  10. use Magento\Framework\DB\Adapter\DuplicateException;
  11. use Magento\Framework\EntityManager\EntityMetadataInterface;
  12. use Magento\Framework\EntityManager\MetadataPool;
  13. use Magento\Framework\EntityManager\Operation\Update;
  14. use Magento\Framework\EntityManager\Operation\Update\UpdateMain;
  15. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  16. class UpdateTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var MetadataPool|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $metadataPool;
  22. /**
  23. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $resourceConnection;
  26. /**
  27. * @var UpdateMain|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $updateMain;
  30. /**
  31. * @var Update
  32. */
  33. private $update;
  34. public function setUp()
  35. {
  36. $this->metadataPool = $this->getMockBuilder(MetadataPool::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->resourceConnection = $this->getMockBuilder(ResourceConnection::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->updateMain = $this->getMockBuilder(UpdateMain::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->update = (new ObjectManager($this))->getObject(Update::class, [
  46. 'metadataPool' => $this->metadataPool,
  47. 'resourceConnection' => $this->resourceConnection,
  48. 'updateMain' => $this->updateMain,
  49. ]);
  50. }
  51. /**
  52. * @expectedException \Magento\Framework\Exception\AlreadyExistsException
  53. */
  54. public function testDuplicateExceptionProcessingOnExecute()
  55. {
  56. $metadata = $this->createMock(EntityMetadataInterface::class);
  57. $this->metadataPool->expects($this->any())->method('getMetadata')->willReturn($metadata);
  58. $connection = $this->createMock(AdapterInterface::class);
  59. $connection->expects($this->once())->method('rollback');
  60. $this->resourceConnection->expects($this->any())->method('getConnectionByName')->willReturn($connection);
  61. $this->updateMain->expects($this->once())->method('execute')->willThrowException(new DuplicateException());
  62. $entity = $this->getMockBuilder(DataObject::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->update->execute($entity);
  66. }
  67. }