|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\E2E\Adapter; |
| 4 | + |
| 5 | +use Redis; |
| 6 | +use ReflectionClass; |
| 7 | +use Utopia\Cache\Adapter\Redis as RedisAdapter; |
| 8 | +use Utopia\Cache\Cache; |
| 9 | +use Utopia\Database\Adapter; |
| 10 | +use Utopia\Database\Adapter\MySQL; |
| 11 | +use Utopia\Database\Adapter\Pool; |
| 12 | +use Utopia\Database\Database; |
| 13 | +use Utopia\Database\Exception; |
| 14 | +use Utopia\Database\Exception\Duplicate; |
| 15 | +use Utopia\Database\Exception\Limit; |
| 16 | +use Utopia\Database\PDO; |
| 17 | +use Utopia\Pools\Pool as UtopiaPool; |
| 18 | + |
| 19 | +class PoolTest extends Base |
| 20 | +{ |
| 21 | + public static ?Database $database = null; |
| 22 | + protected static ?UtopiaPool $pool = null; |
| 23 | + protected static string $namespace; |
| 24 | + |
| 25 | + /** |
| 26 | + * @return Database |
| 27 | + * @throws Exception |
| 28 | + * @throws Duplicate |
| 29 | + * @throws Limit |
| 30 | + */ |
| 31 | + public static function getDatabase(): Database |
| 32 | + { |
| 33 | + if (!is_null(self::$database)) { |
| 34 | + return self::$database; |
| 35 | + } |
| 36 | + |
| 37 | + $redis = new Redis(); |
| 38 | + $redis->connect('redis', 6379); |
| 39 | + $redis->flushAll(); |
| 40 | + $cache = new Cache(new RedisAdapter($redis)); |
| 41 | + |
| 42 | + $pool = new UtopiaPool('mysql', 10, function () { |
| 43 | + $dbHost = 'mysql'; |
| 44 | + $dbPort = '3307'; |
| 45 | + $dbUser = 'root'; |
| 46 | + $dbPass = 'password'; |
| 47 | + |
| 48 | + return new MySQL(new PDO( |
| 49 | + dsn: "mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", |
| 50 | + username: $dbUser, |
| 51 | + password: $dbPass, |
| 52 | + config: MySQL::getPDOAttributes(), |
| 53 | + )); |
| 54 | + }); |
| 55 | + |
| 56 | + $database = new Database(new Pool($pool), $cache); |
| 57 | + |
| 58 | + $database |
| 59 | + ->setDatabase('utopiaTests') |
| 60 | + ->setNamespace(static::$namespace = 'myapp_' . uniqid()); |
| 61 | + |
| 62 | + if ($database->exists()) { |
| 63 | + $database->delete(); |
| 64 | + } |
| 65 | + |
| 66 | + $database->create(); |
| 67 | + |
| 68 | + self::$pool = $pool; |
| 69 | + |
| 70 | + return self::$database = $database; |
| 71 | + } |
| 72 | + |
| 73 | + protected static function deleteColumn(string $collection, string $column): bool |
| 74 | + { |
| 75 | + $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; |
| 76 | + $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; |
| 77 | + |
| 78 | + self::$pool->use(function (Adapter $adapter) use ($sql) { |
| 79 | + // Hack to get adapter PDO |
| 80 | + $class = new ReflectionClass($adapter); |
| 81 | + $property = $class->getProperty('pdo'); |
| 82 | + $property->setAccessible(true); |
| 83 | + $pdo = $property->getValue($adapter); |
| 84 | + $pdo->exec($sql); |
| 85 | + }); |
| 86 | + |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + protected static function deleteIndex(string $collection, string $index): bool |
| 91 | + { |
| 92 | + $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; |
| 93 | + $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; |
| 94 | + |
| 95 | + self::$pool->use(function (Adapter $adapter) use ($sql) { |
| 96 | + // Hack to get adapter PDO |
| 97 | + $class = new ReflectionClass($adapter); |
| 98 | + $property = $class->getProperty('pdo'); |
| 99 | + $property->setAccessible(true); |
| 100 | + $pdo = $property->getValue($adapter); |
| 101 | + $pdo->exec($sql); |
| 102 | + }); |
| 103 | + |
| 104 | + return true; |
| 105 | + } |
| 106 | +} |
0 commit comments