Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WebhookMessageReaderTest.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Signifyd\Model\SignifydGateway\Response\WebhookMessageFactory;
13 
17 class WebhookMessageReaderTest extends \PHPUnit\Framework\TestCase
18 {
22  private $model;
23 
27  private $decoder;
28 
32  private $webhookMessageFactory;
33 
37  private $webhookRequest;
38 
42  protected function setUp()
43  {
44  $this->decoder = $this->getMockBuilder(DecoderInterface::class)
45  ->getMockForAbstractClass();
46 
47  $this->webhookMessageFactory = $this->getMockBuilder(WebhookMessageFactory::class)
48  ->setMethods(['create'])
49  ->disableOriginalConstructor()
50  ->getMock();
51 
52  $this->webhookRequest = $this->getMockBuilder(WebhookRequest::class)
53  ->disableOriginalConstructor()
54  ->getMock();
55 
56  $this->model = new WebhookMessageReader(
57  $this->decoder,
58  $this->webhookMessageFactory
59  );
60  }
61 
66  public function testReadSuccess()
67  {
68  $rawBody = 'body';
69  $topic = 'topic';
70  $decodedData = ['status' => "DISMISSED", 'orderId' => '19418'];
71 
72  $this->webhookRequest->expects($this->once())
73  ->method('getBody')
74  ->willReturn($rawBody);
75  $this->webhookRequest->expects($this->once())
76  ->method('getEventTopic')
77  ->willReturn('topic');
78  $this->decoder->expects($this->once())
79  ->method('decode')
80  ->with($rawBody)
81  ->willReturn($decodedData);
82  $webhookMessage = $this->getMockBuilder(WebhookMessage::class)
83  ->disableOriginalConstructor()
84  ->getMock();
85  $this->webhookMessageFactory->expects($this->once())
86  ->method('create')
87  ->with(
88  [
89  'data' => $decodedData,
90  'eventTopic' => $topic
91  ]
92  )
93  ->willReturn($webhookMessage);
94 
95  $this->assertEquals(
96  $webhookMessage,
97  $this->model->read($this->webhookRequest)
98  );
99  }
100 
106  public function testReadFail()
107  {
108  $this->decoder->expects($this->once())
109  ->method('decode')
110  ->willThrowException(new \Exception('Error'));
111 
112  $this->model->read($this->webhookRequest);
113  }
114 }