Skip to content

Commit

Permalink
[BUGFIX] Allow creation of custom tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Jun 5, 2020
1 parent 58e16fd commit 83a79e7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 37 deletions.
25 changes: 20 additions & 5 deletions src/Domain/Service/TaskFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types = 1);
declare(strict_types=1);

namespace TYPO3\Surf\Domain\Service;

Expand All @@ -23,18 +23,33 @@ class TaskFactory implements ContainerAwareInterface
use ContainerAwareTrait;

/**
* Create a task instance from the given task name
*
* @return ShellCommandServiceAwareInterface|Task
*/
public function createTaskInstance(string $taskName)
{
$task = $this->container->get($taskName);
$task = $this->createTask($taskName);

if (!$task instanceof Task) {
if ( ! $task instanceof Task) {
throw new SurfException(sprintf('The task %s is not a subclass of %s but of class %s', $taskName, Task::class, get_class($task)), 1451210811);
}

return $task;
}

/**
* @return ShellCommandServiceAwareInterface|Task
*/
private function createTask(string $taskName)
{
if ( ! $this->container->has($taskName)) {
$task = new $taskName();
if ($task instanceof ShellCommandServiceAwareInterface) {
$task->setShellCommandService(new ShellCommandService());
}
} else {
$task = $this->container->get($taskName);
}

return $task;
}
}
60 changes: 28 additions & 32 deletions tests/Unit/Domain/Service/TaskFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,51 @@
*/

use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\DependencyInjection\ContainerInterface;
use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
use TYPO3\Surf\Domain\Model\Node;
use TYPO3\Surf\Domain\Model\Task;
use TYPO3\Surf\Domain\Service\ShellCommandService;
use TYPO3\Surf\Domain\Service\ShellCommandServiceAwareInterface;
use TYPO3\Surf\Domain\Service\ShellCommandServiceAwareTrait;
use TYPO3\Surf\Domain\Service\TaskFactory;
use TYPO3\Surf\Exception as SurfException;
use TYPO3\Surf\Task\CreateArchiveTask;
use TYPO3\Surf\Tests\Unit\KernelAwareTrait;

class TaskFactoryTest extends TestCase
{
use KernelAwareTrait;

/**
* @var TaskFactory
*/
protected $subject;

/**
* @var ContainerInterface|ObjectProphecy
*/
private $container;

protected function setUp()
protected function setUp(): void
{
$this->container = $this->prophesize(ContainerInterface::class);
$container = self::getKernel()->getContainer();
$this->subject = new TaskFactory();
$this->subject->setContainer($this->container->reveal());
$this->subject->setContainer($container);
}

/**
* @test
*/
public function createTaskInstance(): void
{
$task = new class extends Task {
public function execute(Node $node, Application $application, Deployment $deployment, array $options = []): void
{
}
};
$this->container->get(get_class($task))->willReturn($task);

$this->assertEquals($task, $this->subject->createTaskInstance(get_class($task)));
$this->assertInstanceOf(CreateArchiveTask::class, $this->subject->createTaskInstance(CreateArchiveTask::class));
}

/**
* @test
*/
public function createTaskInstanceImplementingShellCommandServiceAwareInterface(): void
public function createSyntheticServiceIfNotExists(): void
{
$task = new class extends Task implements ShellCommandServiceAwareInterface {
public function execute(Node $node, Application $application, Deployment $deployment, array $options = []): void
{
}

public function setShellCommandService(ShellCommandService $shellCommandService): void
{
}
};
$this->container->get(get_class($task))->willReturn($task);

$this->assertEquals($task, $this->subject->createTaskInstance(get_class($task)));
/** @var CustomTask $customTask */
$customTask = $this->subject->createTaskInstance(CustomTask::class);
$this->assertNotNull($customTask->getShell());
$this->assertInstanceOf(CustomTask::class, $customTask);
}

/**
Expand All @@ -81,8 +64,21 @@ public function createTaskInstanceThrowsExceptionClassIsNotOfCorrectSubclass():
{
$task = new class {
};
$this->container->get(get_class($task))->willReturn($task);
$this->expectException(SurfException::class);
$this->subject->createTaskInstance(get_class($task));
}
}

class CustomTask extends Task implements ShellCommandServiceAwareInterface
{
use ShellCommandServiceAwareTrait;

public function execute(Node $node, Application $application, Deployment $deployment, array $options = [])
{
}

public function getShell(): ShellCommandService
{
return $this->shell;
}
}

0 comments on commit 83a79e7

Please sign in to comment.