You are here

moopapi.test in Module Object Oriented Programming API 7.2

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

File

tests/moopapi.test
View source
<?php

/**
 * Tests for Moopapi module.
 */

/**
 * Tests that Patchwork library is available, installed and
 * works properly and tests Moopapi interface.
 */
class MoopapiWebTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Moopapi tests',
      'description' => 'Tests that Patchwork library is available, installed and works properly and tests Moopapi interface.',
      'group' => 'Moopapi',
    );
  }
  public function setUp() {
    parent::setUp(array(
      'moopapi',
    ));

    // Turn on Patchwork interception.
    moopapi_boot();
  }
  public function testMonkeyPatch() {
    $library = libraries_detect('patchwork');

    // Check Patchwork library.
    if ($this
      ->assertTrue($library, 'Patchwork library is detected')) {
      if ($this
        ->assertTrue(is_array($library) && !empty($library['installed']), 'Patchwork library is installed')) {
        $library = libraries_load('patchwork');
        $this
          ->assertTrue(!empty($library['loaded']), 'Patchwork library is loaded');
      }
      else {
        $message = 'Error while installing Patchwork library';
        if (!empty($library['error'])) {
          $message = 'Library Patchwork is ' . $library['error'];
        }
        if (!empty($library['error message'])) {
          $message .= ': ' . $library['error message'];
        }
        $this
          ->fail($message);
      }
    }

    // Only now we include necessary file to make Patchwork
    // interception work.
    module_load_include('inc', 'moopapi', 'tests/monkey_patch');
    Patchwork\replace("doesMonkeyPatchWork", function () {
      return TRUE;
    });
    $this
      ->assertTrue(doesMonkeyPatchWork(), 'Monkey patching works');
  }
  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();
      module_load_include('inc', 'moopapi', 'tests/monkey_patch');

      // 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();
    }
  }

}

Classes

Namesort descending Description
MoopapiWebTestCase Tests that Patchwork library is available, installed and works properly and tests Moopapi interface.