Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MaintenanceAllowIpsCommandTest.php
Go to the documentation of this file.
1 <?php
8 
11 use Symfony\Component\Console\Tester\CommandTester;
12 
13 class MaintenanceAllowIpsCommandTest extends \PHPUnit\Framework\TestCase
14 {
18  private $maintenanceMode;
19 
23  private $ipValidator;
24 
28  private $command;
29 
30  public function setUp()
31  {
32  $this->maintenanceMode = $this->createMock(\Magento\Framework\App\MaintenanceMode::class);
33  $this->ipValidator = $this->createMock(\Magento\Setup\Validator\IpValidator::class);
34  $this->command = new MaintenanceAllowIpsCommand($this->maintenanceMode, $this->ipValidator);
35  }
36 
43  public function testExecute(array $input, array $validatorMessages, $expectedMessage)
44  {
45  if (isset($input['--none']) && !$input['--none'] && isset($input['ip'])) {
46  $this->ipValidator->expects($this->once())->method('validateIps')->willReturn($validatorMessages);
47  if (empty($validatorMessages) && !empty($input['ip'])) {
48  $this->maintenanceMode
49  ->expects($this->once())
50  ->method('setAddresses')
51  ->with(implode(',', $input['ip']));
52  $this->maintenanceMode
53  ->expects($this->once())
54  ->method('getAddressInfo')
55  ->willReturn($input['ip']);
56  }
57  } elseif (isset($input['--none']) && $input['--none']) {
58  $this->ipValidator->expects($this->never())->method('validateIps')->willReturn($validatorMessages);
59  $this->maintenanceMode
60  ->expects($this->once())
61  ->method('setAddresses')
62  ->with('');
63  }
64  $tester = new CommandTester($this->command);
65  $tester->execute($input);
66  $this->assertEquals($expectedMessage, $tester->getDisplay());
67  }
68 
76  public function testExecuteWithAdd(array $addressInfo, array $input, array $validatorMessages, $expectedMessage)
77  {
78  $newAddressInfo = array_unique(array_merge($addressInfo, $input['ip']));
79 
80  $this->ipValidator->expects($this->once())->method('validateIps')->willReturn($validatorMessages);
81  $this->maintenanceMode
82  ->expects($this->once())
83  ->method('setAddresses')
84  ->with(implode(',', $newAddressInfo));
85 
86  $this->maintenanceMode
87  ->expects($this->exactly(2))
88  ->method('getAddressInfo')
89  ->willReturnOnConsecutiveCalls($addressInfo, $newAddressInfo);
90 
91  $tester = new CommandTester($this->command);
92  $tester->execute($input);
93  $this->assertEquals($expectedMessage, $tester->getDisplay());
94  }
95 
99  public function executeDataProvider()
100  {
101  return [
102  [
103  ['ip' => ['127.0.0.1', '127.0.0.2'], '--none' => false],
104  [],
105  'Set exempt IP-addresses: 127.0.0.1 127.0.0.2' . PHP_EOL
106  ],
107  [
108  ['--none' => true],
109  [],
110  'Set exempt IP-addresses: none' . PHP_EOL
111  ],
112  [
113  ['ip' => ['127.0.0.1', '127.0.0.2'], '--none' => true],
114  [],
115  'Set exempt IP-addresses: none' . PHP_EOL
116  ],
117  [
118  ['ip' => ['127.0'], '--none' => false],
119  ['Invalid IP 127.0'],
120  'Invalid IP 127.0' . PHP_EOL
121  ],
122  [
123  ['ip' => [], '--none' => false],
124  [],
125  ''
126  ]
127  ];
128  }
129 
133  public function executeWithAddDataProvider()
134  {
135  return [
136  [
137  [],
138  ['ip' => ['127.0.0.1'], '--add' => true],
139  [],
140  'Set exempt IP-addresses: 127.0.0.1' . PHP_EOL,
141  ],
142  [
143  ['127.0.0.1'],
144  ['ip' => ['127.0.0.1'], '--add' => true],
145  [],
146  'Set exempt IP-addresses: 127.0.0.1' . PHP_EOL,
147  ],
148  [
149  ['127.0.0.1'],
150  ['ip' => ['127.0.0.2'], '--add' => true],
151  [],
152  'Set exempt IP-addresses: 127.0.0.1 127.0.0.2' . PHP_EOL,
153  ],
154  ];
155  }
156 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
testExecuteWithAdd(array $addressInfo, array $input, array $validatorMessages, $expectedMessage)
testExecute(array $input, array $validatorMessages, $expectedMessage)