bartko-s /
stefano-tree
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace StefanoTreeTest; |
||
| 6 | |||
| 7 | use Doctrine\DBAL; |
||
| 8 | use Laminas\Db\Adapter\Adapter as LaminasDbAdapter; |
||
| 9 | use PDO; |
||
| 10 | use StefanoTree\NestedSet; |
||
| 11 | use StefanoTree\NestedSet\Adapter; |
||
| 12 | use StefanoTree\NestedSet\Adapter\AdapterInterface; |
||
| 13 | use StefanoTree\NestedSet\Options; |
||
| 14 | |||
| 15 | class TestUtil |
||
| 16 | { |
||
| 17 | private static $dbConnection; |
||
| 18 | private static $laminasDbAdapter; |
||
| 19 | private static $zend1DbAdapter; |
||
| 20 | private static $doctrine2Connection; |
||
| 21 | |||
| 22 | public static function createDbScheme() |
||
| 23 | { |
||
| 24 | $connection = self::getPDOConnection(); |
||
| 25 | |||
| 26 | $queries = array(); |
||
| 27 | |||
| 28 | if ('mysql' == TEST_STEFANO_DB_VENDOR) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 29 | $queries[] = 'DROP TABLE IF EXISTS tree_traversal'; |
||
| 30 | $queries[] = 'DROP TABLE IF EXISTS tree_traversal_with_scope'; |
||
| 31 | $queries[] = 'DROP TABLE IF EXISTS tree_traversal_metadata'; |
||
| 32 | |||
| 33 | $queries[] = 'CREATE TABLE `tree_traversal` ( |
||
| 34 | `tree_traversal_id` int(11) NOT NULL AUTO_INCREMENT, |
||
| 35 | `name` varchar(255) COLLATE utf8_bin DEFAULT NULL, |
||
| 36 | `lft` int(11) NOT NULL, |
||
| 37 | `rgt` int(11) NOT NULL, |
||
| 38 | `parent_id` int(11) DEFAULT NULL, |
||
| 39 | `level` int(11) DEFAULT NULL, |
||
| 40 | PRIMARY KEY (`tree_traversal_id`), |
||
| 41 | KEY `parent_id` (`parent_id`), |
||
| 42 | KEY `level` (`level`), |
||
| 43 | KEY `lft` (`lft`), |
||
| 44 | KEY `rgt` (`rgt`) |
||
| 45 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin'; |
||
| 46 | |||
| 47 | $queries[] = 'ALTER TABLE `tree_traversal` |
||
| 48 | ADD FOREIGN KEY (`parent_id`) |
||
| 49 | REFERENCES `tree_traversal` (`tree_traversal_id`) |
||
| 50 | ON DELETE CASCADE ON UPDATE CASCADE'; |
||
| 51 | |||
| 52 | $queries[] = 'CREATE TABLE `tree_traversal_with_scope` ( |
||
| 53 | `tree_traversal_id` int(11) NOT NULL AUTO_INCREMENT, |
||
| 54 | `name` varchar(255) COLLATE utf8_bin DEFAULT NULL, |
||
| 55 | `lft` int(11) NOT NULL, |
||
| 56 | `rgt` int(11) NOT NULL, |
||
| 57 | `parent_id` int(11) DEFAULT NULL, |
||
| 58 | `level` int(11) DEFAULT NULL, |
||
| 59 | `scope` int(11) NOT NULL, |
||
| 60 | PRIMARY KEY (`tree_traversal_id`), |
||
| 61 | KEY `parent_id` (`parent_id`), |
||
| 62 | KEY `level` (`level`), |
||
| 63 | KEY `lft` (`lft`), |
||
| 64 | KEY `rgt` (`rgt`), |
||
| 65 | KEY `scope` (`scope`) |
||
| 66 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin'; |
||
| 67 | |||
| 68 | $queries[] = 'CREATE TABLE `tree_traversal_metadata` ( |
||
| 69 | `tree_traversal_metadata_id` int(11) NOT NULL AUTO_INCREMENT, |
||
| 70 | `tree_traversal_id` int(11) NOT NULL, |
||
| 71 | `lft` int(11) DEFAULT NULL, |
||
| 72 | `name` varchar(255) COLLATE utf8_bin DEFAULT NULL, |
||
| 73 | PRIMARY KEY (`tree_traversal_metadata_id`), |
||
| 74 | KEY `tree_traversal_id` (`tree_traversal_id`) |
||
| 75 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin'; |
||
| 76 | |||
| 77 | $queries[] = 'ALTER TABLE `tree_traversal_with_scope` |
||
| 78 | ADD FOREIGN KEY (`parent_id`) |
||
| 79 | REFERENCES `tree_traversal_with_scope` (`tree_traversal_id`) |
||
| 80 | ON DELETE CASCADE ON UPDATE CASCADE'; |
||
| 81 | } elseif ('pgsql' == TEST_STEFANO_DB_VENDOR) { |
||
| 82 | $queries[] = 'DROP TABLE IF EXISTS tree_traversal'; |
||
| 83 | $queries[] = 'DROP TABLE IF EXISTS tree_traversal_with_scope'; |
||
| 84 | $queries[] = 'DROP TABLE IF EXISTS tree_traversal_metadata'; |
||
| 85 | |||
| 86 | $queries[] = 'CREATE TABLE tree_traversal ( |
||
| 87 | tree_traversal_id serial NOT NULL, |
||
| 88 | name character varying(255), |
||
| 89 | lft integer NOT NULL, |
||
| 90 | rgt integer NOT NULL, |
||
| 91 | parent_id integer, |
||
| 92 | level integer, |
||
| 93 | CONSTRAINT tree_traversal_pkey PRIMARY KEY (tree_traversal_id), |
||
| 94 | CONSTRAINT tree_traversal_parent_id_fkey FOREIGN KEY (parent_id) |
||
| 95 | REFERENCES public.tree_traversal (tree_traversal_id) MATCH SIMPLE |
||
| 96 | ON UPDATE CASCADE ON DELETE CASCADE |
||
| 97 | )'; |
||
| 98 | |||
| 99 | $queries[] = 'CREATE INDEX tree_traversal_level |
||
| 100 | ON public.tree_traversal |
||
| 101 | USING btree (level)'; |
||
| 102 | |||
| 103 | $queries[] = 'CREATE INDEX tree_traversal_lft |
||
| 104 | ON public.tree_traversal |
||
| 105 | USING btree (lft)'; |
||
| 106 | |||
| 107 | $queries[] = 'CREATE INDEX tree_traversal_parent_id |
||
| 108 | ON public.tree_traversal |
||
| 109 | USING btree (parent_id)'; |
||
| 110 | |||
| 111 | $queries[] = 'CREATE INDEX tree_traversal_rgt |
||
| 112 | ON public.tree_traversal |
||
| 113 | USING btree (rgt)'; |
||
| 114 | |||
| 115 | $queries[] = 'CREATE TABLE tree_traversal_with_scope ( |
||
| 116 | tree_traversal_id serial NOT NULL, |
||
| 117 | name character varying(255), |
||
| 118 | lft integer NOT NULL, |
||
| 119 | rgt integer NOT NULL, |
||
| 120 | parent_id integer, |
||
| 121 | level integer, |
||
| 122 | scope integer NOT NULL, |
||
| 123 | CONSTRAINT tree_traversal_with_scope_pkey PRIMARY KEY (tree_traversal_id), |
||
| 124 | CONSTRAINT tree_traversal_with_scope_parent_id_fkey FOREIGN KEY (parent_id) |
||
| 125 | REFERENCES public.tree_traversal_with_scope (tree_traversal_id) MATCH SIMPLE |
||
| 126 | ON UPDATE CASCADE ON DELETE CASCADE |
||
| 127 | )'; |
||
| 128 | |||
| 129 | $queries[] = 'CREATE INDEX tree_traversal_with_scope_level |
||
| 130 | ON public.tree_traversal_with_scope |
||
| 131 | USING btree (level)'; |
||
| 132 | |||
| 133 | $queries[] = 'CREATE INDEX tree_traversal_with_scope_lft |
||
| 134 | ON public.tree_traversal_with_scope |
||
| 135 | USING btree (lft)'; |
||
| 136 | |||
| 137 | $queries[] = 'CREATE INDEX tree_traversal_with_scope_parent_id |
||
| 138 | ON public.tree_traversal_with_scope |
||
| 139 | USING btree (parent_id)'; |
||
| 140 | |||
| 141 | $queries[] = 'CREATE INDEX tree_traversal_with_scope_rgt |
||
| 142 | ON public.tree_traversal_with_scope |
||
| 143 | USING btree (rgt)'; |
||
| 144 | |||
| 145 | $queries[] = 'CREATE INDEX tree_traversal_with_scope_scope |
||
| 146 | ON public.tree_traversal_with_scope |
||
| 147 | USING btree (scope)'; |
||
| 148 | |||
| 149 | $queries[] = 'CREATE TABLE tree_traversal_metadata ( |
||
| 150 | tree_traversal_metadata_id serial NOT NULL, |
||
| 151 | tree_traversal_id integer NOT NULL, |
||
| 152 | lft integer, |
||
| 153 | name character varying(255), |
||
| 154 | CONSTRAINT tree_traversal_metadata_pkey PRIMARY KEY (tree_traversal_metadata_id) |
||
| 155 | )'; |
||
| 156 | |||
| 157 | $queries[] = 'CREATE INDEX tree_traversal_metadata_tree_traversal_id |
||
| 158 | ON public.tree_traversal_metadata |
||
| 159 | USING btree (tree_traversal_id)'; |
||
| 160 | } else { |
||
| 161 | throw new \Exception(sprintf('Unsupported vendor %s', TEST_STEFANO_DB_VENDOR)); |
||
| 162 | } |
||
| 163 | |||
| 164 | foreach ($queries as $query) { |
||
| 165 | $connection->query($query); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Singleton. |
||
| 171 | * |
||
| 172 | * @return PDO |
||
| 173 | */ |
||
| 174 | public static function getPDOConnection() |
||
| 175 | { |
||
| 176 | if (null == self::$dbConnection) { |
||
| 177 | $adapter = strtolower(TEST_STEFANO_DB_VENDOR); |
||
| 178 | $hostname = TEST_STEFANO_DB_HOSTNAME; |
||
| 179 | $dbName = TEST_STEFANO_DB_DB_NAME; |
||
| 180 | $user = TEST_STEFANO_DB_USER; |
||
| 181 | $password = TEST_STEFANO_DB_PASSWORD; |
||
| 182 | |||
| 183 | self::$dbConnection = new PDO( |
||
| 184 | $adapter.':host='.$hostname.';dbname=' |
||
| 185 | .$dbName, |
||
| 186 | $user, |
||
| 187 | $password |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 191 | return self::$dbConnection; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Singleton. |
||
| 196 | * |
||
| 197 | * @return LaminasDbAdapter |
||
| 198 | */ |
||
| 199 | public static function getLaminasDbAdapter() |
||
| 200 | { |
||
| 201 | if (null == self::$laminasDbAdapter) { |
||
| 202 | self::$laminasDbAdapter = new LaminasDbAdapter(array( |
||
| 203 | 'driver' => 'Pdo_'.ucfirst(TEST_STEFANO_DB_VENDOR), |
||
| 204 | 'hostname' => TEST_STEFANO_DB_HOSTNAME, |
||
| 205 | 'database' => TEST_STEFANO_DB_DB_NAME, |
||
| 206 | 'username' => TEST_STEFANO_DB_USER, |
||
| 207 | 'password' => TEST_STEFANO_DB_PASSWORD, |
||
| 208 | )); |
||
| 209 | } |
||
| 210 | |||
| 211 | return self::$laminasDbAdapter; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Singleton. |
||
| 216 | * |
||
| 217 | * @return \Zend_Db_Adapter_Abstract |
||
| 218 | */ |
||
| 219 | public static function getZend1DbAdapter() |
||
| 220 | { |
||
| 221 | if (null == self::$zend1DbAdapter) { |
||
| 222 | self::$zend1DbAdapter = \Zend_Db::factory('Pdo_'.ucfirst(TEST_STEFANO_DB_VENDOR), array( |
||
| 223 | 'host' => TEST_STEFANO_DB_HOSTNAME, |
||
| 224 | 'dbname' => TEST_STEFANO_DB_DB_NAME, |
||
| 225 | 'username' => TEST_STEFANO_DB_USER, |
||
| 226 | 'password' => TEST_STEFANO_DB_PASSWORD, |
||
| 227 | )); |
||
| 228 | } |
||
| 229 | |||
| 230 | return self::$zend1DbAdapter; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Singleton. |
||
| 235 | * |
||
| 236 | * @return DBAL\Connection |
||
| 237 | */ |
||
| 238 | public static function getDoctrine2Connection() |
||
| 239 | { |
||
| 240 | if (null == self::$doctrine2Connection) { |
||
| 241 | $config = new DBAL\Configuration(); |
||
| 242 | $connectionParams = array( |
||
| 243 | 'dbname' => TEST_STEFANO_DB_DB_NAME, |
||
| 244 | 'user' => TEST_STEFANO_DB_USER, |
||
| 245 | 'password' => TEST_STEFANO_DB_PASSWORD, |
||
| 246 | 'host' => TEST_STEFANO_DB_HOSTNAME, |
||
| 247 | 'driver' => 'pdo_'.strtolower(TEST_STEFANO_DB_VENDOR), |
||
| 248 | ); |
||
| 249 | |||
| 250 | self::$doctrine2Connection = DBAL\DriverManager::getConnection($connectionParams, $config); |
||
| 251 | } |
||
| 252 | |||
| 253 | return self::$doctrine2Connection; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Build Adapter based on ENV variable TEST_STEFANO_ADAPTER. |
||
| 258 | * |
||
| 259 | * @param Options $options |
||
| 260 | * |
||
| 261 | * @return AdapterInterface |
||
| 262 | */ |
||
| 263 | public static function buildAdapter(Options $options): NestedSet\Adapter\AdapterInterface |
||
| 264 | { |
||
| 265 | switch (TEST_STEFANO_ADAPTER) { |
||
| 266 | case 'pdo': |
||
| 267 | $adapter = new Adapter\Pdo($options, self::getPDOConnection()); |
||
| 268 | |||
| 269 | break; |
||
| 270 | |||
| 271 | case 'zend1': |
||
| 272 | $adapter = new Adapter\Zend1($options, self::getZend1DbAdapter()); |
||
| 273 | |||
| 274 | break; |
||
| 275 | |||
| 276 | case 'laminas-db': |
||
| 277 | $adapter = new Adapter\LaminasDb($options, self::getLaminasDbAdapter()); |
||
| 278 | |||
| 279 | break; |
||
| 280 | |||
| 281 | case 'doctrine2-dbal': |
||
| 282 | $adapter = new Adapter\Doctrine2DBAL($options, self::getDoctrine2Connection()); |
||
| 283 | |||
| 284 | break; |
||
| 285 | |||
| 286 | default: |
||
| 287 | throw new \Exception(sprintf('Unknown adapter "%s"', TEST_STEFANO_ADAPTER)); |
||
| 288 | } |
||
| 289 | |||
| 290 | return $adapter; |
||
| 291 | } |
||
| 292 | } |
||
| 293 |