|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +RED='\033[0;31m' |
| 4 | +GREEN='\033[0;32m' |
| 5 | +NC='\033[0m' # No Color |
| 6 | + |
| 7 | +section_title() { |
| 8 | + printf "${RED}\n-------------------------------- ${NC}" |
| 9 | + printf "${RED}$1${NC}" |
| 10 | + printf "${RED} --------------------------------\n${NC}" |
| 11 | +} |
| 12 | + |
| 13 | +# Build docker containers |
| 14 | +do_build() { |
| 15 | + section_title "Rebuilding containers" |
| 16 | + docker compose -p query_builder_test build; |
| 17 | +} |
| 18 | + |
| 19 | +# Start docker containers |
| 20 | +do_up() { |
| 21 | + section_title "Up containers" |
| 22 | + docker compose -p query_builder_test up -d --force-recreate --remove-orphans |
| 23 | +} |
| 24 | + |
| 25 | +# Stop docker containers |
| 26 | +do_down() { |
| 27 | + section_title "Down containers" |
| 28 | + docker compose -p query_builder_test down |
| 29 | +} |
| 30 | + |
| 31 | +do_composer_install() { |
| 32 | + echo 'composer install' |
| 33 | + docker compose -p query_builder_test exec phpunit composer install |
| 34 | +} |
| 35 | + |
| 36 | +# Launch composer checks (for Static analysis & Code style fixer) |
| 37 | +do_checks() { |
| 38 | + section_title "Composer checks" |
| 39 | + |
| 40 | + do_composer_install |
| 41 | + |
| 42 | + echo 'composer checks' |
| 43 | + docker compose -p query_builder_test exec phpunit composer checks |
| 44 | +} |
| 45 | + |
| 46 | +# Launch PHPUnit tests without any database vendor |
| 47 | +do_unittest() { |
| 48 | + section_title "PHPUnit unit tests" |
| 49 | + docker compose -p query_builder_test exec phpunit vendor/bin/phpunit |
| 50 | +} |
| 51 | + |
| 52 | +do_test_mysql57() { |
| 53 | + section_title "Running tests with MySQL 5.7" |
| 54 | + docker compose -p query_builder_test exec \ |
| 55 | + -e DBAL_DRIVER=pdo_mysql \ |
| 56 | + -e DBAL_DBNAME=test_db \ |
| 57 | + -e DBAL_HOST=mysql57 \ |
| 58 | + -e DBAL_PASSWORD=password \ |
| 59 | + -e DBAL_PORT=3306 \ |
| 60 | + -e DBAL_ROOT_PASSWORD=password \ |
| 61 | + -e DBAL_ROOT_USER="root" \ |
| 62 | + -e DBAL_USER=root \ |
| 63 | + -e DATABASE_URL=mysql://root:password@mysql57:3306/test_db?serverVersion=5.7 \ |
| 64 | + phpunit vendor/bin/phpunit $@ |
| 65 | +} |
| 66 | + |
| 67 | +do_test_mysql80() { |
| 68 | + section_title "Running tests with MySQL 8" |
| 69 | + docker compose -p query_builder_test exec \ |
| 70 | + -e DBAL_DRIVER=pdo_mysql \ |
| 71 | + -e DBAL_DBNAME=test_db \ |
| 72 | + -e DBAL_HOST=mysql80 \ |
| 73 | + -e DBAL_PASSWORD=password \ |
| 74 | + -e DBAL_PORT=3306 \ |
| 75 | + -e DBAL_ROOT_PASSWORD=password \ |
| 76 | + -e DBAL_ROOT_USER=root \ |
| 77 | + -e DBAL_USER=root \ |
| 78 | + -e DATABASE_URL=mysql://root:password@mysql80:3306/test_db?serverVersion=8 \ |
| 79 | + phpunit vendor/bin/phpunit $@ |
| 80 | +} |
| 81 | + |
| 82 | +do_test_mariadb11() { |
| 83 | + section_title "Running tests with MariaDB 11" |
| 84 | + docker compose -p query_builder_test exec \ |
| 85 | + -e DBAL_DRIVER=pdo_mysql \ |
| 86 | + -e DBAL_DBNAME=test_db \ |
| 87 | + -e DBAL_HOST=mariadb11 \ |
| 88 | + -e DBAL_PASSWORD=password \ |
| 89 | + -e DBAL_PORT=3306 \ |
| 90 | + -e DBAL_ROOT_PASSWORD="password" \ |
| 91 | + -e DBAL_ROOT_USER="root" \ |
| 92 | + -e DBAL_USER=root \ |
| 93 | + -e DATABASE_URL=mysql://root:password@mariadb11:3306/test_db?serverVersion=11.1.3-MariaDB \ |
| 94 | + phpunit vendor/bin/phpunit $@ |
| 95 | +} |
| 96 | + |
| 97 | +do_test_mysql() { |
| 98 | + do_test_mysql57 |
| 99 | + do_test_mysql80 |
| 100 | + do_test_mariadb11 |
| 101 | +} |
| 102 | + |
| 103 | +do_test_postgresql10() { |
| 104 | + section_title "Running tests with PostgreSQL 10" |
| 105 | + docker compose -p query_builder_test exec \ |
| 106 | + -e DBAL_DRIVER=pdo_pgsql \ |
| 107 | + -e DBAL_DBNAME=test_db \ |
| 108 | + -e DBAL_HOST=postgresql10 \ |
| 109 | + -e DBAL_PASSWORD=password \ |
| 110 | + -e DBAL_PORT=5432 \ |
| 111 | + -e DBAL_ROOT_PASSWORD=password \ |
| 112 | + -e DBAL_ROOT_USER=postgres \ |
| 113 | + -e DBAL_USER=postgres \ |
| 114 | + -e DATABASE_URL="postgresql://postgres:password@postgresql10:5432/test_db?serverVersion=10&charset=utf8" \ |
| 115 | + phpunit vendor/bin/phpunit $@ |
| 116 | +} |
| 117 | + |
| 118 | +do_test_postgresql16() { |
| 119 | + section_title "Running tests with PostgreSQL 16" |
| 120 | + docker compose -p query_builder_test exec \ |
| 121 | + -e DBAL_DRIVER=pdo_pgsql \ |
| 122 | + -e DBAL_DBNAME=test_db \ |
| 123 | + -e DBAL_HOST=postgresql16 \ |
| 124 | + -e DBAL_PASSWORD=password \ |
| 125 | + -e DBAL_PORT=5432 \ |
| 126 | + -e DBAL_ROOT_PASSWORD=password \ |
| 127 | + -e DBAL_ROOT_USER=postgres \ |
| 128 | + -e DBAL_USER=postgres \ |
| 129 | + -e DATABASE_URL="postgresql://postgres:password@postgresql16:5432/test_db?serverVersion=16&charset=utf8" \ |
| 130 | + phpunit vendor/bin/phpunit $@ |
| 131 | +} |
| 132 | + |
| 133 | +do_test_postgresql() { |
| 134 | + do_test_postgresql10 |
| 135 | + do_test_postgresql16 |
| 136 | +} |
| 137 | + |
| 138 | +do_test_sqlsrv2019() { |
| 139 | + section_title "Running tests with SQL Server 2019" |
| 140 | + docker compose -p query_builder_test exec \ |
| 141 | + -e DBAL_DRIVER=pdo_sqlsrv \ |
| 142 | + -e DBAL_DBNAME=test_db \ |
| 143 | + -e DBAL_HOST=sqlsrv2019 \ |
| 144 | + -e DBAL_PASSWORD=P@ssword123 \ |
| 145 | + -e DBAL_PORT=1433 \ |
| 146 | + -e DBAL_ROOT_PASSWORD=P@ssword123 \ |
| 147 | + -e DBAL_ROOT_USER=sa \ |
| 148 | + -e DBAL_USER=sa \ |
| 149 | + -e DATABASE_URL="pdo-sqlsrv://sa:P%40ssword123@sqlsrv2019:1433/test_db?serverVersion=2019&charset=utf8&driverOptions[TrustServerCertificate]=true" \ |
| 150 | + phpunit vendor/bin/phpunit $@ |
| 151 | +} |
| 152 | + |
| 153 | +do_test_sqlsrv() { |
| 154 | + do_test_sqlsrv2019 |
| 155 | +} |
| 156 | + |
| 157 | +# SQLite version depends upon the PHP embeded version or linked |
| 158 | +# library, we cannot target X or Y version. |
| 159 | +do_test_sqlite() { |
| 160 | + section_title "Running tests with SQLite" |
| 161 | + docker compose -p query_builder_test exec \ |
| 162 | + -e DBAL_DRIVER=pdo_sqlite \ |
| 163 | + -e DBAL_DBNAME=test_db \ |
| 164 | + -e DBAL_HOST=127.0.0.1 \ |
| 165 | + -e DATABASE_URL="pdo-sqlite:///:memory:" \ |
| 166 | + phpunit vendor/bin/phpunit $@ |
| 167 | +} |
| 168 | + |
| 169 | +# Run PHPunit tests for all database vendors |
| 170 | +do_test_all() { |
| 171 | + do_composer_install |
| 172 | + |
| 173 | + do_test_mysql57 |
| 174 | + do_test_mysql80 |
| 175 | + do_test_mariadb11 |
| 176 | + do_test_postgresql10 |
| 177 | + do_test_postgresql16 |
| 178 | + do_test_sqlsrv2019 |
| 179 | + do_test_sqlite |
| 180 | +} |
| 181 | + |
| 182 | +do_test_notice() { |
| 183 | + section_title "Test a specicif database vendor or version" |
| 184 | + printf "\nThis action will allow you to test a specific vendor or version." |
| 185 | + printf "\n" |
| 186 | + printf "\nLaunch this action with one of these available options:" |
| 187 | + printf "\n" |
| 188 | + printf "\n - ${GREEN}mysql${NC}: Launch test for MySQL 5.7, MySQL 8.0 & MariaDB 11" |
| 189 | + printf "\n - ${GREEN}mysql57${NC}: Launch test for MySQL 5.7" |
| 190 | + printf "\n - ${GREEN}mysql80${NC}: Launch test for MySQL 8.0" |
| 191 | + printf "\n - ${GREEN}mariadb11${NC}: Launch test for MariaDB 11" |
| 192 | + printf "\n - ${GREEN}postgresql${NC}: Launch test for PostgreSQL 10 & 16" |
| 193 | + printf "\n - ${GREEN}postgresql10${NC}: Launch test for PostgreSQL 10" |
| 194 | + printf "\n - ${GREEN}postgresql16${NC}: Launch test for PostgreSQL 16" |
| 195 | + printf "\n - ${GREEN}sqlsrv${NC}: Launch test for SQL Server 2019" |
| 196 | + printf "\n - ${GREEN}sqlsrv2019${NC}: Launch test for SQL Server 2019" |
| 197 | + printf "\n - ${GREEN}sqlite${NC}: Launch test for SQLite" |
| 198 | + printf "\n\nYou can then use PHPUnit option as usual:" |
| 199 | + printf "\n${GREEN}./dev.sh test mysql --filter AnonymizatorFactoryTest${NC}" |
| 200 | + printf "\n\n" |
| 201 | +} |
| 202 | + |
| 203 | +do_test() { |
| 204 | + suit=${1-} |
| 205 | + |
| 206 | + if [[ -n $@ ]];then shift;fi |
| 207 | + |
| 208 | + case $suit in |
| 209 | + mysql57|mysql80|mariadb11|mysql|postgresql10|postgresql16|postgresql|sqlsrv2019|sqlsrv|sqlite) do_composer_install && do_test_$suit "$@";; |
| 210 | + *) do_test_notice;; |
| 211 | + esac |
| 212 | +} |
| 213 | + |
| 214 | +# Display help |
| 215 | +do_notice() { |
| 216 | + section_title "QueryBuilder dev scripts" |
| 217 | + |
| 218 | + printf "\nWelcome to QueryBuilder dev script !" |
| 219 | + printf "\n" |
| 220 | + printf "\nThis script will help you to contribute to the QueryBuilder." |
| 221 | + printf "\n" |
| 222 | + printf "\nIt will allow you to :" |
| 223 | + printf "\n" |
| 224 | + printf "\n - build, up and down a complete docker stack with all database vendors" |
| 225 | + printf "\n and versions that the QueryBuilder supports" |
| 226 | + printf "\n - run Code Style Fixer (with PHP CS Fixer) and launch Static Analysis (with PHPStan)" |
| 227 | + printf "\n - run PHPUnit tests for all database vendors" |
| 228 | + printf "\n\n--\n" |
| 229 | + printf "\nLaunch the script with one of these available actions:" |
| 230 | + printf "\n" |
| 231 | + printf "\n - ${GREEN}build${NC}: Build docker containers" |
| 232 | + printf "\n - ${GREEN}up${NC}: Start docker containers" |
| 233 | + printf "\n - ${GREEN}down${NC}: Stop docker containers" |
| 234 | + printf "\n - ${GREEN}checks${NC}: Launch composer checks (for Static analysis & Code style fixer)" |
| 235 | + printf "\n - ${GREEN}test_all${NC}: Run PHPUnit tests for all database vendors." |
| 236 | + printf "\n PHPUnit options can be used as usual:" |
| 237 | + printf "\n ${GREEN}./dev.sh test_all --filter FunctionalTest${NC}" |
| 238 | + printf "\n - ${GREEN}test${NC}: Run PHPUnit tests for a specific database vendors or version" |
| 239 | + printf "\n - ${GREEN}unittest${NC}: Run PHPUnit tests without any database vendor" |
| 240 | + printf "\n - ${GREEN}notice${NC}: Display this help" |
| 241 | + printf "\n\n" |
| 242 | +} |
| 243 | + |
| 244 | +args=${@:-usage} |
| 245 | +action=${1-} |
| 246 | + |
| 247 | +if [[ -n $@ ]];then shift;fi |
| 248 | + |
| 249 | +case $action in |
| 250 | + build|up|down|checks|test_all|unittest|test|notice) do_$action "$@";; |
| 251 | + *) do_notice;; |
| 252 | +esac |
0 commit comments