Files
agent-unit-testing/templates/docker/test-runner.sh
T
Pham-Tan-Thien 2045c856e9 feat: complete Smartgift AI Laravel unit testing toolkit
- Add 10 core testing guides (setup, AAA principles, PHPUnit/Pest, mocking, anti-patterns, PHP 8.4+, debugging, data factories, advanced edge cases)
- Add 13 component testing guides covering all Laravel layers
- Add standard templates for PHPUnit 10/11 and Pest v2/v3 including ArchitectureTest
- Add Docker smart test runner, dependency installer, and CI/CD templates (GitHub Actions, GitLab CI)
- Add 4 Claude Code skills (laravel-test-setup, laravel-unit-test with --all mode, laravel-test-review, laravel-mock-helper)
- Add single unified installer script (sg-install-unit-testing.sh) with global support, CLI runners, and 1-click updater (sg-test-update)
- Add comprehensive documentation (README.md, USAGE-GUIDE.md)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-09-07 16:41:37 +07:00

65 lines
2.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# ==============================================================================
# Smart Test Runner for Laravel
# Tự động nhận diện môi trường: Laravel Sail -> Docker Compose -> Local PHP
# ==============================================================================
set -e
# Màu sắc thông báo
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${YELLOW}🔍 Đang phát hiện môi trường chạy test...${NC}"
# Hàm chạy test
run_test() {
local runner="$1"
shift
echo -e "${GREEN}🚀 Chạy test bằng: ${runner} $*${NC}"
$runner "$@"
}
# 1. Kiểm tra Laravel Sail
if [ -f "./vendor/bin/sail" ] && docker info >/dev/null 2>&1; then
if docker ps --format '{{.Names}}' | grep -q "laravel.test"; then
echo -e "${GREEN}✓ Phát hiện Laravel Sail đang hoạt động.${NC}"
run_test "./vendor/bin/sail test" "$@"
exit 0
fi
fi
# 2. Kiểm tra Docker Compose tự cấu hình
if [ -f "docker-compose.yml" ] || [ -f "compose.yaml" ]; then
if docker info >/dev/null 2>&1; then
# Tìm container service chạy php
SERVICES=("app" "php" "php-fpm" "backend" "workspace" "web")
for svc in "${SERVICES[@]}"; do
if docker compose ps --services --filter "status=running" 2>/dev/null | grep -q "^${svc}$"; then
echo -e "${GREEN}✓ Phát hiện Docker Compose với service: '${svc}'${NC}"
run_test "docker compose exec -T ${svc} php artisan test" "$@"
exit 0
fi
done
fi
fi
# 3. Fallback về PHP CLI cục bộ
if command -v php >/dev/null 2>&1; then
echo -e "${YELLOW}ℹ Không tìm thấy container Docker đang chạy. Chuyển về PHP CLI máy local.${NC}"
# Kiểm tra extension pdo_sqlite
if ! php -m | grep -q -i sqlite; then
echo -e "${RED}⚠️ Cảnh báo: PHP CLI hiện tại chưa bật extension pdo_sqlite. Unit test dùng In-memory SQLite có thể bị lỗi!${NC}"
fi
run_test "php artisan test" "$@"
exit 0
fi
echo -e "${RED}❌ Không tìm thấy môi trường phù hợp (Sail, Docker Compose hoặc PHP CLI) để chạy test!${NC}"
exit 1