ServerEventsTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Sabre\DAV;
  3. use Sabre\HTTP;
  4. require_once 'Sabre/DAV/AbstractServer.php';
  5. class ServerEventsTest extends AbstractServer {
  6. private $tempPath;
  7. private $exception;
  8. function testAfterBind() {
  9. $this->server->on('afterBind', [$this, 'afterBindHandler']);
  10. $newPath = 'afterBind';
  11. $this->tempPath = '';
  12. $this->server->createFile($newPath, 'body');
  13. $this->assertEquals($newPath, $this->tempPath);
  14. }
  15. function afterBindHandler($path) {
  16. $this->tempPath = $path;
  17. }
  18. function testAfterResponse() {
  19. $mock = $this->getMock('stdClass', ['afterResponseCallback']);
  20. $mock->expects($this->once())->method('afterResponseCallback');
  21. $this->server->on('afterResponse', [$mock, 'afterResponseCallback']);
  22. $this->server->httpRequest = HTTP\Sapi::createFromServerArray([
  23. 'REQUEST_METHOD' => 'GET',
  24. 'REQUEST_URI' => '/test.txt',
  25. ]);
  26. $this->server->exec();
  27. }
  28. function testBeforeBindCancel() {
  29. $this->server->on('beforeBind', [$this, 'beforeBindCancelHandler']);
  30. $this->assertFalse($this->server->createFile('bla', 'body'));
  31. // Also testing put()
  32. $req = HTTP\Sapi::createFromServerArray([
  33. 'REQUEST_METHOD' => 'PUT',
  34. 'REQUEST_URI' => '/barbar',
  35. ]);
  36. $this->server->httpRequest = $req;
  37. $this->server->exec();
  38. $this->assertEquals(500, $this->server->httpResponse->getStatus());
  39. }
  40. function beforeBindCancelHandler($path) {
  41. return false;
  42. }
  43. function testException() {
  44. $this->server->on('exception', [$this, 'exceptionHandler']);
  45. $req = HTTP\Sapi::createFromServerArray([
  46. 'REQUEST_METHOD' => 'GET',
  47. 'REQUEST_URI' => '/not/exisitng',
  48. ]);
  49. $this->server->httpRequest = $req;
  50. $this->server->exec();
  51. $this->assertInstanceOf('Sabre\\DAV\\Exception\\NotFound', $this->exception);
  52. }
  53. function exceptionHandler(Exception $exception) {
  54. $this->exception = $exception;
  55. }
  56. function testMethod() {
  57. $k = 1;
  58. $this->server->on('method', function($request, $response) use (&$k) {
  59. $k += 1;
  60. return false;
  61. });
  62. $this->server->on('method', function($request, $response) use (&$k) {
  63. $k += 2;
  64. return false;
  65. });
  66. try {
  67. $this->server->invokeMethod(
  68. new HTTP\Request('BLABLA', '/'),
  69. new HTTP\Response(),
  70. false
  71. );
  72. } catch (Exception $e) {}
  73. $this->assertEquals(2, $k);
  74. }
  75. }