You are here

function ModuleBuilderModuleGenerationTestCase::assertFunction in Module Builder 8.3

Assert a string contains a function declaration.

Parameters

$string: The text to check for a function declaration.

$function_name: The name of the function.

$message = NULL: The assertion message.

1 call to ModuleBuilderModuleGenerationTestCase::assertFunction()
ModuleBuilderModuleGenerationTestCase::assertHookImplementation in tests/module_builder.test
Assert a string contains a hook implementation function declaration.

File

tests/module_builder.test, line 287
Contains tests for the Module builder module.

Class

ModuleBuilderModuleGenerationTestCase
Test case for Module Builder module generation.

Code

function assertFunction($string, $function_name, $message = NULL) {
  $expected_regex = "^function {$function_name}\\((.*)\\)";
  $matches = array();
  $match = preg_match("[{$expected_regex}]m", $string, $matches);
  if (empty($message)) {
    $message = "The code string contains the function {$function_name}().";
  }
  $this
    ->assertTrue($match, $message);

  // Check the function parameters are properly formed.
  if (!empty($matches[1])) {
    $parameters = explode(', ', $matches[1]);
    $param_regex = '@
        ^
        ( \\w+ \\  ) ?  # type hint
        \\$ \\w+        # parameter name
        ( \\  = \\      # default value, one of:
          (
            \\d+ |             # numerical
            [[:upper:]]+ |    # constant
            \' .* \' |        # string
            array\\(\\)         # empty array
          )
        ) ?
        @x';
    foreach ($parameters as $parameter) {
      $match = preg_match($param_regex, $parameter);
      $this
        ->assertTrue($match, "Function parameter is correctly formed.");
    }
  }
}