Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
JsonConverterTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class JsonConverterTest extends \PHPUnit\Framework\TestCase
12 {
16  private $objectManagerHelper;
17 
21  private $serializerMock;
22 
26  private $converter;
27 
31  protected function setUp()
32  {
33  $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
34  $this->serializerMock = $this->getMockBuilder(Json::class)
35  ->disableOriginalConstructor()
36  ->getMock();
37  $this->converter = $this->objectManagerHelper->getObject(
38  JsonConverter::class,
39  ['serializer' => $this->serializerMock]
40  );
41  }
42 
46  public function testConverterContainsHeader()
47  {
48  $this->assertEquals(
49  'Content-Type: ' . JsonConverter::CONTENT_MEDIA_TYPE,
50  $this->converter->getContentTypeHeader()
51  );
52  }
53 
59  public function testConvertBody($unserializedResult, $expected)
60  {
61  $this->serializerMock->expects($this->once())
62  ->method('unserialize')
63  ->willReturn($unserializedResult);
64  $this->assertEquals($expected, $this->converter->fromBody('body'));
65  }
66 
70  public function convertBodyDataProvider()
71  {
72  return [
73  [null, ['body']],
74  [['unserializedBody'], ['unserializedBody']]
75  ];
76  }
77 
81  public function testConvertData()
82  {
83  $this->serializerMock->expects($this->once())
84  ->method('serialize')
85  ->willReturn('serializedResult');
86  $this->assertEquals('serializedResult', $this->converter->toBody(["token" => "secret-token"]));
87  }
88 }