View source
<?php
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',
));
moopapi_boot();
}
public function testMonkeyPatch() {
$library = libraries_detect('patchwork');
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);
}
}
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) {
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();
module_load_include('inc', 'moopapi', 'tests/monkey_patch');
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();
}
}
}