From 77bf67321bb77d582e15ba4aade223a5e2750f86 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 28 Mar 2023 10:39:00 +0200 Subject: [PATCH 01/12] Actually run tests on different PHP versions --- .env.dist | 1 + .github/workflows/test.yml | 7 ++- .gitignore | 1 + CONTRIBUTING.md | 88 ++++++++++++++++++++++++++++++++++++++ Makefile | 5 ++- README.md | 43 +------------------ composer.json | 2 +- docker-compose.yml | 5 +++ tests/docker/Dockerfile | 18 ++++---- 9 files changed, 116 insertions(+), 54 deletions(-) create mode 100644 .env.dist create mode 100644 CONTRIBUTING.md diff --git a/.env.dist b/.env.dist new file mode 100644 index 00000000..063c3ec3 --- /dev/null +++ b/.env.dist @@ -0,0 +1 @@ +PHP_VERSION=7.4 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 728e0d95..43e78c61 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,12 +20,15 @@ jobs: strategy: fail-fast: true matrix: - php-versions: ['7.1', '7.2', '7.3', '7.4'] + php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] # TODO use cache steps: - uses: actions/checkout@v2 + - name: Set PHP Version + run: echo "PHP_VERSION=${{ matrix.php-versions }}" > .env + # Run every tests inside Docker container - name: Docker Compose Setup uses: ndeloof/install-compose-action@v0.0.1 @@ -40,7 +43,7 @@ jobs: run: make up - name: Install Docker and composer dependencies - run: make installdocker + run: docker-compose exec php php -v && make installdocker - name: Migrate run: make migrate diff --git a/.gitignore b/.gitignore index 7d285b75..c6342c0c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /composer.lock /phpunit.xml /.php_cs.cache +/.env diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..1bc30139 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,88 @@ +Yii2-openapi Contribution Docs +============================== + +To contribute or play around, steps to set up this project up locally are: + +```bash +# in your CLI +git clone https://github.com/cebe/yii2-openapi.git +cd yii2-openapi +make clean_all +make up +make installdocker +make migrate + +# to check everything is setup up correctly ensure all tests passes +make cli +./vendor/bin/phpunit + +# create new branch from master and Happy contributing! +``` + +These commands are available to develop and check the tests. It is available inside the Docker container. To enter into bash shell of container, run `make cli` . + +```bash +cd tests +./yii migrate-mysql/up +./yii migrate-mysql/down 4 + +./yii migrate-maria/up +./yii migrate-maria/down 4 + +./yii migrate-pgsql/up +./yii migrate-pgsql/down 4 +``` + +To apply multiple migration with one command: + +```bash +./yii migrate-mysql/up --interactive=0 && \ +./yii migrate-mysql/down --interactive=0 4 && \ +./yii migrate-maria/up --interactive=0 && \ +./yii migrate-maria/down --interactive=0 4 && \ +./yii migrate-pgsql/up --interactive=0 && \ +./yii migrate-pgsql/down --interactive=0 4 +``` + + +Switching PHP versions +---------------------- + +You can switch the PHP version of the docker runtime by changing the `PHP_VERSION` environment variable in the `.env` file. + +If you have no `.env` file yet, create it by copying `.env.dist` to `.env`. + +After changing the PHP Version you need to run `make down up` to start the new container with new version. + +Example: + +``` +$ echo "PHP_VERSION=7.4" > .env +$ make down up cli +Stopping yii2-openapi_php_1 ... done +Stopping yii2-openapi_maria_1 ... done +Stopping yii2-openapi_postgres_1 ... done +Stopping yii2-openapi_mysql_1 ... done +Removing yii2-openapi_php_1 ... done +Removing yii2-openapi_maria_1 ... done +Removing yii2-openapi_postgres_1 ... done +Removing yii2-openapi_mysql_1 ... done +Removing network yii2-openapi_default +Creating network "yii2-openapi_default" with driver "bridge" +Creating yii2-openapi_maria_1 ... done +Creating yii2-openapi_mysql_1 ... done +Creating yii2-openapi_postgres_1 ... done +Creating yii2-openapi_php_1 ... done +docker-compose exec php bash + +root@f9928598f841:/app# php -v + +PHP 7.4.27 (cli) (built: Jan 26 2022 18:08:44) ( NTS ) +Copyright (c) The PHP Group +Zend Engine v3.4.0, Copyright (c) Zend Technologies +with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies +with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans +``` + + + diff --git a/Makefile b/Makefile index 207969af..86f407b4 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,9 @@ clean: sudo rm -rf tests/tmp/app/* sudo rm -rf tests/tmp/docker_app/* +down: + docker-compose down --remove-orphans + up: docker-compose up -d echo "Waiting for mariadb to start up..." @@ -54,7 +57,7 @@ testdocker: efs: clean_all up migrate # Everything From Scratch -.PHONY: all check-style fix-style install test clean clean_all up cli installdocker migrate testdocker efs +.PHONY: all check-style fix-style install test clean clean_all up down cli installdocker migrate testdocker efs # Docs: diff --git a/README.md b/README.md index 31fb8e0e..c379d4ff 100644 --- a/README.md +++ b/README.md @@ -472,48 +472,7 @@ Generated files: # Development -To contribute or play around, steps to set up this project locally are: - -```bash -# in your CLI -git clone https://github.com/cebe/yii2-openapi.git -cd yii2-openapi -make clean_all -make up -make cli -composer install -make migrate - -# to check everything is setup up correctly ensure all tests passes -./vendor/bin/phpunit - -# create new branch from master and Happy contributing! -``` - -These commands are available to develop and check the tests. It is available inside the Docker container. To enter into bash shell of container, run `make cli` . - -```bash -cd tests -./yii migrate-mysql/up -./yii migrate-mysql/down 4 - -./yii migrate-maria/up -./yii migrate-maria/down 4 - -./yii migrate-pgsql/up -./yii migrate-pgsql/down 4 -``` - -To apply multiple migration with one command: - -```bash -./yii migrate-mysql/up --interactive=0 && \ -./yii migrate-mysql/down --interactive=0 4 && \ -./yii migrate-maria/up --interactive=0 && \ -./yii migrate-maria/down --interactive=0 4 && \ -./yii migrate-pgsql/up --interactive=0 && \ -./yii migrate-pgsql/down --interactive=0 4 -``` +To contribute or play around, steps to set up this project up locally are in [CONTRIBUTING.md](./CONTRIBUTING.md). # Support diff --git a/composer.json b/composer.json index 9c8a4da6..71fae522 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "cebe/php-openapi": "^1.5.0", "yiisoft/yii2": "~2.0.15", "yiisoft/yii2-gii": "~2.0.0 | ~2.1.0 | ~2.2.0| ~2.3.0", - "laminas/laminas-code": "^3.4", + "laminas/laminas-code": "^3.4 | ^4.0", "insolita/yii2-fractal": "^1.0.0", "fakerphp/faker": "^1.9", "sam-it/yii2-mariadb": "^2.0" diff --git a/docker-compose.yml b/docker-compose.yml index 7354f954..d18c077f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,9 +1,14 @@ version: "3.5" services: php: + image: yii2-openapi-php:${PHP_VERSION:-7.4} build: dockerfile: tests/docker/Dockerfile context: . + args: + - PHP_VERSION=${PHP_VERSION:-7.4} + extra_hosts: # https://stackoverflow.com/a/67158212/1106908 + - "host.docker.internal:host-gateway" volumes: - ./tests/tmp/.composer:/root/.composer:rw - .:/app diff --git a/tests/docker/Dockerfile b/tests/docker/Dockerfile index 06a6173a..8adfa1cc 100644 --- a/tests/docker/Dockerfile +++ b/tests/docker/Dockerfile @@ -1,4 +1,6 @@ -FROM php:7.1-cli +ARG PHP_VERSION +#FROM php:7.1-cli +FROM php:$PHP_VERSION-cli ENV DEBIAN_FRONTEND=noninteractive @@ -19,6 +21,7 @@ RUN apt-get update && \ libicu-dev \ libzip-dev \ libpq-dev \ + libonig-dev \ nano \ git \ unzip\ @@ -29,7 +32,7 @@ RUN apt-get update && \ --no-install-recommends && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \ - && pecl install xdebug-2.9.6 \ + && pecl install xdebug \ && docker-php-ext-enable xdebug \ && docker-php-ext-install \ zip \ @@ -62,11 +65,10 @@ RUN curl -o /tmp/composer-setup.php https://getcomposer.org/installer \ ENV XDEBUGINI_PATH=/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini RUN echo "xdebug.idekey=PHP_STORM" >> $XDEBUGINI_PATH && \ - echo "xdebug.default_enable=1" >> $XDEBUGINI_PATH && \ - echo "xdebug.remote_enable=1" >> $XDEBUGINI_PATH && \ - echo "xdebug.remote_connect_back=1" >> $XDEBUGINI_PATH && \ - echo "xdebug.remote_log=1" >> $XDEBUGINI_PATH && \ - echo "xdebug.remote_port=9000" >> $XDEBUGINI_PATH && \ - echo "xdebug.remote_autostart=1" >> $XDEBUGINI_PATH + echo "xdebug.mode=debug" >> $XDEBUGINI_PATH && \ + echo "xdebug.start_with_request=yes" >> $XDEBUGINI_PATH && \ + echo "xdebug.discover_client_host=1" >> $XDEBUGINI_PATH && \ + echo "xdebug.client_port=9003" >> $XDEBUGINI_PATH && \ + echo "xdebug.client_host=host.docker.internal" >> $XDEBUGINI_PATH WORKDIR /app From 779ff6de3afd451d37a4bb98c00056ceb7e62733 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 28 Mar 2023 10:52:34 +0200 Subject: [PATCH 02/12] fix xdebug version --- .github/workflows/test.yml | 4 ++-- composer.json | 2 +- tests/docker/Dockerfile | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 43e78c61..1d9a2580 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,9 +18,9 @@ jobs: DB_PASSWORD: dbpass DB_CHARSET: utf8 strategy: - fail-fast: true +# fail-fast: true matrix: - php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] + php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] # TODO use cache steps: diff --git a/composer.json b/composer.json index 71fae522..aa4acab0 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "source": "https://github.com/cebe/yii2-openapi" }, "require": { - "php": ">=7.1.0", + "php": ">=7.2.0", "cebe/php-openapi": "^1.5.0", "yiisoft/yii2": "~2.0.15", "yiisoft/yii2-gii": "~2.0.0 | ~2.1.0 | ~2.2.0| ~2.3.0", diff --git a/tests/docker/Dockerfile b/tests/docker/Dockerfile index 8adfa1cc..fc6d4b1a 100644 --- a/tests/docker/Dockerfile +++ b/tests/docker/Dockerfile @@ -32,7 +32,8 @@ RUN apt-get update && \ --no-install-recommends && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \ - && pecl install xdebug \ + # https://xdebug.org/docs/compat \ + && if [ "$PHP_VERSION" = "8.2" ] ; then pecl install xdebug else pecl install xdebug-3.1.5 fi \ && docker-php-ext-enable xdebug \ && docker-php-ext-install \ zip \ From fcc5b520471a317352d65dd83d1955cf8487295b Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 28 Mar 2023 10:55:54 +0200 Subject: [PATCH 03/12] fix syntax error --- tests/docker/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/docker/Dockerfile b/tests/docker/Dockerfile index fc6d4b1a..a31333f2 100644 --- a/tests/docker/Dockerfile +++ b/tests/docker/Dockerfile @@ -33,7 +33,7 @@ RUN apt-get update && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \ # https://xdebug.org/docs/compat \ - && if [ "$PHP_VERSION" = "8.2" ] ; then pecl install xdebug else pecl install xdebug-3.1.5 fi \ + && if [ "$PHP_VERSION" = "8.2" ] ; then pecl install xdebug ; else pecl install xdebug-3.1.5 ; fi \ && docker-php-ext-enable xdebug \ && docker-php-ext-install \ zip \ From c90b6c3e57941f454100c8176f99106c63236950 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 28 Mar 2023 11:08:10 +0200 Subject: [PATCH 04/12] fix build error --- docker-compose.yml | 2 +- tests/docker/Dockerfile | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index d18c077f..f36f7f87 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,7 @@ services: dockerfile: tests/docker/Dockerfile context: . args: - - PHP_VERSION=${PHP_VERSION:-7.4} + - BUILD_PHP_VERSION=${PHP_VERSION:-7.4} extra_hosts: # https://stackoverflow.com/a/67158212/1106908 - "host.docker.internal:host-gateway" volumes: diff --git a/tests/docker/Dockerfile b/tests/docker/Dockerfile index a31333f2..796c1f68 100644 --- a/tests/docker/Dockerfile +++ b/tests/docker/Dockerfile @@ -1,6 +1,8 @@ -ARG PHP_VERSION +ARG BUILD_PHP_VERSION #FROM php:7.1-cli -FROM php:$PHP_VERSION-cli +FROM php:$BUILD_PHP_VERSION-cli +# ARG is lost after FROM, so we need to specify it again https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact +ARG BUILD_PHP_VERSION ENV DEBIAN_FRONTEND=noninteractive @@ -33,7 +35,7 @@ RUN apt-get update && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \ # https://xdebug.org/docs/compat \ - && if [ "$PHP_VERSION" = "8.2" ] ; then pecl install xdebug ; else pecl install xdebug-3.1.5 ; fi \ + && if [ "$BUILD_PHP_VERSION" = "8.2" ] ; then pecl install xdebug ; else pecl install xdebug-3.1.5 ; fi \ && docker-php-ext-enable xdebug \ && docker-php-ext-install \ zip \ From 8aea39a73537aad6d1daac1d3cb35d5f361b91df Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 28 Mar 2023 11:19:12 +0200 Subject: [PATCH 05/12] remove platform config from composer.json --- composer.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/composer.json b/composer.json index aa4acab0..7aff2b7e 100644 --- a/composer.json +++ b/composer.json @@ -44,9 +44,6 @@ } }, "config": { - "platform": { - "php": "7.1.3" - }, "allow-plugins": { "yiisoft/yii2-composer": true } From 05a9f7f7f8bc5d405a03f61f9737f47a87875133 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 28 Mar 2023 11:47:20 +0200 Subject: [PATCH 06/12] upgrade phpunit to version 8 --- .github/workflows/test.yml | 4 ++-- .gitignore | 1 + Makefile | 12 +++++------ composer.json | 2 +- phpunit.xml.dist | 21 +++++++++++--------- tests/DbTestCase.php | 4 ++-- tests/TestCase.php | 10 ---------- tests/unit/MultiDbFreshMigrationTest.php | 4 ++-- tests/unit/MultiDbSecondaryMigrationTest.php | 4 ++-- 9 files changed, 28 insertions(+), 34 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1d9a2580..d4913626 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,9 +1,9 @@ name: yii2-openapi on: push: - branches: [ master, wip] + branches: [ master ] pull_request: - branches: [ master, wip ] + branches: [ master ] paths-ignore: - 'docs/**' - '*.md' diff --git a/.gitignore b/.gitignore index c6342c0c..0a02b40c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /phpunit.xml /.php_cs.cache /.env +/.phpunit.result.cache diff --git a/Makefile b/Makefile index 86f407b4..c50ec273 100644 --- a/Makefile +++ b/Makefile @@ -42,18 +42,18 @@ up: docker-compose exec -T mysql timeout 60s sh -c "while ! (mysql --execute \"ALTER USER 'dbuser'@'%' IDENTIFIED WITH mysql_native_password BY 'dbpass';\" > /dev/null 2>&1); do echo -n '.'; sleep 0.1 ; done; echo 'ok'" || (docker-compose ps; docker-compose logs; exit 1) cli: - docker-compose exec php bash + docker-compose exec --user=$$(id -u) php bash migrate: - docker-compose run --rm php sh -c 'mkdir -p "tests/tmp/app"' - docker-compose run --rm php sh -c 'mkdir -p "tests/tmp/docker_app"' - docker-compose run --rm php sh -c 'cd /app/tests && ./yii migrate --interactive=0' + docker-compose run --user=$$(id -u) --rm php sh -c 'mkdir -p "tests/tmp/app"' + docker-compose run --user=$$(id -u) --rm php sh -c 'mkdir -p "tests/tmp/docker_app"' + docker-compose run --user=$$(id -u) --rm php sh -c 'cd /app/tests && ./yii migrate --interactive=0' installdocker: - docker-compose run --rm php composer install && chmod +x tests/yii + docker-compose run --user=$$(id -u) --rm php composer install && chmod +x tests/yii testdocker: - docker-compose run --rm php sh -c 'vendor/bin/phpunit' + docker-compose run --user=$$(id -u) --rm php sh -c 'vendor/bin/phpunit' efs: clean_all up migrate # Everything From Scratch diff --git a/composer.json b/composer.json index 7aff2b7e..01f2879b 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "require-dev": { "cebe/indent": "*", "friendsofphp/php-cs-fixer": "~2.16", - "phpunit/phpunit": "^6.5|^8.0|^9.0", + "phpunit/phpunit": "^8.0", "yiisoft/yii2-gii": ">=2.1.0" }, "autoload": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index fb71ca51..1be87526 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -5,15 +5,18 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnFailure="false"> - - - ./tests/unit - - - - + + + ./tests/unit + + + + + ./src + ./vendor ./tests - - + + + diff --git a/tests/DbTestCase.php b/tests/DbTestCase.php index 1c668240..8eb0027d 100644 --- a/tests/DbTestCase.php +++ b/tests/DbTestCase.php @@ -35,7 +35,7 @@ protected function destroyApplication() Yii::$container = new Container(); } - protected function setUp() + protected function setUp(): void { if (getenv('IN_DOCKER') !== 'docker') { $this->markTestSkipped('For docker env only'); @@ -45,7 +45,7 @@ protected function setUp() parent::setUp(); } - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); if (getenv('IN_DOCKER') === 'docker') { diff --git a/tests/TestCase.php b/tests/TestCase.php index 3f3723b6..4ccfe376 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -13,16 +13,6 @@ class TestCase extends \PHPUnit\Framework\TestCase { - public static function assertEqualsCanonicalizing($expected, $actual, string $message = '') - { - if ((int)Version::id()[0] >= 8) { - parent::assertEqualsCanonicalizing($expected, $actual, $message); - } else { - self::assertTrue(empty(array_diff($expected, $actual)) && empty(array_diff($actual, $expected))); - } - - } - protected function prepareTempDir() { FileHelper::removeDirectory(__DIR__ . '/tmp/app'); diff --git a/tests/unit/MultiDbFreshMigrationTest.php b/tests/unit/MultiDbFreshMigrationTest.php index 56eaaf91..4ebc2c7c 100644 --- a/tests/unit/MultiDbFreshMigrationTest.php +++ b/tests/unit/MultiDbFreshMigrationTest.php @@ -58,7 +58,7 @@ public function testMysql() $this->compareFiles($expectedFiles, $testFile); } - protected function setUp() + protected function setUp(): void { if (getenv('IN_DOCKER') !== 'docker') { $this->markTestSkipped('For docker env only'); @@ -68,7 +68,7 @@ protected function setUp() parent::setUp(); } - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); if (getenv('IN_DOCKER') === 'docker') { diff --git a/tests/unit/MultiDbSecondaryMigrationTest.php b/tests/unit/MultiDbSecondaryMigrationTest.php index 2ae0b662..404f64bf 100644 --- a/tests/unit/MultiDbSecondaryMigrationTest.php +++ b/tests/unit/MultiDbSecondaryMigrationTest.php @@ -67,7 +67,7 @@ public function testMysql() $this->compareFiles($expectedFiles, $testFile); } - protected function setUp() + protected function setUp(): void { if (getenv('IN_DOCKER') !== 'docker') { $this->markTestSkipped('For docker env only'); @@ -77,7 +77,7 @@ protected function setUp() parent::setUp(); } - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); if (getenv('IN_DOCKER') === 'docker') { From 1fb758c0956ec32fa5652f863facdec71e7f2df6 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 28 Mar 2023 12:06:11 +0200 Subject: [PATCH 07/12] avoid permission issues --- .github/workflows/test.yml | 4 ++-- Makefile | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d4913626..a7abbad5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -46,10 +46,10 @@ jobs: run: docker-compose exec php php -v && make installdocker - name: Migrate - run: make migrate + run: make UID=0 migrate - name: Check style run: make check-style-from-host - name: Run tests - run: make testdocker + run: make UID=0 testdocker diff --git a/Makefile b/Makefile index c50ec273..31a107d9 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,8 @@ PHPARGS=-dmemory_limit=64M #PHPARGS=-dmemory_limit=64M -dzend_extension=xdebug.so -dxdebug.remote_enable=1 -dxdebug.remote_host=127.0.0.1 -dxdebug.remote_autostart=1 #PHPARGS=-dmemory_limit=64M -dxdebug.remote_enable=1 +UID=$(shell id -u) + all: check-style: @@ -42,18 +44,18 @@ up: docker-compose exec -T mysql timeout 60s sh -c "while ! (mysql --execute \"ALTER USER 'dbuser'@'%' IDENTIFIED WITH mysql_native_password BY 'dbpass';\" > /dev/null 2>&1); do echo -n '.'; sleep 0.1 ; done; echo 'ok'" || (docker-compose ps; docker-compose logs; exit 1) cli: - docker-compose exec --user=$$(id -u) php bash + docker-compose exec --user=$(UID) php bash migrate: - docker-compose run --user=$$(id -u) --rm php sh -c 'mkdir -p "tests/tmp/app"' - docker-compose run --user=$$(id -u) --rm php sh -c 'mkdir -p "tests/tmp/docker_app"' - docker-compose run --user=$$(id -u) --rm php sh -c 'cd /app/tests && ./yii migrate --interactive=0' + docker-compose run --user=$(UID) --rm php sh -c 'mkdir -p "tests/tmp/app"' + docker-compose run --user=$(UID) --rm php sh -c 'mkdir -p "tests/tmp/docker_app"' + docker-compose run --user=$(UID) --rm php sh -c 'cd /app/tests && ./yii migrate --interactive=0' installdocker: - docker-compose run --user=$$(id -u) --rm php composer install && chmod +x tests/yii + docker-compose run --user=$(UID) --rm php composer install && chmod +x tests/yii testdocker: - docker-compose run --user=$$(id -u) --rm php sh -c 'vendor/bin/phpunit' + docker-compose run --user=$(UID) --rm php sh -c 'vendor/bin/phpunit' efs: clean_all up migrate # Everything From Scratch From 6d37aef76ba08736ee3f574e35964f5483eff750 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 28 Mar 2023 12:12:39 +0200 Subject: [PATCH 08/12] don't fail fast to be able to compare failures accross php versions --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a7abbad5..5700e086 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,7 @@ jobs: DB_PASSWORD: dbpass DB_CHARSET: utf8 strategy: -# fail-fast: true + fail-fast: false matrix: php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] From 63c2c32885a1f8935371b8799fb6b4178818fc2c Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 28 Mar 2023 14:00:23 +0200 Subject: [PATCH 09/12] fix inconsistencies in between laminas 3 and 4 so tests pass on both versions --- .github/workflows/test.yml | 3 +- composer.json | 1 + tests/DbTestCase.php | 30 ++++++++++++++++++-- tests/unit/GeneratorTest.php | 9 +----- tests/unit/MultiDbFreshMigrationTest.php | 19 +++---------- tests/unit/MultiDbSecondaryMigrationTest.php | 11 ------- 6 files changed, 36 insertions(+), 37 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5700e086..58464380 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,7 +24,7 @@ jobs: # TODO use cache steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set PHP Version run: echo "PHP_VERSION=${{ matrix.php-versions }}" > .env @@ -49,6 +49,7 @@ jobs: run: make UID=0 migrate - name: Check style + if: "!contains(matrix.php-versions, '8.')" run: make check-style-from-host - name: Run tests diff --git a/composer.json b/composer.json index 01f2879b..4fbfc425 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,7 @@ "cebe/indent": "*", "friendsofphp/php-cs-fixer": "~2.16", "phpunit/phpunit": "^8.0", + "symfony/polyfill-php80": "^1.16", "yiisoft/yii2-gii": ">=2.1.0" }, "autoload": { diff --git a/tests/DbTestCase.php b/tests/DbTestCase.php index 8eb0027d..ca7b17ff 100644 --- a/tests/DbTestCase.php +++ b/tests/DbTestCase.php @@ -90,6 +90,22 @@ protected function changeDbToPgsql() self::assertInstanceOf(PgSqlSchema::class, Yii::$app->db->schema); } + /** + * Compare actual files to a base of expected files + * @param array $actual array of files to compare to expected files. + * @param string $testFile config file, which defines the directory, e.g. /tests/blog.php -> /tests/blog + * @return void + */ + protected function compareFiles(array $actual, string $testFile) + { + foreach ($actual as $file) { + $expectedFile = str_replace('@app', substr($testFile, 0, -4), $file); + $actualFile = str_replace('@app', Yii::getAlias('@app'), $file); + // exec('cp '.$actualFile.' '.$expectedFile); + $this->checkFiles([$actualFile], [$expectedFile]); + } + } + protected function checkFiles(array $actual, array $expected) { self::assertEquals( @@ -100,6 +116,16 @@ protected function checkFiles(array $actual, array $expected) $expectedFilePath = $expected[$index]; self::assertFileExists($file); self::assertFileExists($expectedFilePath); + $packages = require(__DIR__ . '/../vendor/composer/installed.php'); + if (str_starts_with($packages['versions']['laminas/laminas-code']['version'], '4')) { + // Laminas Code adds 2 newlines between { } in version 3, but does not in version 4 + file_put_contents($file, + preg_replace('~(class .*)\n{\n}~i', "$1\n{\n\n\n}", + preg_replace('~(class .*?)\n{\n*( *public function.*)}\n}\n\n$~is', "$1\n{\n\n$2}\n\n\n}\n\n", + file_get_contents($file) + )) + ); + } $this->assertFileEquals($expectedFilePath, $file, "Failed asserting that file contents of\n$file\nare equal to file contents of\n$expectedFilePath \n\n cp $file $expectedFilePath \n\n "); } @@ -138,7 +164,7 @@ protected function runFaker() protected function runUpMigrations(string $db = 'mysql', int $number = 2): void { // up - exec('cd tests; ./yii migrate-'.$db.' --interactive=0', $upOutput, $upExitCode); + exec('cd tests; php -dxdebug.mode=develop ./yii migrate-'.$db.' --interactive=0', $upOutput, $upExitCode); $last = count($upOutput) - 1; $lastThird = count($upOutput) - 3; $this->assertSame($upExitCode, 0); @@ -151,7 +177,7 @@ protected function runUpMigrations(string $db = 'mysql', int $number = 2): void protected function runDownMigrations(string $db = 'mysql', int $number = 2): void { // down - exec('cd tests; ./yii migrate-'.$db.'/down --interactive=0 '.$number, $downOutput, $downExitCode); + exec('cd tests; php -dxdebug.mode=develop ./yii migrate-'.$db.'/down --interactive=0 '.$number, $downOutput, $downExitCode); $last = count($downOutput) - 1; $lastThird = count($downOutput) - 3; $this->assertSame($downExitCode, 0); diff --git a/tests/unit/GeneratorTest.php b/tests/unit/GeneratorTest.php index 4937aa0f..30d55c77 100644 --- a/tests/unit/GeneratorTest.php +++ b/tests/unit/GeneratorTest.php @@ -94,14 +94,7 @@ function($file) { sort($actualFiles); $this->assertEquals($expectedFiles, $actualFiles); - foreach ($expectedFiles as $file) { - $expectedFile = str_replace('@app', substr($testFile, 0, -4), $file); - $actualFile = str_replace('@app', Yii::getAlias('@app'), $file); - $this->assertFileExists($expectedFile); - $this->assertFileExists($actualFile); - // exec('cp '.$actualFile.' '.$expectedFile); - $this->assertFileEquals($expectedFile, $actualFile, "Failed asserting that file contents of\n$actualFile\nare equal to file contents of\n$expectedFile"); - } + $this->compareFiles($actualFiles, $testFile); if ($testFile === '/app/tests/specs/postgres_custom.php' || $testFile === '/app/tests/specs/menu.php' diff --git a/tests/unit/MultiDbFreshMigrationTest.php b/tests/unit/MultiDbFreshMigrationTest.php index 4ebc2c7c..64b1883b 100644 --- a/tests/unit/MultiDbFreshMigrationTest.php +++ b/tests/unit/MultiDbFreshMigrationTest.php @@ -89,17 +89,6 @@ protected function runGenerator($configFile, string $dbName = 'mysql') } } - protected function compareFiles(array $expected, string $testFile) - { - foreach ($expected as $file) { - $expectedFile = str_replace('@app', substr($testFile, 0, -4), $file); - $actualFile = str_replace('@app', Yii::getAlias('@app'), $file); - self::assertFileExists($expectedFile); - self::assertFileExists($actualFile); - $this->assertFileEquals($expectedFile, $actualFile, "Failed asserting that file contents of\n$actualFile\nare equal to file contents of\n$expectedFile\n\n cp $actualFile $expectedFile \n\n "); - } - } - protected function findActualFiles():array { $actualFiles = array_map(function($file) { @@ -195,8 +184,8 @@ public function testAfterKeyword() $dbSchema, 'tableName', $columnSchema, false, false ); - $this->assertContains('AFTER username', $column->getCode()); - $this->assertNotContains('AFTER username', $columnWithoutPreviousCol->getCode()); + $this->assertStringContainsString('AFTER username', $column->getCode()); + $this->assertStringNotContainsString('AFTER username', $columnWithoutPreviousCol->getCode()); // test `after` for fluent part in function call `after()` unset($column, $columnWithoutPreviousCol); @@ -208,7 +197,7 @@ public function testAfterKeyword() $dbSchema, 'tableName', $columnSchema, true, false ); - $this->assertContains("->after('username')", $column->getCode()); - $this->assertNotContains("->after('username')", $columnWithoutPreviousCol->getCode()); + $this->assertStringContainsString("->after('username')", $column->getCode()); + $this->assertStringNotContainsString("->after('username')", $columnWithoutPreviousCol->getCode()); } } diff --git a/tests/unit/MultiDbSecondaryMigrationTest.php b/tests/unit/MultiDbSecondaryMigrationTest.php index 404f64bf..1fc00715 100644 --- a/tests/unit/MultiDbSecondaryMigrationTest.php +++ b/tests/unit/MultiDbSecondaryMigrationTest.php @@ -99,17 +99,6 @@ protected function runGenerator($configFile, string $dbName = 'mysql') } } - protected function compareFiles(array $expected, string $testFile) - { - foreach ($expected as $file) { - $expectedFile = str_replace('@app', substr($testFile, 0, -4), $file); - $actualFile = str_replace('@app', Yii::getAlias('@app'), $file); - self::assertFileExists($expectedFile); - self::assertFileExists($actualFile); - $this->assertFileEquals($expectedFile, $actualFile, "Failed asserting that file contents of\n$actualFile\nare equal to file contents of\n$expectedFile"); - } - } - protected function findActualFiles():array { $actualFiles = array_map(function($file) { From 5bb0899a1e2970d755d126225884cc7966185a1c Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 28 Mar 2023 14:56:41 +0200 Subject: [PATCH 10/12] fix php 8 deprecation errors --- src/lib/ColumnToCode.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index 87045697..c9c1887e 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -156,14 +156,14 @@ public function getCode(bool $quoted = false):string return '$this->' . $this->fluentParts['type']; } if ($this->isBuiltinType) { - $parts = [ + $parts = array_filter([ $this->fluentParts['type'], $this->fluentParts['nullable'], $this->fluentParts['default'], $this->fluentParts['position'] - ]; + ]); array_unshift($parts, '$this'); - return implode('->', array_filter(array_map('trim', $parts), 'trim')); + return implode('->', array_filter(array_map('trim', $parts))); } if ($this->rawParts['default'] === null) { $default = ''; @@ -508,7 +508,7 @@ public function resolvePosition() if ($this->position === BaseMigrationBuilder::POS_FIRST) { $this->fluentParts['position'] = 'first()'; $this->rawParts['position'] = BaseMigrationBuilder::POS_FIRST; - } elseif (strpos($this->position, BaseMigrationBuilder::POS_AFTER.' ') !== false) { + } elseif ($this->position !== null && strpos($this->position, BaseMigrationBuilder::POS_AFTER.' ') !== false) { $previousColumn = str_replace(BaseMigrationBuilder::POS_AFTER.' ', '', $this->position); $this->fluentParts['position'] = 'after(\''.$previousColumn.'\')'; $this->rawParts['position'] = BaseMigrationBuilder::POS_AFTER.' '.$previousColumn; From 1ac75a09b52da7aaed2887dcd36fdff067947d97 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 28 Mar 2023 13:41:01 +0530 Subject: [PATCH 11/12] Attempt to fix failing test on Github CI --- tests/unit/RelationsInFakerTest.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/unit/RelationsInFakerTest.php b/tests/unit/RelationsInFakerTest.php index d2be8989..876f7dc2 100644 --- a/tests/unit/RelationsInFakerTest.php +++ b/tests/unit/RelationsInFakerTest.php @@ -32,14 +32,25 @@ public function testIndex() $finalSortedModels = static::sortModels($fakers); $this->assertSame($finalSortedModels, [ + // 'Account', + // 'C123', + // 'D123', + // 'B123', + // 'E123', + // 'Domain', + // 'A123', + // 'Routing', + + // 2nd order - sort() case 'Account', 'C123', 'D123', 'B123', - 'E123', - 'Domain', 'A123', + 'Domain', + 'E123', 'Routing', + ]); $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ @@ -74,6 +85,7 @@ public static function sortModels(array $fakers, string $fakerNamespace = 'app\\ } } + // this models are not dependent on any models $standalone = array_filter($modelsDependencies, function ($elm) { return $elm === null; }); @@ -84,6 +96,7 @@ public static function sortModels(array $fakers, string $fakerNamespace = 'app\\ $justDepenentModels = array_keys($dependent); $sortedDependentModels = $justDepenentModels; + sort($sortedDependentModels); foreach ($justDepenentModels as $model) { if ($modelsDependencies[$model] !== null) { From 14548ab7a15247b1774e9d8f5c2a02b022879424 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 28 Mar 2023 13:54:59 +0530 Subject: [PATCH 12/12] Attempt to fix failing test on Github CI - 2 --- tests/unit/RelationsInFakerTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit/RelationsInFakerTest.php b/tests/unit/RelationsInFakerTest.php index 876f7dc2..b8f3f4c6 100644 --- a/tests/unit/RelationsInFakerTest.php +++ b/tests/unit/RelationsInFakerTest.php @@ -111,8 +111,9 @@ public static function sortModels(array $fakers, string $fakerNamespace = 'app\\ } } } - - $finalSortedModels = array_merge(array_keys($standalone), $sortedDependentModels); + $standalone = array_keys($standalone); + sort($standalone); + $finalSortedModels = array_merge($standalone, $sortedDependentModels); return $finalSortedModels; }