AttributeTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model\ResourceModel;
  7. /**
  8. * Class AttributeTest
  9. */
  10. class AttributeTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Sales\Model\ResourceModel\Attribute|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $attribute;
  16. /**
  17. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $appResourceMock;
  20. /**
  21. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $eventManagerMock;
  24. /**
  25. * @var \Magento\Sales\Model\AbstractModel|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $modelMock;
  28. /**
  29. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $connectionMock;
  32. protected function setUp()
  33. {
  34. $this->appResourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  35. $this->eventManagerMock = $this->getMockForAbstractClass(
  36. \Magento\Framework\Event\ManagerInterface::class,
  37. [],
  38. '',
  39. false,
  40. false,
  41. true,
  42. []
  43. );
  44. $this->modelMock = $this->getMockForAbstractClass(
  45. \Magento\Sales\Model\AbstractModel::class,
  46. [],
  47. '',
  48. false,
  49. false,
  50. true,
  51. ['__wakeup', 'getId', 'getEventPrefix', 'getEventObject']
  52. );
  53. $this->connectionMock = $this->createPartialMock(
  54. \Magento\Framework\DB\Adapter\Pdo\Mysql::class,
  55. ['describeTable', 'insert', 'lastInsertId', 'beginTransaction', 'rollback', 'commit']
  56. );
  57. $this->connectionMock->expects($this->any())
  58. ->method('describeTable')
  59. ->will($this->returnValue([]));
  60. $this->connectionMock->expects($this->any())
  61. ->method('insert');
  62. $this->connectionMock->expects($this->any())
  63. ->method('lastInsertId');
  64. $this->attribute = new \Magento\Sales\Model\ResourceModel\Attribute(
  65. $this->appResourceMock,
  66. $this->eventManagerMock
  67. );
  68. }
  69. /**
  70. * @throws \Exception
  71. */
  72. public function testSave()
  73. {
  74. $this->appResourceMock->expects($this->once())
  75. ->method('getConnection')
  76. ->will($this->returnValue($this->connectionMock));
  77. $this->modelMock->expects($this->any())
  78. ->method('getEventPrefix')
  79. ->will($this->returnValue('event_prefix'));
  80. $this->modelMock->expects($this->any())
  81. ->method('getEventObject')
  82. ->will($this->returnValue('event_object'));
  83. $this->eventManagerMock->expects($this->at(0))
  84. ->method('dispatch')
  85. ->with('event_prefix_save_attribute_before', [
  86. 'event_object' => $this->attribute,
  87. 'object' => $this->modelMock,
  88. 'attribute' => ['attribute']
  89. ]);
  90. $this->eventManagerMock->expects($this->at(1))
  91. ->method('dispatch')
  92. ->with('event_prefix_save_attribute_after', [
  93. 'event_object' => $this->attribute,
  94. 'object' => $this->modelMock,
  95. 'attribute' => ['attribute']
  96. ]);
  97. $this->connectionMock->expects($this->once())
  98. ->method('beginTransaction');
  99. $this->connectionMock->expects($this->once())
  100. ->method('commit');
  101. $this->assertEquals($this->attribute, $this->attribute->saveAttribute($this->modelMock, 'attribute'));
  102. }
  103. /**
  104. * @expectedException \Exception
  105. * @expectedExceptionMessage Expected Exception
  106. * @throws \Exception
  107. */
  108. public function testSaveFailed()
  109. {
  110. $this->modelMock->expects($this->any())
  111. ->method('getEventPrefix')
  112. ->will($this->returnValue('event_prefix'));
  113. $this->modelMock->expects($this->any())
  114. ->method('getEventObject')
  115. ->will($this->returnValue('event_object'));
  116. $this->appResourceMock->expects($this->once())
  117. ->method('getConnection')
  118. ->will($this->returnValue($this->connectionMock));
  119. $exception = new \Exception('Expected Exception');
  120. $this->modelMock->expects($this->any())
  121. ->method('getId')
  122. ->will($this->throwException($exception));
  123. $this->connectionMock->expects($this->once())
  124. ->method('beginTransaction');
  125. $this->connectionMock->expects($this->once())
  126. ->method('rollback');
  127. $this->eventManagerMock->expects($this->once())
  128. ->method('dispatch')
  129. ->with(
  130. 'event_prefix_save_attribute_before',
  131. [
  132. 'event_object' => $this->attribute,
  133. 'object' => $this->modelMock,
  134. 'attribute' => ['attribute']
  135. ]
  136. );
  137. $this->attribute->saveAttribute($this->modelMock, 'attribute');
  138. }
  139. }