1<?php
2
3namespace Sabre\DAV;
4use Sabre\HTTP;
5
6require_once 'Sabre/DAV/AbstractServer.php';
7
8class ServerEventsTest extends AbstractServer {
9
10    private $tempPath;
11
12    private $exception;
13
14    function testAfterBind() {
15
16        $this->server->on('afterBind', [$this,'afterBindHandler']);
17        $newPath = 'afterBind';
18
19        $this->tempPath = '';
20        $this->server->createFile($newPath,'body');
21        $this->assertEquals($newPath, $this->tempPath);
22
23    }
24
25    function afterBindHandler($path) {
26
27       $this->tempPath = $path;
28
29    }
30
31    function testAfterResponse() {
32
33        $mock = $this->getMock('stdClass', array('afterResponseCallback'));
34        $mock->expects($this->once())->method('afterResponseCallback');
35
36        $this->server->on('afterResponse', [$mock, 'afterResponseCallback']);
37
38        $this->server->httpRequest = HTTP\Sapi::createFromServerArray(array(
39            'REQUEST_METHOD'    => 'GET',
40            'REQUEST_URI'       => '/test.txt',
41        ));
42
43        $this->server->exec();
44
45    }
46
47    function testBeforeBindCancel() {
48
49        $this->server->on('beforeBind', [$this,'beforeBindCancelHandler']);
50        $this->assertFalse($this->server->createFile('bla','body'));
51
52        // Also testing put()
53        $req = HTTP\Sapi::createFromServerArray(array(
54            'REQUEST_METHOD' => 'PUT',
55            'REQUEST_URI' => '/barbar',
56        ));
57
58        $this->server->httpRequest = $req;
59        $this->server->exec();
60
61        $this->assertEquals('',$this->server->httpResponse->status);
62
63    }
64
65    function beforeBindCancelHandler() {
66
67        return false;
68
69    }
70
71    function testException() {
72
73        $this->server->on('exception', [$this, 'exceptionHandler']);
74
75        $req = HTTP\Sapi::createFromServerArray(array(
76            'REQUEST_METHOD' => 'GET',
77            'REQUEST_URI' => '/not/exisitng',
78        ));
79        $this->server->httpRequest = $req;
80        $this->server->exec();
81
82        $this->assertInstanceOf('Sabre\\DAV\\Exception\\NotFound', $this->exception);
83
84    }
85
86    function exceptionHandler(Exception $exception) {
87
88        $this->exception = $exception;
89
90    }
91
92    function testMethod() {
93
94        $k = 1;
95        $this->server->on('method', function() use (&$k) {
96
97            $k+=1;
98
99            return false;
100
101        });
102        $this->server->on('method', function() use (&$k) {
103
104            $k+=2;
105
106            return false;
107
108        });
109
110        $this->server->invokeMethod(
111            new HTTP\Request('BLABLA', '/'),
112            new HTTP\Response(),
113            false
114        );
115
116        $this->assertEquals(2, $k);
117
118
119    }
120
121}
122