You are here

public function MoopapiWebTestCase::testRegister in Module Object Oriented Programming API 7.2

Same name and namespace in other branches
  1. 6.2 tests/moopapi.test \MoopapiWebTestCase::testRegister()
  2. 6 tests/moopapi.test \MoopapiWebTestCase::testRegister()
  3. 7 tests/moopapi.test \MoopapiWebTestCase::testRegister()

File

tests/moopapi.test, line 55

Class

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

Code

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