DataTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config\Test\Unit;
  7. class DataTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\Config\ReaderInterface|\PHPUnit_Framework_MockObject_MockObject
  11. */
  12. private $readerMock;
  13. /**
  14. * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $cacheMock;
  17. /**
  18. * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $serializerMock;
  21. protected function setUp()
  22. {
  23. $this->readerMock = $this->createMock(\Magento\Framework\Config\ReaderInterface::class);
  24. $this->cacheMock = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
  25. $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
  26. }
  27. public function testGetConfigNotCached()
  28. {
  29. $data = ['a' => 'b'];
  30. $cacheId = 'test';
  31. $this->cacheMock->expects($this->once())
  32. ->method('load')
  33. ->willReturn(false);
  34. $this->readerMock->expects($this->once())
  35. ->method('read')
  36. ->willReturn($data);
  37. $this->serializerMock->expects($this->once())
  38. ->method('serialize')
  39. ->with($data);
  40. $config = new \Magento\Framework\Config\Data(
  41. $this->readerMock,
  42. $this->cacheMock,
  43. $cacheId,
  44. $this->serializerMock
  45. );
  46. $this->assertEquals($data, $config->get());
  47. $this->assertEquals('b', $config->get('a'));
  48. $this->assertEquals(null, $config->get('a/b'));
  49. $this->assertEquals(33, $config->get('a/b', 33));
  50. }
  51. public function testGetConfigCached()
  52. {
  53. $data = ['a' => 'b'];
  54. $serializedData = '{"a":"b"}';
  55. $cacheId = 'test';
  56. $this->cacheMock->expects($this->once())
  57. ->method('load')
  58. ->willReturn($serializedData);
  59. $this->readerMock->expects($this->never())
  60. ->method('read');
  61. $this->serializerMock->expects($this->once())
  62. ->method('unserialize')
  63. ->with($serializedData)
  64. ->willReturn($data);
  65. $config = new \Magento\Framework\Config\Data(
  66. $this->readerMock,
  67. $this->cacheMock,
  68. $cacheId,
  69. $this->serializerMock
  70. );
  71. $this->assertEquals($data, $config->get());
  72. $this->assertEquals('b', $config->get('a'));
  73. }
  74. public function testReset()
  75. {
  76. $serializedData = '';
  77. $cacheId = 'test';
  78. $this->cacheMock->expects($this->once())
  79. ->method('load')
  80. ->willReturn($serializedData);
  81. $this->serializerMock->expects($this->once())
  82. ->method('unserialize')
  83. ->with($serializedData)
  84. ->willReturn([]);
  85. $this->cacheMock->expects($this->once())
  86. ->method('remove')
  87. ->with($cacheId);
  88. $config = new \Magento\Framework\Config\Data(
  89. $this->readerMock,
  90. $this->cacheMock,
  91. $cacheId,
  92. $this->serializerMock
  93. );
  94. $config->reset();
  95. }
  96. }