Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
webbasics
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
webbasics
Commits
6e91a09d
Commit
6e91a09d
authored
12 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Unit tested handlers.php, solving some bugs
parent
a733b45e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
handlers.php
+9
-7
9 additions, 7 deletions
handlers.php
tests/test_handlers.php
+84
-0
84 additions, 0 deletions
tests/test_handlers.php
with
93 additions
and
7 deletions
handlers.php
+
9
−
7
View file @
6e91a09d
...
...
@@ -10,28 +10,30 @@ namespace webbasics;
require_once
'router.php'
;
class
BaseHandler
implements
RouteHandler
{
abstract
class
BaseHandler
implements
RouteHandler
{
function
handleRequest
(
array
$data
)
{
$request_type
=
strtolower
(
$_SERVER
[
'REQUEST_METHOD'
]);
// Try to use first match value as method name, e.g. getAction() if
// first match value is "action"
if
(
count
(
$data
))
{
$method_name
=
$request_type
.
Inflector
::
capitalize
(
array_sp
li
c
e
(
$data
,
0
,
1
)
);
$method_name
=
$request_type
.
came
li
z
e
(
$data
[
0
],
true
);
if
(
method_exists
(
$this
,
$method_name
))
{
if
(
count
(
$data
))
$this
->
$method_name
(
$data
);
array_shift
(
$data
);
if
(
count
(
$data
))
return
$this
->
$method_name
(
$data
);
else
$this
->
$method_name
();
return
$this
->
$method_name
();
}
// get($data) or post($data)
$this
->
$request_type
(
$data
);
return
$this
->
$request_type
(
$data
);
}
// get() or post()
$this
->
$request_type
();
return
$this
->
$request_type
();
}
}
...
...
This diff is collapsed.
Click to expand it.
tests/test_handlers.php
0 → 100644
+
84
−
0
View file @
6e91a09d
<?php
require_once
'handlers.php'
;
class
MyHandler
extends
webbasics\BaseHandler
{
function
get
(
array
$args
=
array
())
{
return
'get '
.
implode
(
'-'
,
$args
);
}
function
getFoo
()
{
return
'getFoo'
;
}
function
getBar
(
array
$args
)
{
return
'getBar '
.
implode
(
'-'
,
$args
);
}
function
post
(
array
$args
=
array
())
{
return
'post '
.
implode
(
'-'
,
$args
);
}
function
postFoo
()
{
return
'postFoo'
;
}
function
postBar
(
array
$args
)
{
return
'postBar '
.
implode
(
'-'
,
$args
);
}
}
class
HandlersTest
extends
PHPUnit_Framework_TestCase
{
private
$myhandler
;
function
setUp
()
{
$this
->
myhandler
=
new
MyHandler
;
}
function
testBaseHandlerGetNoMethodNoArgs
()
{
$this
->
assertHandlesGet
(
'get '
,
$this
->
myhandler
,
array
());
}
function
testBaseHandlerGetNoMethodArgs
()
{
$this
->
assertHandlesGet
(
'get baz'
,
$this
->
myhandler
,
array
(
'baz'
));
}
function
testBaseHandlerGetMethodNoArgs
()
{
$this
->
assertHandlesGet
(
'getFoo'
,
$this
->
myhandler
,
array
(
'foo'
));
}
function
testBaseHandlerGetMethodArgs
()
{
$this
->
assertHandlesGet
(
'getBar foo-baz'
,
$this
->
myhandler
,
array
(
'bar'
,
'foo'
,
'baz'
));
}
function
testBaseHandlerPostNoMethodNoArgs
()
{
$this
->
assertHandlesPost
(
'post '
,
$this
->
myhandler
,
array
());
}
function
testBaseHandlerPostNoMethodArgs
()
{
$this
->
assertHandlesPost
(
'post baz'
,
$this
->
myhandler
,
array
(
'baz'
));
}
function
testBaseHandlerPostMethodNoArgs
()
{
$this
->
assertHandlesPost
(
'postFoo'
,
$this
->
myhandler
,
array
(
'foo'
));
}
function
testBaseHandlerPostMethodArgs
()
{
$this
->
assertHandlesPost
(
'postBar foo-baz'
,
$this
->
myhandler
,
array
(
'bar'
,
'foo'
,
'baz'
));
}
function
assertHandlesGet
(
$result
,
$handler
,
array
$args
=
array
())
{
$this
->
assertHandlesMethod
(
$result
,
$handler
,
$args
,
'GET'
);
}
function
assertHandlesPost
(
$result
,
$handler
,
array
$args
=
array
())
{
$this
->
assertHandlesMethod
(
$result
,
$handler
,
$args
,
'POST'
);
}
function
assertHandlesMethod
(
$result
,
$handler
,
array
$args
,
$request_method
)
{
$_SERVER
[
'REQUEST_METHOD'
]
=
$request_method
;
$this
->
assertEquals
(
$result
,
$handler
->
handleRequest
(
$args
));
}
}
?>
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment