Skip to content

Commit

Permalink
Added initial unit test file, autoloader, phpunit config, and boostrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Tuke committed Jun 15, 2014
1 parent 352dc29 commit 1f903ee
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
40 changes: 40 additions & 0 deletions plugins/restapi/tests/AutoLoader.php
@@ -0,0 +1,40 @@
<?php

class AutoLoader {

static private $classNames = array();

/**
* Store the filename (sans extension) & full path of all ".php" files found
*/
public static function registerDirectory($dirName) {

$di = new DirectoryIterator($dirName);
foreach ($di as $file) {

if ($file->isDir() && !$file->isLink() && !$file->isDot()) {
// recurse into directories other than a few special ones
self::registerDirectory($file->getPathname());
} elseif (substr($file->getFilename(), -4) === '.php') {
// save the class name / path of a .php file found
$className = substr($file->getFilename(), 0, -4);
AutoLoader::registerClass($className, $file->getPathname());
}
}
}

public static function registerClass($className, $fileName) {
AutoLoader::$classNames[$className] = $fileName;
}

public static function loadClass($className) {
if (isset(AutoLoader::$classNames[$className])) {
require_once(AutoLoader::$classNames[$className]);
}
}

}

spl_autoload_register(array('AutoLoader', 'loadClass'));

?>
8 changes: 8 additions & 0 deletions plugins/restapi/tests/bootstrap.php
@@ -0,0 +1,8 @@
<?php

include_once( 'AutoLoader.php' );

// Register the directory to your include files
AutoLoader::registerDirectory( '../includes' );

?>
5 changes: 5 additions & 0 deletions plugins/restapi/tests/phpunit.xml
@@ -0,0 +1,5 @@
<phpunit bootstrap="bootstrap.php">
<php>
<const name="API_URL_BASE_PATH" value="http://local.pl/admin/?page=call&amp;pi=restapi"/>
</php>
</phpunit>
82 changes: 82 additions & 0 deletions plugins/restapi/tests/restapi.php
@@ -0,0 +1,82 @@
<?php

/**
* Class Test_
*/
class TestRestapi extends \PHPUnit_Framework_TestCase
{

public $userId;
public $loginName;
public $password;
public $url;

function setUp()
{
$this->loginName = 'admin';
$this->password = 'phplist';
// Set URL from constant stored in phpunit.xml
$this->url = API_URL_BASE_PATH;
}

function tearDown() {
}

/**
* Make a call to the API using cURL
* @return string result of the CURL execution
*/
private function callApi( $command, $post_params, $decode = true )
{
$post_params['cmd'] = $command;

// Serialise and encode query
$post_params = http_build_query( $post_params );

// Prepare cURL
$c = curl_init();
curl_setopt( $c, CURLOPT_URL, $this->url );
curl_setopt( $c, CURLOPT_HEADER, 0 );
curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $c, CURLOPT_POST, 1 );
curl_setopt( $c, CURLOPT_POSTFIELDS, $post_params);
// FIXME: this tmp path mustn't be hardcoded
curl_setopt( $c, CURLOPT_COOKIEFILE, '/tmp'.'/phpList_RESTAPI_Helper_cookiejar.txt');
curl_setopt( $c, CURLOPT_COOKIEJAR, '/tmp'.'/phpList_RESTAPI_Helper_cookiejar.txt');
curl_setopt( $c, CURLOPT_HTTPHEADER, array( 'Connection: Keep-Alive', 'Keep-Alive: 60' ) );

// Execute the call
$result = curl_exec( $c );

// Check if decoding of result is required
if ( $decode === true )
{
$result = json_decode( $result );
}

return $result;
}

/**
* Use a real login to test login api call
* @return bool true if user exists and login successful
*/
function testLogin()
{
// Set the username and pwd to login with
$post_params = array(
'login' => $this->loginName,
'password' => $this->password
);

// Execute the login with the credentials as params
$result = $this->callAPI( 'login', $post_params );

// Check if the login was successful
$this->assertEquals( 'success', $result->status );

}

}

?>

0 comments on commit 1f903ee

Please sign in to comment.