2020use Utopia \Database \Helpers \Role ;
2121use Utopia \Database \Query ;
2222use Utopia \Database \Validator \Authorization ;
23+ use function _PHPStan_bc6352b8e \Symfony \Component \String \s ;
2324
2425trait GeneralTests
2526{
2627 public function testPing (): void
2728 {
28- $ this ->assertEquals (true , static ::getDatabase ()->ping ());
29+ /** @var Database $database */
30+ $ database = static ::getDatabase ();
31+
32+ $ this ->assertEquals (true , $ database ->ping ());
2933 }
3034
3135 /**
@@ -43,11 +47,14 @@ public function testQueryTimeout(): void
4347 return ;
4448 }
4549
46- static ::getDatabase ()->createCollection ('global-timeouts ' );
50+ /** @var Database $database */
51+ $ database = static ::getDatabase ();
52+
53+ $ database ->createCollection ('global-timeouts ' );
4754
4855 $ this ->assertEquals (
4956 true ,
50- static :: getDatabase () ->createAttribute (
57+ $ database ->createAttribute (
5158 collection: 'global-timeouts ' ,
5259 id: 'longtext ' ,
5360 type: Database::VAR_STRING ,
@@ -57,7 +64,7 @@ public function testQueryTimeout(): void
5764 );
5865
5966 for ($ i = 0 ; $ i < 20 ; $ i ++) {
60- static :: getDatabase () ->createDocument ('global-timeouts ' , new Document ([
67+ $ database ->createDocument ('global-timeouts ' , new Document ([
6168 'longtext ' => file_get_contents (__DIR__ . '/../../../resources/longtext.txt ' ),
6269 '$permissions ' => [
6370 Permission::read (Role::any ()),
@@ -67,16 +74,16 @@ public function testQueryTimeout(): void
6774 ]));
6875 }
6976
70- static :: getDatabase () ->setTimeout (1 );
77+ $ database ->setTimeout (1 );
7178
7279 try {
73- static :: getDatabase () ->find ('global-timeouts ' , [
80+ $ database ->find ('global-timeouts ' , [
7481 Query::notEqual ('longtext ' , 'appwrite ' ),
7582 ]);
7683 $ this ->fail ('Failed to throw exception ' );
7784 } catch (\Exception $ e ) {
78- static :: getDatabase () ->clearTimeout ();
79- static :: getDatabase () ->deleteCollection ('global-timeouts ' );
85+ $ database ->clearTimeout ();
86+ $ database ->deleteCollection ('global-timeouts ' );
8087 $ this ->assertInstanceOf (TimeoutException::class, $ e );
8188 }
8289 }
@@ -87,25 +94,28 @@ public function testPreserveDatesUpdate(): void
8794 {
8895 Authorization::disable ();
8996
90- static ::getDatabase ()->setPreserveDates (true );
97+ /** @var Database $database */
98+ $ database = static ::getDatabase ();
99+
100+ $ database ->setPreserveDates (true );
91101
92- static :: getDatabase () ->createCollection ('preserve_update_dates ' );
102+ $ database ->createCollection ('preserve_update_dates ' );
93103
94- static :: getDatabase () ->createAttribute ('preserve_update_dates ' , 'attr1 ' , Database::VAR_STRING , 10 , false );
104+ $ database ->createAttribute ('preserve_update_dates ' , 'attr1 ' , Database::VAR_STRING , 10 , false );
95105
96- $ doc1 = static :: getDatabase () ->createDocument ('preserve_update_dates ' , new Document ([
106+ $ doc1 = $ database ->createDocument ('preserve_update_dates ' , new Document ([
97107 '$id ' => 'doc1 ' ,
98108 '$permissions ' => [],
99109 'attr1 ' => 'value1 ' ,
100110 ]));
101111
102- $ doc2 = static :: getDatabase () ->createDocument ('preserve_update_dates ' , new Document ([
112+ $ doc2 = $ database ->createDocument ('preserve_update_dates ' , new Document ([
103113 '$id ' => 'doc2 ' ,
104114 '$permissions ' => [],
105115 'attr1 ' => 'value2 ' ,
106116 ]));
107117
108- $ doc3 = static :: getDatabase () ->createDocument ('preserve_update_dates ' , new Document ([
118+ $ doc3 = $ database ->createDocument ('preserve_update_dates ' , new Document ([
109119 '$id ' => 'doc3 ' ,
110120 '$permissions ' => [],
111121 'attr1 ' => 'value3 ' ,
@@ -114,9 +124,9 @@ public function testPreserveDatesUpdate(): void
114124 $ newDate = '2000-01-01T10:00:00.000+00:00 ' ;
115125
116126 $ doc1 ->setAttribute ('$updatedAt ' , $ newDate );
117- $ doc1 = static :: getDatabase () ->updateDocument ('preserve_update_dates ' , 'doc1 ' , $ doc1 );
127+ $ doc1 = $ database ->updateDocument ('preserve_update_dates ' , 'doc1 ' , $ doc1 );
118128 $ this ->assertEquals ($ newDate , $ doc1 ->getAttribute ('$updatedAt ' ));
119- $ doc1 = static :: getDatabase () ->getDocument ('preserve_update_dates ' , 'doc1 ' );
129+ $ doc1 = $ database ->getDocument ('preserve_update_dates ' , 'doc1 ' );
120130 $ this ->assertEquals ($ newDate , $ doc1 ->getAttribute ('$updatedAt ' ));
121131
122132 $ this ->getDatabase ()->updateDocuments (
@@ -132,14 +142,14 @@ public function testPreserveDatesUpdate(): void
132142 ]
133143 );
134144
135- $ doc2 = static :: getDatabase () ->getDocument ('preserve_update_dates ' , 'doc2 ' );
136- $ doc3 = static :: getDatabase () ->getDocument ('preserve_update_dates ' , 'doc3 ' );
145+ $ doc2 = $ database ->getDocument ('preserve_update_dates ' , 'doc2 ' );
146+ $ doc3 = $ database ->getDocument ('preserve_update_dates ' , 'doc3 ' );
137147 $ this ->assertEquals ($ newDate , $ doc2 ->getAttribute ('$updatedAt ' ));
138148 $ this ->assertEquals ($ newDate , $ doc3 ->getAttribute ('$updatedAt ' ));
139149
140- static :: getDatabase () ->deleteCollection ('preserve_update_dates ' );
150+ $ database ->deleteCollection ('preserve_update_dates ' );
141151
142- static :: getDatabase () ->setPreserveDates (false );
152+ $ database ->setPreserveDates (false );
143153
144154 Authorization::reset ();
145155 }
@@ -148,22 +158,25 @@ public function testPreserveDatesCreate(): void
148158 {
149159 Authorization::disable ();
150160
151- static ::getDatabase ()->setPreserveDates (true );
161+ /** @var Database $database */
162+ $ database = static ::getDatabase ();
152163
153- static :: getDatabase ()-> createCollection ( ' preserve_create_dates ' );
164+ $ database -> setPreserveDates ( true );
154165
155- static ::getDatabase ()->createAttribute ('preserve_create_dates ' , 'attr1 ' , Database::VAR_STRING , 10 , false );
166+ $ database ->createCollection ('preserve_create_dates ' );
167+
168+ $ database ->createAttribute ('preserve_create_dates ' , 'attr1 ' , Database::VAR_STRING , 10 , false );
156169
157170 $ date = '2000-01-01T10:00:00.000+00:00 ' ;
158171
159- static :: getDatabase () ->createDocument ('preserve_create_dates ' , new Document ([
172+ $ database ->createDocument ('preserve_create_dates ' , new Document ([
160173 '$id ' => 'doc1 ' ,
161174 '$permissions ' => [],
162175 'attr1 ' => 'value1 ' ,
163176 '$createdAt ' => $ date
164177 ]));
165178
166- static :: getDatabase () ->createDocuments ('preserve_create_dates ' , [
179+ $ database ->createDocuments ('preserve_create_dates ' , [
167180 new Document ([
168181 '$id ' => 'doc2 ' ,
169182 '$permissions ' => [],
@@ -178,16 +191,16 @@ public function testPreserveDatesCreate(): void
178191 ]),
179192 ], batchSize: 2 );
180193
181- $ doc1 = static :: getDatabase () ->getDocument ('preserve_create_dates ' , 'doc1 ' );
182- $ doc2 = static :: getDatabase () ->getDocument ('preserve_create_dates ' , 'doc2 ' );
183- $ doc3 = static :: getDatabase () ->getDocument ('preserve_create_dates ' , 'doc3 ' );
194+ $ doc1 = $ database ->getDocument ('preserve_create_dates ' , 'doc1 ' );
195+ $ doc2 = $ database ->getDocument ('preserve_create_dates ' , 'doc2 ' );
196+ $ doc3 = $ database ->getDocument ('preserve_create_dates ' , 'doc3 ' );
184197 $ this ->assertEquals ($ date , $ doc1 ->getAttribute ('$createdAt ' ));
185198 $ this ->assertEquals ($ date , $ doc2 ->getAttribute ('$createdAt ' ));
186199 $ this ->assertEquals ($ date , $ doc3 ->getAttribute ('$createdAt ' ));
187200
188- static :: getDatabase () ->deleteCollection ('preserve_create_dates ' );
201+ $ database ->deleteCollection ('preserve_create_dates ' );
189202
190- static :: getDatabase () ->setPreserveDates (false );
203+ $ database ->setPreserveDates (false );
191204
192205 Authorization::reset ();
193206 }
@@ -271,7 +284,11 @@ public function testFindOrderByAfterException(): void
271284 ]);
272285
273286 $ this ->expectException (Exception::class);
274- static ::getDatabase ()->find ('movies ' , [
287+
288+ /** @var Database $database */
289+ $ database = static ::getDatabase ();
290+
291+ $ database ->find ('movies ' , [
275292 Query::limit (2 ),
276293 Query::offset (0 ),
277294 Query::cursorAfter ($ document )
@@ -405,7 +422,7 @@ public function testSharedTablesTenantPerDocument(): void
405422 $ this ->assertEquals (1 , \count ($ docs ));
406423 $ this ->assertEquals ($ doc1Id , $ docs [0 ]->getId ());
407424
408- if (static :: getDatabase () ->getAdapter ()->getSupportForUpserts ()) {
425+ if ($ database ->getAdapter ()->getSupportForUpserts ()) {
409426 // Test upsert with tenant per doc
410427 $ doc3Id = ID ::unique ();
411428 $ database
@@ -521,7 +538,10 @@ public function testSharedTablesTenantPerDocument(): void
521538
522539 public function testCacheFallback (): void
523540 {
524- if (!static ::getDatabase ()->getAdapter ()->getSupportForCacheSkipOnFailure ()) {
541+ /** @var Database $database */
542+ $ database = static ::getDatabase ();
543+
544+ if (!$ database ->getAdapter ()->getSupportForCacheSkipOnFailure ()) {
525545 $ this ->expectNotToPerformAssertions ();
526546 return ;
527547 }
0 commit comments