class TaskRunnerTest in Webform Scheduled Tasks 8.2
Same name in this branch
- 8.2 tests/src/Unit/TaskRunnerTest.php \Drupal\Tests\webform_scheduled_tasks\Unit\TaskRunnerTest
- 8.2 tests/src/Kernel/TaskRunnerTest.php \Drupal\Tests\webform_scheduled_tasks\Kernel\TaskRunnerTest
@coversDefaultClass \Drupal\webform_scheduled_tasks\TaskRunner @group webform_scheduled_tasks
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\webform_scheduled_tasks\Unit\TaskRunnerTest
Expanded class hierarchy of TaskRunnerTest
File
- tests/
src/ Unit/ TaskRunnerTest.php, line 21
Namespace
Drupal\Tests\webform_scheduled_tasks\UnitView source
class TaskRunnerTest extends UnitTestCase {
/**
* The task runner.
*
* @var \Drupal\webform_scheduled_tasks\TaskRunnerInterface
*/
protected $taskRunner;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->taskRunner = new TaskRunner($this
->prophesize(EntityTypeManagerInterface::class)
->reveal(), $this
->prophesize(TimeInterface::class)
->reveal());
}
/**
* @covers ::executeTasks
*/
public function testSuccessfulTaskExecuted() {
$scheduled_task = $this
->createTestScheduledTask();
$scheduled_task
->registerSuccessfulTask()
->shouldBeCalled();
$scheduled_task
->incrementTaskRunDateByInterval()
->shouldBeCalled();
$this->taskRunner
->executeTasks([
$scheduled_task
->reveal(),
]);
}
/**
* @covers ::executeTasks
* @dataProvider haltScheduledTaskExceptionThrownTestCases
*/
public function testHaltScheduledTaskExceptionThrown($exception) {
$task = $this
->prophesize(TaskPluginInterface::class);
$task
->executeTask(Argument::any(), Argument::any())
->willThrow($exception);
$scheduled_task = $this
->createTestScheduledTask($task);
// When a halt schedule exception is thrown, the task will be halted and a
// fail will be registered.
$scheduled_task
->registerFailedTask($exception)
->shouldBeCalled();
$scheduled_task
->halt('An error was encountered when running the task: Failed to do something.')
->shouldBeCalled();
$this->taskRunner
->executeTasks([
$scheduled_task
->reveal(),
]);
}
/**
* Test cases for ::testHaltScheduledTaskExceptionThrown.
*/
public function haltScheduledTaskExceptionThrownTestCases() {
return [
'Retry exception' => [
new HaltScheduledTaskException('Failed to do something.'),
],
'Normal exception' => [
new \Exception('Failed to do something.'),
],
];
}
/**
* @covers ::executeTasks
*/
public function testRetryScheduledTaskExceptionThrown() {
$task = $this
->prophesize(TaskPluginInterface::class);
$task
->executeTask(Argument::any(), Argument::any())
->willThrow(new RetryScheduledTaskException('Failed to do something.'));
$scheduled_task = $this
->createTestScheduledTask($task);
// When a retry exception is thrown, a fail will be registered but the task
// date will be incremented and the task will not be halted.
$scheduled_task
->registerFailedTask(Argument::any())
->shouldBeCalled();
$scheduled_task
->incrementTaskRunDateByInterval()
->shouldBeCalled();
$this->taskRunner
->executeTasks([
$scheduled_task
->reveal(),
]);
}
/**
* @covers ::executeTasks
*/
public function testMultipleTasksRun() {
$failing_task = $this
->prophesize(TaskPluginInterface::class);
$failing_task
->executeTask(Argument::any(), Argument::any())
->willThrow(new \Exception('Failed!'));
$failing_scheduled_task = $this
->createTestScheduledTask($failing_task);
$failing_scheduled_task
->registerFailedTask(Argument::any())
->shouldBeCalled();
$failing_scheduled_task
->halt(Argument::any())
->shouldBeCalled();
$passing_scheduled_task = $this
->createTestScheduledTask();
$passing_scheduled_task
->registerSuccessfulTask()
->shouldBeCalled();
$passing_scheduled_task
->incrementTaskRunDateByInterval()
->shouldBeCalled();
// One failed task should not impact the running of another.
$this->taskRunner
->executeTasks([
$failing_scheduled_task
->reveal(),
$passing_scheduled_task
->reveal(),
]);
}
/**
* Create a mock scheduled task to pass into the task runner.
*
* @param \Drupal\search_api\Task\TaskInterface|null $task
* (Optional) A mock task or NULL if a default one should be setup.
* @param \Drupal\webform_scheduled_tasks\Plugin\WebformScheduledTasks\ResultSetPluginInterface|null $result_set
* (Optional) A mock result set or NULL if a default one should be setup.
*
* @return \Drupal\webform_scheduled_tasks\Entity\WebformScheduledTaskInterface
* A scheduled task entity.
*/
protected function createTestScheduledTask($task = NULL, $result_set = NULL) {
$iterator = new \ArrayIterator([]);
$webform = $this
->prophesize(WebformInterface::class);
if ($result_set === NULL) {
$result_set = $this
->prophesize(ResultSetPluginInterface::class);
$result_set
->getResultSet()
->willReturn($iterator);
}
if ($task === NULL) {
$task = $this
->prophesize(TaskPluginInterface::class);
$task
->executeTask($iterator)
->shouldBeCalled();
}
$scheduled_task = $this
->prophesize(WebformScheduledTaskInterface::class);
$scheduled_task
->getTaskPlugin()
->willReturn($task
->reveal());
$scheduled_task
->getResultSetPlugin()
->willReturn($result_set
->reveal());
$scheduled_task
->getWebform()
->willReturn($webform
->reveal());
return $scheduled_task;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
TaskRunnerTest:: |
protected | property | The task runner. | |
TaskRunnerTest:: |
protected | function | Create a mock scheduled task to pass into the task runner. | |
TaskRunnerTest:: |
public | function | Test cases for ::testHaltScheduledTaskExceptionThrown. | |
TaskRunnerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
TaskRunnerTest:: |
public | function | @covers ::executeTasks @dataProvider haltScheduledTaskExceptionThrownTestCases | |
TaskRunnerTest:: |
public | function | @covers ::executeTasks | |
TaskRunnerTest:: |
public | function | @covers ::executeTasks | |
TaskRunnerTest:: |
public | function | @covers ::executeTasks | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |