Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FlagManagerTest.php
Go to the documentation of this file.
1 <?php
7 
12 use \PHPUnit_Framework_MockObject_MockObject as Mock;
13 
17 class FlagManagerTest extends \PHPUnit\Framework\TestCase
18 {
22  private $flagFactoryMock;
23 
27  private $flagMock;
28 
32  private $flagResourceMock;
33 
37  private $flagManager;
38 
42  protected function setUp()
43  {
44  $this->flagFactoryMock = $this->getMockBuilder(FlagFactory::class)
45  ->disableOriginalConstructor()
46  ->getMock();
47  $this->flagResourceMock = $this->getMockBuilder(FlagResource::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50  $this->flagMock = $this->getMockBuilder(Flag::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53 
54  $this->flagManager = new FlagManager(
55  $this->flagFactoryMock,
56  $this->flagResourceMock
57  );
58  }
59 
60  public function testGetFlagData()
61  {
62  $flagCode = 'flag';
63  $this->setupFlagObject($flagCode);
64  $this->flagMock->expects($this->once())
65  ->method('getFlagData')
66  ->willReturn(10);
67 
68  $this->assertEquals($this->flagManager->getFlagData($flagCode), 10);
69  }
70 
71  public function testSaveFlag()
72  {
73  $flagCode = 'flag';
74  $this->setupFlagObject($flagCode);
75  $this->flagMock->expects($this->once())
76  ->method('setFlagData')
77  ->with(10);
78  $this->flagResourceMock->expects($this->once())
79  ->method('save')
80  ->with($this->flagMock);
81 
82  $this->assertTrue(
83  $this->flagManager->saveFlag($flagCode, 10)
84  );
85  }
86 
92  public function testDeleteFlag($isFlagExist)
93  {
94  $flagCode = 'flag';
95 
96  $this->setupFlagObject($flagCode);
97 
98  $this->flagMock
99  ->expects($this->once())
100  ->method('getId')
101  ->willReturn($isFlagExist);
102 
103  if ($isFlagExist) {
104  $this->flagResourceMock
105  ->expects($this->once())
106  ->method('delete')
107  ->with($this->flagMock);
108  }
109 
110  $this->assertTrue(
111  $this->flagManager->deleteFlag($flagCode)
112  );
113  }
114 
118  private function setupFlagObject($flagCode)
119  {
120  $this->flagFactoryMock->expects($this->once())
121  ->method('create')
122  ->with(['data' => ['flag_code' => $flagCode]])
123  ->willReturn($this->flagMock);
124  $this->flagResourceMock->expects($this->once())
125  ->method('load')
126  ->with($this->flagMock, $flagCode, 'flag_code');
127  }
128 
134  public function flagExistDataProvider()
135  {
136  return [
137  [true],
138  [false]
139  ];
140  }
141 }