AddressTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\Order;
  7. /**
  8. * Class PaymentTest
  9. *
  10. * @package Magento\Sales\Model\Order
  11. */
  12. class AddressTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Sales\Model\Order\Address
  16. */
  17. protected $address;
  18. /**
  19. * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $orderMock;
  22. /**
  23. * @var \Magento\Directory\Model\RegionFactory|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $regionFactoryMock;
  26. /**
  27. * @var \Magento\Directory\Model\Region|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $regionMock;
  30. protected function setUp()
  31. {
  32. $this->orderMock = $this->createMock(\Magento\Sales\Model\Order::class);
  33. $this->orderMock = $this->createMock(\Magento\Sales\Model\Order::class);
  34. $this->regionFactoryMock = $this->createMock(\Magento\Directory\Model\RegionFactory::class);
  35. $this->regionMock = $this->createPartialMock(
  36. \Magento\Directory\Model\Region::class,
  37. ['load', 'getCountryId', 'getCode']
  38. );
  39. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  40. $this->address = $objectManager->getObject(
  41. \Magento\Sales\Model\Order\Address::class,
  42. [
  43. 'regionFactory' => $this->regionFactoryMock
  44. ]
  45. );
  46. }
  47. public function testSetOrder()
  48. {
  49. $this->assertEquals($this->address, $this->address->setOrder($this->orderMock));
  50. }
  51. public function testGetRegionCodeRegionIsSet()
  52. {
  53. $regionId = 1;
  54. $this->address->setData('region', 'region');
  55. $this->address->setData('region_id', $regionId);
  56. $this->address->setData('country_id', 2);
  57. $this->regionMock->expects(static::once())
  58. ->method('load')
  59. ->with($regionId)
  60. ->willReturnSelf();
  61. $this->regionMock->expects(static::once())
  62. ->method('getCountryId')
  63. ->willReturn(1);
  64. $this->regionFactoryMock->expects(static::once())
  65. ->method('create')
  66. ->willReturn($this->regionMock);
  67. $this->assertEquals('region', $this->address->getRegionCode());
  68. }
  69. /**
  70. * @return array
  71. */
  72. public function regionProvider()
  73. {
  74. return [ [1, null], [null, 1]];
  75. }
  76. /**
  77. * @dataProvider regionProvider
  78. */
  79. public function testGetRegionCodeRegion($region, $regionId)
  80. {
  81. $this->address->setData('region', $region);
  82. $this->address->setData('region_id', $regionId);
  83. $this->address->setData('country_id', 1);
  84. $this->regionFactoryMock->expects($this->once())
  85. ->method('create')
  86. ->willReturn($this->regionMock);
  87. $this->regionMock->expects($this->once())
  88. ->method('load')
  89. ->with(1)
  90. ->willReturn($this->regionMock);
  91. $this->regionMock->expects($this->once())
  92. ->method('getCountryId')
  93. ->willReturn(1);
  94. $this->regionMock->expects($this->once())
  95. ->method('getCode')
  96. ->willReturn('region');
  97. $this->assertEquals('region', $this->address->getRegionCode());
  98. }
  99. public function testGetRegionCodeRegionFailure()
  100. {
  101. $this->address->setData('region', 1);
  102. $this->address->setData('region_id', 1);
  103. $this->address->setData('country_id', 1);
  104. $this->regionFactoryMock->expects($this->once())
  105. ->method('create')
  106. ->willReturn($this->regionMock);
  107. $this->regionMock->expects($this->once())
  108. ->method('load')
  109. ->with(1)
  110. ->willReturn($this->regionMock);
  111. $this->regionMock->expects($this->once())
  112. ->method('getCountryId')
  113. ->willReturn(2);
  114. $this->regionMock->expects($this->never())
  115. ->method('getCode');
  116. $this->assertEquals(null, $this->address->getRegionCode());
  117. }
  118. public function testGetName()
  119. {
  120. $this->address->setData('suffix', 'suffix');
  121. $this->address->setData('prefix', 'prefix');
  122. $this->address->setData('firstname', 'firstname');
  123. $this->address->setData('middlename', 'middlename');
  124. $this->address->setData('lastname', 'lastname');
  125. $this->assertEquals('prefix firstname middlename lastname suffix', $this->address->getName());
  126. }
  127. }