Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateTest.php
Go to the documentation of this file.
1 <?php
7 
15 
16 class UpdateTest extends \PHPUnit\Framework\TestCase
17 {
21  private $connectorMock;
22 
26  private $configWriterMock;
27 
31  private $flagManagerMock;
32 
36  private $reinitableConfigMock;
37 
41  private $analyticsTokenMock;
42 
46  private $update;
47 
48  protected function setUp()
49  {
50  $this->connectorMock = $this->getMockBuilder(Connector::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53  $this->configWriterMock = $this->getMockBuilder(WriterInterface::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->flagManagerMock = $this->getMockBuilder(FlagManager::class)
57  ->disableOriginalConstructor()
58  ->getMock();
59  $this->reinitableConfigMock = $this->getMockBuilder(ReinitableConfigInterface::class)
60  ->disableOriginalConstructor()
61  ->getMock();
62  $this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class)
63  ->disableOriginalConstructor()
64  ->getMock();
65 
66  $this->update = new Update(
67  $this->connectorMock,
68  $this->configWriterMock,
69  $this->reinitableConfigMock,
70  $this->flagManagerMock,
71  $this->analyticsTokenMock
72  );
73  }
74 
78  public function testExecuteWithoutToken()
79  {
80  $this->flagManagerMock
81  ->method('getFlagData')
83  ->willReturn(10);
84  $this->connectorMock
85  ->expects($this->once())
86  ->method('execute')
87  ->with('update')
88  ->willReturn(false);
89  $this->analyticsTokenMock
90  ->expects($this->once())
91  ->method('isTokenExist')
92  ->willReturn(false);
93  $this->addFinalOutputAsserts();
94  $this->assertFalse($this->update->execute());
95  }
96 
100  private function addFinalOutputAsserts(bool $isExecuted = true)
101  {
102  $this->flagManagerMock
103  ->expects($this->exactly(2 * $isExecuted))
104  ->method('deleteFlag')
105  ->withConsecutive(
108  );
109  $this->configWriterMock
110  ->expects($this->exactly((int)$isExecuted))
111  ->method('delete')
113  $this->reinitableConfigMock
114  ->expects($this->exactly((int)$isExecuted))
115  ->method('reinit')
116  ->with();
117  }
118 
124  public function testExecuteWithEmptyReverseCounter($counterData)
125  {
126  $this->flagManagerMock
127  ->method('getFlagData')
129  ->willReturn($counterData);
130  $this->connectorMock
131  ->expects($this->never())
132  ->method('execute')
133  ->with('update')
134  ->willReturn(false);
135  $this->analyticsTokenMock
136  ->method('isTokenExist')
137  ->willReturn(true);
138  $this->addFinalOutputAsserts();
139  $this->assertFalse($this->update->execute());
140  }
141 
148  {
149  return [
150  [null],
151  [0]
152  ];
153  }
154 
163  public function testExecuteRegularScenario(
164  int $reverseCount,
165  bool $commandResult,
166  bool $finalConditionsIsExpected,
167  bool $functionResult
168  ) {
169  $this->flagManagerMock
170  ->method('getFlagData')
172  ->willReturn($reverseCount);
173  $this->connectorMock
174  ->expects($this->once())
175  ->method('execute')
176  ->with('update')
177  ->willReturn($commandResult);
178  $this->analyticsTokenMock
179  ->method('isTokenExist')
180  ->willReturn(true);
181  $this->addFinalOutputAsserts($finalConditionsIsExpected);
182  $this->assertSame($functionResult, $this->update->execute());
183  }
184 
189  {
190  return [
191  'The last attempt with command execution result False' => [
192  'Reverse count' => 1,
193  'Command result' => false,
194  'Executed final output conditions' => true,
195  'Function result' => false,
196  ],
197  'Not the last attempt with command execution result False' => [
198  'Reverse count' => 10,
199  'Command result' => false,
200  'Executed final output conditions' => false,
201  'Function result' => false,
202  ],
203  'Command execution result True' => [
204  'Reverse count' => 10,
205  'Command result' => true,
206  'Executed final output conditions' => true,
207  'Function result' => true,
208  ],
209  ];
210  }
211 }
testExecuteRegularScenario(int $reverseCount, bool $commandResult, bool $finalConditionsIsExpected, bool $functionResult)
Definition: UpdateTest.php:163