Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddressSnapshotTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class AddressSnapshotTest extends \PHPUnit\Framework\TestCase
11 {
15  private $model;
16 
20  private $metadataMock;
21 
22  protected function setUp()
23  {
24  $this->metadataMock = $this->getMockBuilder(
25  \Magento\Framework\Model\ResourceModel\Db\VersionControl\Metadata::class
26  )->disableOriginalConstructor()->getMock();
27 
28  $this->model = new AddressSnapshot(
29  $this->metadataMock
30  );
31  }
32 
40  public function testIsModified(
41  $isCustomerSaveTransaction,
42  $isDefaultBilling,
43  $isDefaultShipping,
44  $expected
45  ) {
46  $entityId = 1;
47 
48  $dataObjectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
49  ->disableOriginalConstructor()
50  ->setMethods([
51  'getId',
52  'getData',
53  'getDataByKey',
54  'getIsDefaultBilling',
55  'getIsDefaultShipping',
56  'getIsCustomerSaveTransaction',
57  ])
58  ->getMock();
59 
60  $dataObjectMock->expects($this->any())
61  ->method('getId')
62  ->willReturn($entityId);
63  $dataObjectMock->expects($this->once())
64  ->method('getData')
65  ->willReturn(['is_billing_address' => 1]);
66  $dataObjectMock->expects($this->once())
67  ->method('getDataByKey')
68  ->with('is_billing_address')
69  ->willReturn(1);
70  $dataObjectMock->expects($this->once())
71  ->method('getIsCustomerSaveTransaction')
72  ->willReturn($isCustomerSaveTransaction);
73  $dataObjectMock->expects($this->any())
74  ->method('getIsDefaultBilling')
75  ->willReturn($isDefaultBilling);
76  $dataObjectMock->expects($this->any())
77  ->method('getIsDefaultShipping')
78  ->willReturn($isDefaultShipping);
79 
80  $this->metadataMock->expects($this->once())
81  ->method('getFields')
82  ->with($dataObjectMock)
83  ->willReturn(['is_billing_address' => null]);
84 
85  $this->model->registerSnapshot($dataObjectMock);
86 
87  $this->assertEquals($expected, $this->model->isModified($dataObjectMock));
88  }
89 
93  public function dataProviderIsModified()
94  {
95  return [
96  [false, 1, 1, true],
97  [true, 0, 0, false],
98  [false, 1, 0, true],
99  [false, 0, 1, true],
100  ];
101  }
102 
103  public function testIsModifiedBypass()
104  {
105  $dataObjectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
106  ->disableOriginalConstructor()
107  ->setMethods([
108  'getId',
109  'getData',
110  ])
111  ->getMock();
112 
113  $dataObjectMock->expects($this->any())
114  ->method('getId')
115  ->willReturn(null);
116  $dataObjectMock->expects($this->once())
117  ->method('getData')
118  ->willReturn(['is_billing_address' => 1]);
119 
120  $this->metadataMock->expects($this->once())
121  ->method('getFields')
122  ->with($dataObjectMock)
123  ->willReturn(['is_billing_address' => null]);
124 
125  $this->model->registerSnapshot($dataObjectMock);
126 
127  $this->assertEquals(true, $this->model->isModified($dataObjectMock));
128  }
129 }
testIsModified( $isCustomerSaveTransaction, $isDefaultBilling, $isDefaultShipping, $expected)