[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/tests/data/scripts/ -> run-scenario.php (source)

   1  <?php
   2  /**
   3   * Boot YOURLS in a fresh PHP process, run a scenario, and report on constants and included files.
   4   *
   5   * Some behaviors cannot be checked from within the test suite process: constants cannot be
   6   * undefined or redefined, and files loaded with require_once are only ever loaded once, so
   7   * whatever the first test to trigger them decided is true for the whole run. This script boots a
   8   * complete YOURLS in a new process, with arbitrary constants defined *before* bootstrap, runs a
   9   * scenario file, and reports what ended up defined and loaded.
  10   *
  11   * Scenarios are plain PHP files in scripts/scenarios/, executed once YOURLS is fully booted.
  12   *
  13   * Usage:   php run-scenario.php <scenario> [<constants as a JSON object>]
  14   * Example: php run-scenario.php maybe-require-auth '{"YOURLS_PRIVATE":false}'
  15   *
  16   * Outputs one line of JSON:
  17   *   {"scenario":"...", "constants":{"YOURLS_PRIVATE":false,...}, "included":["includes/....php",...]}
  18   * where "constants" holds every defined YOURLS_* constant and "included" every loaded file below
  19   * YOURLS_ABSPATH, as a path relative to it, in loading order.
  20   *
  21   * The test suite side of this is tests/includes/new-process.php (NewProcessTrait).
  22   */
  23  
  24  // Scenario to run: a file name in scenarios/, no path allowed
  25  $scenario      = isset( $argv[1] ) ? basename( $argv[1], '.php' ) : '';
  26  $scenario_file = __DIR__ . '/scenarios/' . $scenario . '.php';
  27  if( $scenario === '' || !is_readable( $scenario_file ) ) {
  28      fwrite( STDERR, sprintf( "Unknown scenario: '%s'\n", $scenario ) );
  29      exit( 1 );
  30  }
  31  
  32  // Constants to define before YOURLS boots, so that config and core see them
  33  $constants = isset( $argv[2] ) ? json_decode( $argv[2], true ) : array();
  34  if( !is_array( $constants ) ) {
  35      fwrite( STDERR, sprintf( "Could not decode constants: %s\n", $argv[2] ) );
  36      exit( 1 );
  37  }
  38  foreach( $constants as $name => $value ) {
  39      define( $name, $value );
  40  }
  41  
  42  // Same config file and same bootstrap as the test suite. Unlike tests/bootstrap.php we do read
  43  // from the database: tables are expected to be already installed by the test suite we're run from.
  44  require_once dirname( __DIR__, 2 ) . '/includes/boot.php';
  45  yut_boot_yourls();
  46  
  47  // Safety net: whatever a scenario does, it must never rewrite the config file of the test suite
  48  yourls_add_filter( 'skip_password_hashing', 'yourls_return_true' );
  49  
  50  require $scenario_file;
  51  
  52  // Report every YOURLS_* constant...
  53  $constants = array_filter(
  54      get_defined_constants( true )['user'],
  55      function( $name ) { return str_starts_with( $name, 'YOURLS_' ); },
  56      ARRAY_FILTER_USE_KEY
  57  );
  58  
  59  // ...and every included file below YOURLS_ABSPATH, relative to it
  60  $root     = rtrim( str_replace( '\\', '/', YOURLS_ABSPATH ), '/' ) . '/';
  61  $included = array();
  62  foreach( get_included_files() as $file ) {
  63      $file = str_replace( '\\', '/', $file );
  64      if( str_starts_with( $file, $root ) ) {
  65          $included[] = substr( $file, strlen( $root ) );
  66      }
  67  }
  68  
  69  echo json_encode( array(
  70      'scenario'  => $scenario,
  71      'constants' => $constants,
  72      'included'  => $included,
  73  ), JSON_UNESCAPED_SLASHES );


Generated: Sat Aug 8 05:10:51 2026 Cross-referenced by PHPXref 0.7.1