Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RefundDataBuilderTest.php
Go to the documentation of this file.
1 <?php
7 
14 use PHPUnit_Framework_MockObject_MockObject as MockObject;
15 
19 class RefundDataBuilderTest extends \PHPUnit\Framework\TestCase
20 {
24  private $subjectReaderMock;
25 
29  private $paymentDOMock;
30 
34  private $paymentModelMock;
35 
39  private $dataBuilder;
40 
44  private $transactionId = 'xsd7n';
45 
46  public function setUp()
47  {
48  $this->paymentModelMock = $this->getMockBuilder(Payment::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51 
52  $this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
53  ->disableOriginalConstructor()
54  ->getMock();
55 
56  $this->dataBuilder = new RefundDataBuilder($this->subjectReaderMock);
57  }
58 
59  public function testBuild()
60  {
61  $this->initPaymentDOMock();
62  $buildSubject = ['payment' => $this->paymentDOMock, 'amount' => 12.358];
63 
64  $this->subjectReaderMock->expects(self::once())
65  ->method('readPayment')
66  ->with($buildSubject)
67  ->willReturn($this->paymentDOMock);
68  $this->paymentModelMock->expects(self::once())
69  ->method('getParentTransactionId')
70  ->willReturn($this->transactionId);
71  $this->subjectReaderMock->expects(self::once())
72  ->method('readAmount')
73  ->with($buildSubject)
74  ->willReturn($buildSubject['amount']);
75 
76  static::assertEquals(
77  [
78  'transaction_id' => $this->transactionId,
79  PaymentDataBuilder::AMOUNT => '12.36',
80  ],
81  $this->dataBuilder->build($buildSubject)
82  );
83  }
84 
85  public function testBuildNullAmount()
86  {
87  $this->initPaymentDOMock();
88  $buildSubject = ['payment' => $this->paymentDOMock];
89 
90  $this->subjectReaderMock->expects(self::once())
91  ->method('readPayment')
92  ->with($buildSubject)
93  ->willReturn($this->paymentDOMock);
94  $this->paymentModelMock->expects(self::once())
95  ->method('getParentTransactionId')
96  ->willReturn($this->transactionId);
97  $this->subjectReaderMock->expects(self::once())
98  ->method('readAmount')
99  ->with($buildSubject)
100  ->willThrowException(new \InvalidArgumentException());
101 
102  static::assertEquals(
103  [
104  'transaction_id' => $this->transactionId,
106  ],
107  $this->dataBuilder->build($buildSubject)
108  );
109  }
110 
112  {
113  $this->initPaymentDOMock();
114  $buildSubject = ['payment' => $this->paymentDOMock];
115  $legacyTxnId = 'xsd7n-' . TransactionInterface::TYPE_CAPTURE;
116 
117  $this->subjectReaderMock->expects(self::once())
118  ->method('readPayment')
119  ->with($buildSubject)
120  ->willReturn($this->paymentDOMock);
121  $this->paymentModelMock->expects(self::once())
122  ->method('getParentTransactionId')
123  ->willReturn($legacyTxnId);
124  $this->subjectReaderMock->expects(self::once())
125  ->method('readAmount')
126  ->with($buildSubject)
127  ->willThrowException(new \InvalidArgumentException());
128 
129  static::assertEquals(
130  [
131  'transaction_id' => $this->transactionId,
133  ],
134  $this->dataBuilder->build($buildSubject)
135  );
136  }
137 
143  private function initPaymentDOMock()
144  {
145  $this->paymentDOMock = $this->createMock(PaymentDataObjectInterface::class);
146  $this->paymentDOMock->expects(self::once())
147  ->method('getPayment')
148  ->willReturn($this->paymentModelMock);
149  }
150 }