Skip to content

Commit

Permalink
Autoloader fixes and initial unit test structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Baker committed Jul 28, 2012
1 parent a0d5f4c commit 357b4ff
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,3 +1,3 @@

unitTests/codeCoverage
*.project
*.buildpath
34 changes: 32 additions & 2 deletions Classes/PHPPowerPoint/Autoloader.php
Expand Up @@ -25,24 +25,54 @@
* @version ##VERSION##, ##DATE##
*/

PHPPowerPoint_Autoloader::Register();
// check mbstring.func_overload
if (ini_get('mbstring.func_overload') & 2) {
throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
}


/**
* PHPPowerPoint_Autoloader
*
* @category PHPPowerPoint
* @package PHPPowerPoint
* @copyright Copyright (c) 2006 - 2012 PHPPowerPoint (https://github.com/Progi1984/PHPPowerPoint)
*/
class PHPPowerPoint_Autoloader
{
/**
* Register the Autoloader with SPL
*
*/
public static function Register() {
if (function_exists('__autoload')) {
// Register any existing autoloader function with SPL, so we don't get any clashes
spl_autoload_register('__autoload');
}
// Register ourselves with SPL
return spl_autoload_register(array('PHPPowerPoint_Autoloader', 'Load'));
} // function Register()


/**
* Autoload a class identified by name
*
* @param string $pClassName Name of the object to load
*/
public static function Load($pObjectName){
if ((class_exists($pObjectName)) || (strpos($pObjectName, 'PHPPowerPoint') === False)) {
return false;
// Either already loaded, or not a PHPPowerPoint class request
return FALSE;
}

$pObjectFilePath = PHPPOWERPOINT_ROOT.
str_replace('_',DIRECTORY_SEPARATOR,$pObjectName).
'.php';

if ((file_exists($pObjectFilePath) === false) || (is_readable($pObjectFilePath) === false)) {
return false;
// Can't load
return FALSE;
}

require($pObjectFilePath);
Expand Down
56 changes: 56 additions & 0 deletions unitTests/PHPPowerPoint/AutoloaderTest.php
@@ -0,0 +1,56 @@
<?php


class AutoloaderTest extends PHPUnit_Framework_TestCase
{

public function setUp()
{
if (!defined('PHPPOWERPOINT_ROOT'))
{
define('PHPPOWERPOINT_ROOT', APPLICATION_PATH . '/');
}
require_once(PHPPOWERPOINT_ROOT . 'PHPPowerPoint/Autoloader.php');
}

public function testAutoloaderNonPHPPowerPointClass()
{
$className = 'InvalidClass';

$result = PHPPowerPoint_Autoloader::Load($className);
// Must return a boolean...
$this->assertTrue(is_bool($result));
// ... indicating failure
$this->assertFalse($result);
}

public function testAutoloaderInvalidPHPPowerPointClass()
{
$className = 'PHPPowerPoint_Invalid_Class';

$result = PHPPowerPoint_Autoloader::Load($className);
// Must return a boolean...
$this->assertTrue(is_bool($result));
// ... indicating failure
$this->assertFalse($result);
}

public function testAutoloadValidPHPPowerPointClass()
{
$className = 'PHPPowerPoint_IOFactory';

$result = PHPPowerPoint_Autoloader::Load($className);
// Check that class has been loaded
$this->assertTrue(class_exists($className));
}

public function testAutoloadInstantiateSuccess()
{
$result = new PHPPowerPoint(1,2,3);
// Must return an object...
$this->assertTrue(is_object($result));
// ... of the correct type
$this->assertTrue(is_a($result,'PHPPowerPoint'));
}

}
44 changes: 44 additions & 0 deletions unitTests/bootstrap.php
@@ -0,0 +1,44 @@
<?php
/**
* $Id: bootstrap.php 2892 2011-08-14 15:11:50Z markbaker@PHPPowerPoint.net $
*
* @copyright Copyright (C) 2011-2012 PHPPowerPoint. All rights reserved.
* @package PHPPowerPoint
* @subpackage PHPPowerPoint Unit Tests
* @author Mark Baker
*/

// PHP 5.3 Compat
date_default_timezone_set('Europe/London');

// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../Classes'));

// Define path to application tests directory
defined('APPLICATION_TESTS_PATH')
|| define('APPLICATION_TESTS_PATH', realpath(dirname(__FILE__) ));

// Define application environment
defined('APPLICATION_ENV') || define('APPLICATION_ENV', 'ci');

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../Classes'),
'./',
get_include_path(),
)));


/**
* @todo Sort out xdebug in vagrant so that this works in all sandboxes
* For now, it is safer to test for it rather then remove it.
*/
echo "PHPPowerPoint tests beginning\n";

if(extension_loaded('xdebug')) {
echo "Xdebug extension loaded and running\n";
xdebug_enable();
} else {
echo 'Xdebug not found, you should run the following at the command line: echo "zend_extension=/usr/lib64/php/modules/xdebug.so" > /etc/php.d/xdebug.ini' . "\n";
}
35 changes: 35 additions & 0 deletions unitTests/phpunit.xml
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./bootstrap.php"
backupGlobals="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
syntaxCheck="true"
verbose="true"
strict="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false">
<php>
<ini name="memory_limit" value="2048M"/>
</php>
<testsuite name="PHPPowerPoint Unit Test Suite">
<directory suffix="Test.php">./PHPPowerPoint</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../Classes</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./codeCoverage" charset="UTF-8"
yui="true" highlight="false"
lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-clover" target="./codeCoverage/codeCoverage.xml"/>
<log type="metrics-xml" target="./metrics/metrics.xml"/>
<log type="test-xml" target="./testResults/logfile.xml" logIncompleteSkipped="false"/>
</logging>
</phpunit>

0 comments on commit 357b4ff

Please sign in to comment.