Skip to content

CursorにUnitTestを作らせた #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Tests

on:
pull_request:
branches:
- main
types: [opened, synchronize, reopened]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['8.3', '8.4']
fail-fast: false

services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: test_openapi_theme
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: intl, zip, pdo_mysql
coverage: none

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache composer dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

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

- name: Wait for MySQL
run: |
while ! mysqladmin ping -h"127.0.0.1" -P"3306" --silent; do
Comment on lines +56 to +57
Copy link
Preview

Copilot AI May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a timeout or maximum number of retries to the MySQL waiting loop to avoid a potential infinite loop if MySQL fails to start.

Suggested change
run: |
while ! mysqladmin ping -h"127.0.0.1" -P"3306" --silent; do
run: |
MAX_RETRIES=30
RETRY_COUNT=0
while ! mysqladmin ping -h"127.0.0.1" -P"3306" --silent; do
RETRY_COUNT=$((RETRY_COUNT+1))
if [ "$RETRY_COUNT" -ge "$MAX_RETRIES" ]; then
echo "MySQL failed to start after $MAX_RETRIES retries."
exit 1
fi

Copilot uses AI. Check for mistakes.

sleep 1
done

- name: Run tests
run: vendor/bin/phpunit
env:
DATABASE_URL: mysql://root:[email protected]/test_openapi_theme?encoding=utf8mb4&timezone=UTC&cacheMetadata=true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/vendor/
/.idea/
.DS_Store
/tests/test_app/src/Controller
18 changes: 18 additions & 0 deletions Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ARG PHP_VERSION
FROM php:${PHP_VERSION}-cli

RUN apt-get update && apt-get install -y \
libicu-dev \
libzip-dev \
unzip \
git \
&& docker-php-ext-install \
intl \
zip \
pdo_mysql

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

WORKDIR /app

CMD ["vendor/bin/phpunit"]
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
"php": ">=8.3",
"cakephp/cakephp": "^5.0 || ^5.1",
"cakephp/bake": "^3.1",
"zircote/swagger-php": "^4.9"
"zircote/swagger-php": "^4.9",
"cakephp/twig-view": "^2.0.0"
},
"require-dev": {
"phpunit/phpunit": "^10.1.0"
"phpunit/phpunit": "^10.1.0",
"cakephp/chronos": "^3.0",
"cakephp/migrations": "^4.0",
"cakephp/plugin-installer": "^2.0"
},
"autoload": {
"psr-4": {
Expand All @@ -20,14 +24,16 @@
"autoload-dev": {
"psr-4": {
"OpenApiTheme\\Test\\": "tests/",
"TestApp\\": "tests/test_app/src/",
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
}
},
"bin": [
"bin/build-swagger-json"
],
"scripts": {
"build:swagger": "build-swagger-json"
"build:swagger": "build-swagger-json",
"test": "phpunit"
},
"config": {
"allow-plugins": {
Expand Down
37 changes: 37 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: '3.8'

services:
test-php84:
build:
context: .
dockerfile: Dockerfile.test
args:
PHP_VERSION: "8.4"
volumes:
- .:/app
depends_on:
- db-test
environment:
- DATABASE_URL=mysql://root:secret@db-test/test_openapi_theme?encoding=utf8mb4&timezone=UTC&cacheMetadata=true

test-php83:
build:
context: .
dockerfile: Dockerfile.test
args:
PHP_VERSION: "8.3"
volumes:
- .:/app
depends_on:
- db-test
environment:
- DATABASE_URL=mysql://root:secret@db-test/test_openapi_theme?encoding=utf8mb4&timezone=UTC&cacheMetadata=true

db-test:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: test_openapi_theme
command: --default-authentication-plugin=mysql_native_password
ports:
- "3306"
17 changes: 17 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# コンテナをビルド
docker-compose -f docker-compose.test.yml build

# PHP 8.4でテスト実行
echo "Running tests with PHP 8.4..."
docker-compose -f docker-compose.test.yml run --rm test-php84 composer install
docker-compose -f docker-compose.test.yml run --rm test-php84

# PHP 8.3でテスト実行
echo "Running tests with PHP 8.3..."
docker-compose -f docker-compose.test.yml run --rm test-php83 composer install
docker-compose -f docker-compose.test.yml run --rm test-php83

# コンテナを停止・削除
docker-compose -f docker-compose.test.yml down
46 changes: 46 additions & 0 deletions tests/Fixture/TestTablesFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace OpenApiTheme\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

class TestTablesFixture extends TestFixture
{
/**
* Fields
*
* @var array<string, mixed>
*/
public array $fields = [
'id' => ['type' => 'integer', 'length' => 11, 'unsigned' => false, 'null' => false, 'default' => null, 'comment' => 'Primary key', 'autoIncrement' => true],
'name' => ['type' => 'string', 'length' => 255, 'null' => false, 'default' => null, 'comment' => 'Name field', 'collate' => 'utf8mb4_general_ci'],
'created' => ['type' => 'datetime', 'length' => null, 'precision' => null, 'null' => false, 'default' => null, 'comment' => ''],
'modified' => ['type' => 'datetime', 'length' => null, 'precision' => null, 'null' => false, 'default' => null, 'comment' => ''],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []],
],
'_options' => [
'engine' => 'InnoDB',
'collation' => 'utf8mb4_general_ci'
],
];

/**
* Init method
*
* @return void
*/
public function init(): void
{
$this->records = [
[
'id' => 1,
'name' => 'Test Record',
'created' => '2024-01-01 00:00:00',
'modified' => '2024-01-01 00:00:00'
],
];
parent::init();
}
}
Loading