-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Add more unit tests for class TaskManager
- Create class TaskFactory. The new class is responsible for the real creation of the task. First step to depedency injection container
- Loading branch information
1 parent
94c758f
commit 2d1a319
Showing
4 changed files
with
179 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace TYPO3\Surf\Domain\Service; | ||
|
||
/* | ||
* 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 TYPO3\Surf\Domain\Model\Task; | ||
use TYPO3\Surf\Exception as SurfException; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class TaskFactory | ||
{ | ||
/** | ||
* Create a task instance from the given task name | ||
* | ||
* @return ShellCommandServiceAwareInterface|Task | ||
*/ | ||
public function createTaskInstance(string $taskName) | ||
{ | ||
$taskClassName = $this->mapTaskNameToTaskClass($taskName); | ||
$task = new $taskClassName(); | ||
|
||
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); | ||
} | ||
|
||
if ($task instanceof ShellCommandServiceAwareInterface) { | ||
$task->setShellCommandService(new ShellCommandService()); | ||
} | ||
return $task; | ||
} | ||
|
||
private function mapTaskNameToTaskClass(string $taskName): string | ||
{ | ||
if (!class_exists($taskName)) { | ||
throw new SurfException(sprintf('No task found for identifier "%s". Make sure this is a valid class name or a defined task with valid base class name!', $taskName), 1451210811); | ||
} | ||
|
||
return $taskName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace TYPO3\Surf\Tests\Unit\Domain\Service; | ||
|
||
/* | ||
* 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 PHPUnit\Framework\TestCase; | ||
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; | ||
|
||
class TaskFactoryTest extends TestCase | ||
{ | ||
/** | ||
* @var TaskFactory | ||
*/ | ||
protected $subject; | ||
|
||
protected function setUp() | ||
{ | ||
$this->subject = new TaskFactory(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function createTaskInstance(): void | ||
{ | ||
$task = new class extends Task { | ||
public function execute(Node $node, Application $application, Deployment $deployment, array $options = []) | ||
{ | ||
} | ||
}; | ||
|
||
$this->assertEquals($task, $this->subject->createTaskInstance(get_class($task))); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function createTaskInstanceImplementingShellCommandServiceAwareInterface(): void | ||
{ | ||
$task = new class extends Task implements ShellCommandServiceAwareInterface { | ||
public function execute(Node $node, Application $application, Deployment $deployment, array $options = []) | ||
{ | ||
} | ||
|
||
public function setShellCommandService(ShellCommandService $shellCommandService) | ||
{ | ||
} | ||
}; | ||
|
||
$this->assertEquals($task, $this->subject->createTaskInstance(get_class($task))); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function createTaskInstanceThrowsExceptionClassDoesNotExist(): void | ||
{ | ||
$this->expectException(SurfException::class); | ||
$this->subject->createTaskInstance('SomeFooBarBaz'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function createTaskInstanceThrowsExceptionClassIsNotOfCorrectSubclass(): void | ||
{ | ||
$task = new class { | ||
}; | ||
|
||
$this->expectException(SurfException::class); | ||
$this->subject->createTaskInstance(get_class($task)); | ||
} | ||
} |
Oops, something went wrong.