Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add command 'make:testing:acceptance' #28

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions Classes/Command/AcceptanceTestsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php

declare(strict_types=1);

/*
* This file is part of TYPO3 CMS-based extension "b13/make" by b13.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/

namespace B13\Make\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use TYPO3\CMS\Core\Package\PackageInterface;
use TYPO3\CMS\Core\Service\MarkerBasedTemplateService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Command for creating a new backend controller component
*/
class AcceptanceTestsCommand extends AbstractCommand
{
/**
* @var Filesystem $filesystem
*/
protected $filesystem;

/**
* @var PackageInterface $package
*/
protected $package;

protected function configure(): void
{
parent::configure();
$this->setDescription('Prepare extensions to run acceptance tests');
$this->filesystem = GeneralUtility::makeInstance(Filesystem::class);
}

/**
* @throws \JsonException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->package = $this->packageResolver->resolvePackage($this->askForExtensionKey());

$packageKey = $this->package->getPackageKey();
$targetPackagePath = $this->package->getPackagePath();

if ($this->updateComposerFile($targetPackagePath)) {
$this->io->writeln('<info>Updated composer.json for EXT:' . $packageKey . '</info>');
} else {
$this->io->writeln('<comment>Failed to update composer.json for EXT:' . $packageKey . '</comment>');
}

$this->io->writeln('Selected package: ' . $packageKey);
$finder = GeneralUtility::makeInstance(Finder::class);

$codeTemplatePath = '/Resources/Private/CodeTemplates/AcceptanceTests';
$templatePath = $this->packageResolver->resolvePackage('b13/make')->getPackagePath() . $codeTemplatePath;

$this->filesystem->mkdir([
$targetPackagePath . '/Tests/Acceptance/Fixtures',
$targetPackagePath . '/Tests/Acceptance/Application',
$targetPackagePath . '/Tests/Acceptance/Support/Extension'
]);

// Create public folder which is required for e.g. acceptance tests to work
$publicFolderPath = $targetPackagePath . '/Resources/Public';
if (!is_dir($publicFolderPath)) {
$createPublic = $this->io->confirm(
'Resource/Public is necessary e.g. for acceptance tests. Do you want to create it now?',
true
);

if ($createPublic) {
$this->filesystem->mkdir([$publicFolderPath]);
// Ensure the folder will be detected by git and committed
$this->filesystem->touch([$publicFolderPath . '/.gitkeep']);
}
}

$files = $finder->in($templatePath)->files();

foreach ($files as $file) {
$target = $targetPackagePath . 'Tests' . explode('AcceptanceTests', $file->getRealPath())[1];

if (!is_file($target)) {
$content = $file->getContents();
$this->substituteMarkersAndSave($content, $target);
} else {
$overwrite = $this->io->confirm('File exists ' . basename($target) . '. Do you want to overwrite it?');
if ($overwrite) {
$content = $file->getContents();
$this->substituteMarkersAndSave($content, $target);
}
}
}

return 0;
}

/**
* Extend/prepare composer.json of the extension
* for acceptance tests
*
* @throws \JsonException
*/
protected function updateComposerFile(string $packagePath): bool
{
$composerFile = $packagePath . '/composer.json';
$composerJson = file_get_contents($composerFile);
$composer = json_decode($composerJson, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \JsonException('Could not parse ' . $composerFile);
}

$namespace = rtrim($this->getNamespace(), '\\');

$addToComposerFile = [
'require-dev' => [
'codeception/codeception' => '^4.1',
'codeception/module-asserts' => '^1.2',
'codeception/module-webdriver' => '^1.1',
'typo3/testing-framework' => '^6.16.2'
],
'autoload-dev' => [
'psr-4' => [
$namespace . '\\Tests\\' => 'Tests/'
]
],
'config' => [
'vendor-dir' => '.Build/vendor',
'bin-dir' => '.Build/bin',
],
'extra' => [
'typo3/cms' => [
'app-dir' => '.Build',
'web-dir' => '.Build/Web',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use public here as Web dates back to TYPO3 Flow times and should not be used in TYPO3 world.

]
]
];

$enhancedComposer = $this->enhanceComposerFile($composer, $addToComposerFile);

return GeneralUtility::writeFile($composerFile, json_encode($enhancedComposer, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), true);
}

/**
* Substitute marker values and save file to extension_key/Tests/
*
* @param string $content
* @param string $target
*/
protected function substituteMarkersAndSave(string $content, string $target): void
{
$markerService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
$templateContent = $markerService->substituteMarker(
$content,
'{{NAMESPACE}}',
rtrim($this->getNamespace(), '\\')
);
$templateContent = $markerService->substituteMarker(
$templateContent,
'{{EXTENSION_KEY}}',
$this->package->getPackageKey()
);

try {
$this->filesystem->dumpFile($target, $templateContent);
} catch (IOException $exception) {
$this->io->writeln('<error>Failed to save file in ' . $target . '</error>');
}
}

protected function getNamespace(): string
{
return (string)key((array)($this->package->getValueFromComposerManifest('autoload')->{'psr-4'} ?? []));
}

private function enhanceComposerFile(array &$composer, array &$addToComposerFile): array
{
foreach ($addToComposerFile as $key => $value) {
if (is_array($value) && isset($composer[$key])) {
$this->enhanceComposerFile($composer[$key], $value);
} else {
$composer[$key] = $value;
}
}

return $composer;
}
}
6 changes: 6 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ services:
command: 'make:testing:setup'
description: 'Create a docker based testing environment setup'
schedulable: false
B13\Make\Command\AcceptanceTestsCommand:
tags:
- name: 'console.command'
command: 'make:testing:acceptance'
description: 'Create files required to run acceptance tests'
schedulable: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class_name: ApplicationTester
modules:
enabled:
- WebDriver:
url: http://web:8000/typo3temp/var/tests/acceptance
browser: chrome
wait: 1
host: chrome
window_size: 1400x800
- \TYPO3\TestingFramework\Core\Acceptance\Helper\Acceptance
- \TYPO3\TestingFramework\Core\Acceptance\Helper\Login:
sessions:
# This sessions must exist in the database fixture to get a logged in state.
editor: ff83dfd81e20b34c27d3e97771a4525a
admin: 886526ce72b86870739cc41991144ec1
- Asserts

extensions:
enabled:
- {{NAMESPACE}}\Tests\Acceptance\Support\Extension\ExtensionEnvironment

groups:
AcceptanceTests-Job-*: AcceptanceTests-Job-*
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace {{NAMESPACE}}\Tests\Acceptance\Backend;

use {{NAMESPACE}}\Tests\Acceptance\Support\ApplicationTester;

class ExampleCest
{
/**
* @param ApplicationTester $I
*/
public function _before(ApplicationTester $I)
{
$I->useExistingSession('admin');
$I->switchToMainFrame();
}

/**
* @param ApplicationTester $I
* @throws \Exception
*/
public function seeTestExample(ApplicationTester $I): void
{
$I->click('Page');
$I->switchToContentFrame();
$I->see('Web>Page module', '.callout-info');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<be_sessions>
<!-- hash_hmac('sha256', '886526ce72b86870739cc41991144ec1', sha1('iAmInvalid' . 'core-session-backend')) -->
<ses_id>9869d429fc72742a476d5073d006d45dfb732962d9c024423efafef537e1c5bd</ses_id>
<ses_iplock>[DISABLED]</ses_iplock>
<ses_userid>1</ses_userid>
<ses_tstamp>1777777777</ses_tstamp>
<ses_data></ses_data>
</be_sessions>
<be_sessions>
<!-- hash_hmac('sha256', 'ff83dfd81e20b34c27d3e97771a4525a', sha1('iAmInvalid' . 'core-session-backend')) -->
<ses_id>f4c02f70058e79a8e7b523a266d4291007deacba6b2ca2536dd72d2fbb23696a</ses_id>
<ses_iplock>[DISABLED]</ses_iplock>
<ses_userid>2</ses_userid>
<ses_tstamp>1777777777</ses_tstamp>
<ses_data></ses_data>
</be_sessions>
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<!-- Creates a page + subpage -->
<pages>
<uid>1</uid>
<pid>0</pid>
<title>Home</title>
<slug></slug>
<doktype>1</doktype>
<deleted>0</deleted>
<is_siteroot>1</is_siteroot>
<perms_everybody>15</perms_everybody>
</pages>
<pages>
<uid>2</uid>
<pid>1</pid>
<sorting>128</sorting>
<title>Subpage</title>
<slug>subpage</slug>
<deleted>0</deleted>
<doktype>1</doktype>
<perms_everybody>15</perms_everybody>
</pages>
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<sys_template>
<uid>1</uid>
<pid>1</pid>
<title>Root Template</title>
<deleted>0</deleted>
<root>1</root>
<clear>3</clear>
<config>
page = PAGE
page.10 = TEXT
page.10.data = page:title
</config>
</sys_template>
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace {{NAMESPACE}}\Tests\Acceptance\Support;

use {{NAMESPACE}}\Tests\Acceptance\Support\_generated\ApplicationTesterActions;
use TYPO3\TestingFramework\Core\Acceptance\Step\FrameSteps;

/**
* Default backend admin or editor actor in the backend
*/
class ApplicationTester extends \Codeception\Actor
{
use ApplicationTesterActions;
use FrameSteps;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace {{NAMESPACE}}\Tests\Acceptance\Support\Extension;

use Symfony\Component\Mailer\Transport\NullTransport;
use TYPO3\TestingFramework\Core\Acceptance\Extension\BackendEnvironment;

/**
* Load various core extensions and styleguide and call styleguide generator
*/
class ExtensionEnvironment extends BackendEnvironment
{
/**
* Load a list of core extensions
*
* @var array
*/
protected $localConfig = [
'coreExtensionsToLoad' => [
'core',
'extbase',
'filelist',
'setup',
'frontend',
'fluid',
'recordlist',
'backend',
'install',
],
'testExtensionsToLoad' => [
'typo3conf/ext/{{EXTENSION_KEY}}',
],
'xmlDatabaseFixtures' => [
'PACKAGE:typo3/testing-framework/Resources/Core/Acceptance/Fixtures/be_users.xml',
'PACKAGE:../Web/typo3conf/ext/{{EXTENSION_KEY}}/Tests/Acceptance/Fixtures/pages.xml',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use public

'PACKAGE:../Web/typo3conf/ext/{{EXTENSION_KEY}}/Tests/Acceptance/Fixtures/be_sessions.xml',
'PACKAGE:../Web/typo3conf/ext/{{EXTENSION_KEY}}/Tests/Acceptance/Fixtures/sys_template.xml',
],
'configurationToUseInTestInstance' => [
'MAIL' => [
'transport' => NullTransport::class,
],
],

// Link files/folders required for your acceptance tests to run
// 'pathsToLinkInTestInstance' => [
// 'typo3conf/ext/{{EXTENSION_KEY}}/Tests/Acceptance/Fixtures/sites' => 'typo3conf/sites',
// ]
];
}
Loading