Skip to content

Commit

Permalink
Test database reset
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Mar 18, 2024
1 parent a10fb7f commit b3d4a83
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/qa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ jobs:
php-version: 8.2

- name: Validate composer.json
run: composer validate --strict
run: composer validate --ansi --strict

- name: Install dependencies
uses: ramsey/composer-install@v2

- name: Run ergebnis/composer-normalize
run: composer normalize --ansi --dry-run

- name: Check CS-Fixer
run: composer cs:check

Expand Down
23 changes: 22 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ jobs:
dependencies: "highest"
phpunit-flags: "--fail-on-risky"

env:
MYSQL_HOST: 127.0.0.1
MYSQL_PORT: 3306
MYSQL_USER: root
MYSQL_PASSWORD: pimcore
MYSQL_DATABASE: pimcore
MYSQL_SERVER_VERSION: "10.11.5-MariaDB"

services:
mariadb:
image: mariadb:10.11.5
env:
MYSQL_ROOT_PASSWORD: pimcore
options: >-
--health-cmd="healthcheck.sh --connect --innodb_initialized"
--health-interval=10s
--health-timeout=5s
--health-retries=5
ports:
- 3306:3306

steps:
- name: Git Checkout
uses: actions/checkout@v4
Expand All @@ -37,7 +58,7 @@ jobs:
php-version: ${{ matrix.php-version }}

- name: Install dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3
with:
dependency-versions: ${{ matrix.dependencies }}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true,
"endroid/installer": true,
"ergebnis/composer-normalize": true,
"phpstan/extension-installer": true
},
"preferred-install": "dist",
Expand Down
54 changes: 54 additions & 0 deletions tests/Functional/ResetDatabaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);

namespace Tests\Functional\Neusta\Pimcore\TestingFramework;

use Doctrine\DBAL\Connection;
use Doctrine\Persistence\ManagerRegistry;
use Neusta\Pimcore\TestingFramework\Database\PimcoreDatabaseResetter;
use Pimcore\Console\Application;
use Pimcore\Test\KernelTestCase;
use Pimcore\Version;

final class ResetDatabaseTest extends KernelTestCase
{
/**
* @test
*
* @dataProvider databaseResetModeProvider
*/
public function it_resets_database(string $dumpLocation): void
{
$_SERVER['DATABASE_DUMP_LOCATION'] = $dumpLocation;

$application = new Application(self::bootKernel());
$application->setAutoExit(false);

/** @var ManagerRegistry $registry */
$registry = self::getContainer()->get('doctrine');

/** @var Connection $connection */
$connection = $registry->getConnection();

$resetter = new PimcoreDatabaseResetter($application, $registry);
$resetter->resetDatabase();

self::assertCount(1, $connection->fetchAllNumeric('SELECT * FROM assets'));
self::assertCount(1, $connection->fetchAllNumeric('SELECT * FROM documents'));
self::assertCount(1, $connection->fetchAllNumeric('SELECT * FROM objects'));
self::assertCount(2, $users = $connection->fetchAllAssociative('SELECT * FROM users'));
self::assertSame('system', $users[0]['name']);
self::assertSame('admin', $users[1]['name']);
}

public function databaseResetModeProvider(): iterable
{
yield 'Default mode' => [''];
yield 'Dump mode' => [self::isPimcore10() ? 'dump-10' : 'dump'];
}

private static function isPimcore10(): bool
{
return !method_exists(Version::class, 'getMajorVersion') || 10 === Version::getMajorVersion();
}
}
14 changes: 14 additions & 0 deletions tests/app/dump-10/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
INSERT INTO `assets`
VALUES (1, 0, 'folder', '', '/', NULL, 1710529075, 1710529075, 1, 1, NULL, 0, 0);

INSERT INTO `documents`
VALUES (1, 0, 'page', '', '/', 999999, 1, 1710529075, 1710529075, 1, 1, 0);

INSERT INTO `documents_page`
VALUES (1, 'App\\Controller\\DefaultController::defaultAction', '', '', '', '', NULL, NULL, '', NULL, NULL, NULL);

INSERT INTO `objects`
VALUES (1, 0, 'folder', '', '/', 999999, 1, 1710529075, 1710529075, 1, 1, NULL, NULL, NULL, NULL, 0);

INSERT INTO `users` (id, parentId, type, name, admin, active)
VALUES (0, 0, 'user', 'system', 1, 1);
11 changes: 11 additions & 0 deletions tests/app/dump/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
INSERT INTO `assets`
VALUES (1, 0, 'folder', '', '/', NULL, 1710529075, 1710529075, NULL, 1, 1, NULL, 0, 0);

INSERT INTO `documents`
VALUES (1, 0, 'page', '', '/', 999999, 1, 1710529075, 1710529075, 1, 1, 0);

INSERT INTO `documents_page`
VALUES (1, 'App\\Controller\\DefaultController::defaultAction', '', '', '', NULL, NULL, '', NULL, NULL, NULL);

INSERT INTO `objects`
VALUES (1, 0, 'folder', '', '/', 999999, 1, 1710529075, 1710529075, 1, 1, NULL, NULL, NULL, NULL, 0);

0 comments on commit b3d4a83

Please sign in to comment.