Async/test/base/ControllerHandlerTest.php

196 lines
7.1 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\Handler\ControllerHandler;
use FuzeWorks\Async\SuperVisor;
use FuzeWorks\Async\Task;
use FuzeWorks\Async\Tasks;
use FuzeWorks\Async\TaskStorage;
use FuzeWorks\Events;
use FuzeWorks\Factory;
use Mock\Handlers\EmptyHandler;
use PHPUnit\Framework\TestCase;
class ControllerHandlerTest extends TestCase
{
/**
* @var Tasks
*/
protected $tasks;
/**
* @var TaskStorage
*/
protected $taskStorage;
public function setUp(): void
{
// Add TaskStorage
/** @var Tasks $tasks */
$this->tasks = Factory::getInstance('libraries')->get('async');
$this->taskStorage = $this->tasks->getTaskStorage();
$this->taskStorage->reset();
// Reset events
Events::$listeners = [];
}
/* ---------------------------------- Test the class itself --------------------------- */
public function testParametersAndClass()
{
// Create a test class
$handler = new ControllerHandler('TestController', 'testMethod', 'testPostMethod', '\Test\Namespace\\');
$parentHandler = new EmptyHandler();
// Set some parameters
$handler->setParentHandler($parentHandler);
$handler->setParentInput('Some Parent Input');
// And test return values
$this->assertInstanceOf(ControllerHandler::class, $handler);
$this->assertSame($parentHandler, $handler->getParentHandler());
}
public function testEmptyController()
{
// Create the handler
$handler = new ControllerHandler('empty', 'primary', 'post', '\Mock\Controllers\\');
$handler->setParentInput('Some input');
// And create the dummy Task
$dummyTask = new Task('testEmptyController', $handler, true, 'para1', 'para2');
// Write the task to TaskStorage
$this->taskStorage->addTask($dummyTask);
// Get the SuperVisor and start running the build
$superVisor = $this->tasks->getSuperVisor();
$this->assertEquals(SuperVisor::RUNNING, $superVisor->cycle());
$this->assertEquals(Task::RUNNING,
$this->taskStorage->getTaskById($dummyTask->getId())->getStatus()
);
// Give the task some time to finish
usleep(750000);
// Assert that the task is now waiting in POST
$this->assertEquals(SuperVisor::RUNNING, $superVisor->cycle());
$this->assertEquals(Task::SUCCESS,
$this->taskStorage->getTaskById($dummyTask->getId())->getStatus()
);
// Cycle again so it goes into POST mode
$this->assertEquals(SuperVisor::RUNNING, $superVisor->cycle());
$this->assertEquals(Task::POST,
$this->taskStorage->getTaskById($dummyTask->getId())->getStatus()
);
// Give the task some extra time to finish
usleep(750000);
// Cycle again so it goes into POST mode
$this->assertEquals(SuperVisor::FINISHED, $superVisor->cycle());
$this->assertEquals(Task::COMPLETED,
$this->taskStorage->getTaskById($dummyTask->getId())->getStatus()
);
// Now that the task is finished, let's see if the results match expectations
$dummyTask = $this->taskStorage->getTaskById($dummyTask->getId());
$this->assertEquals('Primary success: para1 + para2 + Some input', $dummyTask->getOutput());
$this->assertEquals('Post success: testEmptyController', $dummyTask->getPostOutput());
}
/**
* @depends testEmptyController
*/
public function testFailingController()
{
// Create the handler
$handler = new ControllerHandler('failing', 'primary', 'post', '\Mock\Controllers\\');
// And create the dummy Task
$dummyTask = new Task('testFailingController', $handler, true, 'para1', 'para2');
// Set this task to not retry
$dummyTask->setSettings(false);
// Write the task to TaskStorage
$this->taskStorage->addTask($dummyTask);
// Get the SuperVisor and start running the build
$superVisor = $this->tasks->getSuperVisor();
$this->assertEquals(SuperVisor::RUNNING, $superVisor->cycle());
$this->assertEquals(Task::RUNNING,
$this->taskStorage->getTaskById($dummyTask->getId())->getStatus()
);
// Give the task some time to finish
usleep(750000);
// Assert that the task is now waiting in POST
$this->assertEquals(SuperVisor::RUNNING, $superVisor->cycle());
$this->assertEquals(Task::FAILED,
$this->taskStorage->getTaskById($dummyTask->getId())->getStatus()
);
// The task should not retry and move to POST now
$this->assertEquals(SuperVisor::RUNNING, $superVisor->cycle());
$this->assertEquals(Task::POST,
$this->taskStorage->getTaskById($dummyTask->getId())->getStatus()
);
// Give the task some extra time to finish
usleep(750000);
// The task should not retry and move to POST now
$this->assertEquals(SuperVisor::FINISHED, $superVisor->cycle());
$this->assertEquals(Task::CANCELLED,
$this->taskStorage->getTaskById($dummyTask->getId())->getStatus()
);
// Now that the task is finished, let's see if the results match expectations
$dummyTask = $this->taskStorage->getTaskById($dummyTask->getId());
$this->assertEquals('Primary success: para1 + para2', $dummyTask->getOutput());
$this->assertEquals('Post success: testFailingController', $dummyTask->getPostOutput());
$this->assertEquals('ERROR \'Logged some task error!\'', $dummyTask->getErrors());
$this->assertEquals('ERROR \'Logged some post error!\'', $dummyTask->getPostErrors());
}
}