270 lines
8.8 KiB
PHP
270 lines
8.8 KiB
PHP
|
<?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\Handler;
|
||
|
use FuzeWorks\Async\Task;
|
||
|
use FuzeWorks\Async\TasksException;
|
||
|
use Mock\Handlers\EmptyHandler;
|
||
|
use PHPUnit\Framework\TestCase;
|
||
|
|
||
|
class TaskTest extends TestCase
|
||
|
{
|
||
|
|
||
|
public function testClass()
|
||
|
{
|
||
|
// Create dummy task
|
||
|
$dummyTask = new Task('testClass', new EmptyHandler());
|
||
|
|
||
|
// 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', new EmptyHandler(), true);
|
||
|
|
||
|
// test the values
|
||
|
$this->assertEquals('testBaseVariables', $dummyTask->getId());
|
||
|
$this->assertInstanceOf(EmptyHandler::class, $dummyTask->getHandler());
|
||
|
$this->assertTrue($dummyTask->getUsePostHandler());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testBaseVariables
|
||
|
*/
|
||
|
public function testArguments()
|
||
|
{
|
||
|
// Create task without arguments
|
||
|
$dummyTask1 = new Task('testArguments1', new EmptyHandler(), true);
|
||
|
$this->assertEmpty($dummyTask1->getArguments());
|
||
|
|
||
|
// Now create a task with some arguments
|
||
|
$dummyTask2 = new Task('testArguments2', new EmptyHandler(), true, 'some', 'arguments');
|
||
|
$this->assertEquals(['some', 'arguments'], $dummyTask2->getArguments());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testBaseVariables
|
||
|
*/
|
||
|
public function testPostHandler()
|
||
|
{
|
||
|
// Create dummy tasks
|
||
|
$dummyTask1 = new Task('testPostHandler1', new EmptyHandler(), true);
|
||
|
$dummyTask2 = new Task('testPostHandler2', new EmptyHandler(), 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', new EmptyHandler(), false);
|
||
|
$dummyTask->addConstraint($stub);
|
||
|
|
||
|
// Assert it exists
|
||
|
$this->assertEquals([$stub], $dummyTask->getConstraints());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testBaseVariables
|
||
|
*/
|
||
|
public function testInitHandler()
|
||
|
{
|
||
|
$mockHandler = $this->createMock(Handler::class);
|
||
|
$mockHandler->expects($this->once())->method('init')
|
||
|
->with($this->callback(function($subject){return $subject instanceof Task;}));
|
||
|
|
||
|
// Then create a class
|
||
|
new Task('testInitHandler', $mockHandler);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testBaseVariables
|
||
|
*/
|
||
|
public function testStatusCodes()
|
||
|
{
|
||
|
// Create dummy task
|
||
|
$dummyTask = new Task('testStatusCodes', new EmptyHandler(), 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', new EmptyHandler(), 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', new EmptyHandler(), 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'));
|
||
|
|
||
|
// Remove attribute
|
||
|
$dummyTask->removeAttribute('testKey');
|
||
|
$this->assertNull($dummyTask->attribute('testKey'));
|
||
|
|
||
|
// Remove attribute not found
|
||
|
$this->expectException(TasksException::class);
|
||
|
$dummyTask->removeAttribute('NotExistant');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testBaseVariables
|
||
|
*/
|
||
|
public function testOutputsAndErrors()
|
||
|
{
|
||
|
// Create dummy task
|
||
|
$dummyTask = new Task('testOutputsAndErrors', new EmptyHandler(), 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', new EmptyHandler(), true);
|
||
|
|
||
|
// Test starting position
|
||
|
$this->assertEquals([
|
||
|
'retryOnFail' => false,
|
||
|
'maxRetries' => 2,
|
||
|
'retryPFailures' => true,
|
||
|
'retryRFailures' => true,
|
||
|
'retryPostFailures' => true,
|
||
|
'maxTime' => 30
|
||
|
], $dummyTask->getSettings());
|
||
|
|
||
|
// Then change the settings
|
||
|
$dummyTask->setSettings(true, 30, 60, false, false, false);
|
||
|
|
||
|
// And test the new positions
|
||
|
$this->assertEquals([
|
||
|
'retryOnFail' => true,
|
||
|
'maxRetries' => 30,
|
||
|
'retryPFailures' => false,
|
||
|
'retryRFailures' => false,
|
||
|
'retryPostFailures' => false,
|
||
|
'maxTime' => 60
|
||
|
], $dummyTask->getSettings());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @depends testBaseVariables
|
||
|
*/
|
||
|
public function testRetries()
|
||
|
{
|
||
|
// Create dummy task
|
||
|
$dummyTask = new Task('testRetries', new EmptyHandler(), 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));
|
||
|
}
|
||
|
}
|