ContextTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\Http;
  7. use \Magento\Framework\App\Http\Context;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. class ContextTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  13. */
  14. protected $objectManager;
  15. /**
  16. * @var Context
  17. */
  18. protected $object;
  19. /**
  20. * @var Json|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $serializerMock;
  23. protected function setUp()
  24. {
  25. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  26. $this->serializerMock = $this->getMockBuilder(Json::class)
  27. ->setMethods(['serialize'])
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->serializerMock->expects($this->any())
  31. ->method('serialize')
  32. ->will(
  33. $this->returnCallback(
  34. function ($value) {
  35. return json_encode($value);
  36. }
  37. )
  38. );
  39. $this->object = $this->objectManager->getObject(
  40. \Magento\Framework\App\Http\Context::class,
  41. [
  42. 'serializer' => $this->serializerMock
  43. ]
  44. );
  45. }
  46. public function testGetValue()
  47. {
  48. $this->assertNull($this->object->getValue('key'));
  49. }
  50. public function testSetGetValue()
  51. {
  52. $this->object->setValue('key', 'value', 'default');
  53. $this->assertEquals('value', $this->object->getValue('key'));
  54. }
  55. public function testSetUnsetGetValue()
  56. {
  57. $this->object->setValue('key', 'value', 'default');
  58. $this->object->unsValue('key');
  59. $this->assertEquals('default', $this->object->getValue('key'));
  60. }
  61. public function testGetData()
  62. {
  63. $this->object->setValue('key1', 'value1', 'default1');
  64. $this->object->setValue('key2', 'value2', 'default2');
  65. $this->object->setValue('key3', 'value3', 'value3');
  66. $this->object->unsValue('key1');
  67. $this->assertEquals(['key2' => 'value2'], $this->object->getData());
  68. }
  69. public function testGetVaryString()
  70. {
  71. $this->object->setValue('key2', 'value2', 'default2');
  72. $this->object->setValue('key1', 'value1', 'default1');
  73. $data = [
  74. 'key2' => 'value2',
  75. 'key1' => 'value1'
  76. ];
  77. ksort($data);
  78. $this->assertEquals(sha1(json_encode($data)), $this->object->getVaryString());
  79. }
  80. public function testToArray()
  81. {
  82. $newObject = new \Magento\Framework\App\Http\Context(['key' => 'value'], [], $this->serializerMock);
  83. $newObject->setValue('key1', 'value1', 'default1');
  84. $newObject->setValue('key2', 'value2', 'default2');
  85. $this->assertEquals(
  86. [
  87. 'data' => ['key' => 'value', 'key1' => 'value1', 'key2' => 'value2'],
  88. 'default' => ['key1' => 'default1', 'key2' => 'default2']
  89. ],
  90. $newObject->toArray()
  91. );
  92. }
  93. }