|
| 1 | +<?php |
| 2 | + |
| 3 | +require __DIR__ . "/../../vendor/autoload.php"; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | +use GuzzleHttp\Handler\MockHandler; |
| 7 | +use GuzzleHttp\HandlerStack; |
| 8 | +use GuzzleHttp\Psr7\Response; |
| 9 | +use GuzzleHttp\Psr7\Request; |
| 10 | +use GuzzleHttp\Exception\RequestException; |
| 11 | + |
| 12 | +class DocumentTest extends \PHPUnit\Framework\TestCase |
| 13 | +{ |
| 14 | + public function setUp() { |
| 15 | + // create the first request to check we can connect, can be added to |
| 16 | + // the mocks for any test that wants it |
| 17 | + $couchdb1 = '{"couchdb":"Welcome","uuid":"fce3d5aabfe189c988273c0ffa8d375b","version":"1.6.0","vendor":{"name":"Ubuntu","version":"15.10"}}'; |
| 18 | + $this->db_response = new Response(200, [], $couchdb1); |
| 19 | + |
| 20 | + // offer a use_response for when selecting this database |
| 21 | + $egdb1 = '{"db_name":"egdb","update_seq":"0-g1AAAABXeJzLYWBgYMpgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUklMiTV____PyuRAY-iPBYgydAApP5D1GYBAJmvHGw","sizes":{"file":8488,"external":0,"active":0},"purge_seq":0,"other":{"data_size":0},"doc_del_count":0,"doc_count":0,"disk_size":8488,"disk_format_version":6,"data_size":0,"compact_running":false,"instance_start_time":"0"}'; |
| 22 | + $this->use_response = new Response(200, [], $egdb1); |
| 23 | + |
| 24 | + $create = '{"ok":true,"id":"abcde12345","rev":"1-928ec193918889e122e7ad45cfd88e47"}'; |
| 25 | + $this->create_response = new Response(201, [], $create); |
| 26 | + $fetch = '{"_id":"abcde12345","_rev":"1-928ec193918889e122e7ad45cfd88e47","noise":"howl"}'; |
| 27 | + $this->fetch_response = new Response(200, [], $fetch); |
| 28 | + } |
| 29 | + |
| 30 | + public function testUpdateConflict() { |
| 31 | + $update = '{"ok":true,"id":"abcde12345","rev":"2-74a0465bd6e3ea40a1a3752b93916762"}'; |
| 32 | + $update_response = new Response(200, [], $update); |
| 33 | + |
| 34 | + $fetch2 = '{"_id":"abcde12345","_rev":"1-928ec193918889e122e7ad45cfd88e47","noise":"howl"}'; |
| 35 | + $fetch_response2 = new Response(200, [], $fetch2); |
| 36 | + |
| 37 | + $mock = new MockHandler([ $this->db_response, $this->use_response, $this->create_response, $this->fetch_response, $update_response, $fetch_response2 ]); |
| 38 | + $handler = HandlerStack::create($mock); |
| 39 | + $client = new Client(['handler' => $handler]); |
| 40 | + |
| 41 | + // userland code starts |
| 42 | + $server = new \PHPCouchDB\Server(["client" => $client]); |
| 43 | + $database = $server->useDB(["name" => "egdb"]); |
| 44 | + $doc = $database->create(["noise" => "howl", "id" => "abcde12345"]); |
| 45 | + |
| 46 | + $doc->noise = "purr"; |
| 47 | + $newdoc = $doc->update(); |
| 48 | + |
| 49 | + $this->assertInstanceOf('PHPCouchDB\Document', $newdoc); |
| 50 | + $this->assertObjectHasAttribute('id', $doc); |
| 51 | + $this->assertEquals("abcde12345", $doc->id); |
| 52 | + $this->assertEquals("purr", $doc->noise); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @expectedException \PHPCouchDB\Exception\DocumentConflictException |
| 57 | + */ |
| 58 | + public function testUpdate() { |
| 59 | + $update = '{"error":"conflict","reason":"Document update conflict."}';; |
| 60 | + $update_response = new Response(409, [], $update); |
| 61 | + |
| 62 | + $mock = new MockHandler([ $this->db_response, $this->use_response, $this->create_response, $this->fetch_response, $update_response ]); |
| 63 | + $handler = HandlerStack::create($mock); |
| 64 | + $client = new Client(['handler' => $handler]); |
| 65 | + |
| 66 | + // userland code starts |
| 67 | + $server = new \PHPCouchDB\Server(["client" => $client]); |
| 68 | + $database = $server->useDB(["name" => "egdb"]); |
| 69 | + $doc = $database->create(["noise" => "howl", "id" => "abcde12345"]); |
| 70 | + |
| 71 | + $doc->noise = "purr"; |
| 72 | + $newdoc = $doc->update(); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments