@@ -1160,6 +1160,28 @@ public function testSharedTables(): void
11601160 ->setNamespace ($ namespace )
11611161 ->setDatabase ($ schema );
11621162 }
1163+ /**
1164+ * @throws LimitException
1165+ * @throws DuplicateException
1166+ * @throws DatabaseException
1167+ */
1168+ public function testCreateDuplicates (): void
1169+ {
1170+ static ::getDatabase ()->createCollection ('duplicates ' , permissions: [
1171+ Permission::read (Role::any ())
1172+ ]);
1173+
1174+ try {
1175+ static ::getDatabase ()->createCollection ('duplicates ' );
1176+ $ this ->fail ('Failed to throw exception ' );
1177+ } catch (Exception $ e ) {
1178+ $ this ->assertInstanceOf (DuplicateException::class, $ e );
1179+ }
1180+
1181+ $ this ->assertNotEmpty (static ::getDatabase ()->listCollections ());
1182+
1183+ static ::getDatabase ()->deleteCollection ('duplicates ' );
1184+ }
11631185 public function testSharedTablesDuplicates (): void
11641186 {
11651187 $ database = static ::getDatabase ();
@@ -1223,4 +1245,253 @@ public function testSharedTablesDuplicates(): void
12231245 ->setNamespace ($ namespace )
12241246 ->setDatabase ($ schema );
12251247 }
1248+
1249+ public function testEvents (): void
1250+ {
1251+ Authorization::skip (function () {
1252+ $ database = static ::getDatabase ();
1253+
1254+ $ events = [
1255+ Database::EVENT_DATABASE_CREATE ,
1256+ Database::EVENT_DATABASE_LIST ,
1257+ Database::EVENT_COLLECTION_CREATE ,
1258+ Database::EVENT_COLLECTION_LIST ,
1259+ Database::EVENT_COLLECTION_READ ,
1260+ Database::EVENT_DOCUMENT_PURGE ,
1261+ Database::EVENT_ATTRIBUTE_CREATE ,
1262+ Database::EVENT_ATTRIBUTE_UPDATE ,
1263+ Database::EVENT_INDEX_CREATE ,
1264+ Database::EVENT_DOCUMENT_CREATE ,
1265+ Database::EVENT_DOCUMENT_PURGE ,
1266+ Database::EVENT_DOCUMENT_UPDATE ,
1267+ Database::EVENT_DOCUMENT_READ ,
1268+ Database::EVENT_DOCUMENT_FIND ,
1269+ Database::EVENT_DOCUMENT_FIND ,
1270+ Database::EVENT_DOCUMENT_COUNT ,
1271+ Database::EVENT_DOCUMENT_SUM ,
1272+ Database::EVENT_DOCUMENT_PURGE ,
1273+ Database::EVENT_DOCUMENT_INCREASE ,
1274+ Database::EVENT_DOCUMENT_PURGE ,
1275+ Database::EVENT_DOCUMENT_DECREASE ,
1276+ Database::EVENT_DOCUMENTS_CREATE ,
1277+ Database::EVENT_DOCUMENT_PURGE ,
1278+ Database::EVENT_DOCUMENT_PURGE ,
1279+ Database::EVENT_DOCUMENT_PURGE ,
1280+ Database::EVENT_DOCUMENTS_UPDATE ,
1281+ Database::EVENT_INDEX_DELETE ,
1282+ Database::EVENT_DOCUMENT_PURGE ,
1283+ Database::EVENT_DOCUMENT_DELETE ,
1284+ Database::EVENT_DOCUMENT_PURGE ,
1285+ Database::EVENT_DOCUMENT_PURGE ,
1286+ Database::EVENT_DOCUMENTS_DELETE ,
1287+ Database::EVENT_DOCUMENT_PURGE ,
1288+ Database::EVENT_ATTRIBUTE_DELETE ,
1289+ Database::EVENT_COLLECTION_DELETE ,
1290+ Database::EVENT_DATABASE_DELETE ,
1291+ Database::EVENT_DOCUMENT_PURGE ,
1292+ Database::EVENT_DOCUMENTS_DELETE ,
1293+ Database::EVENT_DOCUMENT_PURGE ,
1294+ Database::EVENT_ATTRIBUTE_DELETE ,
1295+ Database::EVENT_COLLECTION_DELETE ,
1296+ Database::EVENT_DATABASE_DELETE
1297+ ];
1298+
1299+ $ database ->on (Database::EVENT_ALL , 'test ' , function ($ event , $ data ) use (&$ events ) {
1300+ $ shifted = array_shift ($ events );
1301+ $ this ->assertEquals ($ shifted , $ event );
1302+ });
1303+
1304+ if ($ this ->getDatabase ()->getAdapter ()->getSupportForSchemas ()) {
1305+ $ database ->setDatabase ('hellodb ' );
1306+ $ database ->create ();
1307+ } else {
1308+ \array_shift ($ events );
1309+ }
1310+
1311+ $ database ->list ();
1312+
1313+ $ database ->setDatabase ($ this ->testDatabase );
1314+
1315+ $ collectionId = ID ::unique ();
1316+ $ database ->createCollection ($ collectionId );
1317+ $ database ->listCollections ();
1318+ $ database ->getCollection ($ collectionId );
1319+ $ database ->createAttribute ($ collectionId , 'attr1 ' , Database::VAR_INTEGER , 2 , false );
1320+ $ database ->updateAttributeRequired ($ collectionId , 'attr1 ' , true );
1321+ $ indexId1 = 'index2_ ' . uniqid ();
1322+ $ database ->createIndex ($ collectionId , $ indexId1 , Database::INDEX_KEY , ['attr1 ' ]);
1323+
1324+ $ document = $ database ->createDocument ($ collectionId , new Document ([
1325+ '$id ' => 'doc1 ' ,
1326+ 'attr1 ' => 10 ,
1327+ '$permissions ' => [
1328+ Permission::delete (Role::any ()),
1329+ Permission::update (Role::any ()),
1330+ Permission::read (Role::any ()),
1331+ ],
1332+ ]));
1333+
1334+ $ executed = false ;
1335+ $ database ->on (Database::EVENT_ALL , 'should-not-execute ' , function ($ event , $ data ) use (&$ executed ) {
1336+ $ executed = true ;
1337+ });
1338+
1339+ $ database ->silent (function () use ($ database , $ collectionId , $ document ) {
1340+ $ database ->updateDocument ($ collectionId , 'doc1 ' , $ document ->setAttribute ('attr1 ' , 15 ));
1341+ $ database ->getDocument ($ collectionId , 'doc1 ' );
1342+ $ database ->find ($ collectionId );
1343+ $ database ->findOne ($ collectionId );
1344+ $ database ->count ($ collectionId );
1345+ $ database ->sum ($ collectionId , 'attr1 ' );
1346+ $ database ->increaseDocumentAttribute ($ collectionId , $ document ->getId (), 'attr1 ' );
1347+ $ database ->decreaseDocumentAttribute ($ collectionId , $ document ->getId (), 'attr1 ' );
1348+ }, ['should-not-execute ' ]);
1349+
1350+ $ this ->assertFalse ($ executed );
1351+
1352+ $ database ->createDocuments ($ collectionId , [
1353+ new Document ([
1354+ 'attr1 ' => 10 ,
1355+ ]),
1356+ new Document ([
1357+ 'attr1 ' => 20 ,
1358+ ]),
1359+ ]);
1360+
1361+ $ database ->updateDocuments ($ collectionId , new Document ([
1362+ 'attr1 ' => 15 ,
1363+ ]));
1364+
1365+ $ database ->deleteIndex ($ collectionId , $ indexId1 );
1366+ $ database ->deleteDocument ($ collectionId , 'doc1 ' );
1367+
1368+ $ database ->deleteDocuments ($ collectionId );
1369+ $ database ->deleteAttribute ($ collectionId , 'attr1 ' );
1370+ $ database ->deleteCollection ($ collectionId );
1371+ $ database ->delete ('hellodb ' );
1372+
1373+ // Remove all listeners
1374+ $ database ->on (Database::EVENT_ALL , 'test ' , null );
1375+ $ database ->on (Database::EVENT_ALL , 'should-not-execute ' , null );
1376+ });
1377+ }
1378+
1379+ public function testCreatedAtUpdatedAt (): void
1380+ {
1381+ $ this ->assertInstanceOf ('Utopia\Database\Document ' , static ::getDatabase ()->createCollection ('created_at ' ));
1382+ static ::getDatabase ()->createAttribute ('created_at ' , 'title ' , Database::VAR_STRING , 100 , false );
1383+ $ document = static ::getDatabase ()->createDocument ('created_at ' , new Document ([
1384+ '$id ' => ID ::custom ('uid123 ' ),
1385+
1386+ '$permissions ' => [
1387+ Permission::read (Role::any ()),
1388+ Permission::create (Role::any ()),
1389+ Permission::update (Role::any ()),
1390+ Permission::delete (Role::any ()),
1391+ ],
1392+ ]));
1393+
1394+ $ this ->assertNotEmpty ($ document ->getInternalId ());
1395+ $ this ->assertNotNull ($ document ->getInternalId ());
1396+ }
1397+
1398+ /**
1399+ * @depends testCreatedAtUpdatedAt
1400+ */
1401+ public function testCreatedAtUpdatedAtAssert (): void
1402+ {
1403+ $ document = static ::getDatabase ()->getDocument ('created_at ' , 'uid123 ' );
1404+ $ this ->assertEquals (true , !$ document ->isEmpty ());
1405+ sleep (1 );
1406+ $ document ->setAttribute ('title ' , 'new title ' );
1407+ static ::getDatabase ()->updateDocument ('created_at ' , 'uid123 ' , $ document );
1408+ $ document = static ::getDatabase ()->getDocument ('created_at ' , 'uid123 ' );
1409+
1410+ $ this ->assertGreaterThan ($ document ->getCreatedAt (), $ document ->getUpdatedAt ());
1411+ $ this ->expectException (DuplicateException::class);
1412+
1413+ static ::getDatabase ()->createCollection ('created_at ' );
1414+ }
1415+
1416+ public function testEnableDisableValidation (): void
1417+ {
1418+ $ database = static ::getDatabase ();
1419+
1420+ $ database ->createCollection ('validation ' , permissions: [
1421+ Permission::create (Role::any ()),
1422+ Permission::read (Role::any ()),
1423+ Permission::update (Role::any ()),
1424+ Permission::delete (Role::any ())
1425+ ]);
1426+
1427+ $ database ->createAttribute (
1428+ 'validation ' ,
1429+ 'name ' ,
1430+ Database::VAR_STRING ,
1431+ 10 ,
1432+ false
1433+ );
1434+
1435+ $ database ->createDocument ('validation ' , new Document ([
1436+ '$id ' => 'docwithmorethan36charsasitsidentifier ' ,
1437+ 'name ' => 'value1 ' ,
1438+ ]));
1439+
1440+ try {
1441+ $ database ->find ('validation ' , queries: [
1442+ Query::equal ('$id ' , ['docwithmorethan36charsasitsidentifier ' ]),
1443+ ]);
1444+ $ this ->fail ('Failed to throw exception ' );
1445+ } catch (Exception $ e ) {
1446+ $ this ->assertInstanceOf (Exception::class, $ e );
1447+ }
1448+
1449+ $ database ->disableValidation ();
1450+
1451+ $ database ->find ('validation ' , queries: [
1452+ Query::equal ('$id ' , ['docwithmorethan36charsasitsidentifier ' ]),
1453+ ]);
1454+
1455+ $ database ->enableValidation ();
1456+
1457+ try {
1458+ $ database ->find ('validation ' , queries: [
1459+ Query::equal ('$id ' , ['docwithmorethan36charsasitsidentifier ' ]),
1460+ ]);
1461+ $ this ->fail ('Failed to throw exception ' );
1462+ } catch (Exception $ e ) {
1463+ $ this ->assertInstanceOf (Exception::class, $ e );
1464+ }
1465+
1466+ $ database ->skipValidation (function () use ($ database ) {
1467+ $ database ->find ('validation ' , queries: [
1468+ Query::equal ('$id ' , ['docwithmorethan36charsasitsidentifier ' ]),
1469+ ]);
1470+ });
1471+ }
1472+
1473+ public function testTransformations (): void
1474+ {
1475+ static ::getDatabase ()->createCollection ('docs ' , attributes: [
1476+ new Document ([
1477+ '$id ' => 'name ' ,
1478+ 'type ' => Database::VAR_STRING ,
1479+ 'size ' => 767 ,
1480+ 'required ' => true ,
1481+ ])
1482+ ]);
1483+
1484+ static ::getDatabase ()->createDocument ('docs ' , new Document ([
1485+ '$id ' => 'doc1 ' ,
1486+ 'name ' => 'value1 ' ,
1487+ ]));
1488+
1489+ static ::getDatabase ()->before (Database::EVENT_DOCUMENT_READ , 'test ' , function (string $ query ) {
1490+ return "SELECT 1 " ;
1491+ });
1492+
1493+ $ result = static ::getDatabase ()->getDocument ('docs ' , 'doc1 ' );
1494+
1495+ $ this ->assertTrue ($ result ->isEmpty ());
1496+ }
12261497}
0 commit comments