You are here

function PatternsTestCase::callbackOnDir in Patterns 7.2

Same name and namespace in other branches
  1. 7 tests/patterns.test \PatternsTestCase::callbackOnDir()

Loads all the pattern files from a directory and executes a callback on each parsed pattern.

The parser is chosen based on the extension of the files in the folder.

The directory must be readable.

Parameters

$mixed $dir The directory in which looking for: patterns files

$mixed $callback The callback function to be: passed to call_user_func. The argument of the callback is the array representation of a pattern, as loaded by patterns_parser_load()

3 calls to PatternsTestCase::callbackOnDir()
PatternsParserTestCase::testInvalidP in tests/parser/parser.test
PatternsParserTestCase::testvalidP in tests/parser/parser.test
PatternsParserTestCase::testValidSyntaxP in tests/parser/parser.test

File

tests/patterns.test, line 189
The base of the Patterns Component tests.

Class

PatternsTestCase
Abstract base class for testing pattern component behavior.

Code

function callbackOnDir($dir, $callback) {
  if (empty($callback)) {
    $this
      ->error(t('No callback passed to runPatternsFromDir.'));
    return FALSE;
  }
  if (!is_callable($callback, TRUE)) {
    $this
      ->error(t('Callback to runPatternsFromDir is not callable.'));
    return FALSE;
  }
  if (!file_exists($dir) || !is_readable($dir)) {
    $this
      ->error(t('Directory not found or not readable: ' . $dir));
    return FALSE;
  }
  if (!($handle = opendir($dir))) {
    $this
      ->error(t('Error opening directory') . ' ' . $dir);
    return FALSE;
  }
  while (false !== ($entry = readdir($handle))) {
    $path_parts = pathinfo($entry);
    $format = $path_parts['extension'];
    if (!patterns_parser_exists($format)) {
      continue;
    }
    $pattern = patterns_parser_load($dir . $entry, $format);
    if (empty($pattern)) {
      $this
        ->fail(t('Error while loading') . ' ' . $entry);
      continue;
    }
    call_user_func($callback, $pattern);
  }
  closedir($handle);
}