|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2017 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Google\Cloud\Firestore\Tests\System; |
| 19 | + |
| 20 | +use Google\ApiCore\ApiException; |
| 21 | +use Google\ApiCore\BidiStream; |
| 22 | +use Google\Cloud\Firestore\V1\Client\FirestoreClient; |
| 23 | +use Google\Cloud\Firestore\V1\ListenRequest; |
| 24 | +use Google\Cloud\Firestore\V1\ListenResponse; |
| 25 | + |
| 26 | +/** |
| 27 | + * @group firestore |
| 28 | + * @group firestore-listen |
| 29 | + */ |
| 30 | +class ListenTest extends FirestoreTestCase |
| 31 | +{ |
| 32 | + const DATABASE = 'projects/%s/databases/(default)'; |
| 33 | + |
| 34 | + private $query; |
| 35 | + |
| 36 | + public function setUp(): void |
| 37 | + { |
| 38 | + $this->query = self::$client->collection(uniqid(self::COLLECTION_NAME)); |
| 39 | + self::$localDeletionQueue->add($this->query); |
| 40 | + if (!$this->projectId = getenv('GOOGLE_CLOUD_FIRESTORE_PROJECT')) { |
| 41 | + $this->markTestSkipped('please set the GOOGLE_CLOUD_FIRESTORE_PROJECT env var'); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public function testListen() |
| 46 | + { |
| 47 | + $database = sprintf(self::DATABASE, $this->projectId); |
| 48 | + |
| 49 | + // Create a client. |
| 50 | + $firestoreClient = new FirestoreClient(); |
| 51 | + |
| 52 | + // Prepare the request message. |
| 53 | + $request = (new ListenRequest()) |
| 54 | + ->setDatabase($database); |
| 55 | + |
| 56 | + // Call the API and handle any network failures. |
| 57 | + /** @var BidiStream $stream */ |
| 58 | + $stream = $firestoreClient->listen([ |
| 59 | + 'headers' => [ |
| 60 | + 'x-goog-request-params' => [ |
| 61 | + 'database=' . $database |
| 62 | + ] |
| 63 | + ] |
| 64 | + ]); |
| 65 | + $stream->writeAll([$request,]); |
| 66 | + |
| 67 | + /** @var ListenResponse $element */ |
| 68 | + foreach ($stream->closeWriteAndReadAll() as $element) { |
| 69 | + // TODO: Assert something |
| 70 | + } |
| 71 | + $this->assertTrue(true); |
| 72 | + } |
| 73 | + |
| 74 | + public function testListenThrowsExceptionWithoutDatabaseHeader() |
| 75 | + { |
| 76 | + $this->expectException(ApiException::class); |
| 77 | + $this->expectExceptionMessage( |
| 78 | + 'Missing required http header (\'google-cloud-resource-prefix\' or \'x-goog-request-params\')' |
| 79 | + . ' or query param \'database\'.' |
| 80 | + ); |
| 81 | + $database = sprintf(self::DATABASE, $this->projectId); |
| 82 | + |
| 83 | + // Create a client. |
| 84 | + $firestoreClient = new FirestoreClient(); |
| 85 | + |
| 86 | + // Prepare the request message. |
| 87 | + $request = (new ListenRequest()) |
| 88 | + ->setDatabase($database); |
| 89 | + |
| 90 | + // Call the API and handle any network failures. |
| 91 | + /** @var BidiStream $stream */ |
| 92 | + $stream = $firestoreClient->listen(); |
| 93 | + $stream->writeAll([$request,]); |
| 94 | + |
| 95 | + /** @var ListenResponse $element */ |
| 96 | + foreach ($stream->closeWriteAndReadAll() as $element) { |
| 97 | + // TODO: Assert something |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments