|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
| 7 | + |
6 | 8 | namespace Magento\Analytics\Test\Unit\Model\Connector\Http;
|
7 | 9 |
|
8 |
| -use Magento\Analytics\Model\Connector\Http\JsonConverter; |
| 10 | +use Magento\Analytics\Model\Connector\Http\ConverterInterface; |
9 | 11 | use Magento\Analytics\Model\Connector\Http\ResponseHandlerInterface;
|
10 | 12 | use Magento\Analytics\Model\Connector\Http\ResponseResolver;
|
11 |
| -use Magento\Framework\Serialize\Serializer\Json; |
12 |
| -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 13 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
13 | 14 |
|
14 | 15 | class ResponseResolverTest extends \PHPUnit\Framework\TestCase
|
15 | 16 | {
|
16 |
| - public function testGetResultHandleResponseSuccess() |
| 17 | + /** |
| 18 | + * @var ObjectManagerHelper |
| 19 | + */ |
| 20 | + private $objectManagerHelper; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var ConverterInterface|\PHPUnit_Framework_MockObject_MockObject |
| 24 | + */ |
| 25 | + private $converterMock; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ResponseHandlerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + private $successResponseHandlerMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ResponseHandlerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + private $notFoundResponseHandlerMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var ResponseResolver |
| 39 | + */ |
| 40 | + private $responseResolver; |
| 41 | + |
| 42 | + /** |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + protected function setUp() |
17 | 46 | {
|
18 |
| - $expectedBody = ['test' => 'testValue']; |
19 |
| - $response = new \Zend_Http_Response(201, [], json_encode($expectedBody)); |
20 |
| - $responseHandlerMock = $this->getMockBuilder(ResponseHandlerInterface::class) |
21 |
| - ->getMockForAbstractClass(); |
22 |
| - $responseHandlerMock->expects($this->once()) |
23 |
| - ->method('handleResponse') |
24 |
| - ->with($expectedBody) |
25 |
| - ->willReturn(true); |
26 |
| - $notFoundResponseHandlerMock = $this->getMockBuilder(ResponseHandlerInterface::class) |
27 |
| - ->getMockForAbstractClass(); |
28 |
| - $notFoundResponseHandlerMock->expects($this->never())->method('handleResponse'); |
29 |
| - $serializerMock = $this->getMockBuilder(Json::class) |
| 47 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 48 | + $this->converterMock = $this->getMockBuilder(ConverterInterface::class) |
30 | 49 | ->disableOriginalConstructor()
|
31 | 50 | ->getMock();
|
32 |
| - $serializerMock->expects($this->once()) |
33 |
| - ->method('unserialize') |
34 |
| - ->willReturn($expectedBody); |
35 |
| - $objectManager = new ObjectManager($this); |
36 |
| - $responseResolver = $objectManager->getObject( |
| 51 | + $this->successResponseHandlerMock = $this->getMockBuilder(ResponseHandlerInterface::class) |
| 52 | + ->getMockForAbstractClass(); |
| 53 | + $this->notFoundResponseHandlerMock = $this->getMockBuilder(ResponseHandlerInterface::class) |
| 54 | + ->getMockForAbstractClass(); |
| 55 | + $this->responseResolver = $this->objectManagerHelper->getObject( |
37 | 56 | ResponseResolver::class,
|
38 | 57 | [
|
39 |
| - 'converter' => $objectManager->getObject( |
40 |
| - JsonConverter::class, |
41 |
| - ['serializer' => $serializerMock] |
42 |
| - ), |
| 58 | + 'converter' => $this->converterMock, |
43 | 59 | 'responseHandlers' => [
|
44 |
| - 201 => $responseHandlerMock, |
45 |
| - 404 => $notFoundResponseHandlerMock, |
| 60 | + 201 => $this->successResponseHandlerMock, |
| 61 | + 404 => $this->notFoundResponseHandlerMock, |
46 | 62 | ]
|
47 | 63 | ]
|
48 | 64 | );
|
49 |
| - $this->assertTrue($responseResolver->getResult($response)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return void |
| 69 | + * @throws \Zend_Http_Exception |
| 70 | + */ |
| 71 | + public function testGetResultHandleResponseSuccess() |
| 72 | + { |
| 73 | + $expectedBody = ['test' => 'testValue']; |
| 74 | + $response = new \Zend_Http_Response(201, ['Content-Type' => 'application/json'], json_encode($expectedBody)); |
| 75 | + $this->converterMock |
| 76 | + ->method('getContentTypeHeader') |
| 77 | + ->willReturn('Content-Type: 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 | + |
| 93 | + /** |
| 94 | + * @return void |
| 95 | + * @throws \Zend_Http_Exception |
| 96 | + */ |
| 97 | + public function testGetResultHandleResponseUnexpectedContentType() |
| 98 | + { |
| 99 | + $expectedBody = 'testString'; |
| 100 | + $response = new \Zend_Http_Response(201, ['Content-Type' => 'plain/text'], $expectedBody); |
| 101 | + $this->converterMock |
| 102 | + ->method('getContentTypeHeader') |
| 103 | + ->willReturn('Content-Type: 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)); |
50 | 116 | }
|
51 | 117 | }
|
0 commit comments