Skip to content

Commit

Permalink
New. PHPUnit. Tests if functions called by cron tasks exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandergull committed Sep 14, 2023
1 parent 4d7d2bb commit 232caa8
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/Common/SpbcCronTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Common;

use PHPUnit\Framework\TestCase;

class SpbcCronTest extends TestCase
{
public function testCronTaskMethodsExist()
{

$files_dir = str_replace('\tests\Common', '', __DIR__) . '\\';
function get_files_with_cron_call($dir, $recursive = true, $include_folders = false)
{
if (!is_dir($dir) ||
(is_dir($dir) && preg_match('/tests|vendor|css|js|backups/', $dir))
) {
return array();
}

$files = array();
$dir = rtrim($dir, '/\\');

foreach (glob("$dir/{,.}[!.,!..]*", GLOB_BRACE) as $file) {
if ( is_dir($file) ) {
if ( $include_folders ) {
$files[] = $file;
}
if ( $recursive ) {
$files = array_merge($files, call_user_func(__FUNCTION__, $file, $recursive, $include_folders));
}
} elseif (strpos($file, '.php') !== false && $content = filesize($file) > 0) {
$content = file_get_contents($file);
if ($content) {
if (strpos($content, 'addTask') !== false) {
$files[] = $file;
}
}
}
}

return $files;
}

$files_with_cron = get_files_with_cron_call($files_dir);

foreach ($files_with_cron as $file) {
$content = @file_get_contents($file);
if ($content) {
preg_match_all('/Cron::addTask\(\'.*\',.?\'(.*)\',.*\);/m', $content, $matches);
}
}

foreach ($matches[1] as $call) {
$this->assertTrue(function_exists($call), 'Cannot find function ' . $call);
}
}
}

0 comments on commit 232caa8

Please sign in to comment.