View source
<?php
class MoopapiWebTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Moopapi tests',
'description' => 'Tests for Moopapi interface',
'group' => 'Moopapi',
'dependencies' => array(
'autoload',
),
);
}
public function setUp() {
parent::setUp('autoload', '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) {
case 'lowercase':
$app = 'exampleapp';
$decorators = array(
'testdecorator',
);
break;
case 'uppercase':
$app = 'EXAMPLEAPP';
$decorators = array(
'TESTDECORATOR',
);
break;
case 'camelcase':
$app = 'ExampleApp';
$decorators = array(
'TestDecorator',
);
break;
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;
}
static $classes;
$classes = array();
$result = moopapi_register($app, $decorators);
$result_string = print_r($result, TRUE);
$this
->pass("{$use_case}: Got following result: {$result_string}");
if (in_array($use_case, array(
'lowercase',
'uppercase',
'camelcase',
'decorators_null',
))) {
$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 {
$this
->assertTrue(empty($classes), "{$use_case}: Register should be empty");
}
unset($result);
$classes = array();
}
}
public function testWrap() {
$use_cases = array(
'lowercase',
'uppercase',
'camelcase',
'decorators_null',
);
foreach ($use_cases as $use_case) {
switch ($use_case) {
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;
case 'decorators_null':
$app = 'ExampleApp';
$method = 'exampleMethod';
$decorators = array();
break;
case 'app_absent':
break;
case 'class_absent':
break;
case 'method_absent':
break;
}
static $objects;
$objects = array();
moopapi_wrap($app, $method, $decorators);
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}");
}
unset($result);
$objects = array();
}
}
}
class ExampleApp {
public function exampleMethod() {
return 'ExampleApp';
}
}
class ExampleAppTestDecorator {
public function exampleMethod() {
return 'TestDecorator';
}
}