Skip to content

Commit

Permalink
TASK: Inject Logger for custom task
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Oct 10, 2023
1 parent 5832255 commit 886b752
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Domain/Service/TaskFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace TYPO3\Surf\Domain\Service;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use TYPO3\Surf\Domain\Model\Task;
Expand All @@ -35,6 +37,9 @@ private function createTask(string $taskName): Task
if ($task instanceof ShellCommandServiceAwareInterface) {
$task->setShellCommandService(new ShellCommandService());
}
if($task instanceof LoggerAwareInterface) {
$task->setLogger($this->container->get(LoggerInterface::class));

Check failure on line 41 in src/Domain/Service/TaskFactory.php

View workflow job for this annotation

GitHub Actions / phpstan

Cannot call method get() on Symfony\Component\DependencyInjection\ContainerInterface|null.

Check failure on line 41 in src/Domain/Service/TaskFactory.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $logger of method Psr\Log\LoggerAwareInterface::setLogger() expects Psr\Log\LoggerInterface, object|null given.
}
} else {
$task = $this->container->get($taskName);

Check failure on line 44 in src/Domain/Service/TaskFactory.php

View workflow job for this annotation

GitHub Actions / phpstan

Cannot call method get() on Symfony\Component\DependencyInjection\ContainerInterface|null.
}
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/Domain/Service/CustomTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace TYPO3\Surf\Tests\Unit\Domain\Service;

use Psr\Log\LoggerInterface;
use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
use TYPO3\Surf\Domain\Model\Node;
Expand All @@ -31,4 +32,9 @@ public function getShell(): ShellCommandService
{
return $this->shell;
}

public function getLogger(): LoggerInterface
{
return $this->logger;
}
}
5 changes: 4 additions & 1 deletion tests/Unit/Domain/Service/TaskFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace TYPO3\Surf\Tests\Unit\Domain\Service;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use TYPO3\Surf\Domain\Service\ShellCommandService;
use TYPO3\Surf\Domain\Service\TaskFactory;
use TYPO3\Surf\Task\CreateArchiveTask;
use TYPO3\Surf\Tests\Unit\KernelAwareTrait;
Expand Down Expand Up @@ -52,7 +54,8 @@ public function createSyntheticServiceIfNotExists(): void
/** @var CustomTask $customTask */
$customTask = $this->subject->createTaskInstance(CustomTask::class);

self::assertNotNull($customTask->getShell());
self::assertInstanceOf(ShellCommandService::class, $customTask->getShell());
self::assertInstanceOf(LoggerInterface::class, $customTask->getLogger());
self::assertInstanceOf(CustomTask::class, $customTask);
}

Expand Down

0 comments on commit 886b752

Please sign in to comment.