Created
December 4, 2024 01:52
-
-
Save ProxiBlue/ac193cbf21660eec017924785ae2d209 to your computer and use it in GitHub Desktop.
cron_unit_test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Uptactics\PpsTheme\Test\Unit; | |
use PHPUnit\Framework\TestCase; | |
class CronListTest extends TestCase | |
{ | |
private $filePath; | |
protected function setUp(): void | |
{ | |
$this->filePath = BP . '/var/cron_test/known_cron_list.txt'; | |
// Ensure the directory exists | |
if (!is_dir(dirname($this->filePath))) { | |
mkdir(dirname($this->filePath), 0755, true); | |
} | |
} | |
public function testCronList() | |
{ | |
$currentCronList = $this->getCurrentCronList(); | |
$previousCronList = $this->getPreviousCronList(); | |
if (!empty($previousCronList)) { | |
$this->assertSame($previousCronList, $currentCronList, $this->getDifferenceAlert($previousCronList, $currentCronList)); | |
} | |
// Save the current list for future runs | |
file_put_contents($this->filePath, implode(PHP_EOL, $currentCronList)); | |
} | |
private function getCurrentCronList(): array | |
{ | |
$output = []; | |
exec('./bin/magento cronma:s', $output); | |
// Process the output to remove headers and format it | |
array_shift($output); // Remove the header line | |
return array_map('trim', $output); | |
} | |
private function getPreviousCronList(): array | |
{ | |
if (file_exists($this->filePath)) { | |
$content = file_get_contents($this->filePath); | |
return explode(PHP_EOL, $content); | |
} | |
return []; | |
} | |
private function getDifferenceAlert(array $previous, array $current): string | |
{ | |
$differences = array_diff($current, $previous); | |
return "The cron list has changed. Differences: \n" . implode("\n", $differences); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment