Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReadinessCheckTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class ReadinessCheckTest extends \PHPUnit\Framework\TestCase
12 {
16  private $dbValidator;
17 
21  private $deploymentConfig;
22 
26  private $filesystem;
27 
31  private $write;
32 
36  private $phpReadinessCheck;
37 
41  private $readinessCheck;
42 
46  private $basePackageInfo;
47 
51  private $expected;
52 
56  private $status;
57 
58  public function setUp()
59  {
60  $this->dbValidator = $this->createMock(\Magento\Setup\Validator\DbValidator::class);
61  $this->deploymentConfig = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
62  $this->deploymentConfig->expects($this->once())
63  ->method('get')
64  ->willReturn(
65  [
70  ]
71  );
72  $this->filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
73  $this->write = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
74  $this->filesystem->expects($this->once())->method('getDirectoryWrite')->willReturn($this->write);
75  $this->phpReadinessCheck = $this->createMock(\Magento\Setup\Model\PhpReadinessCheck::class);
76  $this->basePackageInfo = $this->createMock(\Magento\Setup\Model\BasePackageInfo::class);
77  $this->basePackageInfo->expects($this->once())->method('getPaths')->willReturn([__FILE__]);
78  $this->status = $this->createMock(\Magento\Setup\Model\Cron\Status::class);
79  $this->readinessCheck = new ReadinessCheck(
80  $this->dbValidator,
81  $this->deploymentConfig,
82  $this->filesystem,
83  $this->phpReadinessCheck,
84  $this->basePackageInfo,
85  $this->status
86  );
87  $this->phpReadinessCheck
88  ->expects($this->once())
89  ->method('checkPhpVersion')
90  ->willReturn(['responseType' => 'success']);
91  $this->phpReadinessCheck
92  ->expects($this->once())
93  ->method('checkPhpExtensions')
94  ->willReturn(['responseType' => 'success']);
95  $this->phpReadinessCheck
96  ->expects($this->once())
97  ->method('checkPhpCronSettings')
98  ->willReturn(['responseType' => 'success']);
99  $this->expected = [
100  ReadinessCheck::KEY_PHP_VERSION_VERIFIED => ['responseType' => 'success'],
101  ReadinessCheck::KEY_PHP_EXTENSIONS_VERIFIED => ['responseType' => 'success'],
102  ReadinessCheck::KEY_PHP_SETTINGS_VERIFIED => ['responseType' => 'success']
103  ];
104  }
105 
107  {
108  $this->dbValidator->expects($this->once())
109  ->method('checkDatabaseConnection')
110  ->willThrowException(new \Magento\Setup\Exception('Connection failure'));
111  $this->write->expects($this->once())->method('isExist')->willReturn(false);
112  $this->write->expects($this->never())->method('readFile');
113  $expected = [
116  'error' => 'Connection failure'
117  ],
118  ReadinessCheck::KEY_PHP_CHECKS => $this->expected,
122  ],
124  ];
125  $expectedJson = json_encode($expected, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
126  $this->write->expects($this->once())
127  ->method('writeFile')
128  ->with(ReadinessCheck::SETUP_CRON_JOB_STATUS_FILE, $expectedJson);
129  $this->readinessCheck->runReadinessCheck();
130  }
131 
133  {
134  $this->dbValidator->expects($this->once())
135  ->method('checkDatabaseConnection')
136  ->willThrowException(new \Magento\Setup\Exception('Database user username does not have write access.'));
137  $this->write->expects($this->once())->method('isExist')->willReturn(false);
138  $this->write->expects($this->never())->method('readFile');
139  $expected = [
142  'error' => 'Database user username does not have write access.'
143  ],
144  ReadinessCheck::KEY_PHP_CHECKS => $this->expected,
148  ],
150  ];
151  $expectedJson = json_encode($expected, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
152  $this->write->expects($this->once())
153  ->method('writeFile')
154  ->with(ReadinessCheck::SETUP_CRON_JOB_STATUS_FILE, $expectedJson);
155  $this->readinessCheck->runReadinessCheck();
156  }
157 
158  public function testRunReadinessCheck()
159  {
160  $this->dbValidator->expects($this->once())->method('checkDatabaseConnection')->willReturn(true);
161  $this->write->expects($this->once())->method('isExist')->willReturn(false);
162  $this->write->expects($this->never())->method('readFile');
163  $expected = [
165  ReadinessCheck::KEY_PHP_CHECKS => $this->expected,
169  ],
171  ];
172  $expectedJson = json_encode($expected, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
173  $this->write->expects($this->once())
174  ->method('writeFile')
175  ->with(ReadinessCheck::SETUP_CRON_JOB_STATUS_FILE, $expectedJson);
176  $this->readinessCheck->runReadinessCheck();
177  }
178 
180  {
181  $this->dbValidator->expects($this->once())->method('checkDatabaseConnection')->willReturn(true);
182  $this->write->expects($this->once())->method('isExist')->willReturn(true);
183  $this->write->expects($this->once())->method('readFile')->willReturn('{"current_timestamp": 50}');
184  $expected = [
186  ReadinessCheck::KEY_PHP_CHECKS => $this->expected,
190  ],
193  ];
194  $expectedJson = json_encode($expected, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
195  $this->write->expects($this->once())
196  ->method('writeFile')
197  ->with(ReadinessCheck::SETUP_CRON_JOB_STATUS_FILE, $expectedJson);
198  $this->readinessCheck->runReadinessCheck();
199  }
200 }
201 
202 namespace Magento\Setup\Model\Cron;
203 
207 function time()
208 {
209  return 100;
210 }
defined('MTF_BOOT_FILE')||define('MTF_BOOT_FILE' __FILE__
Definition: bootstrap.php:7