Skip to content

Commit

Permalink
Merge pull request #29 from b13/feature/make-runtests
Browse files Browse the repository at this point in the history
[FEATURE] Add command 'make:testing:setup'
  • Loading branch information
o-ba authored Jul 13, 2022
2 parents 893b6af + 297e01e commit 78557ff
Show file tree
Hide file tree
Showing 17 changed files with 868 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Build/php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

$config = \TYPO3\CodingStandards\CsFixerConfig::create();
$config->getFinder()->exclude(['var']);
$config->getFinder()->exclude(['var', 'Resources/Private/CodeTemplates']);
return $config;
2 changes: 1 addition & 1 deletion Build/testing-docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ services:
set -x
fi
php -v | grep '^PHP';
find . -name \\*.php ! -path "./.Build/\\*" -print0 | xargs -0 -n1 -P4 php -dxdebug.mode=off -l >/dev/null
find . -name \\*.php ! -path "./.Build/\\*" ! -path "./Resources/Private/CodeTemplates\\*" -print0 | xargs -0 -n1 -P4 php -dxdebug.mode=off -l >/dev/null
"
phpstan:
Expand Down
32 changes: 27 additions & 5 deletions Classes/Command/Component/SimpleComponentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ abstract class SimpleComponentCommand extends AbstractCommand
/** @var ArrayConfiguration */
protected $arrayConfiguration;

/** @var bool */
protected $showFlushCacheMessage = true;

abstract protected function createComponent(): ComponentInterface;
abstract protected function publishComponentConfiguration(ComponentInterface $component): bool;

Expand Down Expand Up @@ -78,8 +81,8 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
protected function execute(InputInterface $input, OutputInterface $output): int
{
$component = $this->createComponent();
$absoluteComponentDirectory = $this->getAbsoluteComponentDirectory($component);

$absoluteComponentDirectory = $this->package->getPackagePath() . $this->getExtensionClassesPath($this->package, $this->psr4Prefix) . $component->getDirectory();
if (!file_exists($absoluteComponentDirectory)) {
try {
GeneralUtility::mkdir_deep($absoluteComponentDirectory);
Expand All @@ -89,9 +92,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

$componentFile = rtrim($absoluteComponentDirectory, '/') . '/' . $component->getName() . '.php';
// Use .php in case no file extension was given
$fileInfo = pathinfo($component->getName());
$filename = $fileInfo['filename'] . '.' . (($fileInfo['extension'] ?? false) ? $fileInfo['extension'] : 'php');

$componentFile = rtrim($absoluteComponentDirectory, '/') . '/' . $filename;
if (file_exists($componentFile)
&& !$this->io->confirm('The file ' . $componentFile . ' already exists. Do you want to override it?', true)
&& !$this->io->confirm('The file ' . $componentFile . ' already exists. Do you want to override it?')
) {
$this->io->note('Aborting component generation.');
return 0;
Expand All @@ -111,7 +118,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

$this->io->note('You might want to flush the cache now');
if ($this->showFlushCacheMessage) {
$this->io->note('You might want to flush the cache now');
}

return 0;
}

Expand Down Expand Up @@ -185,7 +195,7 @@ protected function initializeArrayConfiguration(string $file, string $directory
/**
* Write the updated array configuration for the current package
*/
public function writeArrayConfiguration(): bool
protected function writeArrayConfiguration(): bool
{
if ($this->arrayConfiguration->getConfiguration() === []) {
// Array configuration was not properly set
Expand All @@ -194,4 +204,16 @@ public function writeArrayConfiguration(): bool

return $this->arrayConfiguration->write();
}

/**
* Returns the absolute path to the component directory, while assuming that all
* components are in the extensions classes directory. Can be overwritten in commands,
* if this is not the case.
*/
protected function getAbsoluteComponentDirectory(ComponentInterface $component): string
{
return $this->package->getPackagePath()
. $this->getExtensionClassesPath($this->package, $this->psr4Prefix)
. $component->getDirectory();
}
}
81 changes: 81 additions & 0 deletions Classes/Command/Component/TestingSetupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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\Component;

use B13\Make\Component\ComponentInterface;
use B13\Make\Component\TestingSetup;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command for creating docker based testing environment setup
*/
class TestingSetupCommand extends SimpleComponentCommand
{
/**
* @var string $folder
*/
protected $folder = '';

/**
* @var string $file
*/
protected $file;

protected function configure(): void
{
parent::configure();
$this->setDescription('Create a docker based testing environment setup');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->showFlushCacheMessage = false;

$this->file = 'runTests.sh';
$this->folder = 'Build/Scripts/';
parent::execute($input, $output);

$this->file = 'docker-compose.yml';
$this->folder = 'Build/testing-docker/';
parent::execute($input, $output);

$this->io->success(
'The docker based testing environment setup is ready. You can enter the root directory of ' .
$this->package->getPackageKey() . ' and execute: "bash Build/Scripts/runTests.sh -h"'
);

$this->io->note('Running specific test suits like "cgl" or "unit" requires installing the corresponding packages and configuration.');

return 0;
}

protected function createComponent(): ComponentInterface
{
return (new TestingSetup($this->psr4Prefix))
->setExtensionKey($this->extensionKey)
->setDirectory($this->folder)
->setName($this->file);
}

protected function publishComponentConfiguration(ComponentInterface $component): bool
{
// As we do not need to publish a configuration, we just return true
return true;
}

protected function getAbsoluteComponentDirectory(ComponentInterface $component): string
{
return $this->package->getPackagePath() . $this->folder;
}
}
4 changes: 2 additions & 2 deletions Classes/Component/AbstractComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ protected function getNamespace(): string
protected function createFileContent(string $fileName, array $replace): string
{
return (string)preg_replace_callback(
'/\{\{([A-Z]*)\}\}/',
'/\{\{([A-Z_]*)\}\}/',
static function ($result) use ($replace): string {
return $replace[$result[1]] ?? $result[0];
},
file_get_contents(__DIR__ . '/../../Resources/Private/CodeTemplates/' . $fileName . '.txt')
file_get_contents(__DIR__ . '/../../Resources/Private/CodeTemplates/' . $fileName)
);
}
}
2 changes: 1 addition & 1 deletion Classes/Component/BackendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function getArrayConfiguration(): array
public function __toString(): string
{
return $this->createFileContent(
'BackendController',
'BackendController.php',
[
'NAMESPACE' => $this->getNamespace(),
'NAME' => $this->name,
Expand Down
2 changes: 1 addition & 1 deletion Classes/Component/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function setSchedulable(bool $schedulable): self
public function __toString(): string
{
return $this->createFileContent(
'Command',
'Command.php',
[
'NAMESPACE' => $this->getNamespace(),
'NAME' => $this->name,
Expand Down
2 changes: 1 addition & 1 deletion Classes/Component/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function setMethodName(string $methodName): self
public function __toString(): string
{
return $this->createFileContent(
'EventListener',
'EventListener.php',
[
'NAMESPACE' => $this->getNamespace(),
'NAME' => $this->name,
Expand Down
2 changes: 1 addition & 1 deletion Classes/Component/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function setAfter(array $after): self
public function __toString(): string
{
return $this->createFileContent(
'Middleware',
'Middleware.php',
[
'NAMESPACE' => $this->getNamespace(),
'NAME' => $this->name,
Expand Down
49 changes: 49 additions & 0 deletions Classes/Component/TestingSetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Component;

/**
* Testing environment component setup
*/
class TestingSetup extends AbstractComponent
{
/** @var string */
protected $extensionKey = '';

public function getExtensionKey(): string
{
return $this->extensionKey;
}

public function setExtensionKey(string $extensionKey): self
{
$this->extensionKey = $extensionKey;
return $this;
}

public function setName(string $name): AbstractComponent
{
$this->name = $name;
return $this;
}

public function __toString(): string
{
return $this->createFileContent(
$this->getDirectory() . $this->getName(),
[
'EXTENSION_KEY' => $this->getExtensionKey(),
]
);
}
}
7 changes: 7 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ services:
command: 'make:middleware'
description: 'Create a PSR-15 middleware'
schedulable: false

B13\Make\Command\Component\TestingSetupCommand:
tags:
- name: 'console.command'
command: 'make:testing:setup'
description: 'Create a docker based testing environment setup'
schedulable: false
Loading

0 comments on commit 78557ff

Please sign in to comment.