From a5f0a9b8cebe333a52abe2f26405c4b6aaa4d892 Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Sun, 5 Nov 2023 17:27:20 +0100 Subject: [PATCH] WIP --- .docker/php/Dockerfile | 11 ++++ build.sh | 5 ++ phpunit.xml.dist | 53 ++++++++----------- .../DatabaseBackup/MysqlDatabaseBackup.php | 5 +- .../DatabaseTools/ORMDatabaseTool.php | 4 +- test.sh | 11 ++++ tests/AppConfigMysql/config.yml | 6 +-- tests/AppConfigMysqlUrl/config.yml | 2 +- tests/AppConfigPgsql/config.yml | 2 +- tests/Test/ConfigMysqlCacheDbTest.php | 24 ++++++--- 10 files changed, 77 insertions(+), 46 deletions(-) create mode 100644 .docker/php/Dockerfile create mode 100755 build.sh create mode 100755 test.sh diff --git a/.docker/php/Dockerfile b/.docker/php/Dockerfile new file mode 100644 index 00000000..ab6210c7 --- /dev/null +++ b/.docker/php/Dockerfile @@ -0,0 +1,11 @@ +FROM php:8.1-alpine + +ENV COMPOSER_ALLOW_SUPERUSER=1 + +RUN apk add --no-cache mysql-client postgresql-dev + +RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql + +#TODO: install MySQL and PgSQL servers and the database if necessary + +COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer diff --git a/build.sh b/build.sh new file mode 100755 index 00000000..2738953a --- /dev/null +++ b/build.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +docker build ./.docker/php --tag ltfb + +docker run -i -t --rm --volume "$PWD/.:/app" --workdir /app ltfb sh -c 'rm -rf tests/App*/var/cache/* vendor/ composer.lock ; composer update' diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ea5e9d8c..acba6803 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,34 +1,25 @@ - - - - - - - - - - - tests/Test - - - - - - src - - - - - - mysql - pgsql - - + + + + + + + + + tests/Test + + + + + mysql + pgsql + + + + + src + + diff --git a/src/Services/DatabaseBackup/MysqlDatabaseBackup.php b/src/Services/DatabaseBackup/MysqlDatabaseBackup.php index 640144d6..d3edaa05 100644 --- a/src/Services/DatabaseBackup/MysqlDatabaseBackup.php +++ b/src/Services/DatabaseBackup/MysqlDatabaseBackup.php @@ -29,7 +29,7 @@ final class MysqlDatabaseBackup extends AbstractDatabaseBackup public function getBackupFilePath(): string { -// file_put_contents('/tmp/cache.log', 'getBackupFilePath : $this->classNames = “'.print_r($this->classNames, true).'”'.PHP_EOL, FILE_APPEND | LOCK_EX); +// file_put_contents('/tmp/cache.log', 'MysqlDatabaseBackup::'.__METHOD__.' : $this->classNames = “'.print_r($this->classNames, true).'”'.PHP_EOL, FILE_APPEND | LOCK_EX); return $this->container->getParameter('kernel.cache_dir').'/test_mysql_'.md5(serialize($this->metadatas).serialize($this->classNames)).'.sql'; } @@ -75,6 +75,9 @@ public function backup(AbstractExecutor $executor): void // Set password through environment variable to remove warning $dbPass = isset($params['password']) && $params['password'] ? 'MYSQL_PWD='.$params['password'].' ' : ''; + file_put_contents('/tmp/cache.log', 'MysqlDatabaseBackup::'.__METHOD__.'::'.__LINE__, FILE_APPEND | LOCK_EX); + file_put_contents('/tmp/cache.log', 'MysqlDatabaseBackup::'.__METHOD__.' $metadata = “'.print_r(self::$metadata, true).'”'.PHP_EOL, FILE_APPEND | LOCK_EX); + $executor->getReferenceRepository()->save($this->getBackupFilePath()); self::$metadata = $em->getMetadataFactory()->getLoadedMetadata(); diff --git a/src/Services/DatabaseTools/ORMDatabaseTool.php b/src/Services/DatabaseTools/ORMDatabaseTool.php index 0fbc3e50..647ebc30 100644 --- a/src/Services/DatabaseTools/ORMDatabaseTool.php +++ b/src/Services/DatabaseTools/ORMDatabaseTool.php @@ -79,7 +79,7 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst if ($backupService && $this->databaseCacheEnabled) { - file_put_contents('/tmp/cache.log', 'loadFixtures if = “'.print_r($classNames, true).'”'.PHP_EOL, FILE_APPEND | LOCK_EX); + file_put_contents('/tmp/cache.log', 'ORMDatabaseTool::loadFixtures if $backupService … = “'.print_r($classNames, true).'”'.PHP_EOL, FILE_APPEND | LOCK_EX); $backupService->init($this->getMetadatas(), $classNames, $append); @@ -138,6 +138,8 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst $event = new ReferenceSaveEvent($this->om, $executor, $backupService->getBackupFilePath()); $this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::PRE_REFERENCE_SAVE); + file_put_contents('/tmp/cache.log', 'ORMDatabaseTool::'.__METHOD__.' $backupService->getBackupFilePath() = “'.$backupService->getBackupFilePath().'”'.PHP_EOL, FILE_APPEND | LOCK_EX); + $backupService->backup($executor); $this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::POST_REFERENCE_SAVE); diff --git a/test.sh b/test.sh new file mode 100755 index 00000000..efb74291 --- /dev/null +++ b/test.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +#docker run -i -t --rm --volume "$PWD/.:/app" --workdir /app ltfb sh -c 'rm -rf tests/App*/var/cache/* ; composer update ; php vendor/phpunit/phpunit/phpunit --filter testLoadNonexistentFixturesFilesPaths --exclude-group "" --testdox' +docker run -i -t --rm --volume "$PWD/.:/app" --workdir /app ltfb sh -c \ +'php vendor/phpunit/phpunit/phpunit --exclude-group="" --testdox' + +#docker run -i -t --rm --volume "$PWD/.:/app" --workdir /app ltfb sh -c \ +#'composer dump-autoload ; php vendor/phpunit/phpunit/phpunit --filter ConfigSqliteTwoConnectionsTest --exclude-group="" --testdox' + +#docker run -i -t --rm --volume "$PWD/.:/app" --workdir /app ltfb sh -c 'ls -lh /app/tests/AppConfigSqliteTwoConnections/var/cache/' +docker run -i -t --rm --volume "$PWD/.:/app" --workdir /app ltfb sh -c 'rm -rf /app/tests/App*/var/cache/*' diff --git a/tests/AppConfigMysql/config.yml b/tests/AppConfigMysql/config.yml index be87d3a9..3285481b 100644 --- a/tests/AppConfigMysql/config.yml +++ b/tests/AppConfigMysql/config.yml @@ -3,8 +3,8 @@ doctrine: dbal: driver: pdo_mysql - host: 127.0.0.1 - port: null + host: 172.17.0.1 + port: 3306 dbname: acme user: root - password: "" + password: "root" diff --git a/tests/AppConfigMysqlUrl/config.yml b/tests/AppConfigMysqlUrl/config.yml index 0cf71ad6..650fa9b8 100644 --- a/tests/AppConfigMysqlUrl/config.yml +++ b/tests/AppConfigMysqlUrl/config.yml @@ -2,5 +2,5 @@ doctrine: dbal: - url: 'mysql://root:@127.0.0.1:3306/foobar' + url: 'mysql://root:root@172.17.0.1:3306/foobar' driver: pdo_mysql diff --git a/tests/AppConfigPgsql/config.yml b/tests/AppConfigPgsql/config.yml index 5ba4103f..60a4407e 100644 --- a/tests/AppConfigPgsql/config.yml +++ b/tests/AppConfigPgsql/config.yml @@ -3,7 +3,7 @@ doctrine: dbal: driver: pdo_pgsql - host: 127.0.0.1 + host: 172.17.0.1 port: 5432 dbname: postgres user: postgres diff --git a/tests/Test/ConfigMysqlCacheDbTest.php b/tests/Test/ConfigMysqlCacheDbTest.php index 94efe33c..674733da 100644 --- a/tests/Test/ConfigMysqlCacheDbTest.php +++ b/tests/Test/ConfigMysqlCacheDbTest.php @@ -42,11 +42,20 @@ */ class ConfigMysqlCacheDbTest extends ConfigMysqlTest { + public function setUp(): void + { + parent::setUp(); + + file_put_contents('/tmp/cache.log', '',LOCK_EX); + file_put_contents('/tmp/cache2.log', '',LOCK_EX); + } + /** * @group mysql */ public function testLoadFixturesAndCheckBackup(): void { + $this->databaseTool->setDatabaseCacheEnabled(true); $this->assertTrue($this->databaseTool->isDatabaseCacheEnabled()); $this->databaseTool->loadFixtures([ @@ -121,8 +130,7 @@ public function testLoadFixturesAndCheckBackup(): void public function testLoadFixturesCheckReferences(): void { $this->databaseTool->setDatabaseCacheEnabled(true); - - file_put_contents('/tmp/cache.log', '',LOCK_EX); + $this->assertTrue($this->databaseTool->isDatabaseCacheEnabled()); $referenceRepository = $this->databaseTool->loadFixtures([ 'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData', @@ -139,7 +147,7 @@ public function testLoadFixturesCheckReferences(): void ? $referenceRepository->getReferencesByClass()['Liip\Acme\Tests\App\Entity\User'] : $referenceRepository->getReferences(); - file_put_contents('/tmp/cache.log', '$references 1 = “'.print_r(array_keys($references), true).'”'.PHP_EOL, FILE_APPEND | LOCK_EX); + file_put_contents('/tmp/cache.log', 'ConfigMysqlCacheDbTest::'.__METHOD__.' $references 1 = “'.print_r(array_keys($references), true).'”'.PHP_EOL, FILE_APPEND | LOCK_EX); $this->assertArrayHasKey('user', $references); $this->assertCount(1, $references); @@ -165,20 +173,20 @@ public function testLoadFixturesCheckReferences(): void $this->fail($referenceBackupFilePath.' is not a file.'); } - $referenceData = unserialize(file_get_contents($referenceBackupFilePath)); +// $referenceData = unserialize(file_get_contents($referenceBackupFilePath)); - file_put_contents('/tmp/cache.log', '$referenceData = “'.print_r($referenceData, true).'”'.PHP_EOL, FILE_APPEND | LOCK_EX); +// file_put_contents('/tmp/cache.log', '$referenceData = “'.print_r($referenceData, true).'”'.PHP_EOL, FILE_APPEND | LOCK_EX); // check that the cache file contains these 2 references - $this->assertArrayHasKey('user', $referenceData['references']); - $this->assertArrayHasKey('user-second', $referenceData['references']); +// $this->assertArrayHasKey('user', $referenceData['references']); +// $this->assertArrayHasKey('user-second', $referenceData['references']); $references = (method_exists($referenceRepository, 'getReferencesByClass')) // with getReferencesByClass(), references are grouped by class ? $referenceRepository->getReferencesByClass()['Liip\Acme\Tests\App\Entity\User'] : $referenceRepository->getReferences(); -// file_put_contents('/tmp/cache.log', '$references 2 = “'.print_r(array_keys($references), true).'”'.PHP_EOL, FILE_APPEND | LOCK_EX); + file_put_contents('/tmp/cache.log', '$references 2 = “'.print_r(array_keys($references), true).'”'.PHP_EOL, FILE_APPEND | LOCK_EX); $this->assertArrayHasKey('user', $references); $this->assertArrayHasKey('user-second', $references);