You are here

abstract class FeedsExUnitTestBase in Feeds extensible parsers 7.2

Base class for units tests.

Hierarchy

Expanded class hierarchy of FeedsExUnitTestBase

File

src/Tests/FeedsExUnitTests.test, line 11
Contains unit tests for feeds_ex.

View source
abstract class FeedsExUnitTestBase extends TUnit {

  /**
   * The module directory.
   *
   * @var string
   */
  protected $moduleDir;
  public function setUp() {
    parent::setUp();

    // Isn't this fun!
    drupal_load('module', 'feeds');
    $feeds = DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds');
    require_once $feeds . '/includes/FeedsConfigurable.inc';
    require_once $feeds . '/includes/FeedsSource.inc';
    require_once $feeds . '/includes/FeedsImporter.inc';
    require_once $feeds . '/plugins/FeedsPlugin.inc';
    require_once $feeds . '/plugins/FeedsFetcher.inc';
    require_once $feeds . '/plugins/FeedsFileFetcher.inc';
    require_once $feeds . '/plugins/FeedsParser.inc';
    require_once $feeds . '/plugins/FeedsProcessor.inc';
    require_once $feeds . '/plugins/FeedsNodeProcessor.inc';
    drupal_load('module', 'feeds_ex');
    $this->moduleDir = DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds_ex');
    require_once $this->moduleDir . '/src/Json/Utility.php';
    require_once $this->moduleDir . '/src/Xml/Utility.php';
    require_once $this->moduleDir . '/src/File/FeedsExLineIterator.php';
    require_once $this->moduleDir . '/src/FeedsExBase.inc';
  }

  /**
   * Returns a mocked FeedsSource object.
   *
   * @param string $fetcher
   *   (optional) The fetcher class. Defaults to FeedsFileFetcher
   * @param string $processor
   *   (optional) The processor class. Defaults to FeedsNodeProcessor.
   *
   * @return FeedsSource
   *   The mocked FeedsSource object,
   */
  protected function getMockFeedsSource($fetcher = 'FeedsFileFetcher', $processor = 'FeedsNodeProcessor') {
    $importer = $this
      ->newInstanceWithoutConstructor('FeedsImporter');
    $fetcher = $this
      ->newInstanceWithoutConstructor($fetcher);
    $this
      ->setProperty($importer, 'fetcher', $fetcher);
    $processor = $this
      ->newInstanceWithoutConstructor($processor);
    $this
      ->setProperty($importer, 'processor', $processor);
    $source = $this
      ->newInstanceWithoutConstructor('FeedsSource');
    $this
      ->setProperty($source, 'importer', $importer);
    return $source;
  }

  /**
   * Downloads JSONPath.
   */
  protected function downloadJsonPath() {

    // We don't use a variable_set() here since we want this to be a unit test.
    if (defined('FEEDS_EX_LIBRARY_PATH')) {
      return;
    }
    $url = 'https://jsonpath.googlecode.com/svn/trunk/src/php/jsonpath.php';
    $filename = 'jsonpath.php';

    // Avoid downloading the file dozens of times.
    $library_dir = $this->originalFileDirectory . '/simpletest/feeds_ex';
    $jsonpath_library_dir = DRUPAL_ROOT . '/' . $library_dir . '/jsonpath';
    if (!file_exists(DRUPAL_ROOT . '/' . $library_dir)) {
      drupal_mkdir(DRUPAL_ROOT . '/' . $library_dir);
    }
    if (!file_exists($jsonpath_library_dir)) {
      drupal_mkdir($jsonpath_library_dir);
    }

    // Local file name.
    $local_file = $jsonpath_library_dir . '/' . $filename;

    // Begin single threaded code.
    if (function_exists('sem_get')) {
      $semaphore = sem_get(ftok(__FILE__, 1));
      sem_acquire($semaphore);
    }

    // Download and extact the archive, but only in one thread.
    if (!file_exists($local_file)) {
      $local_file = system_retrieve_file($url, $local_file, FALSE, FILE_EXISTS_REPLACE);
    }
    if (function_exists('sem_get')) {
      sem_release($semaphore);
    }

    // Verify that the file was successfully downloaded.
    $this
      ->assertTrue(file_exists($local_file), format_string('@file found.', array(
      '@file' => $local_file,
    )));

    // Set the library directory.
    define('FEEDS_EX_LIBRARY_PATH', $library_dir);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsExUnitTestBase::$moduleDir protected property The module directory.
FeedsExUnitTestBase::downloadJsonPath protected function Downloads JSONPath.
FeedsExUnitTestBase::getMockFeedsSource protected function Returns a mocked FeedsSource object.
FeedsExUnitTestBase::setUp public function 7