Skip to content

Instantly share code, notes, and snippets.

@ProxiBlue
Created December 4, 2024 01:52
Show Gist options
  • Save ProxiBlue/ac193cbf21660eec017924785ae2d209 to your computer and use it in GitHub Desktop.
Save ProxiBlue/ac193cbf21660eec017924785ae2d209 to your computer and use it in GitHub Desktop.
cron_unit_test
<?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