Module testing coverage 100%

This commit is contained in:
Joshua Sherman 2014-01-17 15:30:58 -05:00
parent 76d3c7cdc4
commit 9cc466bcd3
4 changed files with 129 additions and 71 deletions

View file

@ -30,29 +30,26 @@ class Module extends Object
/** /**
* Page Title * Page Title
* *
* @access protected * @var string, null by default
* @var string, null by default * @todo Abandon for $this->meta
* @todo Move to public scope, then abandon for $this->meta
*/ */
protected $title = null; public $title = null;
/** /**
* Meta Description * Meta Description
* *
* @access protected * @var string, null by default
* @var string, null by default * @todo Abandon for $this->meta
* @todo Move to public scope, then abandon for $this->meta
*/ */
protected $description = null; public $description = null;
/** /**
* Meta Keywords (comma separated) * Meta Keywords (comma separated)
* *
* @access protected * @var string, null by default
* @var string, null by default * @todo Abandon for $this->meta
* @todo Move to public scope, then abandon for $this->meta
*/ */
protected $keywords = null; public $keywords = null;
/** /**
* Meta Data * Meta Data
@ -77,11 +74,9 @@ class Module extends Object
/** /**
* Security Settings * Security Settings
* *
* @access protected * @var boolean, null by default
* @var boolean, null by default
* @todo Move to public scope
*/ */
protected $security = null; public $security = null;
/** /**
* AJAX * AJAX
@ -90,36 +85,30 @@ class Module extends Object
* errors should be returned as JSON or if it should use the Error class * errors should be returned as JSON or if it should use the Error class
* which can be interrogated from within a template. * which can be interrogated from within a template.
* *
* @access protected * @var boolean, false (not AJAX) by default
* @var boolean, false (not AJAX) by default * @todo Doesn't seem to be in use, but I have it defined on Clipinary
* @todo Move to public scope * don't want to remove until I drop it else it would end up in the
* @todo Doesn't seem to be in use, but I have it defined on Clipinary * module return array.
* don't want to remove until I drop it else it would end up in the
* module return array.
*/ */
protected $ajax = false; public $ajax = false;
/** /**
* Method * Method
* *
* Request methods that are allowed to access the module. * Request methods that are allowed to access the module.
* *
* @access protected * @var string or array, null by default
* @var string or array, null by default
* @todo Move to public scope
*/ */
protected $method = null; public $method = null;
/** /**
* Validate * Validate
* *
* Variables to validate. * Variables to validate.
* *
* @access protected * @var array
* @var array, null by default
* @todo Move to public scope
*/ */
protected $validate = null; public $validate = [];
/** /**
* Template * Template
@ -128,8 +117,7 @@ class Module extends Object
* 'template' return type in the Display class. Parent templates are found * 'template' return type in the Display class. Parent templates are found
* in ./templates/__shared and use the phtml extension. * in ./templates/__shared and use the phtml extension.
* *
* @access protected * @var string, 'index' by default
* @var string, 'index' by default
*/ */
public $template = 'index'; public $template = 'index';
@ -141,12 +129,11 @@ class Module extends Object
* cannot get the variable unless you reference the return array explicitly * cannot get the variable unless you reference the return array explicitly
* $this->return['variable'] * $this->return['variable']
* *
* @access protected * @var array
* @var array * @todo Rename __return so it's kinda obscured
* @todo Move to public scope and rename __return so it's kinda obscured * @todo Will need to update leaderbin and sndcrd to use new variable
* @todo Will need to update leaderbin and sndcrd to use new variable
*/ */
protected $return = []; public $return = [];
/** /**
* Output * Output
@ -205,51 +192,36 @@ class Module extends Object
/** /**
* Magic Setter Method * Magic Setter Method
* *
* Places the variables that are being modified in the return array that is * Places undefined properties into the return array as part of the
* returned if nothing is returned by the module itself. This also prohibits * module's payload.
* the direct modification of module variables which could cause issues.
* *
* @param string $name name of the variable to be set * @param string $name name of the variable to be set
* @param mixed $value value of the variable to be set * @param mixed $value value of the variable to be set
* @todo Ditch the $name check once everything is public
*/ */
public function __set($name, $value) public function __set($name, $value)
{ {
if ($name == 'method') $this->return[$name] = $value;
{
$this->method = $value;
}
else
{
$this->return[$name] = $value;
}
} }
/** /**
* Magic Getter Method * Magic Getter Method
* *
* Attempts to load the module variable. If it's not set, will attempt to * Any variables not defined in this class are set in the return array and
* load from the config. * default to false if not defined there.
* *
* @param string $name name of the variable requested * @param string $name name of the variable requested
* @return mixed value of the variable or boolean false * @return mixed value of the variable or boolean false
* @todo Unsure how necessary this will be moving forward, ideally would like to delete entirely
*/ */
public function __get($name) public function __get($name)
{ {
if (!isset($this->$name)) if (!isset($this->return[$name]))
{ {
if (isset($this->config->pickles[$name])) return false;
{ }
$this->$name = $this->config->pickles[$name]; else
} {
else return $this->return[$name];
{
$this->$name = false;
}
} }
return $this->$name;
} }
/** /**
@ -266,7 +238,7 @@ class Module extends Object
{ {
$errors = []; $errors = [];
if ($this->validate !== false) if ($this->validate)
{ {
if (is_array($this->method)) if (is_array($this->method))
{ {
@ -275,14 +247,22 @@ class Module extends Object
switch (strtoupper($this->method)) switch (strtoupper($this->method))
{ {
case 'GET': $global = &$_GET; break; case 'GET':
case 'POST': $global = &$_POST; break; $global = &$_GET;
default: $global = &$_REQUEST; break; break;
case 'POST':
$global = &$_POST;
break;
default:
$global = &$_REQUEST;
break;
} }
foreach ($this->validate as $variable => $rules) foreach ($this->validate as $variable => $rules)
{ {
if (!is_array($rules)) if (!is_array($rules) && $rules !== true)
{ {
$variable = $rules; $variable = $rules;
$rules = true; $rules = true;

View file

@ -1,6 +1,14 @@
<?php <?php
set_exit_overload(function(){ return false; }); set_exit_overload(function($status = false)
{
if ($status)
{
echo $status;
}
return false;
});
ob_start(); ob_start();
@session_start(); @session_start();

View file

@ -274,7 +274,7 @@ class ControllerTest extends PHPUnit_Framework_TestCase
setUpRequest('validationerrors'); setUpRequest('validationerrors');
$module = '<?php class validationerrors extends Module { ' $module = '<?php class validationerrors extends Module { '
. 'protected $validate = ["test"];' . 'public $validate = ["test"];'
. 'public function __default() { return ["foo" => "bar"]; }' . 'public function __default() { return ["foo" => "bar"]; }'
. '} ?>'; . '} ?>';

View file

@ -0,0 +1,70 @@
<?php
$_POST['field2'] = 'short';
$_GET['field2'] = 'short';
$_REQUEST['field2'] = 'short';
class MockParentModule extends Module
{
public $validate = [
'field1',
'field2' => [
'length:<:10' => 'Too short',
'length:>:50' => 'Too long',
],
];
}
class MockChildModule extends MockParentModule
{
public $method = ['POST', 'GET'];
}
class ModuleTest extends PHPUnit_Framework_TestCase
{
public function testAutoRun()
{
$this->assertInstanceOf('Module', new Module(true));
}
public function testAutoRunParentError()
{
$this->expectOutputString('Errors encountered, this is a @todo for form validation when calling modules from inside of modules');
$model = new MockChildModule(true);
}
public function testSetGetReturn()
{
$module = new Module();
$module->foo = 'bar';
$this->assertEquals('bar', $module->foo);
}
public function testGetMissing()
{
$module = new Module();
$this->assertFalse($module->missing);
}
public function testValidateGet()
{
$module = new MockParentModule();
$module->method = 'GET';
$this->assertEquals(['The field1 field is required.', 'Too long'], $module->__validate());
}
public function testValidatePost()
{
$module = new MockParentModule();
$this->assertEquals(['The field1 field is required.', 'Too long'], $module->__validate());
}
public function testValidateRequest()
{
$module = new MockParentModule();
$module->method = null;
$this->assertEquals(['The field1 field is required.', 'Too long'], $module->__validate());
}
}
?>