AddressRegistryTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Unit test for converter \Magento\Customer\Model\AddressRegistry
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Test\Unit\Model;
  9. class AddressRegistryTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Customer\Model\AddressRegistry
  13. */
  14. private $unit;
  15. /**
  16. * @var \Magento\Customer\Model\AddressFactory|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $addressFactory;
  19. protected function setUp()
  20. {
  21. $this->addressFactory = $this->getMockBuilder(\Magento\Customer\Model\AddressFactory::class)
  22. ->disableOriginalConstructor()
  23. ->setMethods(['create'])
  24. ->getMock();
  25. $this->unit = new \Magento\Customer\Model\AddressRegistry($this->addressFactory);
  26. }
  27. public function testRetrieve()
  28. {
  29. $addressId = 1;
  30. $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
  31. ->disableOriginalConstructor()
  32. ->setMethods(['load', 'getId', '__wakeup'])
  33. ->getMock();
  34. $address->expects($this->once())
  35. ->method('load')
  36. ->with($addressId)
  37. ->will($this->returnValue($address));
  38. $address->expects($this->once())
  39. ->method('getId')
  40. ->will($this->returnValue($addressId));
  41. $this->addressFactory->expects($this->once())
  42. ->method('create')
  43. ->will($this->returnValue($address));
  44. $actual = $this->unit->retrieve($addressId);
  45. $this->assertEquals($address, $actual);
  46. $actualCached = $this->unit->retrieve($addressId);
  47. $this->assertEquals($address, $actualCached);
  48. }
  49. /**
  50. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  51. */
  52. public function testRetrieveException()
  53. {
  54. $addressId = 1;
  55. $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
  56. ->setMethods(['load', 'getId', '__wakeup'])
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $address->expects($this->once())
  60. ->method('load')
  61. ->with($addressId)
  62. ->will($this->returnValue($address));
  63. $address->expects($this->once())
  64. ->method('getId')
  65. ->will($this->returnValue(null));
  66. $this->addressFactory->expects($this->once())
  67. ->method('create')
  68. ->will($this->returnValue($address));
  69. $this->unit->retrieve($addressId);
  70. }
  71. public function testRemove()
  72. {
  73. $addressId = 1;
  74. $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
  75. ->disableOriginalConstructor()
  76. ->setMethods(['load', 'getId', '__wakeup'])
  77. ->getMock();
  78. $address->expects($this->exactly(2))
  79. ->method('load')
  80. ->with($addressId)
  81. ->will($this->returnValue($address));
  82. $address->expects($this->exactly(2))
  83. ->method('getId')
  84. ->will($this->returnValue($addressId));
  85. $this->addressFactory->expects($this->exactly(2))
  86. ->method('create')
  87. ->will($this->returnValue($address));
  88. $actual = $this->unit->retrieve($addressId);
  89. $this->assertEquals($address, $actual);
  90. $this->unit->remove($addressId);
  91. $actual = $this->unit->retrieve($addressId);
  92. $this->assertEquals($address, $actual);
  93. }
  94. }