FlagTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Test\Unit;
  7. class FlagTest extends \PHPUnit\Framework\TestCase
  8. {
  9. public function testConstruct()
  10. {
  11. $flagCode = 'synchronize';
  12. $flag = $this->createFlagInstance();
  13. $flag->setFlagCode($flagCode);
  14. $this->assertEquals($flagCode, $flag->getFlagCode());
  15. }
  16. public function testGetFlagDataJson()
  17. {
  18. $data = ['foo' => 'bar'];
  19. $serializedData = '{"foo":"bar"}';
  20. $flag = $this->createFlagInstance(['flag_code' => 'synchronize']);
  21. $this->assertNull($flag->getFlagData());
  22. $flag->setData('flag_data', $serializedData);
  23. $this->assertEquals($data, $flag->getFlagData());
  24. }
  25. public function testGetFlagDataSerialized()
  26. {
  27. $data = 'foo';
  28. $serializedData = 's:3:"foo";';
  29. $flag = $this->createFlagInstance(['flag_code' => 'synchronize']);
  30. $this->assertNull($flag->getFlagData());
  31. $flag->setData('flag_data', $serializedData);
  32. $this->assertEquals($data, $flag->getFlagData());
  33. }
  34. public function testSetFlagData()
  35. {
  36. $data = ['foo' => 'bar'];
  37. $serializedData = '{"foo":"bar"}';
  38. $flag = $this->createFlagInstance(['flag_code' => 'synchronize']);
  39. $flag->setFlagData($data);
  40. $this->assertEquals($serializedData, $flag->getData('flag_data'));
  41. }
  42. public function testLoadSelf()
  43. {
  44. $flag = $this->createFlagInstance(['flag_code' => 'synchronize']);
  45. $this->assertInstanceOf(\Magento\Framework\Flag::class, $flag->loadSelf());
  46. }
  47. /**
  48. * @expectedException \Magento\Framework\Exception\LocalizedException
  49. * @expectedExceptionMessage Please define flag code.
  50. */
  51. public function testLoadSelfException()
  52. {
  53. $flag = $this->createFlagInstance();
  54. $flag->loadSelf();
  55. }
  56. public function testBeforeSave()
  57. {
  58. $flagCode = 'synchronize';
  59. $flag = $this->createFlagInstance(['flag_code' => $flagCode]);
  60. $flag->setData('block', 'blockNmae');
  61. $this->assertSame($flag, $flag->save());
  62. $this->assertEquals($flagCode, $flag->getFlagCode());
  63. }
  64. /**
  65. * @expectedException \Magento\Framework\Exception\LocalizedException
  66. * @expectedExceptionMessage Please define flag code.
  67. */
  68. public function testBeforeSaveException()
  69. {
  70. $flag = $this->createFlagInstance();
  71. $flag->setData('block', 'blockNmae');
  72. $flag->beforeSave();
  73. }
  74. /**
  75. * @param array $data
  76. * @return \Magento\Framework\Flag
  77. */
  78. private function createFlagInstance(array $data = [])
  79. {
  80. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  81. $eventManagerMock = $this->createPartialMock(\Magento\Framework\Event\Manager::class, ['dispatch']);
  82. /** @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject $contextMock */
  83. $contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
  84. $contextMock->expects($this->once())
  85. ->method('getEventDispatcher')
  86. ->willReturn($eventManagerMock);
  87. $connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  88. $connectionMock->expects($this->any())
  89. ->method('beginTransaction')
  90. ->willReturnSelf();
  91. $appResource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  92. $appResource->expects($this->any())
  93. ->method('getConnection')
  94. ->willReturn($connectionMock);
  95. $dbContextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
  96. $dbContextMock->expects($this->once())
  97. ->method('getResources')
  98. ->willReturn($appResource);
  99. $resourceMock = $this->getMockBuilder(\Magento\Framework\Flag\FlagResource::class)
  100. ->setMethods(['__wakeup', 'load', 'save', 'addCommitCallback', 'commit', 'rollBack'])
  101. ->setConstructorArgs(['context' => $dbContextMock])
  102. ->getMock();
  103. $resourceMock->expects($this->any())
  104. ->method('addCommitCallback')
  105. ->willReturnSelf();
  106. return $objectManager->getObject(
  107. \Magento\Framework\Flag::class,
  108. [
  109. 'context' => $contextMock,
  110. 'resource' => $resourceMock,
  111. 'data' => $data,
  112. 'json' => new \Magento\Framework\Serialize\Serializer\Json(),
  113. 'serialize' => new \Magento\Framework\Serialize\Serializer\Serialize()
  114. ]
  115. );
  116. }
  117. }