|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Vectorial1024\LaravelProcessAsync\Tests; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use LogicException; |
| 7 | +use RuntimeException; |
| 8 | +use Vectorial1024\LaravelProcessAsync\AsyncTask; |
| 9 | +use Vectorial1024\LaravelProcessAsync\AsyncTaskStatus; |
| 10 | +use Vectorial1024\LaravelProcessAsync\FakeAsyncTaskStatus; |
| 11 | +use Vectorial1024\LaravelProcessAsync\Tests\Tasks\DummyAsyncTask; |
| 12 | +use Vectorial1024\LaravelProcessAsync\Tests\Tasks\SleepingAsyncTask; |
| 13 | +use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutENoticeTask; |
| 14 | +use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutErrorTask; |
| 15 | +use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutNoOpTask; |
| 16 | +use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutNormalTask; |
| 17 | + |
| 18 | +// a series of tests that ensure the fake tasks are indeed fake while still look like the same |
| 19 | +class FakeAsyncTaskTest extends BaseTestCase |
| 20 | +{ |
| 21 | + public function testFakeTaskDoesNotRun() |
| 22 | + { |
| 23 | + // the fake task should not even run |
| 24 | + $testFileName = $this->getStoragePath("testFakeTaskDoesNotRun.txt"); |
| 25 | + $task = new AsyncTask(new DummyAsyncTask("Hello world!", $testFileName)); |
| 26 | + $fakeTask = $task->fake(); |
| 27 | + |
| 28 | + // fake start it |
| 29 | + $fakeTask->start(); |
| 30 | + sleep(1); |
| 31 | + // there should have no file outputs |
| 32 | + $this->assertFileDoesNotExist($testFileName); |
| 33 | + } |
| 34 | + |
| 35 | + public function testFakeTaskSameParams() |
| 36 | + { |
| 37 | + // the fake task should preserve its parameters |
| 38 | + $testFileName = $this->getStoragePath("testFakeTaskSameDetails.txt"); |
| 39 | + $task = new AsyncTask(new DummyAsyncTask("Hello world!", $testFileName)); |
| 40 | + $randomDuration = rand(1, 10); |
| 41 | + $task->withTimeLimit($randomDuration); |
| 42 | + |
| 43 | + // fake it... |
| 44 | + $fakeTask = $task->fake(); |
| 45 | + // ...and the parameters stay the same |
| 46 | + $this->assertEquals($task->getTimeLimit(), $fakeTask->getTimeLimit()); |
| 47 | + |
| 48 | + // it is difficult to test the task ID since it would be exposing something that should not be exposed, so we will just have to believe it. |
| 49 | + } |
| 50 | + |
| 51 | + public function testFakeTaskStatus() |
| 52 | + { |
| 53 | + $taskID = "testFakeTaskStatus"; |
| 54 | + $taskStatus = new AsyncTaskStatus($taskID); |
| 55 | + $fakeTaskStatusFromFake = $taskStatus->fake(); |
| 56 | + $fakeTaskStatusFromNew = new FakeAsyncTaskStatus($taskID); |
| 57 | + |
| 58 | + // should have same task ID |
| 59 | + $this->assertEquals($taskStatus->getEncodedTaskID(), $fakeTaskStatusFromFake->getEncodedTaskID()); |
| 60 | + $this->assertEquals($taskStatus->getEncodedTaskID(), $fakeTaskStatusFromNew->getEncodedTaskID()); |
| 61 | + } |
| 62 | +} |
0 commit comments