Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added initial unit test file, autoloader, phpunit config, and boostrap
- Loading branch information
Sam Tuke
committed
Jun 15, 2014
1 parent
352dc29
commit 1f903ee
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
include_once( 'AutoLoader.php' ); | ||
|
||
// Register the directory to your include files | ||
AutoLoader::registerDirectory( '../includes' ); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<phpunit bootstrap="bootstrap.php"> | ||
<php> | ||
<const name="API_URL_BASE_PATH" value="http://local.pl/admin/?page=call&pi=restapi"/> | ||
</php> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
|
||
} | ||
|
||
} | ||
|
||
?> |