Skip to content

Commit

Permalink
[TASK] Streamline FlushCachesTask and it's tests with v2
Browse files Browse the repository at this point in the history
  • Loading branch information
t3easy committed Jun 26, 2020
1 parent cae2dd6 commit ca0f280
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 26 deletions.
13 changes: 7 additions & 6 deletions src/Task/TYPO3/CMS/FlushCachesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* 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;
Expand Down Expand Up @@ -47,18 +48,18 @@ protected function getSuitableCliArguments(Node $node, CMS $application, Deploym
{
switch ($this->getAvailableCliPackage($node, $application, $deployment, $options)) {
case 'typo3_console':
if ($options['flushCacheOptions'] !== []) {
return [$this->getConsoleScriptFileName($node, $application, $deployment, $options), 'cache:flush', implode(' ', $options['flushCacheOptions'])];
}
return [$this->getConsoleScriptFileName($node, $application, $deployment, $options), 'cache:flush'];
return array_merge([$this->getConsoleScriptFileName($node, $application, $deployment, $options), 'cache:flush'], $options['arguments']);
default:
return [];
}
}

protected function resolveOptions(OptionsResolver $resolver)
{
$resolver->setDefault('flushCacheOptions', ['--files-only']);
$resolver->setAllowedTypes('flushCacheOptions', ['array']);
$resolver->setDefault('arguments', [])
->setAllowedTypes('arguments', ['array', 'string'])
->setNormalizer('arguments', function (Options $options, $value) {
return (array)$value;
});
}
}
87 changes: 67 additions & 20 deletions tests/Unit/Task/TYPO3/CMS/FlushCachesTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,114 @@
*/

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, $this->application, $this->deployment, []);
$this->task->execute($this->node, $invalidApplication, $this->deployment, []);
}

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

/**
* @test
*/
public function executeFlushCacheCommandWithWrongOptionsType(): void
public function executeWithoutArgumentsExecutesCacheFlushWithoutArguments(): void
{
$this->expectException(InvalidConfigurationException::class);
$application = new CMS();
$options = ['scriptFileName' => 'typo3cms', 'flushCacheOptions' => 1];
$this->task->execute($this->node, $application, $this->deployment, $options);
$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 executeFlushCacheCommandSuccessfully(): void
public function executeWithEmptyArgumentsExecutesCacheFlushWithoutArguments(): void
{
$application = new CMS();
$options = ['scriptFileName' => 'typo3cms'];
$this->task->execute($this->node, $application, $this->deployment, $options);
$this->assertCommandExecuted('/php \'typo3cms\' \'cache:flush\' \'--files-only\'$/');
$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 executeFlushCacheWithCustomOptionsCommandSuccessfully(): void
public function executeWithFilesOnlyArgumentExecutesCacheFlushWithFilesOnlyArgument(): void
{
$application = new CMS();
$options = ['scriptFileName' => 'typo3cms', 'flushCacheOptions' => ['--foo-bar-baz', '--foo-qux']];
$this->task->execute($this->node, $application, $this->deployment, $options);
$this->assertCommandExecuted('/php \'typo3cms\' \'cache:flush\' \'--foo-bar-baz --foo-qux\'$/');
$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'$/");
}

protected function createTask()
/**
* @test
*/
public function executeWithMultipleArgumentExecutesCacheFlushWithArguments(): void
{
return new FlushCachesTask();
$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 ca0f280

Please sign in to comment.