[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/tests/tests/api/ -> output.php (source)

   1  <?php
   2  
   3  /**
   4   * Main API Output tests (content and headers sent)
   5   *
   6   * @group API
   7   * @since 0.1
   8   */
   9  class API_Output_Tests extends PHPUnit\Framework\TestCase {
  10  
  11      protected $header_content;
  12  
  13      protected $header_status;
  14  
  15      protected function tearDown(): void {
  16          yourls_remove_all_actions( 'content_type_header' );
  17          yourls_remove_all_actions( 'status_header' );
  18      }
  19  
  20      /**
  21       * Check that yourls_api_output selectively echoes or returns content
  22       *
  23       * @since 0.1
  24       */
  25      public function test_output_return_or_echo() {
  26          $content = array( 'simple' => 'test' );
  27  
  28          // return
  29          $this->assertSame( 'test', yourls_api_output( 'simple', $content, false, false ) );
  30          // echo
  31          $this->expectOutputString( 'test' );
  32          yourls_api_output( 'simple', $content, false );
  33      }
  34  
  35      public function get_content_header( $args ) {
  36          $this->header_content = $args[0];
  37      }
  38  
  39      public function get_status_header( $args ) {
  40          $this->header_status = $args[0];
  41      }
  42  
  43      /**
  44       * Provide API output mode and the associated expected content-type header
  45       */
  46      public function API_type() {
  47          return array(
  48              array( 'xml',      'application/xml' ),
  49              array( 'json',     'application/json' ),
  50              array( 'jsonp',    'application/javascript' ),
  51              array( 'simple',   'text/plain' ),
  52              array( rand_str(), 'text/plain' ),
  53          );
  54      }
  55  
  56      /**
  57       * Check that yourls_api_output selectively sends expected content-type headers
  58       *
  59       * @dataProvider API_type
  60       * @since 0.1
  61       */
  62      public function test_output_content_headers( $type, $expected_header ) {
  63          yourls_add_action( 'content_type_header', array( $this, 'get_content_header' ) );
  64          yourls_add_action( 'status_header', array( $this, 'get_status_header' ) );
  65  
  66          $content = array( 'hello' => 'test' );
  67          $result  = yourls_api_output( $type, $content, true, false );
  68  
  69          $this->assertSame( $expected_header, $this->header_content );
  70      }
  71  
  72      /**
  73       * Provide mocked API output and the expected status codes
  74       */
  75      public function status_code() {
  76          $success_code = mt_rand( 1, 10 );
  77          $error_code   = mt_rand( 1, 10 );
  78  
  79          $content_success = array( 'statusCode'    => $success_code );
  80          $content_error   = array( 'errorCode'     => $error_code );
  81          $content_random  = array( 'thereIsNoCode' => rand_str() );
  82  
  83          return array(
  84              array( $content_success, $success_code ),
  85              array( $content_error,   $error_code ),
  86              array( $content_random,  200 ),
  87          );
  88      }
  89  
  90      /**
  91       * Check that yourls_api_output status header
  92       *
  93       * @dataProvider status_code
  94       * @since 0.1
  95       */
  96      public function test_output_status_headers( $content, $expected_status ) {
  97          yourls_add_action( 'status_header', array( $this, 'get_status_header' ) );
  98  
  99          $result  = yourls_api_output( 'simple', $content, true, false );
 100  
 101          $this->assertSame( $expected_status, $this->header_status );
 102      }
 103  
 104  }


Generated: Wed Sep 28 05:10:02 2022 Cross-referenced by PHPXref 0.7.1