Added Unit Tests and fixed bugs with Autoloader

This commit is contained in:
Gabriel Bull 2013-12-11 15:08:54 -05:00
parent fa77c4f159
commit 50a9ab52b0
8 changed files with 47 additions and 12 deletions

1
.gitignore vendored
View File

@ -7,6 +7,7 @@ Desktop.ini
.idea .idea
phpunit.xml phpunit.xml
composer.lock composer.lock
composer.phar
vendor vendor
/.settings /.settings
/.buildpath /.buildpath

View File

@ -14,6 +14,9 @@
"require": { "require": {
"php": ">=5.2.0" "php": ">=5.2.0"
}, },
"require-dev": {
"phpunit/phpunit": "3.7.28"
},
"autoload": { "autoload": {
"psr-0": { "psr-0": {
"PHPWord": "src/" "PHPWord": "src/"

View File

@ -0,0 +1,16 @@
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="test/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="PHPWord Test Suite">
<directory>./test/PHPWord/</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@ -28,8 +28,8 @@
/** PHPWORD_BASE_PATH */ /** PHPWORD_BASE_PATH */
if (!defined('PHPWORD_BASE_PATH')) { if (!defined('PHPWORD_BASE_PATH')) {
define('PHPWORD_BASE_PATH', dirname(__FILE__) . '/'); define('PHPWORD_BASE_PATH', dirname(__FILE__) . '/');
require PHPWORD_BASE_PATH . 'PHPWord/Autoloader.php'; require_once PHPWORD_BASE_PATH . 'PHPWord/Autoloader.php';
PHPWord_Autoloader::Register(); PHPWord_Autoloader::register();
} }

View File

@ -25,6 +25,8 @@
* @version Beta 0.6.3, 08.07.2011 * @version Beta 0.6.3, 08.07.2011
*/ */
define('PHPWORD_BASE_PATH', realpath(__DIR__ . '/../'));
class PHPWord_Autoloader class PHPWord_Autoloader
{ {
/** /**
@ -45,17 +47,12 @@ class PHPWord_Autoloader
*/ */
public static function load($strObjectName) public static function load($strObjectName)
{ {
if ((class_exists($strObjectName)) || (strpos($strObjectName, 'PHPWord') === false)) { $strObjectFilePath = __DIR__ . '/../' . str_replace('_', '/', $strObjectName) . '.php';
return null; if (file_exists($strObjectFilePath) && is_readable($strObjectFilePath)) {
}
$strObjectFilePath = PHPWORD_BASE_PATH . str_replace('_', '/', $strObjectName) . '.php';
if ((file_exists($strObjectFilePath) === false) || (is_readable($strObjectFilePath) === false)) {
return null;
}
require_once $strObjectFilePath; require_once $strObjectFilePath;
return true; return true;
} }
return null;
}
} }

View File

@ -0,0 +1,14 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord_Autoloader;
class AutoloaderTest extends PHPUnit_Framework_TestCase
{
public function testAutoload()
{
$this->assertNull(PHPWord_Autoloader::load('Foo'), 'PHPWord_Autoloader::load() is trying to load classes outside of the PHPWord namespace');
$this->assertTrue(PHPWord_Autoloader::load('PHPWord'), 'PHPWord_Autoloader::load() failed to autoload the PHPWord class');
}
}

View File

4
test/bootstrap.php Normal file
View File

@ -0,0 +1,4 @@
<?php
require_once __DIR__ . "/../src/PHPWord/Autoloader.php";
PHPWord_Autoloader::register();