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
Hide 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 {
* @return string
*/
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
require_once
'SingletonTestCase.php'
;
require_once
'autoloader.php'
;
use
webbasics\Autoloader
;
define
(
'PATH'
,
'tests/_files/'
);
class
AutoloaderTest
extends
PHPUnit_Framework_
TestCase
{
function
setUp
()
{
$this
->
autoloader
=
new
Autoloader
(
PATH
)
;
class
AutoloaderTest
extends
Singleton
TestCase
{
function
getClassName
()
{
return
'webbasics\Autoloader'
;
}
function
testSetRootNamespace
()
{
$this
->
assertAttributeEquals
(
'\\'
,
'root_namespace'
,
$this
->
autoloader
);
$this
->
autoloader
->
setRootNamespace
(
'Foo'
);
$this
->
assertAttributeEquals
(
'Foo\\'
,
'root_namespace'
,
$this
->
autoloader
);
$this
->
autoloader
->
setRootNamespace
(
'Foo\\'
);
$this
->
assertAttributeEquals
(
'Foo\\'
,
'root_namespace'
,
$this
->
autoloader
);
function
tearDown
()
{
Autoloader
::
getInstance
()
->
setThrowExceptions
(
false
);
}
/**
* @depends testSetRootNamespace
*/
function
testGetRootNamespace
()
{
$this
->
autoloader
->
setRootNamespace
(
'Foo'
);
$this
->
assertEquals
(
$this
->
autoloader
->
getRootNamespace
(),
'Foo\\'
);
}
/**
* @depends testSetRootNamespace
*/
function
testConstructRootNamespace
()
{
$autoloader
=
new
Autoloader
(
PATH
,
'Foo'
);
$this
->
assertAttributeEquals
(
'Foo\\'
,
'root_namespace'
,
$autoloader
);
function
testStripNamespace
()
{
$rmethod
=
new
ReflectionMethod
(
'webbasics\Autoloader'
,
'stripNamespace'
);
$rmethod
->
setAccessible
(
true
);
$this
->
assertEquals
(
'Bar'
,
$rmethod
->
invoke
(
null
,
'foo'
,
'foo\Bar'
));
$this
->
assertEquals
(
'Bar'
,
$rmethod
->
invoke
(
null
,
'\foo'
,
'\foo\Bar'
));
}
/**
* @depends testSetRootNamespace
*/
function
testStripRootNamespace
()
{
$strip
=
new
ReflectionMethod
(
'webbasics\Autoloader'
,
'stripRootNamespace'
);
$strip
->
setAccessible
(
true
);
function
testAddDirectory
()
{
$autoloader
=
Autoloader
::
getInstance
();
$rprop
=
new
ReflectionProperty
(
$autoloader
,
'directories'
);
$rprop
->
setAccessible
(
true
);
$this
->
autoloader
->
setRootNamespace
(
'Foo'
);
$this
->
assertEquals
(
$strip
->
invoke
(
$this
->
autoloader
,
'Foo\Bar'
),
'Bar'
);
}
function
testSetRootDirectory
()
{
$this
->
autoloader
->
setRootDirectory
(
'tests'
);
$this
->
assertEquals
(
$this
->
autoloader
->
getRootDirectory
(),
'tests/'
);
}
function
testClassnameToFilename
()
{
$this
->
assertEquals
(
Autoloader
::
classnameToFilename
(
'Foo'
),
'foo'
);
$this
->
assertEquals
(
Autoloader
::
classnameToFilename
(
'FooBar'
),
'foo_bar'
);
$this
->
assertEquals
(
Autoloader
::
classnameToFilename
(
'fooBar'
),
'foo_bar'
);
$this
->
assertEquals
(
Autoloader
::
classnameToFilename
(
'FooBarBaz'
),
'foo_bar_baz'
);
$autoloader
->
addDirectory
(
PATH
);
$this
->
assertEquals
(
array
(
'\\'
=>
array
(
PATH
)
),
$rprop
->
getValue
(
$autoloader
));
$autoloader
->
addDirectory
(
PATH
);
$this
->
assertEquals
(
array
(
'\\'
=>
array
(
PATH
)
),
$rprop
->
getValue
(
$autoloader
));
$autoloader
->
addDirectory
(
'foo'
);
$this
->
assertEquals
(
array
(
'\\'
=>
array
(
PATH
,
'foo/'
)
),
$rprop
->
getValue
(
$autoloader
));
$autoloader
->
addDirectory
(
'bar'
);
$this
->
assertEquals
(
array
(
'\\'
=>
array
(
PATH
,
'foo/'
,
'bar/'
)
),
$rprop
->
getValue
(
$autoloader
));
$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
()
{
$this
->
assertEquals
(
$this
->
autoloader
->
createPath
(
'Foo'
),
PATH
.
'foo.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
createPath
(
'\Foo'
),
PATH
.
'foo.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
createPath
(
'Foo\Bar'
),
PATH
.
'foo/bar.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
createPath
(
'Foo\Bar\Baz'
),
PATH
.
'foo/bar/baz.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
createPath
(
'FooBar\Baz'
),
PATH
.
'foo_bar/baz.php'
);
$rmethod
=
new
ReflectionMethod
(
'webbasics\Autoloader'
,
'createPath'
);
$rmethod
->
setAccessible
(
true
);
$this
->
assertEquals
(
$rmethod
->
invoke
(
null
,
'Foo'
),
'Foo.php'
);
$this
->
assertEquals
(
$rmethod
->
invoke
(
null
,
'\Foo'
),
'Foo.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
()
{
$this
->
assertFalse
(
$this
->
autoloader
->
getThrowErrors
());
$this
->
autoloader
->
setThrowErrors
(
true
);
$this
->
assertTrue
(
$this
->
autoloader
->
getThrowErrors
());
function
testThrowExceptions
()
{
$autoloader
=
Autoloader
::
getInstance
();
$this
->
assertFalse
(
$autoloader
->
getThrowExceptions
());
$autoloader
->
setThrowExceptions
(
true
);
$this
->
assertTrue
(
$autoloader
->
getThrowExceptions
());
}
/**
* @depends testCreatePath
* @depends testThrowE
rror
s
* @depends testThrowE
xception
s
*/
function
testLoadClassNotFound
()
{
$this
->
assertFalse
(
$this
->
autoloader
->
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'
);
$this
->
assertFalse
(
Autoloader
::
getInstance
()
->
loadClass
(
'foobar'
));
}
/**
* @depends testLoadClassNotFound
* @expectedException webbasics\FileNotFoundError
* @expectedExceptionMessage File "tests/_files/foobar.php" does not exist.
* @expectedException webbasics\ClassNotFoundError
*/
function
testLoadClassNotFoundNoerrorOverwrite
()
{
$this
->
autoloader
->
loadClass
(
'foobar'
,
true
);
function
testLoadClassNotFoundException
()
{
$autoloader
=
Autoloader
::
getInstance
();
$autoloader
->
setThrowExceptions
(
true
);
$autoloader
->
loadClass
(
'foobar'
);
}
/**
* @depends testLoadClassNotFound
*/
function
testLoadClassNotFoundErrorOverwrite
()
{
$this
->
autoloader
->
setThrowErrors
(
true
);
$this
->
assertFalse
(
$this
->
autoloader
->
loadClass
(
'foobar'
,
false
));
}
/**
* @depends testLoadClassNotFound
*/
function
testLoadClass
()
{
$this
->
assertTrue
(
$this
->
autoloader
->
loadClass
(
'Foo'
));
function
testLoadClassSuccess
()
{
$autoloader
=
Autoloader
::
getInstance
();
$this
->
assertTrue
(
$autoloader
->
loadClass
(
'Foo'
));
$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
));
}
/**
* @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