|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kreait\Firebase\Tests\Integration\Factory; |
| 6 | + |
| 7 | +use Google\Cloud\Core\Exception\NotFoundException; |
| 8 | +use Kreait\Firebase\Tests\IntegrationTestCase; |
| 9 | +use Kreait\Firebase\Util; |
| 10 | +use PHPUnit\Framework\Attributes\Test; |
| 11 | + |
| 12 | +/** |
| 13 | + * @internal |
| 14 | + */ |
| 15 | +final class FirestoreTest extends IntegrationTestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * If we write a document into the instance created without an explicit database name, |
| 19 | + * we should be able to read it from the instance created with an explicit database name. |
| 20 | + */ |
| 21 | + #[Test] |
| 22 | + public function itUsesTheDefaultDatabaseByDefault(): void |
| 23 | + { |
| 24 | + $collection = __FUNCTION__; |
| 25 | + $documentName = __FUNCTION__.self::randomString(); |
| 26 | + |
| 27 | + $default = self::$factory->createFirestore()->database()->collection($collection); |
| 28 | + $explicit = self::$factory->createFirestore('(default)')->database()->collection($collection); |
| 29 | + |
| 30 | + try { |
| 31 | + $default->document($documentName)->create(['field' => 'value']); |
| 32 | + |
| 33 | + $this->assertTrue($explicit->document($documentName)->snapshot()->exists()); |
| 34 | + $this->assertSame(['field' => 'value'], $explicit->document($documentName)->snapshot()->data()); |
| 35 | + } finally { |
| 36 | + $default->document($documentName)->delete(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + #[Test] |
| 41 | + public function testItCannotConnectToAnUnknownDatabase(): void |
| 42 | + { |
| 43 | + $name = self::randomString(); |
| 44 | + |
| 45 | + $database = self::$factory->createFirestore($name)->database(); |
| 46 | + |
| 47 | + $this->expectException(NotFoundException::class); |
| 48 | + // No need to deserialize the returned JSON |
| 49 | + $this->expectExceptionMessageMatches("/$name/"); |
| 50 | + |
| 51 | + $database->collection('foo')->document(__FUNCTION__)->create(); |
| 52 | + } |
| 53 | + |
| 54 | + #[Test] |
| 55 | + public function itCanConnectToACustomDatabase(): void |
| 56 | + { |
| 57 | + $collection = __FUNCTION__; |
| 58 | + $documentName = __FUNCTION__.self::randomString(); |
| 59 | + |
| 60 | + $database = self::$factory->createFirestore($this->customDBName())->database(); |
| 61 | + |
| 62 | + try { |
| 63 | + $database->collection($collection)->document($documentName)->create(); |
| 64 | + $this->assertTrue($database->collection($collection)->document($documentName)->snapshot()->exists()); |
| 65 | + } finally { |
| 66 | + $database->collection($collection)->document($documentName)->delete(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @deprecated 7.19.0 |
| 72 | + */ |
| 73 | + #[Test] |
| 74 | + public function itSupportsOverridingTheDefaultFirestoreDatabase(): void |
| 75 | + { |
| 76 | + $collection = __FUNCTION__; |
| 77 | + $documentName = __FUNCTION__.self::randomString(); |
| 78 | + |
| 79 | + $database = self::$factory->withFirestoreDatabase($this->customDBName())->createFirestore()->database(); |
| 80 | + |
| 81 | + try { |
| 82 | + $database->collection($collection)->document($documentName)->create(); |
| 83 | + $this->assertTrue($database->collection($collection)->document($documentName)->snapshot()->exists()); |
| 84 | + } finally { |
| 85 | + $database->collection($collection)->document($documentName)->delete(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + #[Test] |
| 90 | + public function itSupportsAdditionalFirestoreConfig(): void |
| 91 | + { |
| 92 | + $collection = __FUNCTION__; |
| 93 | + $documentName = __FUNCTION__.self::randomString(); |
| 94 | + |
| 95 | + $database = self::$factory |
| 96 | + ->withFirestoreClientConfig(['database' => $this->customDBName()]) |
| 97 | + ->createFirestore()->database(); |
| 98 | + |
| 99 | + try { |
| 100 | + $database->collection($collection)->document($documentName)->create(); |
| 101 | + $this->assertTrue($database->collection($collection)->document($documentName)->snapshot()->exists()); |
| 102 | + } finally { |
| 103 | + $database->collection($collection)->document($documentName)->delete(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @return non-empty-string |
| 109 | + */ |
| 110 | + private function customDBName(): string |
| 111 | + { |
| 112 | + $customDBName = Util::getenv('TEST_FIRESTORE_CUSTOM_DB_NAME'); |
| 113 | + |
| 114 | + if ($customDBName == null) { |
| 115 | + $this->markTestSkipped('No custom Firestore DB name set via the environment variable `TEST_FIRESTORE_CUSTOM_DB_NAME`'); |
| 116 | + } |
| 117 | + |
| 118 | + return $customDBName; |
| 119 | + } |
| 120 | +} |
0 commit comments