ResponseTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Test for Webapi Response model.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Webapi\Test\Unit;
  9. class ResponseTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * Response object.
  13. *
  14. * @var \Magento\Framework\Webapi\Response
  15. */
  16. protected $_response;
  17. protected function setUp()
  18. {
  19. /** Initialize SUT. */
  20. $this->_response = new \Magento\Framework\Webapi\Response();
  21. parent::setUp();
  22. }
  23. protected function tearDown()
  24. {
  25. unset($this->_response);
  26. parent::tearDown();
  27. }
  28. /**
  29. * Test addMessage, hasMessage, getMessage, and clearMessages methods.
  30. */
  31. public function testMessagesCrud()
  32. {
  33. /** Test that new object does not contain any messages. */
  34. $this->assertFalse($this->_response->hasMessages(), 'New object contains messages.');
  35. /** Test message adding functionality. */
  36. $this->_response->addMessage(
  37. 'Message text',
  38. \Magento\Framework\Webapi\Response::HTTP_OK,
  39. ['key' => 'value'],
  40. \Magento\Framework\Webapi\Response::MESSAGE_TYPE_SUCCESS
  41. );
  42. $this->assertTrue($this->_response->hasMessages(), 'New message is not added correctly.');
  43. /** Test message getting functionality. */
  44. $expectedMessage = [
  45. \Magento\Framework\Webapi\Response::MESSAGE_TYPE_SUCCESS => [
  46. [
  47. 'key' => 'value',
  48. 'message' => 'Message text',
  49. 'code' => \Magento\Framework\Webapi\Response::HTTP_OK,
  50. ],
  51. ],
  52. ];
  53. $this->assertEquals($expectedMessage, $this->_response->getMessages(), 'Message is got incorrectly.');
  54. /** Test message clearing functionality. */
  55. $this->_response->clearMessages();
  56. $this->assertFalse($this->_response->hasMessages(), 'Message is not cleared.');
  57. }
  58. }