-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New. PHPUnit. Tests if functions called by cron tasks exist.
- Loading branch information
1 parent
4d7d2bb
commit 232caa8
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
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
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); | ||
} | ||
} | ||
} |