1
+ #! /bin/bash
2
+
3
+ # Exit on any error
4
+ set -euo pipefail
5
+
6
+ # Define constants
7
+ readonly PREFIX=" laravel-query-builder"
8
+ readonly DEFAULT_PHP_VERSION=" 8.3"
9
+ readonly DEFAULT_LARAVEL_VERSION=" 11.*"
10
+
11
+ # Function to display help message
12
+ show_help () {
13
+ echo " Usage: $0 [options]"
14
+ echo " "
15
+ echo " Options:"
16
+ echo " -h, --help Display this help message"
17
+ echo " -v, --version Display version information"
18
+ echo " -p PHP_VERSION Set the PHP version (default: ${DEFAULT_PHP_VERSION} )"
19
+ echo " -l LARAVEL_VERSION Set the Laravel version (default: ${DEFAULT_LARAVEL_VERSION} )"
20
+ echo " --filter FILTER Specify test filter(s)"
21
+ echo " "
22
+ echo " Example:"
23
+ echo " $0 --filter FieldsTest"
24
+ }
25
+
26
+ # Parse command-line arguments
27
+ while [[ $# -gt 0 ]]; do
28
+ case " $1 " in
29
+ -h|--help)
30
+ show_help
31
+ exit 0
32
+ ;;
33
+ -v|--version)
34
+ echo " Version: 1.0"
35
+ exit 0
36
+ ;;
37
+ -p|--php-version)
38
+ shift
39
+ PHP_VERSION=" $1 "
40
+ ;;
41
+ -l|--laravel-version)
42
+ shift
43
+ LARAVEL_VERSION=" $1 "
44
+ ;;
45
+ --filter)
46
+ shift
47
+ FILTER=" $1 "
48
+ ;;
49
+ * )
50
+ echo " Unknown option: $1 " >&2
51
+ exit 1
52
+ ;;
53
+ esac
54
+ shift
55
+ done
56
+
57
+ # Set default values if not provided
58
+ PHP_VERSION=" ${PHP_VERSION:- $DEFAULT_PHP_VERSION } "
59
+ LARAVEL_VERSION=" ${LARAVEL_VERSION:- $DEFAULT_LARAVEL_VERSION } "
60
+
61
+ # Ensure we're in the project root
62
+ cd " $( dirname " $0 " ) "
63
+
64
+ # Create a custom Docker network
65
+ DOCKER_NETWORK_NAME=" ${PREFIX} -network"
66
+ docker network create " ${DOCKER_NETWORK_NAME} " || true
67
+
68
+ # Function to remove and recreate a container if it exists
69
+ recreate_container () {
70
+ local container_name=" $1 "
71
+
72
+ # Remove the container if it exists (forcefully)
73
+ if docker ps -a --format ' {{.Names}}' | grep -q " ^${container_name} $" ; then
74
+ echo " Removing existing container: ${container_name} "
75
+ docker rm -f " ${container_name} "
76
+ fi
77
+ }
78
+
79
+ # Prepare container names with prefix
80
+ MYSQL_CONTAINER_NAME=" ${PREFIX} -mysql"
81
+ REDIS_CONTAINER_NAME=" ${PREFIX} -redis"
82
+ TEST_RUNNER_IMAGE_NAME=" ${PREFIX} -test-runner"
83
+ TEST_CONTAINER_NAME=" ${PREFIX} -test-runner-container"
84
+
85
+ # Recreate containers
86
+ recreate_container " ${MYSQL_CONTAINER_NAME} "
87
+ recreate_container " ${REDIS_CONTAINER_NAME} "
88
+ recreate_container " ${TEST_CONTAINER_NAME} "
89
+
90
+ # Set project root (parent of script directory)
91
+ PROJECT_ROOT=" $( dirname " $( pwd) " ) "
92
+
93
+ # Build the Docker image
94
+ docker build -t " ${TEST_RUNNER_IMAGE_NAME} " -f - " $PROJECT_ROOT " << EOF
95
+ FROM php:$PHP_VERSION -cli
96
+
97
+ # Install system dependencies
98
+ RUN apt-get update && apt-get install -y \
99
+ git \
100
+ unzip \
101
+ libzip-dev \
102
+ libpng-dev \
103
+ libonig-dev \
104
+ libxml2-dev \
105
+ libcurl4-openssl-dev \
106
+ libmagickwand-dev \
107
+ libmcrypt-dev \
108
+ libreadline-dev \
109
+ libfreetype6-dev \
110
+ libjpeg62-turbo-dev \
111
+ default-mysql-client \
112
+ redis-tools
113
+
114
+ # Install PHP extensions
115
+ RUN docker-php-ext-install \
116
+ dom \
117
+ curl \
118
+ xml \
119
+ mbstring \
120
+ zip \
121
+ pcntl \
122
+ pdo \
123
+ pdo_mysql \
124
+ bcmath \
125
+ intl \
126
+ gd \
127
+ exif \
128
+ iconv
129
+
130
+ # Install Composer
131
+ COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
132
+
133
+ # Copy project files
134
+ COPY . /app
135
+
136
+ # Set working directory
137
+ WORKDIR /app
138
+
139
+ # Install dependencies
140
+ RUN composer require "laravel/framework:${LARAVEL_VERSION} " "orchestra/testbench:9.*" --no-interaction --no-update
141
+ RUN composer update --prefer-dist --no-interaction
142
+ EOF
143
+
144
+ # Run MySQL container
145
+ docker run -d --name " ${MYSQL_CONTAINER_NAME} " \
146
+ --network " ${DOCKER_NETWORK_NAME} " \
147
+ -e MYSQL_ROOT_PASSWORD=secretroot \
148
+ -e MYSQL_DATABASE=laravel_query_builder \
149
+ -e MYSQL_USER=user \
150
+ -e MYSQL_PASSWORD=secret \
151
+ mysql:8.0
152
+
153
+ # Run Redis container
154
+ docker run -d --name " ${REDIS_CONTAINER_NAME} " \
155
+ --network " ${DOCKER_NETWORK_NAME} " \
156
+ redis
157
+
158
+ # Wait for MySQL to be fully ready
159
+ max_tries=30
160
+ tries=0
161
+ while [ $tries -lt $max_tries ]; do
162
+ if docker exec " ${MYSQL_CONTAINER_NAME} " mysql -h localhost -u user -psecret -e " SELECT 1" laravel_query_builder 2> /dev/null; then
163
+ echo " MySQL is ready!"
164
+ break
165
+ fi
166
+ sleep 4
167
+ tries=$(( tries+ 1 ))
168
+ done
169
+
170
+ if [ $tries -eq $max_tries ]; then
171
+ echo " MySQL did not become ready in time"
172
+ exit 1
173
+ fi
174
+
175
+ # Run tests in Docker
176
+ docker run --rm \
177
+ --name " ${TEST_CONTAINER_NAME} " \
178
+ --network " ${DOCKER_NETWORK_NAME} " \
179
+ -e DB_HOST=" ${MYSQL_CONTAINER_NAME} " \
180
+ -e DB_PORT=3306 \
181
+ -e DB_USERNAME=user \
182
+ -e DB_PASSWORD=secret \
183
+ -e REDIS_HOST=" ${REDIS_CONTAINER_NAME} " \
184
+ -e REDIS_PORT=6379 \
185
+ " ${TEST_RUNNER_IMAGE_NAME} " \
186
+ vendor/bin/pest ${FILTER: +--filter " $FILTER " }
187
+
188
+ # Cleanup containers
189
+ docker stop " ${MYSQL_CONTAINER_NAME} " " ${REDIS_CONTAINER_NAME} " " ${TEST_CONTAINER_NAME} "
190
+ docker rm " ${MYSQL_CONTAINER_NAME} " " ${REDIS_CONTAINER_NAME} " " ${TEST_CONTAINER_NAME} "
191
+ docker network rm " ${DOCKER_NETWORK_NAME} "
192
+
193
+ echo " Tests completed successfully!"
0 commit comments