Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ResponseResolverTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
15 class ResponseResolverTest extends \PHPUnit\Framework\TestCase
16 {
20  private $objectManagerHelper;
21 
25  private $converterMock;
26 
30  private $successResponseHandlerMock;
31 
35  private $notFoundResponseHandlerMock;
36 
40  private $responseResolver;
41 
45  protected function setUp()
46  {
47  $this->objectManagerHelper = new ObjectManagerHelper($this);
48  $this->converterMock = $this->getMockBuilder(ConverterInterface::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51  $this->successResponseHandlerMock = $this->getMockBuilder(ResponseHandlerInterface::class)
52  ->getMockForAbstractClass();
53  $this->notFoundResponseHandlerMock = $this->getMockBuilder(ResponseHandlerInterface::class)
54  ->getMockForAbstractClass();
55  $this->responseResolver = $this->objectManagerHelper->getObject(
56  ResponseResolver::class,
57  [
58  'converter' => $this->converterMock,
59  'responseHandlers' => [
60  201 => $this->successResponseHandlerMock,
61  404 => $this->notFoundResponseHandlerMock,
62  ]
63  ]
64  );
65  }
66 
72  {
73  $expectedBody = ['test' => 'testValue'];
74  $response = new \Zend_Http_Response(201, ['Content-Type' => 'application/json'], json_encode($expectedBody));
75  $this->converterMock
76  ->method('getContentMediaType')
77  ->willReturn('application/json');
78 
79  $this->successResponseHandlerMock
80  ->expects($this->once())
81  ->method('handleResponse')
82  ->with($expectedBody)
83  ->willReturn(true);
84  $this->notFoundResponseHandlerMock
85  ->expects($this->never())
86  ->method('handleResponse');
87  $this->converterMock
88  ->method('fromBody')
89  ->willReturn($expectedBody);
90  $this->assertTrue($this->responseResolver->getResult($response));
91  }
92 
98  {
99  $expectedBody = 'testString';
100  $response = new \Zend_Http_Response(201, ['Content-Type' => 'plain/text'], $expectedBody);
101  $this->converterMock
102  ->method('getContentMediaType')
103  ->willReturn('application/json');
104  $this->converterMock
105  ->expects($this->never())
106  ->method('fromBody');
107  $this->successResponseHandlerMock
108  ->expects($this->once())
109  ->method('handleResponse')
110  ->with([])
111  ->willReturn(false);
112  $this->notFoundResponseHandlerMock
113  ->expects($this->never())
114  ->method('handleResponse');
115  $this->assertFalse($this->responseResolver->getResult($response));
116  }
117 }
$response
Definition: 404.php:11