Skip to content

Commit

Permalink
Merge pull request #19 from andreaswolf/phpunit-tests
Browse files Browse the repository at this point in the history
Integrate PhpUnit tests
  • Loading branch information
andreaswolf authored Aug 6, 2023
2 parents b5cbaab + 47704e7 commit f212821
Show file tree
Hide file tree
Showing 16 changed files with 372 additions and 75 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,63 @@ jobs:
- "7.4"
command:
- "normalize"

functional-tests:
name: "Functional tests"
runs-on: ubuntu-22.04
env:
DB_DATABASE: typo3
DB_USER: root
DB_PASSWORD: root
DB_HOST: localhost
steps:
- name: "Checkout"
uses: actions/checkout@v3
- name: "Install PHP"
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php-version }}"
tools: composer:v2.4
extensions: mysqli
coverage: none
- name: "Show Composer version"
run: "composer --version"
- name: "Show the Composer configuration"
run: "composer config --global --list"
- name: "Cache dependencies installed with composer"
uses: actions/cache@v3
with:
key: "php${{ matrix.php-version }}-typo3${{ matrix.typo3-version }}-${{ matrix.composer-dependencies }}-composer-${{ hashFiles('**/composer.json') }}"
path: ~/.cache/composer
restore-keys: "php${{ matrix.php-version }}-typo3${{ matrix.typo3-version }}-${{ matrix.composer-dependencies }}-composer-\n"
- name: "Install TYPO3 Core"
env:
TYPO3: "${{ matrix.typo3-version }}"
run: |
composer require --no-ansi --no-interaction --no-progress --no-install typo3/cms-core:"$TYPO3"
composer show
- name: "Install lowest dependencies with composer"
if: "matrix.composer-dependencies == 'lowest'"
run: |
composer update --no-ansi --no-interaction --no-progress --with-dependencies --prefer-lowest
composer show
- name: "Install highest dependencies with composer"
if: "matrix.composer-dependencies == 'highest'"
run: |
composer update --no-ansi --no-interaction --no-progress --with-dependencies
composer show
- name: "Start MySQL"
run: "sudo /etc/init.d/mysql start"
- name: "Run functional tests"
run: |
export typo3DatabaseName="$DB_DATABASE";
export typo3DatabaseHost="$DB_HOST";
export typo3DatabaseUsername="$DB_USER";
export typo3DatabasePassword="$DB_PASSWORD";
composer test:php:functional
strategy:
fail-fast: false
matrix:
typo3-version: ["^11.5"]
php-version: ["7.4", "8.0", "8.1"]
composer-dependencies: ["lowest", "highest"]
54 changes: 54 additions & 0 deletions Build/FunctionalTests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!--
Boilerplate for a functional test suite setup.
This file is loosely maintained within TYPO3 testing-framework, extensions
are encouraged to not use it directly, but to copy it to an own place,
for instance Build/FunctionalTests.xml.
Note FunctionalTestsBootstrap.php should be copied along the way.
Functional tests should extend \TYPO3\TestingFramework\Core\Tests\FunctionalTestCase,
take a look at this class for further documentation on how to run the suite.
TYPO3 CMS functional test suite also needs phpunit bootstrap code, the
file is located next to this .xml as FunctionalTestsBootstrap.php
phpunit v9 compatible version, use -10.xml file for phpunit 10.
-->
<phpunit
backupGlobals="true"
bootstrap="../vendor/typo3/testing-framework/Resources/Core/Build/FunctionalTestsBootstrap.php"
cacheResult="false"
colors="true"
convertErrorsToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
convertNoticesToExceptions="true"
forceCoversAnnotation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="false"
beStrictAboutTestsThatDoNotTestAnything="false"
failOnWarning="true"
failOnRisky="true"
>
<testsuites>
<testsuite name="Functional tests">
<directory>../Tests/Functional/</directory>
</testsuite>
</testsuites>
<php>
<!-- @deprecated: will be removed with next major version, constant TYPO3_MODE is deprecated -->
<const name="TYPO3_MODE" value="BE" />
<!--
@deprecated: Set this to not suppress warnings, notices and deprecations in functional tests
with TYPO3 core v11 and up.
Will always be done with next major version.
To still suppress warnings, notices and deprecations, do NOT define the constant at all.
-->
<const name="TYPO3_TESTING_FUNCTIONAL_REMOVE_ERROR_HANDLER" value="true" />
<ini name="display_errors" value="1" />
<env name="TYPO3_CONTEXT" value="Testing" />
</php>
</phpunit>
2 changes: 1 addition & 1 deletion Classes/Service/DoctrineService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

class DoctrineService implements LoggerAwareInterface
{
private const MIGRATION_TABLE_NAME = 'doctrine_migrationstatus';
public const MIGRATION_TABLE_NAME = 'doctrine_migrationstatus';

use LoggerAwareTrait;

Expand Down
37 changes: 37 additions & 0 deletions Classes/Tester/DoctrineCommandRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace KayStrobach\Migrations\Tester;

use KayStrobach\Migrations\Command\MigrateCommand;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;

/**
* Helper class to run the doctrine/migrations CLI commands within functional tests.
*/
final class DoctrineCommandRunner
{
private ContainerInterface $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public function executeMigrateCommand(): void
{
$command = $this->container->get(MigrateCommand::class);
$command->setHelperSet(new HelperSet([
'question' => new QuestionHelper(),
]));
$commandTester = new CommandTester($command);

$commandTester->execute(['--no-interaction']);

$commandTester->assertCommandIsSuccessful();
}
}
5 changes: 5 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ services:
- '@TYPO3\CMS\Core\Database\ConnectionPool'
- '@TYPO3\CMS\Core\Log\LogManager'

KayStrobach\Migrations\Tester\DoctrineCommandRunner:
public: true
arguments:
- '@Psr\Container\ContainerInterface'

KayStrobach\Migrations\Command\DiffCommand:
arguments:
- '@KayStrobach\Migrations\Service\DoctrineService'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace KayStrobach\Migrations\TestFixtures\Migrations\Mysql;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

class Version20230804102700 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('INSERT INTO pages (title, uid, pid) VALUES ("My test page", 1, 0)');
}

public function down(Schema $schema): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "typo3-cms-extension",
"autoload": {
"psr-4": {
"KayStrobach\\Migrations\\TestFixtures\\Migrations\\": "Migrations/"
}
}
}
19 changes: 19 additions & 0 deletions Tests/Functional/Command/Fixtures/test_migrations/ext_emconf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

$EM_CONF[$_EXTKEY] = [
'title' => 'Test migrations',
'description' => 'Test migrations for testing EXT:migrations',
'category' => 'service',
'state' => 'alpha',
'version' => '1.0.0',
'author' => 'Andreas Wolf',
'author_email' => '[email protected]',
'constraints' => [
'depends' => [
],
'conflicts' => [
],
'suggests' => [
],
],
];
48 changes: 48 additions & 0 deletions Tests/Functional/Command/MigrateCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace KayStrobach\Migrations\Tests\Functional\Command;

use KayStrobach\Migrations\Command\MigrateCommand;
use KayStrobach\Migrations\Service\DoctrineService;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

/**
* @covers \KayStrobach\Migrations\Command\MigrateCommand
*/
class MigrateCommandTest extends FunctionalTestCase
{
protected array $testExtensionsToLoad = [
'typo3conf/ext/migrations',
'typo3conf/ext/migrations/Tests/Functional/Command/Fixtures/test_migrations',
];

/** @test */
public function migrationCommandExecutesMigrationsDefinedInExtensionWhenMigrationNamespaceIsRegisteredInComposer(): void
{
$migrateCommand = $this->get(MigrateCommand::class);
$migrateCommand->setHelperSet(new HelperSet([
'question' => new QuestionHelper(),
]));
$commandTester = new CommandTester($migrateCommand);

$commandTester->execute(['--no-interaction']);

$commandTester->assertCommandIsSuccessful();

$connection = $this->get(ConnectionPool::class)
->getConnectionForTable(DoctrineService::MIGRATION_TABLE_NAME);
$result = $connection->select(['*'], DoctrineService::MIGRATION_TABLE_NAME)->fetchAllAssociative();

self::assertCount(1, $result);
self::assertSame('20230804102700', $result[0]['version']);

$result = $connection->select(['*'], 'pages', ['uid' => 1])->fetchAllAssociative();
self::assertSame('My test page', $result[0]['title']);
}
}
44 changes: 44 additions & 0 deletions Tests/Functional/Migration/AbstractDataHandlerMigrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace KayStrobach\Migrations\Tests\Functional\Migration;

use KayStrobach\Migrations\Service\DoctrineService;
use KayStrobach\Migrations\Tester\DoctrineCommandRunner;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\CommandLineUserAuthentication;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

/**
* @covers \KayStrobach\Migrations\Migration\AbstractDataHandlerMigration
*/
class AbstractDataHandlerMigrationTest extends FunctionalTestCase
{
protected array $testExtensionsToLoad = [
'typo3conf/ext/migrations',
'typo3conf/ext/migrations/Tests/Functional/Migration/Fixtures/test_migrations_datahandler',
];

/** @test */
public function dataHandlerMigrationRunsDataHandler(): void
{
Bootstrap::initializeBackendUser(CommandLineUserAuthentication::class);
Bootstrap::initializeLanguageObject();
$GLOBALS['BE_USER']->workspace = 0;
$this->get(DoctrineCommandRunner::class)->executeMigrateCommand();

$connection = $this->get(ConnectionPool::class)
->getConnectionForTable(DoctrineService::MIGRATION_TABLE_NAME);
$result = $connection->select(['*'], DoctrineService::MIGRATION_TABLE_NAME)->fetchAllAssociative();

self::assertCount(1, $result, 'No or more than one migration was executed');
self::assertSame('20230804162200', $result[0]['version']);

$result = BackendUtility::getRecord('pages', 1);
self::assertIsArray($result);
self::assertSame('My DataHandler test page', $result['title']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace KayStrobach\Migrations\TestFixtures\Migrations\Mysql;

use Doctrine\DBAL\Schema\Schema;
use KayStrobach\Migrations\Migration\AbstractDataHandlerMigration;

class Version20230804162200 extends AbstractDataHandlerMigration
{
public function preUp(Schema $schema): void
{
parent::preUp($schema);

$this->dataMap = [
'pages' => [
'NEW1234' => [
'pid' => 0,
'title' => 'My DataHandler test page',
],
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "typo3-cms-extension",
"autoload": {
"psr-4": {
"KayStrobach\\Migrations\\TestFixtures\\Migrations\\": "Migrations/"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

$EM_CONF[$_EXTKEY] = [
'title' => 'Test migrations',
'description' => 'Test migrations for testing EXT:migrations',
'category' => 'service',
'state' => 'alpha',
'version' => '1.0.0',
'author' => 'Andreas Wolf',
'author_email' => '[email protected]',
'constraints' => [
'depends' => [
],
'conflicts' => [
],
'suggests' => [
],
],
];
Loading

0 comments on commit f212821

Please sign in to comment.