Skip to content

Commit

Permalink
Merge 334c37d into eff2cbb
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Jun 26, 2020
2 parents eff2cbb + 334c37d commit a6aab1f
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 57 deletions.
5 changes: 5 additions & 0 deletions src/Exception/InvalidConfigurationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public static function createNoNodesConfigured(): InvalidConfigurationException
return new self('No nodes configured for application', 1334652427);
}

public static function createTypo3ConsoleScriptNotFound(string $class): InvalidConfigurationException
{
return new self('TYPO3 Console script was not found. Make sure it is available in your project and you set the "scriptFileName" option correctly. Alternatively you can remove this task (' . $class . ') in your deployment configuration.', 1481489230);
}

public static function createNoDeploymentNameGiven(): self
{
return new static('No deployment name given!', 1451865016);
Expand Down
65 changes: 16 additions & 49 deletions src/Task/TYPO3/CMS/AbstractCliTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* file that was distributed with this source code.
*/

use TYPO3\Flow\Utility\Files;
use TYPO3\Surf\Application\TYPO3\CMS;
use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
Expand Down Expand Up @@ -42,7 +41,7 @@ abstract class AbstractCliTask extends Task implements ShellCommandServiceAwareI
protected function executeCliCommand(array $cliArguments, Node $node, CMS $application, Deployment $deployment, array $options = [])
{
$this->determineWorkingDirectoryAndTargetNode($node, $application, $deployment, $options);
$phpBinaryPathAndFilename = isset($options['phpBinaryPathAndFilename']) ? $options['phpBinaryPathAndFilename'] : 'php';
$phpBinaryPathAndFilename = $options['phpBinaryPathAndFilename'] ?? 'php';
$commandPrefix = '';
if (isset($options['context'])) {
$commandPrefix = 'TYPO3_CONTEXT=' . escapeshellarg($options['context']) . ' ';
Expand All @@ -60,7 +59,7 @@ public function simulate(Node $node, Application $application, Deployment $deplo
$this->execute($node, $application, $deployment, $options);
}

protected function determineWorkingDirectoryAndTargetNode(Node $node, Application $application, Deployment $deployment, array $options = [])
protected function determineWorkingDirectoryAndTargetNode(Node $node, Application $application, Deployment $deployment, array $options = []): void
{
if (!isset($this->workingDirectory, $this->targetNode)) {
if (isset($options['useApplicationWorkspace']) && $options['useApplicationWorkspace'] === true) {
Expand All @@ -73,66 +72,34 @@ protected function determineWorkingDirectoryAndTargetNode(Node $node, Applicatio
}
}

/**
* @return string
*/
protected function getAvailableCliPackage(Node $node, CMS $application, Deployment $deployment, array $options = [])
protected function getAvailableCliPackage(Node $node, CMS $application, Deployment $deployment, array $options = []): ?string
{
try {
$this->getConsoleScriptFileName($node, $application, $deployment, $options);
return 'typo3_console';
} catch (InvalidConfigurationException $e) {
return null;
}
return null;
}

/**
* @return string
*/
protected function getConsoleScriptFileName(Node $node, CMS $application, Deployment $deployment, array $options = [])
protected function getConsoleScriptFileName(Node $node, CMS $application, Deployment $deployment, array $options = []): string
{
if (isset($options['scriptFileName']) && strpos($options['scriptFileName'], 'typo3cms') !== false && $this->fileExists($options['scriptFileName'], $node, $application, $deployment, $options)) {
return $options['scriptFileName'];
if (!isset($options['scriptFileName'])) {
throw InvalidConfigurationException::createTypo3ConsoleScriptNotFound(get_class($this));
}
throw new InvalidConfigurationException('TYPO3 Console script was not found. Make sure it is available in your project and you set the "scriptFileName" option correctly. Alternatively you can remove this task (' . get_class($this) . ') in your deployment configuration.', 1481489230);
}

/**
* Checks if a package exists in the packages directory
*
* @param string $packageKey
* @param Node $node
* @param CMS $application
* @param Deployment $deployment
* @param array $options
* @return bool
*/
protected function packageExists($packageKey, Node $node, CMS $application, Deployment $deployment, array $options = [])
{
$webDirectory = isset($options['webDirectory']) ? trim($options['webDirectory'], '\\/') : '';
return $this->directoryExists($webDirectory . '/typo3conf/ext/' . $packageKey, $node, $application, $deployment, $options);
}
if (false === strpos($options['scriptFileName'], 'typo3cms')) {
throw InvalidConfigurationException::createTypo3ConsoleScriptNotFound(get_class($this));
}

/**
* Checks if a given directory exists.
*
* @param string $directory
* @return bool
*/
protected function directoryExists($directory, Node $node, CMS $application, Deployment $deployment, array $options = [])
{
$this->determineWorkingDirectoryAndTargetNode($node, $application, $deployment, $options);
$directory = Files::concatenatePaths([$this->workingDirectory, $directory]);
return $this->shell->executeOrSimulate('test -d ' . escapeshellarg($directory), $this->targetNode, $deployment, true) !== false;
if (false === $this->fileExists($options['scriptFileName'], $node, $application, $deployment, $options)) {
throw InvalidConfigurationException::createTypo3ConsoleScriptNotFound(get_class($this));
}

return $options['scriptFileName'];
}

/**
* Checks if a given file exists.
*
* @param string $pathAndFileName
* @return bool
*/
protected function fileExists($pathAndFileName, Node $node, CMS $application, Deployment $deployment, array $options = [])
protected function fileExists(string $pathAndFileName, Node $node, CMS $application, Deployment $deployment, array $options = []): bool
{
$this->determineWorkingDirectoryAndTargetNode($node, $application, $deployment, $options);
$pathAndFileName = $this->workingDirectory . '/' . $pathAndFileName;
Expand Down
3 changes: 2 additions & 1 deletion src/Task/TYPO3/CMS/CompareDatabaseTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CompareDatabaseTask extends AbstractCliTask
{
public function execute(Node $node, Application $application, Deployment $deployment, array $options = [])
{
/** @var CMS $application */
Assert::isInstanceOf($application, CMS::class);
$cliArguments = $this->getSuitableCliArguments($node, $application, $deployment, $options);
if (empty($cliArguments)) {
Expand All @@ -51,7 +52,7 @@ public function execute(Node $node, Application $application, Deployment $deploy
protected function getSuitableCliArguments(Node $node, CMS $application, Deployment $deployment, array $options = [])
{
if ($this->getAvailableCliPackage($node, $application, $deployment, $options) === 'typo3_console') {
$databaseCompareMode = isset($options['databaseCompareMode']) ? $options['databaseCompareMode'] : '*.add,*.change';
$databaseCompareMode = $options['databaseCompareMode'] ?? '*.add,*.change';
return [$this->getConsoleScriptFileName($node, $application, $deployment, $options), 'database:updateschema', $databaseCompareMode];
}

Expand Down
20 changes: 18 additions & 2 deletions src/Task/TYPO3/CMS/FlushCachesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* file that was distributed with this source code.
*/

use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use TYPO3\Surf\Application\TYPO3\CMS;
use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
Expand All @@ -22,7 +24,12 @@ class FlushCachesTask extends AbstractCliTask
{
public function execute(Node $node, Application $application, Deployment $deployment, array $options = [])
{

/** @var CMS $application */
Assert::isInstanceOf($application, CMS::class);

$options = $this->configureOptions($options);

$cliArguments = $this->getSuitableCliArguments($node, $application, $deployment, $options);
if (empty($cliArguments)) {
$deployment->getLogger()->warning('Extension "typo3_console" was not found! Make sure it is available in your project, or remove this task (' . __CLASS__ . ') in your deployment configuration!');
Expand All @@ -37,13 +44,22 @@ public function execute(Node $node, Application $application, Deployment $deploy
);
}

protected function getSuitableCliArguments(Node $node, CMS $application, Deployment $deployment, array $options = [])
protected function getSuitableCliArguments(Node $node, CMS $application, Deployment $deployment, array $options = []): ?array
{
switch ($this->getAvailableCliPackage($node, $application, $deployment, $options)) {
case 'typo3_console':
return [$this->getConsoleScriptFileName($node, $application, $deployment, $options), 'cache:flush', '--force'];
return array_merge([$this->getConsoleScriptFileName($node, $application, $deployment, $options), 'cache:flush'], $options['arguments']);
default:
return [];
}
}

protected function resolveOptions(OptionsResolver $resolver)
{
$resolver->setDefault('arguments', [])
->setAllowedTypes('arguments', ['array', 'string'])
->setNormalizer('arguments', function (Options $options, $value) {
return (array)$value;
});
}
}
8 changes: 4 additions & 4 deletions tests/Unit/Task/BaseTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface;
use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
Expand Down Expand Up @@ -60,7 +61,7 @@ abstract class BaseTaskTest extends TestCase
protected $deployment;

/**
* @var LoggerInterface|PHPUnit_Framework_MockObject_MockObject
* @var LoggerInterface|ObjectProphecy
*/
protected $mockLogger;

Expand Down Expand Up @@ -110,9 +111,8 @@ protected function setUp()
$this->node->setHostname('hostname');
$this->deployment = new Deployment('TestDeployment');
$this->deployment->setContainer(static::getKernel()->getContainer());
/** @var PHPUnit_Framework_MockObject_MockObject|\Psr\Log\LoggerInterface $mockLogger */
$this->mockLogger = $this->createMock(LoggerInterface::class);
$this->deployment->setLogger($this->mockLogger);
$this->mockLogger = $this->prophesize(LoggerInterface::class);
$this->deployment->setLogger($this->mockLogger->reveal());
$this->deployment->setWorkspacesBasePath('./Data/Surf');
$this->application = new Application('TestApplication');

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Task/Generic/RollbackTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function canNotRollbackTooFewReleasesExist()
current
previous',
];
$this->mockLogger->expects($this->once())->method('notice')->with('No more releases you can revert to.');
$this->mockLogger->notice('No more releases you can revert to.')->shouldBeCalledOnce();
$this->task->execute($this->node, $this->application, $this->deployment);
$this->assertCount(1, $this->commands['executed']);
}
Expand Down
123 changes: 123 additions & 0 deletions tests/Unit/Task/TYPO3/CMS/FlushCachesTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace TYPO3\Surf\Tests\Unit\Task\TYPO3\CMS;

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

use Prophecy\Argument;
use TYPO3\Surf\Application\BaseApplication;
use TYPO3\Surf\Application\TYPO3\CMS;
use TYPO3\Surf\Exception\InvalidConfigurationException;
use TYPO3\Surf\Task\TYPO3\CMS\FlushCachesTask;
use TYPO3\Surf\Tests\Unit\Task\BaseTaskTest;

class FlushCachesTaskTest extends BaseTaskTest
{
/**
* @var FlushCachesTask
*/
protected $task;

/**
* @return FlushCachesTask
*/
protected function createTask(): FlushCachesTask
{
return new FlushCachesTask();
}

protected function setUp(): void
{
parent::setUp();
$this->application = new CMS('TestApplication');
$this->application->setDeploymentPath('/home/jdoe/app');
}

/**
* @test
*/
public function executeFlushCacheCommandWithWrongOptionsType(): void
{
$this->expectException(InvalidConfigurationException::class);
$options = [
'scriptFileName' => 'typo3cms',
'arguments' => 1
];
$this->task->execute($this->node, $this->application, $this->deployment, $options);
}

/**
* @test
*/
public function wrongApplicationTypeGivenThrowsException(): void
{
$invalidApplication = new BaseApplication('Hello world app');
$this->expectException(\InvalidArgumentException::class);
$this->task->execute($this->node, $invalidApplication, $this->deployment, []);
}

/**
* @test
*/
public function noSuitableCliArgumentsGiven(): void
{
$this->task->execute($this->node, $this->application, $this->deployment, []);
$this->mockLogger->warning(Argument::any())->shouldBeCalledOnce();
}

/**
* @test
*/
public function executeWithoutArgumentsExecutesCacheFlushWithoutArguments(): void
{
$options = [
'scriptFileName' => 'vendor/bin/typo3cms'
];
$this->task->execute($this->node, $this->application, $this->deployment, $options);
$this->assertCommandExecuted("/php 'vendor\/bin\/typo3cms' 'cache:flush'$/");
}

/**
* @test
*/
public function executeWithEmptyArgumentsExecutesCacheFlushWithoutArguments(): void
{
$options = [
'scriptFileName' => 'vendor/bin/typo3cms',
'arguments' => []
];
$this->task->execute($this->node, $this->application, $this->deployment, $options);
$this->assertCommandExecuted("/php 'vendor\/bin\/typo3cms' 'cache:flush'$/");
}

/**
* @test
*/
public function executeWithFilesOnlyArgumentExecutesCacheFlushWithFilesOnlyArgument(): void
{
$options = [
'scriptFileName' => 'vendor/bin/typo3cms',
'arguments' => ['--files-only']
];
$this->task->execute($this->node, $this->application, $this->deployment, $options);
$this->assertCommandExecuted("/php 'vendor\/bin\/typo3cms' 'cache:flush' '--files-only'$/");
}

/**
* @test
*/
public function executeWithMultipleArgumentExecutesCacheFlushWithArguments(): void
{
$options = [
'scriptFileName' => 'vendor/bin/typo3cms',
'arguments' => ['--files-only', '--force']
];
$this->task->execute($this->node, $this->application, $this->deployment, $options);
$this->assertCommandExecuted("/php 'vendor\/bin\/typo3cms' 'cache:flush' '--files-only' '--force'$/");
}
}

0 comments on commit a6aab1f

Please sign in to comment.