Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
QuoteAddressValidatorTest.php
Go to the documentation of this file.
1 <?php
8 
9 class QuoteAddressValidatorTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $model;
15 
19  protected $objectManager;
20 
25 
30 
34  protected $quoteAddressMock;
35 
40 
41  protected function setUp()
42  {
43  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
44 
45  $this->addressRepositoryMock = $this->createMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
46  $this->quoteAddressMock = $this->createPartialMock(
47  \Magento\Quote\Model\Quote\Address::class,
48  ['getCustomerId', 'load', 'getId', '__wakeup']
49  );
50  $this->customerRepositoryMock = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
51  $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
52  $this->model = $this->objectManager->getObject(
53  \Magento\Quote\Model\QuoteAddressValidator::class,
54  [
55  'addressRepository' => $this->addressRepositoryMock,
56  'customerRepository' => $this->customerRepositoryMock,
57  'customerSession' => $this->customerSessionMock
58  ]
59  );
60  }
61 
66  public function testValidateInvalidCustomer()
67  {
68  $customerId = 100;
69  $address = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
70  $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
71 
72  $address->expects($this->atLeastOnce())->method('getCustomerId')->willReturn($customerId);
73  $this->customerRepositoryMock->expects($this->once())->method('getById')->with($customerId)
74  ->willReturn($customerMock);
75  $this->model->validate($address);
76  }
77 
82  public function testValidateInvalidAddress()
83  {
84  $address = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
85  $this->customerRepositoryMock->expects($this->never())->method('getById');
86  $address->expects($this->atLeastOnce())->method('getCustomerAddressId')->willReturn(101);
87  $address->expects($this->once())->method('getId')->willReturn(101);
88 
89  $this->addressRepositoryMock->expects($this->once())->method('getById')
90  ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException());
91 
92  $this->model->validate($address);
93  }
94 
98  public function testValidateNewAddress()
99  {
100  $this->customerRepositoryMock->expects($this->never())->method('getById');
101  $this->addressRepositoryMock->expects($this->never())->method('getById');
102  $address = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
103  $this->assertTrue($this->model->validate($address));
104  }
105 
107  {
108  $addressCustomer = 100;
109  $customerAddressId = 42;
110 
111  $address = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
112  $address->expects($this->atLeastOnce())->method('getCustomerId')->willReturn($addressCustomer);
113  $address->expects($this->atLeastOnce())->method('getCustomerAddressId')->willReturn($customerAddressId);
114  $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
115  $customerAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
116 
117  $this->customerRepositoryMock->expects($this->exactly(2))->method('getById')->willReturn($customerMock);
118  $customerMock->expects($this->once())->method('getId')->willReturn($addressCustomer);
119 
120  $this->addressRepositoryMock->expects($this->once())->method('getById')->willReturn($this->quoteAddressMock);
121  $this->quoteAddressMock->expects($this->any())->method('getCustomerId')->willReturn($addressCustomer);
122 
123  $customerMock->expects($this->once())->method('getAddresses')->willReturn([$customerAddress]);
124  $customerAddress->expects($this->once())->method('getId')->willReturn(42);
125 
126  $this->assertTrue($this->model->validate($address));
127  }
128 }
$address
Definition: customer.php:38
$customerAddress