Skip to content

Commit

Permalink
Tests and devcontainer
Browse files Browse the repository at this point in the history
- add basic devcontainer
- add multi-PHP docker test scripts
- update & refactor UTs
- add github action
- remove travis-ci :(
  • Loading branch information
marcogiovinazzi committed Nov 15, 2021
1 parent 772cdfa commit 9153883
Show file tree
Hide file tree
Showing 32 changed files with 862 additions and 823 deletions.
28 changes: 28 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# [Choice] PHP version: 7, 7.4, 7.3
ARG VARIANT=7.4
FROM php:${VARIANT}-cli

# [Option] Upgrade OS packages to their latest versions
ARG UPGRADE_PACKAGES="true"

# Install needed packages and setup non-root user. Use a separate RUN statement to add your own dependencies.
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID
ENV XDEBUG_MODE=coverage
COPY scripts/setup.sh /tmp/scripts/
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& bash /tmp/scripts/setup.sh "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" \
&& pecl install xdebug \
&& docker-php-ext-install -j$(nproc) zip \
&& docker-php-ext-enable xdebug \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/scripts

# Install composer
RUN curl -sSL https://getcomposer.org/installer | php \
&& chmod +x composer.phar \
&& mv composer.phar /usr/local/bin/composer

# [Optional] Uncomment this section to install additional packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>
29 changes: 29 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "PHP",
"build": {
"dockerfile": "Dockerfile",
"args": {
// Update VARIANT to pick a PHP version: 7, 7.4, 7.3
"VARIANT": "7.4"
}
},
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"editor.formatOnSave": false
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"felixfbecker.php-debug",
"felixfbecker.php-intellisense",
"mrmlnc.vscode-apache",
"eamodio.gitlens",
"bmewburn.vscode-intelephense-client"
],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [8080],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "sudo chmod a+x \"$(pwd)\" && sudo rm -rf /var/www/html && sudo ln -s \"$(pwd)\" /var/www/html"
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
195 changes: 195 additions & 0 deletions .devcontainer/scripts/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
#!/usr/bin/env bash

USERNAME=${1:-"automatic"}
USER_UID=${2:-"automatic"}
USER_GID=${3:-"automatic"}
UPGRADE_PACKAGES=${4:-"true"}

set -e

if [ "$(id -u)" -ne 0 ]; then
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi

# If in automatic mode, determine if a user already exists, if not use vscode
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
USERNAME=""
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
for CURRENT_USER in ${POSSIBLE_USERS[@]}; do
if id -u ${CURRENT_USER} > /dev/null 2>&1; then
USERNAME=${CURRENT_USER}
break
fi
done
if [ "${USERNAME}" = "" ]; then
USERNAME=vscode
fi
elif [ "${USERNAME}" = "none" ]; then
USERNAME=root
USER_UID=0
USER_GID=0
fi

# Load markers to see which steps have already run
MARKER_FILE="/usr/local/etc/vscode-dev-containers/common"
if [ -f "${MARKER_FILE}" ]; then
echo "Marker file found:"
cat "${MARKER_FILE}"
source "${MARKER_FILE}"
fi

# Ensure apt is in non-interactive to avoid prompts
export DEBIAN_FRONTEND=noninteractive

# Function to call apt-get if needed
apt-get-update-if-needed()
{
if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update
else
echo "Skipping apt-get update."
fi
}

# Run install apt-utils to avoid debconf warning then verify presence of other common developer tools and dependencies
if [ "${PACKAGES_ALREADY_INSTALLED}" != "true" ]; then
apt-get-update-if-needed

PACKAGE_LIST="apt-utils \
git \
curl \
unzip \
zip \
libzip-dev \
vim-tiny \
less \
lsb-release \
dialog \
libc6 \
libgcc1 \
libicu[0-9][0-9] \
liblttng-ust0 \
libstdc++6 \
zlib1g \
locales \
sudo \
strace \
ssh \
wget"

# Install libssl1.1 if available
if [[ ! -z $(apt-cache --names-only search ^libssl1.1$) ]]; then
PACKAGE_LIST="${PACKAGE_LIST} libssl1.1"
fi

# Install appropriate version of libssl1.0.x if available
LIBSSL=$(dpkg-query -f '${db:Status-Abbrev}\t${binary:Package}\n' -W 'libssl1\.0\.?' 2>&1 || echo '')
if [ "$(echo "$LIBSSL" | grep -o 'libssl1\.0\.[0-9]:' | uniq | sort | wc -l)" -eq 0 ]; then
if [[ ! -z $(apt-cache --names-only search ^libssl1.0.2$) ]]; then
# Debian 9
PACKAGE_LIST="${PACKAGE_LIST} libssl1.0.2"
elif [[ ! -z $(apt-cache --names-only search ^libssl1.0.0$) ]]; then
# Ubuntu 18.04, 16.04, earlier
PACKAGE_LIST="${PACKAGE_LIST} libssl1.0.0"
fi
fi

echo "Packages to verify are installed: ${PACKAGE_LIST}"
apt-get -y install --no-install-recommends ${PACKAGE_LIST} 2> >( grep -v 'debconf: delaying package configuration, since apt-utils is not installed' >&2 )

PACKAGES_ALREADY_INSTALLED="true"
fi

# Get to latest versions of all packages
if [ "${UPGRADE_PACKAGES}" = "true" ]; then
apt-get-update-if-needed
apt-get -y upgrade --no-install-recommends
apt-get autoremove -y
fi

# Ensure at least the en_US.UTF-8 UTF-8 locale is available.
# Common need for both applications and things like the agnoster ZSH theme.
if [ "${LOCALE_ALREADY_SET}" != "true" ] && ! grep -o -E '^\s*en_US.UTF-8\s+UTF-8' /etc/locale.gen > /dev/null; then
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
locale-gen
LOCALE_ALREADY_SET="true"
fi

# Create or update a non-root user to match UID/GID.
if id -u ${USERNAME} > /dev/null 2>&1; then
# User exists, update if needed
if [ "${USER_GID}" != "automatic" ] && [ "$USER_GID" != "$(id -G $USERNAME)" ]; then
groupmod --gid $USER_GID $USERNAME
usermod --gid $USER_GID $USERNAME
fi
if [ "${USER_UID}" != "automatic" ] && [ "$USER_UID" != "$(id -u $USERNAME)" ]; then
usermod --uid $USER_UID $USERNAME
fi
else
# Create user
if [ "${USER_GID}" = "automatic" ]; then
groupadd $USERNAME
else
groupadd --gid $USER_GID $USERNAME
fi
if [ "${USER_UID}" = "automatic" ]; then
useradd -s /bin/bash --gid $USERNAME -m $USERNAME
else
useradd -s /bin/bash --uid $USER_UID --gid $USERNAME -m $USERNAME
fi
fi

# Add add sudo support for non-root user
if [ "${USERNAME}" != "root" ] && [ "${EXISTING_NON_ROOT_USER}" != "${USERNAME}" ]; then
echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME
chmod 0440 /etc/sudoers.d/$USERNAME
EXISTING_NON_ROOT_USER="${USERNAME}"
fi

# ** Shell customization section **
if [ "${USERNAME}" = "root" ]; then
USER_RC_PATH="/root"
else
USER_RC_PATH="/home/${USERNAME}"
fi

# .bashrc/.zshrc snippet
RC_SNIPPET="$(cat << EOF
export USER=\$(whoami)
export PATH=\$PATH:\$HOME/.local/bin
EOF
)"

# code shim, it fallbacks to code-insiders if code is not available
cat << 'EOF' > /usr/local/bin/code
#!/bin/sh
get_in_path_except_current() {
which -a "$1" | grep -v "$0" | head -1
}
code="$(get_in_path_except_current code)"
if [ -n "$code" ]; then
exec "$code" "$@"
elif [ "$(command -v code-insiders)" ]; then
exec code-insiders "$@"
else
echo "code or code-insiders is not installed" >&2
exit 127
fi
EOF
chmod +x /usr/local/bin/code

# Write marker file
mkdir -p "$(dirname "${MARKER_FILE}")"
echo -e "\
PACKAGES_ALREADY_INSTALLED=${PACKAGES_ALREADY_INSTALLED}\n\
LOCALE_ALREADY_SET=${LOCALE_ALREADY_SET}\n\
EXISTING_NON_ROOT_USER=${EXISTING_NON_ROOT_USER}\n\
RC_SNIPPET_ALREADY_ADDED=${RC_SNIPPET_ALREADY_ADDED}" > "${MARKER_FILE}"

echo "Done!"
11 changes: 11 additions & 0 deletions .docker/php74.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM php:7.4-cli
WORKDIR /opt
ENV XDEBUG_MODE=coverage
RUN apt-get update && apt-get install -y \
git unzip \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
&& php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php composer-setup.php --quiet \
&& rm composer-setup.php \
&& mv composer.phar /usr/local/bin/composer
11 changes: 11 additions & 0 deletions .docker/php80.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM php:8.0-cli
WORKDIR /opt
ENV XDEBUG_MODE=coverage
RUN apt-get update && apt-get install -y \
git unzip \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
&& php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php composer-setup.php --quiet \
&& rm composer-setup.php \
&& mv composer.phar /usr/local/bin/composer
30 changes: 30 additions & 0 deletions .docker/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

# Set default vars
PACKAGE=cookies
CURRENTIMAGE=1
SOURCEDIR=`pwd`
DOCKER=/usr/bin/docker
COMPOSER=/usr/local/bin/composer
ENVS=(74 80)
declare -A RESULTS

for E in ${ENVS[@]}; do
if [[ "$($DOCKER images -q local:$PACKAGE-test-$CURRENTIMAGE-$E 2> /dev/null)" == "" ]]; then
$DOCKER build -t local:$PACKAGE-test-$CURRENTIMAGE-$E -f $SOURCEDIR/php$E.dockerfile .
fi
done

for E in ${ENVS[@]}; do
$DOCKER run --rm -v ${PWD}:/opt local:$PACKAGE-test-$CURRENTIMAGE-$E /bin/bash -c "$COMPOSER update; php vendor/bin/phpunit --coverage-text"
[[ $? -ne 0 ]] && RESULTS[$E]='FAIL' || RESULTS[$E]='SUCCESS'
done

# Print result summary
printf "\n*******************\n"
printf "Test result summary:\n"
for i in "${!RESULTS[@]}"
do
printf "PHP-$i: ${RESULTS[$i]}\n"
done
printf "*******************\n"
39 changes: 39 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: build

on:
push:
branches: [ "master"]
pull_request:
branches: [ '**' ]
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.4','8.0']

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, intl
coverage: xdebug

- name: Validate composer.json
run: composer validate

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run test suite
run: composer run-script test

- name: Upload to scrutinizer
if: ${{ matrix.php-versions != '8.0'}}
run: composer run-script scrutinizer

0 comments on commit 9153883

Please sign in to comment.