Skip to content

Commit 50a9ab5

Browse files
author
Gabriel Bull
committed
Added Unit Tests and fixed bugs with Autoloader
1 parent fa77c4f commit 50a9ab5

File tree

8 files changed

+47
-12
lines changed

8 files changed

+47
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Desktop.ini
77
.idea
88
phpunit.xml
99
composer.lock
10+
composer.phar
1011
vendor
1112
/.settings
1213
/.buildpath

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"require": {
1515
"php": ">=5.2.0"
1616
},
17+
"require-dev": {
18+
"phpunit/phpunit": "3.7.28"
19+
},
1720
"autoload": {
1821
"psr-0": {
1922
"PHPWord": "src/"

phpunit.xml.dist

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<phpunit backupGlobals="false"
2+
backupStaticAttributes="false"
3+
bootstrap="test/bootstrap.php"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false"
10+
syntaxCheck="false">
11+
<testsuites>
12+
<testsuite name="PHPWord Test Suite">
13+
<directory>./test/PHPWord/</directory>
14+
</testsuite>
15+
</testsuites>
16+
</phpunit>

src/PHPWord.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
/** PHPWORD_BASE_PATH */
2929
if (!defined('PHPWORD_BASE_PATH')) {
3030
define('PHPWORD_BASE_PATH', dirname(__FILE__) . '/');
31-
require PHPWORD_BASE_PATH . 'PHPWord/Autoloader.php';
32-
PHPWord_Autoloader::Register();
31+
require_once PHPWORD_BASE_PATH . 'PHPWord/Autoloader.php';
32+
PHPWord_Autoloader::register();
3333
}
3434

3535

src/PHPWord/Autoloader.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* @version Beta 0.6.3, 08.07.2011
2626
*/
2727

28+
define('PHPWORD_BASE_PATH', realpath(__DIR__ . '/../'));
29+
2830
class PHPWord_Autoloader
2931
{
3032
/**
@@ -45,17 +47,12 @@ public static function register()
4547
*/
4648
public static function load($strObjectName)
4749
{
48-
if ((class_exists($strObjectName)) || (strpos($strObjectName, 'PHPWord') === false)) {
49-
return null;
50-
}
51-
52-
$strObjectFilePath = PHPWORD_BASE_PATH . str_replace('_', '/', $strObjectName) . '.php';
53-
54-
if ((file_exists($strObjectFilePath) === false) || (is_readable($strObjectFilePath) === false)) {
55-
return null;
50+
$strObjectFilePath = __DIR__ . '/../' . str_replace('_', '/', $strObjectName) . '.php';
51+
if (file_exists($strObjectFilePath) && is_readable($strObjectFilePath)) {
52+
require_once $strObjectFilePath;
53+
return true;
5654
}
5755

58-
require_once $strObjectFilePath;
59-
return true;
56+
return null;
6057
}
6158
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord_Autoloader;
6+
7+
class AutoloaderTest extends PHPUnit_Framework_TestCase
8+
{
9+
public function testAutoload()
10+
{
11+
$this->assertNull(PHPWord_Autoloader::load('Foo'), 'PHPWord_Autoloader::load() is trying to load classes outside of the PHPWord namespace');
12+
$this->assertTrue(PHPWord_Autoloader::load('PHPWord'), 'PHPWord_Autoloader::load() failed to autoload the PHPWord class');
13+
}
14+
}

test/PHPWord/Tests/_files/.gitkeep

Whitespace-only changes.

test/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
require_once __DIR__ . "/../src/PHPWord/Autoloader.php";
4+
PHPWord_Autoloader::register();

0 commit comments

Comments
 (0)