Cleaned up unit tests

Wanted to get back to the point that the tests were running and not erroring.
This commit is contained in:
Josh Sherman 2014-09-25 23:11:29 -04:00
parent ebc5584660
commit 39d5b8d40b
6 changed files with 241 additions and 621 deletions

View file

@ -1,358 +0,0 @@
<?php
class ControllerTest extends PHPUnit_Framework_TestCase
{
private $config;
public function setUp()
{
$this->config = Config::getInstance();
$this->config->data['pickles']['disabled'] = false;
$this->config->data['pickles']['profiler'] = false;
$this->config->data['security']['levels'][10] = 'USER';
$this->config->data['security']['levels'][20] = 'ADMIN';
setUpRequest('home');
$module = '<?php class home extends Module { }';
file_put_contents(SITE_MODULE_PATH . 'home.php', $module);
}
public function testSiteDown()
{
$this->config->data['pickles']['disabled'] = true;
$this->expectOutputRegex('/Test Server is currently down for maintenance/');
new Controller();
}
public function testCustomSiteDown()
{
$this->config->data['pickles']['disabled'] = true;
file_put_contents(SITE_TEMPLATE_PATH . '__shared/maintenance.phtml', '<h1>Custom Down for Maintenance</h1>');
new Controller();
$this->expectOutputRegex('/<h1>Custom Down for Maintenance<\/h1>/');
}
public function testAttributesInURI()
{
setUpRequest('home/id:123');
new Controller();
$this->assertEquals(123, Browser::get('id'));
setUpRequest('home/id:456/foo:bar');
new Controller();
// Compensates for 2 empty template executions of the Controller
$this->expectOutputString('[][]');
$this->assertEquals(456, Browser::get('id'));
$this->assertEquals('bar', Browser::get('foo'));
}
public function testUpperCaseURI()
{
setUpRequest('TESTING');
new Controller();
$this->assertTrue(in_array('Location: /testing', xdebug_get_headers()));
}
public function testForceSecure()
{
setUpRequest('secure');
$module = '<?php class secure extends Module { public $secure = true; }';
file_put_contents(SITE_MODULE_PATH . 'secure.php', $module);
new Controller();
$this->assertTrue(in_array('Location: https://testsite.com/secure', xdebug_get_headers()));
}
public function testForceInsecure()
{
setUpRequest('insecure');
$_SERVER['HTTPS'] = 'on';
$module = '<?php class insecure extends Module { public $secure = false; }';
file_put_contents(SITE_MODULE_PATH . 'insecure.php', $module);
new Controller();
$this->assertTrue(in_array('Location: http://testsite.com/insecure', xdebug_get_headers()));
}
public function testNotAuthenticated()
{
setUpRequest('notauth');
$module = '<?php class notauth extends Module { public $security = SECURITY_LEVEL_USER; }';
file_put_contents(SITE_MODULE_PATH . 'notauth.php', $module);
new Controller();
// Compensates for an empty template due to exit() being skipped
$this->expectOutputString('[]');
$this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers()));
}
public function testSecurityArray()
{
setUpRequest('securityarray');
$module = '<?php class securityarray extends Module { public $security = [SECURITY_LEVEL_USER, SECURITY_LEVEL_ADMIN]; }';
file_put_contents(SITE_MODULE_PATH . 'securityarray.php', $module);
new Controller();
// Compensates for an empty template due to exit() being skipped
$this->expectOutputString('[]');
$this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers()));
}
public function testSecurityArrayTypeString()
{
setUpRequest('securityarraytypestring');
$module = '<?php class securityarraytypestring extends Module { public $security = ["type" => "IS", "level" => SECURITY_LEVEL_USER]; }';
file_put_contents(SITE_MODULE_PATH . 'securityarraytypestring.php', $module);
new Controller();
// Compensates for an empty template due to exit() being skipped
$this->expectOutputString('[]');
$this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers()));
}
public function testSecurityArrayTypeArray()
{
setUpRequest('securityarraytypearray');
$module = '<?php class securityarraytypearray extends Module { public $security = ["type" => "IS", "level" => [SECURITY_LEVEL_USER, SECURITY_LEVEL_ADMIN]]; }';
file_put_contents(SITE_MODULE_PATH . 'securityarraytypearray.php', $module);
new Controller();
// Compensates for an empty template due to exit() being skipped
$this->expectOutputString('[]');
$this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers()));
}
public function testSecurityArrayTypeBetween()
{
setUpRequest('securityarraytypebetween');
$module = '<?php class securityarraytypebetween extends Module { public $security = ["type" => "BETWEEN", "levels" => [SECURITY_LEVEL_USER, SECURITY_LEVEL_ADMIN]]; }';
file_put_contents(SITE_MODULE_PATH . 'securityarraytypebetween.php', $module);
new Controller();
// Compensates for an empty template due to exit() being skipped
$this->expectOutputString('[]');
$this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers()));
}
public function testSecurityArrayTypeHas()
{
setUpRequest('securityarraytypehas');
$module = '<?php class securityarraytypehas extends Module { public $security = ["type" => "HAS", "level" => SECURITY_LEVEL_USER]; }';
file_put_contents(SITE_MODULE_PATH . 'securityarraytypehas.php', $module);
new Controller();
// Compensates for an empty template due to exit() being skipped
$this->expectOutputString('[]');
$this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers()));
}
public function testNotAuthenticatedPOST()
{
setUpRequest('notauthpost', 'POST');
$module = '<?php class notauthpost extends Module { public $security = SECURITY_LEVEL_USER; }';
file_put_contents(SITE_MODULE_PATH . 'notauthpost.php', $module);
new Controller();
$this->expectOutputRegex('/You are not properly authenticated/');
}
public function testAuthenticated()
{
setUpRequest('auth');
$module = '<?php class auth extends Module { '
. 'public $security = SECURITY_LEVEL_USER;'
. 'public function __default() { return ["foo" => "bar"]; }'
. '}';
file_put_contents(SITE_MODULE_PATH . 'auth.php', $module);
Security::login(1, 10, 'USER');
new Controller();
$this->expectOutputString('{"foo":"bar"}');
}
public function testRoleDefaultMethod()
{
setUpRequest('rolemethod');
$module = '<?php class rolemethod extends Module { '
. 'public $security = SECURITY_LEVEL_USER;'
. 'public function __default() { return ["foo" => "bar"]; }'
. 'public function __default_USER() { return ["user" => "me"]; }'
. '}';
file_put_contents(SITE_MODULE_PATH . 'rolemethod.php', $module);
Security::login(1, 10, 'USER');
new Controller();
$this->expectOutputString('{"user":"me"}');
}
public function testValidRequestMethod()
{
setUpRequest('validrequestmethod');
$module = '<?php class validrequestmethod extends Module { '
. 'public $method = "GET";'
. 'public function __default() { return ["foo" => "bar"]; }'
. '}';
file_put_contents(SITE_MODULE_PATH . 'validrequestmethod.php', $module);
new Controller();
$this->expectOutputString('{"foo":"bar"}');
}
public function testInvalidRequestMethod()
{
setUpRequest('invalidrequestmethod');
$module = '<?php class invalidrequestmethod extends Module { '
. 'public $method = "POST";'
. 'public function __default() { return ["foo" => "bar"]; }'
. '}';
file_put_contents(SITE_MODULE_PATH . 'invalidrequestmethod.php', $module);
new Controller();
$this->expectOutputString('{"status":"error","message":"There was a problem with your request method."}');
}
public function testValidationErrors()
{
setUpRequest('validationerrors');
$module = '<?php class validationerrors extends Module { '
. 'public $validate = ["test"];'
. 'public function __default() { return ["foo" => "bar"]; }'
. '}';
file_put_contents(SITE_MODULE_PATH . 'validationerrors.php', $module);
new Controller();
$this->expectOutputString('{"status":"error","message":"The test field is required."}');
}
public function testError404()
{
setUpRequest('fourohfour');
new Controller();
$this->assertTrue(in_array('Status: 404 Not Found', xdebug_get_headers()));
$this->expectOutputRegex('/<h1>Not Found<\/h1>/');
}
public function testCustomError404()
{
setUpRequest('customfourohfour');
file_put_contents(SITE_TEMPLATE_PATH . '__shared/404.phtml', '<h1>Custom Not Found</h1>');
new Controller();
$this->assertTrue(in_array('Status: 404 Not Found', xdebug_get_headers()));
$this->expectOutputRegex('/<h1>Custom Not Found<\/h1>/');
}
public function testProfilerOutput()
{
$this->config->data['pickles']['profiler'] = true;
$this->expectOutputRegex('/id="pickles-profiler"/');
new Controller();
}
public function testTwoValidTemplates()
{
$this->config->data['pickles']['profiler'] = true;
setUpRequest('validtemplates');
$module = '<?php class validtemplates extends Module { }';
file_put_contents(SITE_MODULE_PATH . 'validtemplates.php', $module);
$child_template = SITE_TEMPLATE_PATH . 'validtemplates.phtml';
file_put_contents($child_template, '<div>child template</div>');
// Vim syntax highlighting borks unless ----v
$child = '<?php require $this->template; ?' . '>' . "\n";
$html = <<<HTML
<!doctype html>
<html>
<body>
<h1>parent template</h1>
{$child}
</body>
</html>
HTML;
file_put_contents(SITE_TEMPLATE_PATH . '__shared/index.phtml', $html);
new Controller();
$this->expectOutputRegex('/^<!doctype html>
<html>
<body>
<h1>parent template<\/h1>
<div>child template<\/div>
<\/body>
<\/html>.+<style>/');
}
}

View file

@ -1,132 +0,0 @@
<?php
class DisplayTest extends PHPUnit_Framework_TestCase
{
private $display, $shared_templates;
private $child_html = '<div>child template</div>';
protected function setUp()
{
parent::setUp();
$this->shared_templates = SITE_TEMPLATE_PATH . '__shared/';
if (!file_exists($this->shared_templates))
{
mkdir($this->shared_templates, 0644, true);
}
$_SERVER['REQUEST_URI'] = '/test';
$_REQUEST['request'] = 'test';
$this->display = new Display();
$this->display->module = [
'pickles' => [
'yummy' => 'gherkin',
'delish' => 'kosher dill',
'yucky' => 'bread & butter'
]
];
}
protected function tearDown()
{
unlink(SITE_TEMPLATE_PATH . 'test.phtml');
unlink($this->shared_templates . 'index.phtml');
}
public function testInvalidReturnType()
{
$this->display->return = 'invalid';
$this->assertEquals('Invalid return type.', $this->display->render());
// Gotta do this or the test will be considered "risky"
ob_end_clean();
}
public function testPHPSESSID()
{
$request_uri = $_SERVER['REQUEST_URI'];
$_SERVER['REQUEST_URI'] .= '?PHPSESSID=session_id';
$return = $this->display->render();
$this->assertTrue(in_array('Location: ' . $request_uri, xdebug_get_headers()));
$this->assertEquals('Requested URI contains PHPSESSID, redirecting.', $return);
// Gotta do this or the test will be considered "risky"
ob_end_clean();
}
public function testNoParentTemplate()
{
$child_template = SITE_TEMPLATE_PATH . 'test.phtml';
file_put_contents($child_template, $this->child_html);
$this->display->templates = [$child_template];
$this->assertEquals($this->child_html, $this->display->render());
}
public function testRenderJSON()
{
$this->assertEquals(
'{"pickles":{"yummy":"gherkin","delish":"kosher dill","yucky":"bread & butter"}}',
$this->display->render()
);
}
public function testRenderJSONPrettyPrint()
{
$_REQUEST['pretty'] = 'true';
$pretty_json = <<<JSON
{
"pickles": {
"yummy": "gherkin",
"delish": "kosher dill",
"yucky": "bread & butter"
}
}
JSON;
$this->assertEquals($pretty_json, $this->display->render());
}
public function testRenderXML()
{
$this->display->return = ['template', 'xml'];
$this->assertEquals(
'<yummy>gherkin</yummy><delish>kosher dill</delish><yucky><![CDATA[bread & butter]]></yucky>',
$this->display->render()
);
}
public function testRenderXMLPrettyPrint()
{
$_REQUEST['pretty'] = 'true';
$pretty_xml = <<<XML
<yummy>gherkin</yummy>
<delish>kosher dill</delish>
<yucky><![CDATA[bread & butter]]></yucky>
XML;
$this->display->return = ['template', 'xml'];
$this->assertEquals($pretty_xml, $this->display->render());
}
/*
public function testRenderRSS()
{
$this->fail('Not yet implemented.');
}
public function testRenderRSSPrettyPrint()
{
$this->fail('Not yet implemented.');
}
*/
}

View file

@ -1,120 +0,0 @@
<?php
class DynamicTest extends PHPUnit_Framework_TestCase
{
private $dynamic;
public static function setUpBeforeClass()
{
// Using actual filesystem because you can't chdir with vfs://
$public_path = '/tmp/pickles-fs/public/';
foreach (['css', 'images', 'js'] as $directory)
{
mkdir($public_path . $directory, 0777, true);
}
touch($public_path . 'images/image.png');
touch($public_path . 'images/invalid');
$css = <<<CSS
body
{
color: #ffcc00;
text-align: center;
}
CSS;
foreach (['css', 'less', 'scss'] as $extension)
{
file_put_contents($public_path . 'css/stylesheet.' . $extension, $css);
}
file_put_contents($public_path . 'css/alternate.css', $css);
$js = <<<JS
function foo()
{
alert('bar');
}
console.log('stuff');
JS;
file_put_contents($public_path . 'js/script.js', $js);
chdir($public_path);
}
public function setUp()
{
$this->dynamic = new Dynamic();
}
public function tearDown()
{
$minified_file = '/tmp/pickles-fs/public/css/stylesheet.min.css';
if (file_exists($minified_file))
{
unlink($minified_file);
}
}
public static function tearDownAfterClass()
{
File::removeDirectory('/tmp/pickles-fs');
}
public function testReference()
{
$this->assertRegExp(
'/^\/images\/image\.\d{10}\.png$/',
$this->dynamic->reference('/images/image.png'
));
}
/**
* @expectedException Exception
* @expectedExceptionMessage Supplied reference does not exist (/images/missing.png)
*/
public function testReferenceMissingFileWithoutFailover()
{
$this->dynamic->reference('/images/missing.png');
}
public function testReferenceMissingFileWithFailover()
{
$this->assertEquals(
'/images/failover.png',
$this->dynamic->reference('/images/missing.png', '/images/failover.png')
);
}
/**
* @expectedException Exception
* @expectedExceptionMessage Filename must have an extension (e.g. /path/to/file.png)
*/
public function testReferenceInvalidFilename()
{
$this->dynamic->reference('/images/invalid');
}
/**
* @expectedException Exception
* @expectedExceptionMessage Reference value must be absolute (e.g. /path/to/file.png)
*/
public function testReferenceNotAbsolute()
{
$this->dynamic->reference('../images/relative.png');
}
public function testReferenceWithQueryString()
{
$this->assertRegExp(
'/^\/images\/image\.\d{10}\.png\?foo=bar$/',
$this->dynamic->reference('/images/image.png?foo=bar'
));
}
}

View file

@ -4,7 +4,7 @@ $_POST['field2'] = 'short';
$_GET['field2'] = 'short';
$_REQUEST['field2'] = 'short';
class MockParentModule extends Module
class MockParentResource extends Resource
{
public $validate = [
'field1',
@ -15,53 +15,53 @@ class MockParentModule extends Module
];
}
class MockChildModule extends MockParentModule
class MockChildResource extends MockParentResource
{
public $method = ['POST', 'GET'];
}
class ModuleTest extends PHPUnit_Framework_TestCase
class ResourceTest extends PHPUnit_Framework_TestCase
{
public function testAutoRun()
{
$this->assertInstanceOf('Module', new Module(true));
$this->assertInstanceOf('Resource', new Resource(true));
}
public function testAutoRunParentError()
{
$this->expectOutputString('');
$model = new MockChildModule(true);
$model = new MockChildResource(true);
}
public function testSetGetReturn()
{
$module = new Module();
$module = new Resource();
$module->foo = 'bar';
$this->assertEquals('bar', $module->foo);
}
public function testGetMissing()
{
$module = new Module();
$module = new Resource();
$this->assertFalse($module->missing);
}
public function testValidateGet()
{
$module = new MockParentModule();
$module = new MockParentResource();
$module->method = 'GET';
$this->assertEquals(['The field1 field is required.', 'Too long'], $module->__validate());
}
public function testValidatePost()
{
$module = new MockParentModule();
$module = new MockParentResource();
$this->assertEquals(['The field1 field is required.', 'Too long'], $module->__validate());
}
public function testValidateRequest()
{
$module = new MockParentModule();
$module = new MockParentResource();
$module->method = null;
$this->assertEquals(['The field1 field is required.', 'Too long'], $module->__validate());
}

View file

@ -0,0 +1,230 @@
<?php
class RouterTest extends PHPUnit_Framework_TestCase
{
private $config;
public function setUp()
{
$this->config = Config::getInstance();
$this->config->data['pickles']['disabled'] = false;
$this->config->data['pickles']['profiler'] = false;
$this->config->data['security']['levels'][10] = 'USER';
$this->config->data['security']['levels'][20] = 'ADMIN';
setUpRequest('home');
$module = '<?php class home extends Resource { }';
file_put_contents(SITE_MODULE_PATH . 'home.php', $module);
}
public function testSiteDown()
{
$this->config->data['pickles']['disabled'] = true;
$this->expectOutputRegex('/Test Server is currently down for maintenance/');
new Router();
}
public function testCustomSiteDown()
{
$this->config->data['pickles']['disabled'] = true;
file_put_contents(SITE_TEMPLATE_PATH . '__shared/maintenance.phtml', '<h1>Custom Down for Maintenance</h1>');
new Router();
$this->expectOutputRegex('/<h1>Custom Down for Maintenance<\/h1>/');
}
public function testAttributesInURI()
{
setUpRequest('home/id:123');
new Router();
$this->assertEquals(123, Browser::get('id'));
setUpRequest('home/id:456/foo:bar');
new Router();
// Compensates for 2 empty template executions of the Router
$this->expectOutputString('[][]');
$this->assertEquals(456, Browser::get('id'));
$this->assertEquals('bar', Browser::get('foo'));
}
public function testUpperCaseURI()
{
setUpRequest('TESTING');
new Router();
$this->assertTrue(in_array('Location: /testing', xdebug_get_headers()));
}
public function testForceSecure()
{
setUpRequest('secure');
$module = '<?php class secure extends Resource { public $secure = true; }';
file_put_contents(SITE_MODULE_PATH . 'secure.php', $module);
new Router();
$this->assertTrue(in_array('Location: https://testsite.com/secure', xdebug_get_headers()));
}
public function testForceInsecure()
{
setUpRequest('insecure');
$_SERVER['HTTPS'] = 'on';
$module = '<?php class insecure extends Resource { public $secure = false; }';
file_put_contents(SITE_MODULE_PATH . 'insecure.php', $module);
new Router();
$this->assertTrue(in_array('Location: http://testsite.com/insecure', xdebug_get_headers()));
}
public function testNotAuthenticated()
{
setUpRequest('notauth');
$module = '<?php class notauth extends Resource { public $security = SECURITY_LEVEL_USER; }';
file_put_contents(SITE_MODULE_PATH . 'notauth.php', $module);
new Router();
// Compensates for an empty template due to exit() being skipped
$this->expectOutputString('[]');
$this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers()));
}
public function testValidRequestMethod()
{
setUpRequest('validrequestmethod');
$module = '<?php class validrequestmethod extends Resource { '
. 'public $method = "GET";'
. 'public function __default() { return ["foo" => "bar"]; }'
. '}';
file_put_contents(SITE_MODULE_PATH . 'validrequestmethod.php', $module);
new Router();
$this->expectOutputString('{"foo":"bar"}');
}
public function testInvalidRequestMethod()
{
setUpRequest('invalidrequestmethod');
$module = '<?php class invalidrequestmethod extends Resource { '
. 'public $method = "POST";'
. 'public function __default() { return ["foo" => "bar"]; }'
. '}';
file_put_contents(SITE_MODULE_PATH . 'invalidrequestmethod.php', $module);
new Router();
$this->expectOutputString('{"status":"error","message":"There was a problem with your request method."}');
}
public function testValidationErrors()
{
setUpRequest('validationerrors');
$module = '<?php class validationerrors extends Resource { '
. 'public $validate = ["test"];'
. 'public function __default() { return ["foo" => "bar"]; }'
. '}';
file_put_contents(SITE_MODULE_PATH . 'validationerrors.php', $module);
new Router();
$this->expectOutputString('{"status":"error","message":"The test field is required."}');
}
public function testError404()
{
setUpRequest('fourohfour');
new Router();
$this->assertTrue(in_array('Status: 404 Not Found', xdebug_get_headers()));
$this->expectOutputRegex('/<h1>Not Found<\/h1>/');
}
public function testCustomError404()
{
setUpRequest('customfourohfour');
file_put_contents(SITE_TEMPLATE_PATH . '__shared/404.phtml', '<h1>Custom Not Found</h1>');
new Router();
$this->assertTrue(in_array('Status: 404 Not Found', xdebug_get_headers()));
$this->expectOutputRegex('/<h1>Custom Not Found<\/h1>/');
}
public function testProfilerOutput()
{
$this->config->data['pickles']['profiler'] = true;
$this->expectOutputRegex('/id="pickles-profiler"/');
new Router();
}
public function testTwoValidTemplates()
{
$this->config->data['pickles']['profiler'] = true;
setUpRequest('validtemplates');
$module = '<?php class validtemplates extends Resource { }';
file_put_contents(SITE_MODULE_PATH . 'validtemplates.php', $module);
$child_template = SITE_TEMPLATE_PATH . 'validtemplates.phtml';
file_put_contents($child_template, '<div>child template</div>');
// Vim syntax highlighting borks unless ----v
$child = '<?php require $this->template; ?' . '>' . "\n";
$html = <<<HTML
<!doctype html>
<html>
<body>
<h1>parent template</h1>
{$child}
</body>
</html>
HTML;
file_put_contents(SITE_TEMPLATE_PATH . '__shared/index.phtml', $html);
new Router();
$this->expectOutputRegex('/^<!doctype html>
<html>
<body>
<h1>parent template<\/h1>
<div>child template<\/div>
<\/body>
<\/html>.+<style>/');
}
}