You are here

function CoderTestFile::load in Coder 7

Same name and namespace in other branches
  1. 6.2 scripts/coder_format/tests/CoderTestFile.php \CoderTestFile::load()
  2. 6 scripts/coder_format/tests/CoderTestFile.php \CoderTestFile::load()
  3. 7.2 scripts/coder_format/tests/CoderTestFile.php \CoderTestFile::load()

Loads this class from a file.

Parameters

string $filename: A filename to load.

1 call to CoderTestFile::load()
CoderTestFile::test in scripts/coder_format/tests/CoderTestFile.php
Implements SimpleExpectation::test().

File

scripts/coder_format/tests/CoderTestFile.php, line 39
Set of tests for the coder_format script.

Class

CoderTestFile
Represents coder test file for full coder_format_string_all() tests.

Code

function load($filename) {
  $this->filename = $filename;
  $fh = fopen($filename, 'r');
  $state = '';
  $unit = 0;
  while (($line = fgets($fh)) !== FALSE) {

    // Normalize newlines.
    $line = rtrim($line, "\n\r");

    // Detect INPUT and EXPECT sections.
    if (substr($line, 0, 2) == '--') {
      $state = trim($line, ' -');

      // If a new INPUT section begins, start a new unit.
      if ($state == 'INPUT') {

        // If previous section has been marked with the keyword 'ONLY', break
        // immediately to process only the marked section.
        if ($this->only[$unit]) {
          break;
        }
        $unit++;
      }
      continue;
    }

    // Process other keywords only outside of INPUT and EXPECT sections.
    if (!$state) {
      list($keyword, $line) = explode(': ', $line, 2);
    }
    else {
      $keyword = $state;
    }
    switch ($keyword) {
      case 'TEST':
        $this->test = $line;
        break;
      case 'FULL':
        $this->full = (bool) $line;
        break;
      case 'INPUT':
        $this->input[$unit] .= $line . "\n";
        break;
      case 'EXPECT':
        $this->expect[$unit] .= $line . "\n";
        break;
      case 'ONLY':
        $this->only[$unit] = TRUE;
        break;
    }
  }
  fclose($fh);
  foreach (range(1, $unit) as $unit) {

    // If no EXPECTed code was defined, INPUT is expected.
    if (!isset($this->expect[$unit])) {
      $this->expect[$unit] = $this->input[$unit];
    }

    // If FULL was *not* defined, add a PHP header to contents.
    if (!$this->full) {
      $prepend = "<?php\n// \$" . "Id\$\n\n";
      $this->input[$unit] = $prepend . rtrim($this->input[$unit], "\n") . "\n\n";
      $this->expect[$unit] = $prepend . rtrim($this->expect[$unit], "\n") . "\n\n";
    }
  }
  if (!empty($this->only[$unit])) {
    $this->input = array(
      $this->input[$unit],
    );
    $this->expect = array(
      $this->expect[$unit],
    );
  }
}