Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddressDataBuilderTest.php
Go to the documentation of this file.
1 <?php
7 
13 use PHPUnit_Framework_MockObject_MockObject as MockObject;
14 
18 class AddressDataBuilderTest extends \PHPUnit\Framework\TestCase
19 {
23  private $paymentDOMock;
24 
28  private $orderMock;
29 
33  private $builder;
34 
38  private $subjectReaderMock;
39 
40  protected function setUp()
41  {
42  $this->paymentDOMock = $this->createMock(PaymentDataObjectInterface::class);
43  $this->orderMock = $this->createMock(OrderAdapterInterface::class);
44  $this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
45  ->disableOriginalConstructor()
46  ->getMock();
47 
48  $this->builder = new AddressDataBuilder($this->subjectReaderMock);
49  }
50 
55  {
56  $buildSubject = [
57  'payment' => null,
58  ];
59 
60  $this->subjectReaderMock->expects(self::once())
61  ->method('readPayment')
62  ->with($buildSubject)
63  ->willThrowException(new \InvalidArgumentException());
64 
65  $this->builder->build($buildSubject);
66  }
67 
68  public function testBuildNoAddresses()
69  {
70  $this->paymentDOMock->expects(static::once())
71  ->method('getOrder')
72  ->willReturn($this->orderMock);
73 
74  $this->orderMock->expects(static::once())
75  ->method('getShippingAddress')
76  ->willReturn(null);
77  $this->orderMock->expects(static::once())
78  ->method('getBillingAddress')
79  ->willReturn(null);
80 
81  $buildSubject = [
82  'payment' => $this->paymentDOMock,
83  ];
84 
85  $this->subjectReaderMock->expects(self::once())
86  ->method('readPayment')
87  ->with($buildSubject)
88  ->willReturn($this->paymentDOMock);
89 
90  static::assertEquals([], $this->builder->build($buildSubject));
91  }
92 
99  public function testBuild($addressData, $expectedResult)
100  {
101  $addressMock = $this->getAddressMock($addressData);
102 
103  $this->paymentDOMock->expects(static::once())
104  ->method('getOrder')
105  ->willReturn($this->orderMock);
106 
107  $this->orderMock->expects(static::once())
108  ->method('getShippingAddress')
109  ->willReturn($addressMock);
110  $this->orderMock->expects(static::once())
111  ->method('getBillingAddress')
112  ->willReturn($addressMock);
113 
114  $buildSubject = [
115  'payment' => $this->paymentDOMock,
116  ];
117 
118  $this->subjectReaderMock->expects(self::once())
119  ->method('readPayment')
120  ->with($buildSubject)
121  ->willReturn($this->paymentDOMock);
122 
123  self::assertEquals($expectedResult, $this->builder->build($buildSubject));
124  }
125 
129  public function dataProviderBuild()
130  {
131  return [
132  [
133  [
134  'first_name' => 'John',
135  'last_name' => 'Smith',
136  'company' => 'Magento',
137  'street_1' => 'street1',
138  'street_2' => 'street2',
139  'city' => 'Chicago',
140  'region_code' => 'IL',
141  'country_id' => 'US',
142  'post_code' => '00000',
143  ],
144  [
148  AddressDataBuilder::COMPANY => 'Magento',
151  AddressDataBuilder::LOCALITY => 'Chicago',
155 
156  ],
160  AddressDataBuilder::COMPANY => 'Magento',
163  AddressDataBuilder::LOCALITY => 'Chicago',
167  ],
168  ],
169  ],
170  ];
171  }
172 
177  private function getAddressMock($addressData)
178  {
179  $addressMock = $this->createMock(AddressAdapterInterface::class);
180 
181  $addressMock->expects(self::exactly(2))
182  ->method('getFirstname')
183  ->willReturn($addressData['first_name']);
184  $addressMock->expects(self::exactly(2))
185  ->method('getLastname')
186  ->willReturn($addressData['last_name']);
187  $addressMock->expects(self::exactly(2))
188  ->method('getCompany')
189  ->willReturn($addressData['company']);
190  $addressMock->expects(self::exactly(2))
191  ->method('getStreetLine1')
192  ->willReturn($addressData['street_1']);
193  $addressMock->expects(self::exactly(2))
194  ->method('getStreetLine2')
195  ->willReturn($addressData['street_2']);
196  $addressMock->expects(self::exactly(2))
197  ->method('getCity')
198  ->willReturn($addressData['city']);
199  $addressMock->expects(self::exactly(2))
200  ->method('getRegionCode')
201  ->willReturn($addressData['region_code']);
202  $addressMock->expects(self::exactly(2))
203  ->method('getPostcode')
204  ->willReturn($addressData['post_code']);
205  $addressMock->expects(self::exactly(2))
206  ->method('getCountryId')
207  ->willReturn($addressData['country_id']);
208 
209  return $addressMock;
210  }
211 }
$addressData
Definition: order.php:19