[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/tests/includes/ -> utils.php (source)

   1  <?php
   2  
   3  /**
   4   * Helper functions & classes for the YOURLS Unit Tests suite
   5   */
   6  
   7  /**
   8   * Return a random string (for option name, etc...)
   9   *
  10   * @since 0.1
  11   * @param string $len Optional string length
  12   * @return string Random string
  13   */
  14  function rand_str( $len=32 ) {
  15      return substr( md5( uniqid( rand() ) ), 0, $len );
  16  }
  17  
  18  /**
  19   * Check if we are running locally (someone typed 'phpunit' in a shell) or in Travis. Return true if local.
  20   *
  21   * @since 0.1
  22   */
  23  function yut_is_local() {
  24      return ! defined( 'YOURLS_TESTS_CI' ) || YOURLS_TESTS_CI === false;
  25  }
  26  
  27  /**
  28   * Dummy function to be called by hook function tests
  29   *
  30   * @since 0.1
  31   */
  32  function change_one_global() {
  33      $var_name = $GLOBALS['test_var'];
  34      $GLOBALS[ $var_name ] = rand_str();
  35  }
  36  
  37  /**
  38   * Dummy function to be called by hook function tests
  39   *
  40   * @since 0.1
  41   */
  42  function change_variable( $var ) {
  43      $var = rand_str();
  44      return $var;
  45  }
  46  
  47  /**
  48   * Dummy class & function to be called by hook function tests
  49   *
  50   * @since 0.1
  51   */
  52  class Change_One_Global {
  53      static function change_it() {
  54          $var_name = $GLOBALS['test_var'];
  55          $GLOBALS[ $var_name ] = rand_str();
  56      }
  57  }
  58  
  59  /**
  60  * Dummy class & function to be called by hook function tests
  61  *
  62  * @since 0.1
  63  */
  64  class Change_Variable {
  65      static function change_it( $var ) {
  66          return rand_str();
  67      }
  68  }
  69  
  70  /**
  71  * print() for Unit Tests
  72  */
  73  function yourls_ut_print( ...$what ) {
  74      ob_start();
  75      $count = count($what);
  76      for ($i = 0; $i < $count; $i++) {
  77          print($what[$i]);
  78      }
  79      $display = ob_get_contents();
  80      ob_end_clean();
  81  
  82      fwrite( STDERR, $display );
  83  }
  84  
  85  /**
  86  * var_dump() for Unit Tests
  87  *
  88  * @since 0.1
  89  */
  90  function yourls_ut_var_dump( ...$what ) {
  91      ob_start();
  92      $count = count($what);
  93      for ($i = 0; $i < $count; $i++) {
  94          var_dump($what[$i]); $line_of_vardump = __LINE__; // lazy: keep track of where var_dump() is called
  95      }
  96      $display = ob_get_contents();
  97      ob_end_clean();
  98  
  99      // If we have xdebug enabled, remove first line of output of each var_dump() (ie `/path/to/tests/includes/utils.php:79:`)
 100      if( ini_get('xdebug.overload_var_dump') == 2 ) {
 101          $line = __FILE__ . ':' . $line_of_vardump . ":";
 102          $line = str_replace('\\', '\\\\', $line); // escape the backslashes on Windows paths otherwise they will break the regex
 103          $display = preg_replace("/$line\n/", '', $display);
 104      }
 105  
 106      fwrite( STDERR, $display );
 107  }
 108  
 109  /**
 110   * Log in a local text file, in case you need to var_dump() stuff within a test
 111   *
 112   * By design, you cannot var_dump() stuff during a unit test. A workaround is to export into a log file.
 113   * Usage : anywhere you would have used a regular var_dump() you can simply add:
 114   * Log_in_File::log( $something );
 115   *
 116   */
 117  class Log_in_File {
 118  
 119      public static $has_logged = false;
 120  
 121  	public static function log( $what ) {
 122          // Don't mess with Travis
 123          if( !yut_is_local() )
 124              return;
 125  
 126          if( ! self::$has_logged ) {
 127              self::$has_logged = true;
 128              self::start_log();
 129          }
 130  
 131          ob_start();
 132          var_dump( $what );
 133          $what = ob_get_clean();
 134  
 135          error_log( $what."\n", 3, dirname( dirname( __FILE__ ) ) . '/log.txt' );
 136      }
 137  
 138  	public static function start_log() {
 139          self::log( "---------------- START TESTS ----------------" );
 140      }
 141  
 142  }


Generated: Tue Sep 27 05:10:01 2022 Cross-referenced by PHPXref 0.7.1