JsonTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Controller\Test\Unit\Result;
  7. /**
  8. * Class JsonTest
  9. *
  10. * @covers \Magento\Framework\Controller\Result\Json
  11. */
  12. class JsonTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @return void
  16. */
  17. public function testRenderResult()
  18. {
  19. $json = '{"data":"data"}';
  20. $translatedJson = '{"data_translated":"data_translated"}';
  21. /** @var \Magento\Framework\Translate\InlineInterface|\PHPUnit_Framework_MockObject_MockObject
  22. * $translateInline
  23. */
  24. $translateInline = $this->createMock(\Magento\Framework\Translate\InlineInterface::class);
  25. $translateInline->expects($this->any())->method('processResponseBody')->with($json, true)->will(
  26. $this->returnValue($translatedJson)
  27. );
  28. $response = $this->createMock(\Magento\Framework\App\Response\HttpInterface::class);
  29. $response->expects($this->atLeastOnce())->method('setHeader')->with('Content-Type', 'application/json', true);
  30. $response->expects($this->atLeastOnce())->method('setBody')->with($json);
  31. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  32. $resultJson = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))
  33. ->getObject(\Magento\Framework\Controller\Result\Json::class, ['translateInline' => $translateInline]);
  34. $resultJson->setJsonData($json);
  35. $this->assertSame($resultJson, $resultJson->renderResult($response));
  36. }
  37. }