test_base_handler.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. require_once 'handlers.php';
  3. class MyHandler extends webbasics\BaseHandler {
  4. function get(array $args=array()) {
  5. return 'get ' . implode('-', $args);
  6. }
  7. function getFoo() {
  8. return 'getFoo';
  9. }
  10. function getBar(array $args) {
  11. return 'getBar ' . implode('-', $args);
  12. }
  13. function post(array $args=array()) {
  14. return 'post ' . implode('-', $args);
  15. }
  16. function postFoo() {
  17. return 'postFoo';
  18. }
  19. function postBar(array $args) {
  20. return 'postBar ' . implode('-', $args);
  21. }
  22. }
  23. class HandlersTest extends PHPUnit_Framework_TestCase {
  24. private $myhandler;
  25. function setUp() {
  26. $this->myhandler = new MyHandler;
  27. }
  28. function testBaseHandlerGetNoMethodNoArgs() {
  29. $this->assertHandlesGet('get ', $this->myhandler, array());
  30. }
  31. function testBaseHandlerGetNoMethodArgs() {
  32. $this->assertHandlesGet('get baz', $this->myhandler, array('baz'));
  33. }
  34. function testBaseHandlerGetMethodNoArgs() {
  35. $this->assertHandlesGet('getFoo', $this->myhandler, array('foo'));
  36. }
  37. function testBaseHandlerGetMethodArgs() {
  38. $this->assertHandlesGet('getBar foo-baz', $this->myhandler, array('bar', 'foo', 'baz'));
  39. }
  40. function testBaseHandlerPostNoMethodNoArgs() {
  41. $this->assertHandlesPost('post ', $this->myhandler, array());
  42. }
  43. function testBaseHandlerPostNoMethodArgs() {
  44. $this->assertHandlesPost('post baz', $this->myhandler, array('baz'));
  45. }
  46. function testBaseHandlerPostMethodNoArgs() {
  47. $this->assertHandlesPost('postFoo', $this->myhandler, array('foo'));
  48. }
  49. function testBaseHandlerPostMethodArgs() {
  50. $this->assertHandlesPost('postBar foo-baz', $this->myhandler, array('bar', 'foo', 'baz'));
  51. }
  52. function assertHandlesGet($result, $handler, array $args=array()) {
  53. $this->assertHandlesMethod($result, $handler, $args, 'GET');
  54. }
  55. function assertHandlesPost($result, $handler, array $args=array()) {
  56. $this->assertHandlesMethod($result, $handler, $args, 'POST');
  57. }
  58. function assertHandlesMethod($result, $handler, array $args, $request_method) {
  59. $_SERVER['REQUEST_METHOD'] = $request_method;
  60. $this->assertEquals($result, $handler->handleRequest($args));
  61. }
  62. }
  63. ?>