Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
alexislefebvre committed Nov 5, 2023
1 parent a622314 commit a5f0a9b
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 46 deletions.
11 changes: 11 additions & 0 deletions .docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -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'
53 changes: 22 additions & 31 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/3.7/en/appendixes.configuration.html -->
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
beStrictAboutOutputDuringTests="true"
>

<php>
<server name="KERNEL_CLASS" value="Liip\Acme\Tests\App\AppKernel" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests/Test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>

<groups>
<exclude>
<group>mysql</group>
<group>pgsql</group>
</exclude>
</groups>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" colors="true" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache">
<coverage/>
<php>
<server name="KERNEL_CLASS" value="Liip\Acme\Tests\App\AppKernel"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests/Test</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>mysql</group>
<group>pgsql</group>
</exclude>
</groups>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
5 changes: 4 additions & 1 deletion src/Services/DatabaseBackup/MysqlDatabaseBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down Expand Up @@ -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();

Expand Down
4 changes: 3 additions & 1 deletion src/Services/DatabaseTools/ORMDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -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/*'
6 changes: 3 additions & 3 deletions tests/AppConfigMysql/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion tests/AppConfigMysqlUrl/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

doctrine:
dbal:
url: 'mysql://root:@127.0.0.1:3306/foobar'
url: 'mysql://root:[email protected].0.1:3306/foobar'
driver: pdo_mysql
2 changes: 1 addition & 1 deletion tests/AppConfigPgsql/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 16 additions & 8 deletions tests/Test/ConfigMysqlCacheDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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',
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit a5f0a9b

Please sign in to comment.