Skip to content

Commit

Permalink
Merge 9948be0 into 69c4307
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Jun 1, 2020
2 parents 69c4307 + 9948be0 commit 7a9b4b9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
4 changes: 4 additions & 0 deletions src/Domain/Service/TaskFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class TaskFactory implements ContainerAwareInterface
*/
public function createTaskInstance(string $taskName)
{
if (!$this->container->has($taskName)) {
$this->container->set($taskName, new $taskName());
}

$task = $this->container->get($taskName);

if (!$task instanceof Task) {
Expand Down
51 changes: 17 additions & 34 deletions tests/Unit/Domain/Service/TaskFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,45 @@
*/

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\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)));
$this->assertInstanceOf(CustomTask::class, $this->subject->createTaskInstance(CustomTask::class));
}

/**
Expand All @@ -81,8 +58,14 @@ 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
{
public function execute(Node $node, Application $application, Deployment $deployment, array $options = [])
{
}
}

0 comments on commit 7a9b4b9

Please sign in to comment.