You are here

moopapi.test in Module Object Oriented Programming API 7

Same filename and directory in other branches
  1. 6.2 tests/moopapi.test
  2. 6 tests/moopapi.test
  3. 7.2 tests/moopapi.test

File

tests/moopapi.test
View source
<?php

/**
 * Tests for Moopapi module.
 */

/**
 * Tests for Moopapi interface.
 */
class MoopapiWebTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Moopapi tests',
      'description' => 'Tests for Moopapi interface',
      'group' => 'Moopapi',
    );
  }
  public function setUp() {
    parent::setUp(array(
      'moopapi',
    ));
  }
  public function testRegister() {
    $use_cases = array(
      'lowercase',
      'uppercase',
      'camelcase',
      'app_null',
      'decorators_null',
      'both_null',
    );
    foreach ($use_cases as $use_case) {
      switch ($use_case) {

        // Formatting.
        case 'lowercase':
          $app = 'exampleapp';
          $decorators = array(
            'testdecorator',
          );
          break;
        case 'uppercase':
          $app = 'EXAMPLEAPP';
          $decorators = array(
            'TESTDECORATOR',
          );
          break;
        case 'camelcase':
          $app = 'ExampleApp';
          $decorators = array(
            'TestDecorator',
          );
          break;

        // Business logic.
        case 'app_null':
          $app = NULL;
          $decorators = array(
            'TestDecorator',
          );
          break;
        case 'decorators_null':
          $app = 'ExampleApp';
          $decorators = array();
          break;
        case 'both_null':
          $app = NULL;
          $decorators = array();
          break;
      }

      // Prepare workspace.
      static $classes;
      $classes = array();

      // Test interface function.
      $result = moopapi_register($app, $decorators);
      $result_string = print_r($result, TRUE);
      $this
        ->pass("{$use_case}: Got following result: {$result_string}");

      // Check result.
      if (in_array($use_case, array(
        'lowercase',
        'uppercase',
        'camelcase',
        'decorators_null',
      ))) {

        // For formatting and decorators_null use cases.
        $app_capitalized_first = ucfirst(strtolower($app));
        if ($this
          ->assertTrue(isset($result[$app_capitalized_first]), "{$use_case}: Application is registered")) {
          if ($use_case == 'decorators_null') {
            $result[$app_capitalized_first] = (array) $result[$app_capitalized_first];
          }
          $decorator_expected = reset($decorators);
          $decorator_real = reset($result[$app_capitalized_first]);
          $this
            ->assertTrue(strcasecmp($decorator_real, $decorator_expected) == 0, "{$use_case}: Application is decorated (expected decorator {$decorator_expected} equals real {$decorator_real})");
        }
      }
      else {

        // Business logic use cases except decorators_null.
        $this
          ->assertTrue(empty($classes), "{$use_case}: Register should be empty");
      }

      // Clear workspace.
      unset($result);
      $classes = array();
    }
  }
  public function testWrap() {
    $use_cases = array(
      'lowercase',
      'uppercase',
      'camelcase',
      'decorators_null',
    );
    foreach ($use_cases as $use_case) {
      switch ($use_case) {

        // Formatting.
        case 'lowercase':
          $app = 'exampleapp';
          $method = 'examplemethod';
          $decorators = array(
            'testdecorator',
          );
          break;
        case 'uppercase':
          $app = 'EXAMPLEAPP';
          $method = 'EXAMPLEMETHOD';
          $decorators = array(
            'TESTDECORATOR',
          );
          break;
        case 'camelcase':
          $app = 'ExampleApp';
          $method = 'ExampleMethod';
          $decorators = array(
            'TestDecorator',
          );
          break;

        // Business logic.
        case 'decorators_null':
          $app = 'ExampleApp';
          $method = 'exampleMethod';
          $decorators = array();
          break;
        case 'app_absent':

          // @todo Moopapi::absent(): Implement app_absent use case.
          break;
        case 'class_absent':

          // @todo Moopapi::absent(): Implement class_absent use case.
          break;
        case 'method_absent':

          // @todo Moopapi::absent(): Implement method_absent use case.
          break;
      }

      // Prepare workspace.
      static $objects;
      $objects = array();

      // Test interface function.
      moopapi_wrap($app, $method, $decorators);

      // Check result.
      if ($use_case === 'decorators_null') {
        $decorator = $app;
      }
      else {
        $decorator = reset($decorators);
      }
      $function_name = "{$app}_{$method}";
      if ($this
        ->assertTrue(function_exists($function_name), "{$use_case}: {$function_name} function was created")) {
        $function_result = call_user_func($function_name);
        $this
          ->assertTrue(strcasecmp($function_result, $decorator) == 0, "{$use_case}: Calling {$app}::{$method} wrapped as {$function_name} brings {$function_result}, which is the same as {$decorator}");
      }

      // Clear workspace.
      unset($result);
      $objects = array();
    }
  }

}

/**
 * It is for testing Moopapi::wrap().
 */
class ExampleApp {
  public function exampleMethod() {
    return 'ExampleApp';
  }

}
class ExampleAppTestDecorator {
  public function exampleMethod() {
    return 'TestDecorator';
  }

}

Classes

Namesort descending Description
ExampleApp It is for testing Moopapi::wrap().
ExampleAppTestDecorator
MoopapiWebTestCase Tests for Moopapi interface.