Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webbasics
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
webbasics
Commits
62641b42
Commit
62641b42
authored
Oct 09, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplified Autoloader class into an easy-to-use singleton
parent
c7d4d826
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
192 additions
and
243 deletions
+192
-243
autoloader.php
autoloader.php
+120
-130
base.php
base.php
+1
-1
tests/test_autoloader.php
tests/test_autoloader.php
+71
-112
No files found.
autoloader.php
View file @
62641b42
This diff is collapsed.
Click to expand it.
base.php
View file @
62641b42
...
@@ -63,7 +63,7 @@ abstract class Base {
...
@@ -63,7 +63,7 @@ abstract class Base {
* @return string
* @return string
*/
*/
static
function
pathWithSlash
(
$directory
)
{
static
function
pathWithSlash
(
$directory
)
{
return
$directory
[
strlen
(
$directory
)
-
1
]
==
'/'
?
$directory
:
$directory
.
'/'
;
return
$directory
[
strlen
(
$directory
)
-
1
]
==
'/'
?
$directory
:
$directory
.
'/'
;
}
}
}
}
...
...
tests/test_autoloader.php
View file @
62641b42
<?php
<?php
require_once
'SingletonTestCase.php'
;
require_once
'autoloader.php'
;
require_once
'autoloader.php'
;
use
webbasics\Autoloader
;
use
webbasics\Autoloader
;
define
(
'PATH'
,
'tests/_files/'
);
define
(
'PATH'
,
'tests/_files/'
);
class
AutoloaderTest
extends
PHPUnit_Framework_
TestCase
{
class
AutoloaderTest
extends
Singleton
TestCase
{
function
setUp
()
{
function
getClassName
()
{
$this
->
autoloader
=
new
Autoloader
(
PATH
)
;
return
'webbasics\Autoloader'
;
}
}
function
testSetRootNamespace
()
{
function
tearDown
()
{
$this
->
assertAttributeEquals
(
'\\'
,
'root_namespace'
,
$this
->
autoloader
);
Autoloader
::
getInstance
()
->
setThrowExceptions
(
false
);
$this
->
autoloader
->
setRootNamespace
(
'Foo'
);
$this
->
assertAttributeEquals
(
'Foo\\'
,
'root_namespace'
,
$this
->
autoloader
);
$this
->
autoloader
->
setRootNamespace
(
'Foo\\'
);
$this
->
assertAttributeEquals
(
'Foo\\'
,
'root_namespace'
,
$this
->
autoloader
);
}
}
/**
function
testStripNamespace
()
{
* @depends testSetRootNamespace
$rmethod
=
new
ReflectionMethod
(
'webbasics\Autoloader'
,
'stripNamespace'
);
*/
$rmethod
->
setAccessible
(
true
);
function
testGetRootNamespace
()
{
$this
->
assertEquals
(
'Bar'
,
$rmethod
->
invoke
(
null
,
'foo'
,
'foo\Bar'
));
$this
->
autoloader
->
setRootNamespace
(
'Foo'
);
$this
->
assertEquals
(
'Bar'
,
$rmethod
->
invoke
(
null
,
'\foo'
,
'\foo\Bar'
));
$this
->
assertEquals
(
$this
->
autoloader
->
getRootNamespace
(),
'Foo\\'
);
}
}
/**
function
testAddDirectory
()
{
* @depends testSetRootNamespace
$autoloader
=
Autoloader
::
getInstance
();
*/
$rprop
=
new
ReflectionProperty
(
$autoloader
,
'directories'
);
function
testConstructRootNamespace
()
{
$rprop
->
setAccessible
(
true
);
$autoloader
=
new
Autoloader
(
PATH
,
'Foo'
);
$this
->
assertAttributeEquals
(
'Foo\\'
,
'root_namespace'
,
$autoloader
);
}
/**
$autoloader
->
addDirectory
(
PATH
);
* @depends testSetRootNamespace
$this
->
assertEquals
(
array
(
*/
'\\'
=>
array
(
PATH
)
function
testStripRootNamespace
()
{
),
$rprop
->
getValue
(
$autoloader
));
$strip
=
new
ReflectionMethod
(
'webbasics\Autoloader'
,
'stripRootNamespace'
);
$strip
->
setAccessible
(
true
);
$this
->
autoloader
->
setRootNamespace
(
'Foo'
);
$autoloader
->
addDirectory
(
PATH
);
$this
->
assertEquals
(
$strip
->
invoke
(
$this
->
autoloader
,
'Foo\Bar'
),
'Bar'
);
$this
->
assertEquals
(
array
(
}
'\\'
=>
array
(
PATH
)
),
$rprop
->
getValue
(
$autoloader
));
function
testSetRootDirectory
()
{
$autoloader
->
addDirectory
(
'foo'
);
$this
->
a
utoloader
->
setRootDirectory
(
'tests'
);
$this
->
a
ssertEquals
(
array
(
$this
->
assertEquals
(
$this
->
autoloader
->
getRootDirectory
(),
'tests/'
);
'\\'
=>
array
(
PATH
,
'foo/'
)
}
),
$rprop
->
getValue
(
$autoloader
));
function
testClassnameToFilename
()
{
$autoloader
->
addDirectory
(
'bar'
);
$this
->
assertEquals
(
Autoloader
::
classnameToFilename
(
'Foo'
),
'foo'
);
$this
->
assertEquals
(
array
(
$this
->
assertEquals
(
Autoloader
::
classnameToFilename
(
'FooBar'
),
'foo_bar'
);
'\\'
=>
array
(
PATH
,
'foo/'
,
'bar/'
)
$this
->
assertEquals
(
Autoloader
::
classnameToFilename
(
'fooBar'
),
'foo_bar'
);
),
$rprop
->
getValue
(
$autoloader
));
$this
->
assertEquals
(
Autoloader
::
classnameToFilename
(
'FooBarBaz'
),
'foo_bar_baz'
);
$autoloader
->
addDirectory
(
'foodir'
,
'foo'
);
$this
->
assertEquals
(
array
(
'\\'
=>
array
(
PATH
,
'foo/'
,
'bar/'
),
'\foo'
=>
array
(
'foodir/'
)
),
$rprop
->
getValue
(
$autoloader
));
$autoloader
->
addDirectory
(
'foobardir'
,
'foobar'
);
$this
->
assertEquals
(
array
(
'\\'
=>
array
(
PATH
,
'foo/'
,
'bar/'
),
'\foo'
=>
array
(
'foodir/'
),
'\foobar'
=>
array
(
'foobardir/'
)
),
$rprop
->
getValue
(
$autoloader
));
}
}
/**
* @depends testClassnameToFilename
*/
function
testCreatePath
()
{
function
testCreatePath
()
{
$this
->
assertEquals
(
$this
->
autoloader
->
createPath
(
'Foo'
),
PATH
.
'foo.php'
);
$rmethod
=
new
ReflectionMethod
(
'webbasics\Autoloader'
,
'createPath'
);
$this
->
assertEquals
(
$this
->
autoloader
->
createPath
(
'\Foo'
),
PATH
.
'foo.php'
);
$rmethod
->
setAccessible
(
true
);
$this
->
assertEquals
(
$this
->
autoloader
->
createPath
(
'Foo\Bar'
),
PATH
.
'foo/bar.php'
);
$this
->
assertEquals
(
$rmethod
->
invoke
(
null
,
'Foo'
),
'Foo.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
createPath
(
'Foo\Bar\Baz'
),
PATH
.
'foo/bar/baz.php'
);
$this
->
assertEquals
(
$rmethod
->
invoke
(
null
,
'\Foo'
),
'Foo.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
createPath
(
'FooBar\Baz'
),
PATH
.
'foo_bar/baz.php'
);
$this
->
assertEquals
(
$rmethod
->
invoke
(
null
,
'foo\Bar'
),
'foo/Bar.php'
);
$this
->
assertEquals
(
$rmethod
->
invoke
(
null
,
'foo\Bar\Baz'
),
'foo/Bar/Baz.php'
);
$this
->
assertEquals
(
$rmethod
->
invoke
(
null
,
'fooBar\Baz'
),
'fooBar/Baz.php'
);
$this
->
assertEquals
(
$rmethod
->
invoke
(
null
,
'foo_bar\Baz'
),
'foo_bar/Baz.php'
);
}
}
function
testThrowErrors
()
{
function
testThrowExceptions
()
{
$this
->
assertFalse
(
$this
->
autoloader
->
getThrowErrors
());
$autoloader
=
Autoloader
::
getInstance
();
$this
->
autoloader
->
setThrowErrors
(
true
);
$this
->
assertFalse
(
$autoloader
->
getThrowExceptions
());
$this
->
assertTrue
(
$this
->
autoloader
->
getThrowErrors
());
$autoloader
->
setThrowExceptions
(
true
);
$this
->
assertTrue
(
$autoloader
->
getThrowExceptions
());
}
}
/**
/**
* @depends testCreatePath
* @depends testCreatePath
* @depends testThrowE
rror
s
* @depends testThrowE
xception
s
*/
*/
function
testLoadClassNotFound
()
{
function
testLoadClassNotFound
()
{
$this
->
assertFalse
(
$this
->
autoloader
->
loadClass
(
'foobar'
));
$this
->
assertFalse
(
Autoloader
::
getInstance
()
->
loadClass
(
'foobar'
));
}
/**
* @depends testLoadClassNotFound
* @expectedException webbasics\FileNotFoundError
* @expectedExceptionMessage File "tests/_files/foobar.php" does not exist.
*/
function
testLoadClassNotFoundError
()
{
$this
->
autoloader
->
setThrowErrors
(
true
);
$this
->
autoloader
->
loadClass
(
'foobar'
);
}
}
/**
/**
* @depends testLoadClassNotFound
* @depends testLoadClassNotFound
* @expectedException webbasics\FileNotFoundError
* @expectedException webbasics\ClassNotFoundError
* @expectedExceptionMessage File "tests/_files/foobar.php" does not exist.
*/
*/
function
testLoadClassNotFoundNoerrorOverwrite
()
{
function
testLoadClassNotFoundException
()
{
$this
->
autoloader
->
loadClass
(
'foobar'
,
true
);
$autoloader
=
Autoloader
::
getInstance
();
$autoloader
->
setThrowExceptions
(
true
);
$autoloader
->
loadClass
(
'foobar'
);
}
}
/**
/**
* @depends testLoadClassNotFound
* @depends testLoadClassNotFound
*/
*/
function
testLoadClassNotFoundErrorOverwrite
()
{
function
testLoadClassSuccess
()
{
$this
->
autoloader
->
setThrowErrors
(
true
);
$autoloader
=
Autoloader
::
getInstance
();
$this
->
assertFalse
(
$this
->
autoloader
->
loadClass
(
'foobar'
,
false
));
$this
->
assertTrue
(
$autoloader
->
loadClass
(
'Foo'
));
}
/**
* @depends testLoadClassNotFound
*/
function
testLoadClass
()
{
$this
->
assertTrue
(
$this
->
autoloader
->
loadClass
(
'Foo'
));
$this
->
assertTrue
(
class_exists
(
'Foo'
,
false
));
$this
->
assertTrue
(
class_exists
(
'Foo'
,
false
));
$this
->
assertTrue
(
$this
->
autoloader
->
loadClass
(
'Foo\Bar'
));
$this
->
assertTrue
(
$autoloader
->
loadClass
(
'Foo\Bar'
));
$this
->
assertTrue
(
class_exists
(
'Foo\Bar'
,
false
));
}
/**
* @depends testLoadClass
* @depends testStripRootNamespace
*/
function
testLoadClassRootNamespace
()
{
$autoloader
=
new
Autoloader
(
PATH
.
'foo'
);
$autoloader
->
setRootNamespace
(
'Foo'
);
$this
->
assertTrue
(
$autoloader
->
loadClass
(
'Bar'
));
$this
->
assertTrue
(
class_exists
(
'Foo\Bar'
,
false
));
$this
->
assertTrue
(
class_exists
(
'Foo\Bar'
,
false
));
}
}
/**
* @depends testLoadClass
*/
function
testRegister
()
{
$this
->
autoloader
->
register
();
$this
->
assertTrue
(
class_exists
(
'Baz'
));
}
/**
* @depends testRegister
* @depends testThrowErrors
*/
function
testRegisterPrepend
()
{
$second_loader
=
new
Autoloader
(
PATH
.
'second'
);
$this
->
autoloader
->
register
();
$second_loader
->
register
(
true
);
// Prepend so that the second loader attemps to load Bar first
$this
->
assertInstanceOf
(
'Foo'
,
new
FooBaz
());
}
}
}
?>
?>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment