SerializedTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Test\Unit\Model\Config\Backend;
  7. use Magento\Config\Model\Config\Backend\Serialized;
  8. use Magento\Framework\Model\Context;
  9. use Magento\Framework\Serialize\Serializer\Json;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. class SerializedTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \Magento\Config\Model\Config\Backend\Serialized */
  14. private $serializedConfig;
  15. /** @var Json|\PHPUnit_Framework_MockObject_MockObject */
  16. private $serializerMock;
  17. protected function setUp()
  18. {
  19. $objectManager = new ObjectManager($this);
  20. $this->serializerMock = $this->createMock(Json::class);
  21. $contextMock = $this->createMock(Context::class);
  22. $eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  23. $contextMock->method('getEventDispatcher')
  24. ->willReturn($eventManagerMock);
  25. $this->serializedConfig = $objectManager->getObject(
  26. Serialized::class,
  27. [
  28. 'serializer' => $this->serializerMock,
  29. 'context' => $contextMock,
  30. ]
  31. );
  32. }
  33. /**
  34. * @param int|double|string|array|boolean|null $expected
  35. * @param int|double|string|array|boolean|null $value
  36. * @param int $numCalls
  37. * @param array $unserializedValue
  38. * @dataProvider afterLoadDataProvider
  39. */
  40. public function testAfterLoad($expected, $value, $numCalls, $unserializedValue = null)
  41. {
  42. $this->serializedConfig->setValue($value);
  43. $this->serializerMock->expects($this->exactly($numCalls))
  44. ->method('unserialize')
  45. ->willReturn($unserializedValue);
  46. $this->serializedConfig->afterLoad();
  47. $this->assertEquals($expected, $this->serializedConfig->getValue());
  48. }
  49. /**
  50. * @return array
  51. */
  52. public function afterLoadDataProvider()
  53. {
  54. return [
  55. 'empty value' => [
  56. false,
  57. '',
  58. 0,
  59. ],
  60. 'value' => [
  61. ['string array'],
  62. 'string array',
  63. 1,
  64. ['string array']
  65. ]
  66. ];
  67. }
  68. /**
  69. * @param string $expected
  70. * @param int|double|string|array|boolean|null $value
  71. * @param int $numCalls
  72. * @param string|null $serializedValue
  73. * @dataProvider beforeSaveDataProvider
  74. */
  75. public function testBeforeSave($expected, $value, $numCalls, $serializedValue = null)
  76. {
  77. $this->serializedConfig->setId('id');
  78. $this->serializedConfig->setValue($value);
  79. $this->serializerMock->expects($this->exactly($numCalls))
  80. ->method('serialize')
  81. ->willReturn($serializedValue);
  82. $this->serializedConfig->beforeSave();
  83. $this->assertEquals($expected, $this->serializedConfig->getValue());
  84. }
  85. /**
  86. * @return array
  87. */
  88. public function beforeSaveDataProvider()
  89. {
  90. return [
  91. 'string' => [
  92. 'string',
  93. 'string',
  94. 0,
  95. ],
  96. 'array' => [
  97. 'string array',
  98. ['string array'],
  99. 1,
  100. 'string array'
  101. ]
  102. ];
  103. }
  104. }