You are here

function ModuleBuilderModuleGenerationTestCase::testModuleGeneration in Module Builder 8.3

Test generating module code.

File

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

Class

ModuleBuilderModuleGenerationTestCase
Test case for Module Builder module generation.

Code

function testModuleGeneration() {
  \DrupalCodeBuilder\Factory::setEnvironmentClass('TestsSampleLocation');

  // Get the hooks directory. WHY?
  $hooks_directory = \DrupalCodeBuilder\Factory::getEnvironment()
    ->getHooksDirectory();
  $mb_task_handler_report = \DrupalCodeBuilder\Factory::getTask('ReportHookData');
  $this
    ->assertTrue(is_object($mb_task_handler_report), "A task handler object was returned.");
  $hook_groups = $mb_task_handler_report
    ->listHookData();
  $this
    ->assertTrue(is_array($hook_groups) || !empty($hook_groups), "An non-empty array of hook data was returned.");

  // READY TO GO! MAKE SOME MODULES!
  $mb_task_handler_generate = \DrupalCodeBuilder\Factory::getTask('Generate', 'module');
  $this
    ->assertTrue(is_object($mb_task_handler_generate), "A task handler object was returned.");

  // Assemble module data.
  $module_name = $this
    ->randomName();
  $module_data = array(
    'base' => 'module',
    'root_name' => $module_name,
    'readable_name' => $this
      ->randomString(),
    'short_description' => $this
      ->randomString(),
    'hooks' => array(
      // These two hooks will go in the .module file.
      'hook_menu',
      'hook_node_info',
      // This goes in a tokens.inc file, and also has complex parameters.
      'hook_tokens',
      // This goes in the .install file.
      'hook_install',
    ),
    'requested_build' => array(
      'all' => TRUE,
    ),
  );
  $files = $this
    ->generateModuleFiles($module_data);
  $this
    ->assertTrue(isset($files["{$module_name}.module"]), "The files list has a .module file.");
  $this
    ->assertTrue(isset($files["{$module_name}.install"]), "The files list has a .install file.");
  $this
    ->assertTrue(isset($files["{$module_name}.info"]), "The files list has a .info file.");

  // Check the .module file.
  $module_file = $files["{$module_name}.module"];

  //debug($module_file);
  $this
    ->assertWellFormedPHP($module_file, "Module file parses as well-formed PHP.");
  $this
    ->assertFileHeader($module_file, "The module file contains the correct PHP open tag and file doc header");
  $this
    ->assertHookDocblock($module_file, 'hook_menu', "The module file contains the docblock for hook_menu().");
  $this
    ->assertHookImplementation($module_file, 'hook_menu', $module_name, "The module file contains a function declaration that implements hook_menu().");
  $this
    ->assertHookDocblock($module_file, 'hook_node_info', "The module file contains the docblock for hook_node_info().");
  $this
    ->assertHookImplementation($module_file, 'hook_node_info', $module_name, "The module file contains a function declaration that implements hook_node_info().");
  $this
    ->assertNoHookDocblock($module_file, 'hook_install', "The module file does not containt the docblock for hook_install().");

  // Check the .install file.
  $install_file = $files["{$module_name}.install"];
  $this
    ->assertWellFormedPHP($install_file, "Install file parses as well-formed PHP.");
  $this
    ->assertFileHeader($install_file, "The install file contains the correct PHP open tag and file doc header");
  $this
    ->assertHookDocblock($install_file, 'hook_install', "The install file contains the docblock for hook_install().");
  $this
    ->assertHookImplementation($install_file, 'hook_install', $module_name, "The instal file contains a function declaration that implements hook_install().");
  $this
    ->assertNoHookDocblock($install_file, 'hook_menu', "The install file does not contain the docblock for hook_menu().");
  $this
    ->assertNoHookDocblock($install_file, 'hook_node_info', "The install file does not contain the docblock for hook_node_info().");

  // Check the .tokens.inc file.
  $tokens_file = $files["{$module_name}.tokens.inc"];
  $this
    ->assertHookDocblock($tokens_file, 'hook_tokens', "The tokens file contains the docblock for hook_tokens().");
  $this
    ->assertHookImplementation($tokens_file, 'hook_tokens', $module_name, "The tokens file contains a function declaration that implements hook_tokens().");

  // Check the .info file.
  $info_file = $files["{$module_name}.info"];
  $this
    ->assertInfoLine($info_file, 'name', $module_data['readable_name'], "The info file declares the module name.");
  $this
    ->assertInfoLine($info_file, 'description', $module_data['short_description'], "The info file declares the module description.");
  $this
    ->assertInfoLine($info_file, 'core', "7.x", "The info file declares the core version.");

  // Create a module, specifying limited build.
  // It is crucial to create a new module name, as we eval() the generated
  // code!
  $module_name = $this
    ->randomName();
  $module_data = array(
    'base' => 'module',
    'root_name' => $module_name,
    'readable_name' => $this
      ->randomString(),
    'short_description' => $this
      ->randomString(),
    'hooks' => array(
      // These two hooks will go in the .module file.
      'hook_menu',
      'hook_node_info',
      // This goes in a tokens.inc file, and also has complex parameters.
      'hook_tokens',
      // This goes in the .install file.
      'hook_install',
    ),
    'requested_build' => array(
      'install' => TRUE,
    ),
  );
  $files = $this
    ->generateModuleFiles($module_data);
  $this
    ->assertEqual(count($files), 1, "Only one file is returned.");

  // Check the .install file.
  $install_file = $files["{$module_name}.install"];
  $this
    ->assertWellFormedPHP($install_file, "Install file parses as well-formed PHP.");
  $this
    ->assertFileHeader($install_file, "The install file contains the correct PHP open tag and file doc header");
  $this
    ->assertHookDocblock($install_file, 'hook_install', "The install file contains the docblock for hook_install().");
  $this
    ->assertHookImplementation($install_file, 'hook_install', $module_name, "The instal file contains a function declaration that implements hook_install().");
}