Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FlagTest.php
Go to the documentation of this file.
1 <?php
7 
8 class FlagTest extends \PHPUnit\Framework\TestCase
9 {
10  public function testConstruct()
11  {
12  $flagCode = 'synchronize';
13  $flag = $this->createFlagInstance();
14  $flag->setFlagCode($flagCode);
15  $this->assertEquals($flagCode, $flag->getFlagCode());
16  }
17 
18  public function testGetFlagDataJson()
19  {
20  $data = ['foo' => 'bar'];
21  $serializedData = '{"foo":"bar"}';
22  $flag = $this->createFlagInstance(['flag_code' => 'synchronize']);
23  $this->assertNull($flag->getFlagData());
24  $flag->setData('flag_data', $serializedData);
25  $this->assertEquals($data, $flag->getFlagData());
26  }
27 
28  public function testGetFlagDataSerialized()
29  {
30  $data = 'foo';
31  $serializedData = 's:3:"foo";';
32  $flag = $this->createFlagInstance(['flag_code' => 'synchronize']);
33  $this->assertNull($flag->getFlagData());
34  $flag->setData('flag_data', $serializedData);
35  $this->assertEquals($data, $flag->getFlagData());
36  }
37 
38  public function testSetFlagData()
39  {
40  $data = ['foo' => 'bar'];
41  $serializedData = '{"foo":"bar"}';
42  $flag = $this->createFlagInstance(['flag_code' => 'synchronize']);
43  $flag->setFlagData($data);
44  $this->assertEquals($serializedData, $flag->getData('flag_data'));
45  }
46 
47  public function testLoadSelf()
48  {
49  $flag = $this->createFlagInstance(['flag_code' => 'synchronize']);
50  $this->assertInstanceOf(\Magento\Framework\Flag::class, $flag->loadSelf());
51  }
52 
57  public function testLoadSelfException()
58  {
59  $flag = $this->createFlagInstance();
60  $flag->loadSelf();
61  }
62 
63  public function testBeforeSave()
64  {
65  $flagCode = 'synchronize';
66  $flag = $this->createFlagInstance(['flag_code' => $flagCode]);
67  $flag->setData('block', 'blockNmae');
68  $this->assertSame($flag, $flag->save());
69  $this->assertEquals($flagCode, $flag->getFlagCode());
70  }
71 
76  public function testBeforeSaveException()
77  {
78  $flag = $this->createFlagInstance();
79  $flag->setData('block', 'blockNmae');
80  $flag->beforeSave();
81  }
82 
87  private function createFlagInstance(array $data = [])
88  {
89  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
90  $eventManagerMock = $this->createPartialMock(\Magento\Framework\Event\Manager::class, ['dispatch']);
92  $contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
93  $contextMock->expects($this->once())
94  ->method('getEventDispatcher')
95  ->willReturn($eventManagerMock);
96  $connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
97  $connectionMock->expects($this->any())
98  ->method('beginTransaction')
99  ->willReturnSelf();
100  $appResource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
101  $appResource->expects($this->any())
102  ->method('getConnection')
103  ->willReturn($connectionMock);
104  $dbContextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
105  $dbContextMock->expects($this->once())
106  ->method('getResources')
107  ->willReturn($appResource);
108  $resourceMock = $this->getMockBuilder(\Magento\Framework\Flag\FlagResource::class)
109  ->setMethods(['__wakeup', 'load', 'save', 'addCommitCallback', 'commit', 'rollBack'])
110  ->setConstructorArgs(['context' => $dbContextMock])
111  ->getMock();
112 
113  $resourceMock->expects($this->any())
114  ->method('addCommitCallback')
115  ->willReturnSelf();
116  return $objectManager->getObject(
117  \Magento\Framework\Flag::class,
118  [
119  'context' => $contextMock,
120  'resource' => $resourceMock,
121  'data' => $data,
122  'json' => new \Magento\Framework\Serialize\Serializer\Json(),
123  'serialize' => new \Magento\Framework\Serialize\Serializer\Serialize()
124  ]
125  );
126  }
127 }
$objectManager
Definition: bootstrap.php:17