[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/tests/includes/ -> new-process.php (source)

   1  <?php
   2  
   3  /**
   4   * Helper to run YOURLS in a fresh PHP process from within a test.
   5   *
   6   * Use this when what's being tested can only happen once per process: a constant being defined
   7   * (or not), a file being loaded with require_once, a bootstrap step behaving differently
   8   * depending on a config constant.
   9   *
  10   * The scenario is a file in tests/data/scripts/scenarios/, run once YOURLS is fully booted with
  11   * the given constants defined beforehand. See tests/data/scripts/run-scenario.php.
  12   */
  13  trait NewProcessTrait {
  14  
  15      /**
  16       * Boot YOURLS in a new PHP process, run a scenario, and return its report
  17       *
  18       * @param string $scenario   Scenario name, ie file scenarios/<name>.php
  19       * @param array  $constants  Constants to define before YOURLS boots, as $name => $value
  20       * @return array             Report with keys 'scenario', 'constants' (defined YOURLS_* constants,
  21       *                           as $name => $value) and 'included' (files loaded, relative to YOURLS_ABSPATH)
  22       */
  23      protected function run_in_new_process( $scenario, $constants = array() ) {
  24          if( !defined( 'YOURLS_PHP_BIN' ) ) {
  25              $this->markTestSkipped( 'No PHP binary defined -- cannot run YOURLS in a new process' );
  26          }
  27  
  28          $cmd = sprintf( '%s %s %s %s',
  29              YOURLS_PHP_BIN,
  30              escapeshellarg( YOURLS_TESTDATA_DIR . '/scripts/run-scenario.php' ),
  31              escapeshellarg( $scenario ),
  32              escapeshellarg( json_encode( $constants ) )
  33          );
  34  
  35          exec( $cmd, $output, $return );
  36          $output = implode( "\n", $output );
  37  
  38          $this->assertSame( 0, $return, sprintf( "Scenario '%s' failed: %s\n%s", $scenario, $cmd, $output ) );
  39  
  40          // The report is the last line: anything a scenario echoes before it is left alone
  41          $report = json_decode( substr( strrchr( "\n" . $output, "\n" ), 1 ), true );
  42  
  43          $this->assertIsArray( $report, sprintf( "Unexpected output for scenario '%s':\n%s", $scenario, $output ) );
  44  
  45          return $report;
  46      }
  47  
  48  }


Generated: Mon Aug 10 05:11:16 2026 Cross-referenced by PHPXref 0.7.1