Release of RC1 #7

Merged
abelhooge merged 34 commits from 3-features into master 2020-06-07 13:54:20 +00:00
12 changed files with 1014 additions and 43 deletions
Showing only changes of commit 3499eed388 - Show all commits

11
.drone.yml Normal file
View File

@ -0,0 +1,11 @@
kind: pipeline
type: docker
name: test
steps:
- name: composer
image: composer:latest
commands:
- composer install

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ composer.lock
.idea/
log/
vendor/
build/
test/temp/

View File

@ -6,6 +6,7 @@ RUN apt-get update &&\
# Install Redis
RUN pecl install redis-5.1.1 && docker-php-ext-enable redis
RUN pecl install xdebug && docker-php-ext-enable xdebug
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

View File

@ -19,7 +19,7 @@
"ext-redis": "*"
},
"require-dev": {
"fuzeworks/tracycomponent": "~1.2.0",
"phpunit/phpunit": "^9",
"fuzeworks/mvcr": "~1.2.0"
},
"autoload": {

View File

@ -170,11 +170,6 @@ class Task
*/
protected $attributes = [];
/**
* @var Process
*/
protected $process;
/* -------- Some settings ------------ */
protected $retryOnFail = false;
@ -375,9 +370,9 @@ class Task
/**
* @todo Handle output from multiple attempts
* @param string $output
* @param string $errors
* @todo Handle output from multiple attempts
*/
public function setOutput(string $output, string $errors)
{
@ -386,9 +381,9 @@ class Task
}
/**
* @todo Handle output from multiple attempts
* @param string $output
* @param string $errors
* @todo Handle output from multiple attempts
*/
public function setPostOutput(string $output, string $errors)
{
@ -396,39 +391,6 @@ class Task
$this->postErrors = $errors;
}
/**
* Sets the initial process for this task.
*
* To be set by Executor
*
* @param Process $process
*/
public function setProcess(Process $process)
{
$this->process = $process;
}
/**
* Returns the initial process for this task
*
* @return Process|null
*/
public function getProcess(): ?Process
{
return $this->process;
}
public function removeProcess(): bool
{
if ($this->process instanceof Process)
{
$this->process = null;
return true;
}
return false;
}
/**
* Set whether this task should retry after a failure, and how many times
*
@ -495,7 +457,8 @@ class Task
* @param $value
* @return bool
*/
private function isSerializable ($value) {
private function isSerializable($value)
{
$return = true;
$arr = array($value);

View File

@ -132,9 +132,12 @@ interface TaskStorage
*
* $attempt refers to $task->getRetries(). If 0, it is the initial attempt. If > 0, it seeks a retry output.
*
* Returns null because that is a very valid response. Oftentimes output will need to be checked and its undesirable
* to always throw an exception for expected behaviour.
*
* @param Task $task
* @param int $attempt
* @return array
* @return array|null
*/
public function readTaskOutput(Task $task, int $attempt = 0): ?array;
@ -147,6 +150,9 @@ interface TaskStorage
*
* $attempt refers to $task->getRetries(). If 0, it is the initial attempt. If > 0, it seeks a retry output.
*
* Returns null because that is a very valid response. Oftentimes output will need to be checked and its undesirable
* to always throw an exception for expected behaviour.
*
* @param Task $task
* @param int $attempt
* @return array|null

View File

@ -0,0 +1,227 @@
<?php
/**
* FuzeWorks Async Library
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2020 TechFuze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2020, TechFuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 1.0.0
*
* @version Version 1.0.0
*/
namespace FuzeWorks\Async\TaskStorage;
use FuzeWorks\Async\Task;
use FuzeWorks\Async\TasksException;
use FuzeWorks\Async\TaskStorage;
/**
* Class DummyTaskStorage
*
* DummyTaskStorage is used when you don't want persistent task storage. Data is simply stored in an array and
* not saved anywhere. When the program terminates all contents are disposed off. Particularly useful for testing
* and development.
*
* @package FuzeWorks\Async\TaskStorage
*/
class DummyTaskStorage implements TaskStorage
{
/**
* Contains the entire task storage
*
* @var Task[]
*/
protected $tasks;
/**
* An array containing all task outputs
*
* @var array
*/
protected $taskOutput;
/**
* @inheritDoc
*/
public function __construct(array $parameters)
{
$this->tasks = array();
$this->taskOutput = array();
}
/**
* @inheritDoc
* @throws TasksException
*/
public function addTask(Task $task): bool
{
// Check if the already exists
$taskId = $task->getId();
foreach ($this->tasks as $t)
{
if ($t->getId() === $taskId)
throw new TasksException("Could not add Task to TaskStorage. Task '$taskId' already exists.");
}
$this->tasks[] = $task;
return true;
}
/**
* @inheritDoc
*/
public function readTasks(): array
{
return $this->tasks;
}
/**
* @inheritDoc
*/
public function refreshTasks()
{// Ignore
}
/**
* @inheritDoc
*/
public function getTaskById(string $identifier): Task
{
foreach ($this->tasks as $t)
if ($t->getId() === $identifier)
return $t;
throw new TasksException("Could not get task by id. Task not found.");
}
/**
* @inheritDoc
*/
public function modifyTask(Task $task): bool
{
$taskId = $task->getId();
for ($i=0;$i<count($this->tasks);$i++)
{
if ($this->tasks[$i]->getId() === $taskId)
{
$this->tasks[$i] = $task;
return true;
}
}
throw new TasksException("Could not modify task. Task '$taskId' doesn't exist.");
}
/**
* @inheritDoc
* @throws TasksException
*/
public function deleteTask(Task $task): bool
{
$taskId = $task->getId();
for ($i=0;$i<count($this->tasks);$i++)
{
if ($this->tasks[$i]->getId() === $taskId)
{
// Remove the task from the main storage
unset($this->tasks[$i]);
return true;
}
}
throw new TasksException("Could not delete task. Task '$taskId' doesn't exist.");
}
/**
* @inheritDoc
*/
public function writeTaskOutput(Task $task, string $output, string $errors, int $statusCode, int $attempt = 0): bool
{
if (isset($this->taskOutput[$task->getId()]['task'][$attempt]))
throw new TasksException("Could not write task output. Output already written.");
$this->taskOutput[$task->getId()]['task'][$attempt] = [
'output' => $output,
'errors' => $errors,
'statusCode' => $statusCode
];
return true;
}
/**
* @inheritDoc
*/
public function writePostOutput(Task $task, string $output, string $errors, int $statusCode, int $attempt = 0): bool
{
if (isset($this->taskOutput[$task->getId()]['post'][$attempt]))
throw new TasksException("Could not write task post output. Output already written.");
$this->taskOutput[$task->getId()]['post'][$attempt] = [
'output' => $output,
'errors' => $errors,
'statusCode' => $statusCode
];
return true;
}
/**
* @inheritDoc
*/
public function readTaskOutput(Task $task, int $attempt = 0): ?array
{
if (isset($this->taskOutput[$task->getId()]['task'][$attempt]))
return $this->taskOutput[$task->getId()]['task'][$attempt];
return null;
}
/**
* @inheritDoc
*/
public function readPostOutput(Task $task, int $attempt = 0): ?array
{
if (isset($this->taskOutput[$task->getId()]['post'][$attempt]))
return $this->taskOutput[$task->getId()]['post'][$attempt];
return null;
}
/**
* @inheritDoc
*/
public function reset(): bool
{
$this->tasks = [];
$this->taskOutput = [];
return true;
}
}

View File

@ -0,0 +1,427 @@
<?php
/**
* FuzeWorks Async Library
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2020 TechFuze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2020, TechFuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 1.0.0
*
* @version Version 1.0.0
*/
use FuzeWorks\Async\Task;
use FuzeWorks\Async\TasksException;
use FuzeWorks\Async\TaskStorage;
use FuzeWorks\Async\TaskStorage\DummyTaskStorage;
use PHPUnit\Framework\TestCase;
class TaskStorageTest extends TestCase
{
/**
* @var TaskStorage
*/
private $taskStorage;
public function setUp(): void
{
$this->taskStorage = new DummyTaskStorage([]);
}
public function testDummyTaskStorageClass()
{
$this->assertInstanceOf('FuzeWorks\Async\TaskStorage\DummyTaskStorage', $this->taskStorage);
}
/* ---------------------------------- Writing and reading tasks ----------------------- */
/**
* @depends testDummyTaskStorageClass
*/
public function testAddAndReadTasks()
{
// Prepare a dummy task
$dummyTask = new Task('testAddTask', 'none');
// Nothing is written yet so it should be empty
$this->assertEmpty($this->taskStorage->readTasks());
// Write task to storage and test properties of readTasks
$this->assertTrue($this->taskStorage->addTask($dummyTask));
$output = $this->taskStorage->readTasks();
$this->assertContains($dummyTask, $output);
// Test if the properties match
$this->assertEquals('testAddTask', $output[0]->getId());
$this->assertEquals('none', $output[0]->getHandlerClass());
}
/**
* @depends testAddAndReadTasks
*/
public function testAddExistingTask()
{
// Prepare a dummy task
$dummyTask = new Task('testAddExistingTask', 'none');
// First check that the task storage starts empty
$this->assertEmpty($this->taskStorage->readTasks());
// Then add the first task
$this->assertTrue($this->taskStorage->addTask($dummyTask));
// But then add another task, which should raise an exception
$this->expectException(TasksException::class);
$this->taskStorage->addTask($dummyTask);
}
/**
* @depends testAddAndReadTasks
*/
public function testGetTaskById()
{
// Prepare a dummy task
$dummyTask1 = new Task('testGetTaskById1', 'none');
$dummyTask2 = new Task('testGetTaskById2', 'none');
// First we add both tasks
$this->assertEmpty($this->taskStorage->readTasks());
$this->assertTrue($this->taskStorage->addTask($dummyTask1));
$this->assertTrue($this->taskStorage->addTask($dummyTask2));
// Afterwards, we attempt to get the separate tasks
$retrievedTask1 = $this->taskStorage->getTaskById('testGetTaskById1');
$retrievedTask2 = $this->taskStorage->getTaskById('testGetTaskById2');
$this->assertInstanceOf('FuzeWorks\Async\Task', $retrievedTask1);
$this->assertInstanceOf('FuzeWorks\Async\Task', $retrievedTask2);
// Assert they have the values we seek
$this->assertEquals('testGetTaskById1', $retrievedTask1->getId());
$this->assertEquals('testGetTaskById2', $retrievedTask2->getId());
// Test they are not the same
$this->assertNotSame($retrievedTask1, $retrievedTask2);
// And test they are the initial dummy tasks
$this->assertSame($dummyTask1, $retrievedTask1);
$this->assertSame($dummyTask2, $retrievedTask2);
}
/**
* @depends testGetTaskById
*/
public function testGetTaskByIdNotFound()
{
// Prepare a dummy task
$dummyTask = new Task('testGetTaskByIdNotFound', 'none');
// First we add the task
$this->assertEmpty($this->taskStorage->readTasks());
$this->assertTrue($this->taskStorage->addTask($dummyTask));
// Afterwards we check if we can get this task
$this->assertInstanceOf('FuzeWorks\Async\Task', $this->taskStorage->getTaskById('testGetTaskByIdNotFound'));
// And afterwards we check if an exception is raised if none exist
$this->expectException(TasksException::class);
$this->taskStorage->getTaskById('DoesNotExist');
}
/**
* @depends testGetTaskById
*/
public function testModifyTask()
{
// Prepare a dummy task
$dummyTask = new Task('testModifyTask', 'none');
$dummyTask->setStatus(Task::RUNNING);
// First we add the task
$this->assertEmpty($this->taskStorage->readTasks());
$this->assertTrue($this->taskStorage->addTask($dummyTask));
// Afterwards we check if this task has the known details
$this->assertEquals(Task::RUNNING, $this->taskStorage->getTaskById('testModifyTask')->getStatus());
// Then we change the task
$dummyTask->setStatus(Task::FAILED);
$this->assertTrue($this->taskStorage->modifyTask($dummyTask));
// And check if the details have been changed
$this->assertEquals(Task::FAILED, $this->taskStorage->getTaskById('testModifyTask')->getStatus());
}
/**
* @depends testModifyTask
*/
public function testModifyTaskNotFound()
{
// Prepare a dummy task
$dummyTask = new Task('testModifyTaskNotFound', 'none');
// Attempt to change this task, which does not exist.
$this->expectException(TasksException::class);
$this->taskStorage->modifyTask($dummyTask);
}
/**
* @depends testGetTaskById
*/
public function testDeleteTask()
{
// Prepare a dummy task
$dummyTask = new Task('testDeleteTask', 'none');
// Add the task to the storage
$this->assertEmpty($this->taskStorage->readTasks());
$this->assertTrue($this->taskStorage->addTask($dummyTask));
// Test that it exists
$this->assertSame($dummyTask, $this->taskStorage->getTaskById('testDeleteTask'));
// Then remove the task
$this->assertTrue($this->taskStorage->deleteTask($dummyTask));
// And test that it can't be found
$this->expectException(TasksException::class);
$this->taskStorage->getTaskById('testDeleteTask');
}
/**
* @depends testDeleteTask
*/
public function testDeleteTaskNotFound()
{
// Prepare a dummy task
$dummyTask = new Task('testDeleteTaskNotFound', 'none');
// Attempt to delete this task, which does not exist.
$this->expectException(TasksException::class);
$this->taskStorage->deleteTask($dummyTask);
}
/* ---------------------------------- Writing and reading task output ----------------- */
/**
* @depends testDummyTaskStorageClass
*/
public function testWriteAndReadTaskOutput()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteTaskOutput', 'none');
// First write the task output
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output', 'errors', 0, 0));
// Then try to read the output
$output = $this->taskStorage->readTaskOutput($dummyTask, 0);
$this->assertEquals('output', $output['output']);
$this->assertEquals('errors', $output['errors']);
$this->assertEquals(0, $output['statusCode']);
}
/**
* @depends testWriteAndReadTaskOutput
*/
public function testWriteAndReadTaskOutputAttempts()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskOutputAttempts', 'none');
// Write the different outputs. Done in a weird order to make sure the default is inserted not first or last
// to make sure the default is not selected by accident by the TaskStorage
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output2', 'errors2', 102, 2));
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output0', 'errors0', 100, 0));
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output1', 'errors1', 101, 1));
// Attempt to load the first output
$output0 = $this->taskStorage->readTaskOutput($dummyTask, 0);
$this->assertEquals('output0', $output0['output']);
$this->assertEquals('errors0', $output0['errors']);
$this->assertEquals(100, $output0['statusCode']);
// Attempt to load the second output
$output1 = $this->taskStorage->readTaskOutput($dummyTask, 1);
$this->assertEquals('output1', $output1['output']);
$this->assertEquals('errors1', $output1['errors']);
$this->assertEquals(101, $output1['statusCode']);
// Attempt to load the third output
$output2 = $this->taskStorage->readTaskOutput($dummyTask, 2);
$this->assertEquals('output2', $output2['output']);
$this->assertEquals('errors2', $output2['errors']);
$this->assertEquals(102, $output2['statusCode']);
// Attempt to load the default output
$output = $this->taskStorage->readTaskOutput($dummyTask);
$this->assertEquals('output0', $output['output']);
$this->assertEquals('errors0', $output['errors']);
$this->assertEquals(100, $output['statusCode']);
}
/**
* @depends testWriteAndReadTaskOutput
*/
public function testWriteAndReadTaskOutputAlreadyExists()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskOutputAlreadyExists', 'none');
// Write a first time
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output', 'errors', 100, 0));
// And write it a second time
$this->expectException(TasksException::class);
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output', 'errors', 100, 0));
}
/**
* @depends testWriteAndReadTaskOutput
*/
public function testWriteAndReadTaskOutputNotExist()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskOutputNotExist', 'none');
$this->assertNull($this->taskStorage->readTaskOutput($dummyTask));
}
/* ---------------------------------- Writing and reading task post output ------------ */
/**
* @depends testDummyTaskStorageClass
*/
public function testWriteAndReadTaskPostOutput()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskPostOutput', 'none');
// First write the task output
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'postOutput', 'errors', 0, 0));
// Then try to read the output
$output = $this->taskStorage->readPostOutput($dummyTask, 0);
$this->assertEquals('postOutput', $output['output']);
$this->assertEquals('errors', $output['errors']);
$this->assertEquals(0, $output['statusCode']);
}
/**
* @depends testWriteAndReadTaskPostOutput
*/
public function testWriteAndReadTaskPostOutputAttempts()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskPostOutputAttempts', 'none');
// Write the different outputs. Done in a weird order to make sure the default is inserted not first or last
// to make sure the default is not selected by accident by the TaskStorage
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'output2', 'errors2', 102, 2));
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'output0', 'errors0', 100, 0));
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'output1', 'errors1', 101, 1));
// Attempt to load the first output
$output0 = $this->taskStorage->readPostOutput($dummyTask, 0);
$this->assertEquals('output0', $output0['output']);
$this->assertEquals('errors0', $output0['errors']);
$this->assertEquals(100, $output0['statusCode']);
// Attempt to load the second output
$output1 = $this->taskStorage->readPostOutput($dummyTask, 1);
$this->assertEquals('output1', $output1['output']);
$this->assertEquals('errors1', $output1['errors']);
$this->assertEquals(101, $output1['statusCode']);
// Attempt to load the third output
$output2 = $this->taskStorage->readPostOutput($dummyTask, 2);
$this->assertEquals('output2', $output2['output']);
$this->assertEquals('errors2', $output2['errors']);
$this->assertEquals(102, $output2['statusCode']);
// Attempt to load the default output
$output = $this->taskStorage->readPostOutput($dummyTask);
$this->assertEquals('output0', $output['output']);
$this->assertEquals('errors0', $output['errors']);
$this->assertEquals(100, $output['statusCode']);
}
/**
* @depends testWriteAndReadTaskPostOutput
*/
public function testWriteAndReadTaskPostOutputAlreadyExists()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskPostOutputAlreadyExists', 'none');
// Write a first time
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'output', 'errors', 100, 0));
// And write it a second time
$this->expectException(TasksException::class);
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'output', 'errors', 100, 0));
}
/**
* @depends testWriteAndReadTaskPostOutput
*/
public function testWriteAndReadTaskPostOutputNotExist()
{
// Prepare a dummy task
$dummyTask = new Task('testWriteAndReadTaskPostOutputNotExist', 'none');
$this->assertNull($this->taskStorage->readPostOutput($dummyTask));
}
/* ---------------------------------- Data persistence and resets --------- ------------ */
/**
* @depends testAddAndReadTasks
* @depends testWriteAndReadTaskOutput
* @depends testWriteAndReadTaskPostOutput
*/
public function testReset()
{
// Prepare a dummy task
$dummyTask = new Task('testReset', 'none');
// Add the task and some output
$this->assertTrue($this->taskStorage->addTask($dummyTask));
$this->assertTrue($this->taskStorage->writeTaskOutput($dummyTask, 'output', 'errors', 100));
$this->assertTrue($this->taskStorage->writePostOutput($dummyTask, 'postOutput', 'errors', 100));
// Then reset the data
$this->assertTrue($this->taskStorage->reset());
// And test if the data is actually gone
$this->assertNull($this->taskStorage->readTaskOutput($dummyTask));
$this->assertNull($this->taskStorage->readPostOutput($dummyTask));
$this->expectException(TasksException::class);
$this->taskStorage->getTaskById('testReset');
}
}

244
test/base/TaskTest.php Normal file
View File

@ -0,0 +1,244 @@
<?php
/**
* FuzeWorks Async Library
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2020 TechFuze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2020, TechFuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 1.0.0
*
* @version Version 1.0.0
*/
use FuzeWorks\Async\Constraint;
use FuzeWorks\Async\Task;
use FuzeWorks\Async\TasksException;
use PHPUnit\Framework\TestCase;
class TaskTest extends TestCase
{
public function testClass()
{
// Create dummy task
$dummyTask = new Task('testClass', 'none');
// And check the class. A pretty useless but standard test
$this->assertInstanceOf('FuzeWorks\Async\Task', $dummyTask);
}
/* ---------------------------------- Basic variables tests --------------------------- */
/**
* @depends testClass
*/
public function testBaseVariables()
{
// Create dummy task
$dummyTask = new Task('testBaseVariables', 'someThing', true);
// test the values
$this->assertEquals('testBaseVariables', $dummyTask->getId());
$this->assertEquals('someThing', $dummyTask->getHandlerClass());
$this->assertTrue($dummyTask->getUsePostHandler());
}
/**
* @depends testBaseVariables
*/
public function testArguments()
{
// Create task without arguments
$dummyTask1 = new Task('testArguments1', 'none', true);
$this->assertEmpty($dummyTask1->getArguments());
// Now create a task with some arguments
$dummyTask2 = new Task('testArguments2', 'none', true, 'some', 'arguments');
$this->assertEquals(['some', 'arguments'], $dummyTask2->getArguments());
}
/**
* @depends testBaseVariables
*/
public function testPostHandler()
{
// Create dummy tasks
$dummyTask1 = new Task('testPostHandler1', 'someThing', true);
$dummyTask2 = new Task('testPostHandler2', 'someThing', false);
$this->assertTrue($dummyTask1->getUsePostHandler());
$this->assertFalse($dummyTask2->getUsePostHandler());
}
/**
* @depends testBaseVariables
*/
public function testConstraints()
{
// First create a mock constraint
$stub = $this->createMock(Constraint::class);
// Then add it to the task
$dummyTask = new Task('testConstraints', 'someThing', false);
$dummyTask->addConstraint($stub);
// Assert it exists
$this->assertEquals([$stub], $dummyTask->getConstraints());
}
/**
* @depends testBaseVariables
*/
public function testStatusCodes()
{
// Create dummy task
$dummyTask = new Task('testStatusCodes', 'someThing', true);
for ($i = 1; $i <= 9; $i++) {
$dummyTask->setStatus($i);
$this->assertEquals($i, $dummyTask->getStatus());
}
}
/**
* @depends testBaseVariables
*/
public function testDelayTime()
{
// Create dummy task
$dummyTask = new Task('testDelayTime', 'someThing', true);
$this->assertEquals(0, $dummyTask->getDelayTime());
$dummyTask->setDelayTime(1000);
$this->assertEquals(1000, $dummyTask->getDelayTime());
}
/**
* @depends testBaseVariables
*/
public function testAttributes()
{
// Create dummy task
$dummyTask = new Task('testAttributes', 'someThing', true);
// First test a non-existing attribute
$this->assertNull($dummyTask->attribute('testKey'));
// Now add it and test if it is there
$dummyTask->addAttribute('testKey', 'SomeContent');
$this->assertEquals('SomeContent', $dummyTask->attribute('testKey'));
}
/**
* @depends testBaseVariables
*/
public function testOutputsAndErrors()
{
// Create dummy task
$dummyTask = new Task('testOutputsAndErrors', 'someThing', true);
// Check if non are filled
$this->assertNull($dummyTask->getOutput());
$this->assertNull($dummyTask->getPostOutput());
$this->assertNull($dummyTask->getErrors());
$this->assertNull($dummyTask->getPostErrors());
// Then write some data to the task
$dummyTask->setOutput('SomeOutput', 'SomeErrors');
$dummyTask->setPostOutput('SomePostOutput', 'SomePostErrors');
// And check again
$this->assertEquals('SomeOutput', $dummyTask->getOutput());
$this->assertEquals('SomePostOutput', $dummyTask->getPostOutput());
$this->assertEquals('SomeErrors', $dummyTask->getErrors());
$this->assertEquals('SomePostErrors', $dummyTask->getPostErrors());
}
/**
* @depends testBaseVariables
*/
public function testRetrySettings()
{
// Create dummy task
$dummyTask = new Task('testRetrySettings', 'someThing', true);
// Test starting position
$this->assertEquals([
'retryOnFail' => false,
'maxRetries' => 2,
'retryPFailures' => true,
'retryRFailures' => true,
'retryPostFailures' => true
], $dummyTask->getRetrySettings());
// Then change the settings
$dummyTask->setRetrySettings(true, 30, false, false, false);
// And test the new positions
$this->assertEquals([
'retryOnFail' => true,
'maxRetries' => 30,
'retryPFailures' => false,
'retryRFailures' => false,
'retryPostFailures' => false
], $dummyTask->getRetrySettings());
}
/**
* @depends testBaseVariables
*/
public function testRetries()
{
// Create dummy task
$dummyTask = new Task('testRetries', 'someThing', true);
// First test the starting position
$this->assertEquals(0, $dummyTask->getRetries());
// Then add one and test
$dummyTask->addRetry();
$this->assertEquals(1, $dummyTask->getRetries());
// Then reset it and test
$dummyTask->resetRetries();
$this->assertEquals(0, $dummyTask->getRetries());
}
public function testGetStatusType()
{
$this->assertEquals('Task::PENDING', Task::getStatusType(Task::PENDING));
$this->assertEquals('Task::RUNNING', Task::getStatusType(Task::RUNNING));
$this->assertEquals('Task::FAILED', Task::getStatusType(Task::FAILED));
$this->assertEquals('Task::PFAILED', Task::getStatusType(Task::PFAILED));
$this->assertEquals('Task::SUCCESS', Task::getStatusType(Task::SUCCESS));
$this->assertEquals('Task::POST', Task::getStatusType(Task::POST));
$this->assertEquals('Task::COMPLETED', Task::getStatusType(Task::COMPLETED));
$this->assertEquals('Task::DELAYED', Task::getStatusType(Task::DELAYED));
$this->assertEquals('Task::CANCELLED', Task::getStatusType(Task::CANCELLED));
$this->assertFalse(Task::getStatusType(10));
}
}

61
test/bootstrap.php Normal file
View File

@ -0,0 +1,61 @@
<?php
/**
* FuzeWorks Async Library
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2020 TechFuze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2020, TechFuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 1.0.0
*
* @version Version 1.0.0
*/
require_once(dirname(__DIR__) . '/vendor/autoload.php');
use FuzeWorks\Logger;
use FuzeWorks\Priority;
// Open configurator
$configurator = new FuzeWorks\Configurator();
// Set up basic settings
$configurator->setTimeZone('Europe/Amsterdam');
$configurator->setTempDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'temp');
$configurator->setLogDirectory(dirname(__FILE__). DIRECTORY_SEPARATOR . 'temp');
$configurator->addComponent(new \FuzeWorks\MVCRComponent());
// Add Async library
$configurator->deferComponentClassMethod('libraries', 'addLibraryClass', null, 'async', '\FuzeWorks\Async\Tasks');
// Debug
$configurator->addDirectory(dirname(__FILE__), 'controllers', Priority::HIGH);
// Create container
$container = $configurator->createContainer();
Logger::enableScreenLog();
return $container;

29
test/phpunit.xml Normal file
View File

@ -0,0 +1,29 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
bootstrap="bootstrap.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
colors="false">
<testsuites>
<testsuite name="Base Functionality">
<directory>./base/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">../</directory>
<exclude>
<directory suffix=".php">../vendor/</directory>
<directory suffix=".php">../test/</directory>
<directory suffix=".php">../src/Layout/</directory>
<directory suffix=".php">../src/Config/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

0
test/temp/placeholder Normal file
View File