1<?php
2
3namespace Sabre\DAV\PartialUpdate;
4
5use Sabre\DAV\FSExt\File;
6use Sabre\DAV\Server;
7use Sabre\HTTP;
8
9/**
10 * This test is an end-to-end sabredav test that goes through all
11 * the cases in the specification.
12 *
13 * See: http://sabre.io/dav/http-patch/
14 */
15class SpecificationTest extends \PHPUnit_Framework_TestCase {
16
17    protected $server;
18
19    public function setUp() {
20
21        $tree = array(
22            new File(SABRE_TEMPDIR . '/foobar.txt')
23        );
24        $server = new Server($tree);
25        $server->debugExceptions = true;
26        $server->addPlugin(new Plugin());
27
28        $tree[0]->put('1234567890');
29
30        $this->server = $server;
31
32    }
33
34    public function tearDown() {
35
36        \Sabre\TestUtil::clearTempDir();
37
38    }
39
40    /**
41     * @dataProvider data
42     */
43    public function testUpdateRange($headerValue, $httpStatus, $endResult, $contentLength = 4) {
44
45        $headers = [
46            'Content-Type' => 'application/x-sabredav-partialupdate',
47            'X-Update-Range' => $headerValue,
48        ];
49
50        if ($contentLength) {
51            $headers['Content-Length'] = (string)$contentLength;
52        }
53
54        $request = new HTTP\Request('PATCH', '/foobar.txt', $headers, '----');
55
56        $request->setBody('----');
57        $this->server->httpRequest = $request;
58        $this->server->httpResponse = new HTTP\ResponseMock();
59        $this->server->sapi = new HTTP\SapiMock();
60        $this->server->exec();
61
62        $this->assertEquals($httpStatus, $this->server->httpResponse->status, 'Incorrect http status received: ' . $this->server->httpResponse->body);
63        if (!is_null($endResult)) {
64            $this->assertEquals($endResult, file_get_contents(SABRE_TEMPDIR . '/foobar.txt'));
65        }
66
67    }
68
69    public function data() {
70
71        return array(
72            // Problems
73            array('foo',       400, null),
74            array('bytes=0-3', 411, null, 0),
75            array('bytes=4-1', 416, null),
76
77            array('bytes=0-3', 204, '----567890'),
78            array('bytes=1-4', 204, '1----67890'),
79            array('bytes=0-',  204, '----567890'),
80            array('bytes=-4',  204, '123456----'),
81            array('bytes=-2',  204, '12345678----'),
82            array('bytes=2-',  204, '12----7890'),
83            array('append',    204, '1234567890----'),
84
85        );
86
87    }
88
89}
90