AddressTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Order;
  7. /**
  8. * Class AddressTest
  9. */
  10. class AddressTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Sales\Model\ResourceModel\Order\Address
  14. */
  15. protected $addressResource;
  16. /**
  17. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $appResourceMock;
  20. /**
  21. * @var \Magento\Sales\Model\Order\Address|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $addressMock;
  24. /**
  25. * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $orderMock;
  28. /**
  29. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $connectionMock;
  32. /**
  33. * @var \Magento\Sales\Model\Order\Address\Validator|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $validatorMock;
  36. /**
  37. * @var \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $entitySnapshotMock;
  40. protected function setUp()
  41. {
  42. $this->addressMock = $this->createPartialMock(
  43. \Magento\Sales\Model\Order\Address::class,
  44. ['__wakeup', 'getParentId', 'hasDataChanges', 'beforeSave', 'afterSave', 'validateBeforeSave', 'getOrder']
  45. );
  46. $this->orderMock = $this->createPartialMock(\Magento\Sales\Model\Order::class, ['__wakeup', 'getId']);
  47. $this->appResourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  48. $this->connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
  49. $this->validatorMock = $this->createMock(\Magento\Sales\Model\Order\Address\Validator::class);
  50. $this->entitySnapshotMock = $this->createMock(
  51. \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot::class
  52. );
  53. $this->appResourceMock->expects($this->any())
  54. ->method('getConnection')
  55. ->will($this->returnValue($this->connectionMock));
  56. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  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->addressResource = $objectManager->getObject(
  65. \Magento\Sales\Model\ResourceModel\Order\Address::class,
  66. [
  67. 'resource' => $this->appResourceMock,
  68. 'validator' => $this->validatorMock,
  69. 'entitySnapshot' => $this->entitySnapshotMock
  70. ]
  71. );
  72. }
  73. /**
  74. * test _beforeSaveMethod via save()
  75. */
  76. public function testSave()
  77. {
  78. $this->validatorMock->expects($this->once())
  79. ->method('validate')
  80. ->with($this->equalTo($this->addressMock))
  81. ->will($this->returnValue([]));
  82. $this->entitySnapshotMock->expects($this->once())
  83. ->method('isModified')
  84. ->with($this->addressMock)
  85. ->willReturn(true);
  86. $this->addressMock->expects($this->once())
  87. ->method('getParentId')
  88. ->will($this->returnValue(1));
  89. $this->addressResource->save($this->addressMock);
  90. }
  91. /**
  92. * test _beforeSaveMethod via save() with failed validation
  93. *
  94. * @expectedException \Magento\Framework\Exception\LocalizedException
  95. * @expectedExceptionMessage We can't save the address:
  96. */
  97. public function testSaveValidationFailed()
  98. {
  99. $this->entitySnapshotMock->expects($this->once())
  100. ->method('isModified')
  101. ->with($this->addressMock)
  102. ->willReturn(true);
  103. $this->addressMock->expects($this->any())
  104. ->method('hasDataChanges')
  105. ->will($this->returnValue(true));
  106. $this->validatorMock->expects($this->once())
  107. ->method('validate')
  108. ->with($this->equalTo($this->addressMock))
  109. ->will($this->returnValue(['warning message']));
  110. $this->addressResource->save($this->addressMock);
  111. }
  112. }