AddressTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model;
  7. class AddressTest extends \PHPUnit\Framework\TestCase
  8. {
  9. const ORIG_CUSTOMER_ID = 1;
  10. const ORIG_PARENT_ID = 2;
  11. /**
  12. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  13. */
  14. protected $objectManager;
  15. /**
  16. * @var \Magento\Customer\Model\Address
  17. */
  18. protected $address;
  19. /**
  20. * @var \Magento\Customer\Model\Customer | \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $customer;
  23. /**
  24. * @var \Magento\Customer\Model\CustomerFactory | \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $customerFactory;
  27. /**
  28. * @var \Magento\Customer\Model\ResourceModel\Address | \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $resource;
  31. protected function setUp()
  32. {
  33. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  34. $this->customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->customer->expects($this->any())
  38. ->method('getId')
  39. ->will($this->returnValue(self::ORIG_CUSTOMER_ID));
  40. $this->customer->expects($this->any())
  41. ->method('load')
  42. ->will($this->returnSelf());
  43. $this->customerFactory = $this->getMockBuilder(\Magento\Customer\Model\CustomerFactory::class)
  44. ->disableOriginalConstructor()
  45. ->setMethods(['create'])
  46. ->getMock();
  47. $this->customerFactory->expects($this->any())
  48. ->method('create')
  49. ->will($this->returnValue($this->customer));
  50. $this->resource = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Address::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->address = $this->objectManager->getObject(
  54. \Magento\Customer\Model\Address::class,
  55. [
  56. 'customerFactory' => $this->customerFactory,
  57. 'resource' => $this->resource,
  58. ]
  59. );
  60. }
  61. public function testCustomerId()
  62. {
  63. $this->address->setParentId(self::ORIG_PARENT_ID);
  64. $this->assertEquals(self::ORIG_PARENT_ID, $this->address->getCustomerId());
  65. $this->address->setCustomerId(self::ORIG_CUSTOMER_ID);
  66. $this->assertEquals(self::ORIG_CUSTOMER_ID, $this->address->getCustomerId());
  67. }
  68. public function testCustomer()
  69. {
  70. $this->address->unsetData('customer_id');
  71. $this->assertFalse($this->address->getCustomer());
  72. $this->address->setCustomerId(self::ORIG_CUSTOMER_ID);
  73. $customer = $this->address->getCustomer();
  74. $this->assertEquals(self::ORIG_CUSTOMER_ID, $customer->getId());
  75. /** @var \Magento\Customer\Model\Customer $customer */
  76. $customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $customer->expects($this->any())
  80. ->method('getId')
  81. ->will($this->returnValue(self::ORIG_CUSTOMER_ID + 1));
  82. $this->address->setCustomer($customer);
  83. $this->assertEquals(self::ORIG_CUSTOMER_ID + 1, $this->address->getCustomerId());
  84. }
  85. public function testGetAttributes()
  86. {
  87. $resultValue = 'test';
  88. $this->resource->expects($this->any())
  89. ->method('loadAllAttributes')
  90. ->will($this->returnSelf());
  91. $this->resource->expects($this->any())
  92. ->method('getSortedAttributes')
  93. ->will($this->returnValue($resultValue));
  94. $this->assertEquals($resultValue, $this->address->getAttributes());
  95. }
  96. public function testRegionId()
  97. {
  98. $this->address->setRegionId(1);
  99. $this->assertEquals(1, $this->address->getRegionId());
  100. }
  101. public function testGetEntityTypeId()
  102. {
  103. $mockEntityType = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
  104. ->disableOriginalConstructor()
  105. ->getMock();
  106. $mockEntityType->expects($this->any())
  107. ->method('getId')
  108. ->will($this->returnValue(self::ORIG_CUSTOMER_ID));
  109. $this->resource->expects($this->any())
  110. ->method('getEntityType')
  111. ->will($this->returnValue($mockEntityType));
  112. $this->assertEquals(self::ORIG_CUSTOMER_ID, $this->address->getEntityTypeId());
  113. }
  114. }