summaryrefslogtreecommitdiff
path: root/plugins/jobs/oooold/tests/Jobs/ShortCircuitTest.php
blob: c330638589854128f83db4a966a5e9f74f000203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php

/**
 * Spiral Framework.
 *
 * @license   MIT
 * @author    Anton Titov (Wolfy-J)
 */

declare(strict_types=1);

namespace Spiral\Jobs\Tests;

use PHPUnit\Framework\TestCase;
use Spiral\Core\Container;
use Spiral\Jobs\Options;
use Spiral\Jobs\Registry\ContainerRegistry;
use Spiral\Jobs\ShortCircuit;
use Spiral\Jobs\Tests\Local\ErrorJob;
use Spiral\Jobs\Tests\Local\Job;

class ShortCircuitTest extends TestCase
{
    protected function tearDown(): void
    {
        if (file_exists(Job::JOB_FILE)) {
            unlink(Job::JOB_FILE);
        }
    }

    public function testLocal(): void
    {
        $c = new ContainerRegistry(new Container());
        $jobs = new ShortCircuit($c, $c);

        $id = $jobs->push(Job::class, ['data' => 100]);

        $this->assertNotEmpty($id);

        $this->assertFileExists(Job::JOB_FILE);

        $data = json_decode(file_get_contents(Job::JOB_FILE), true);
        $this->assertSame($id, $data['id']);
        $this->assertSame(100, $data['data']);
    }

    public function testLocalDelayed(): void
    {
        $c = new ContainerRegistry(new Container());
        $jobs = new ShortCircuit($c, $c);

        $t = microtime(true);
        $id = $jobs->push(Job::class, ['data' => 100], Options::delayed(1));

        $this->assertTrue(microtime(true) - $t >= 1);

        $this->assertNotEmpty($id);

        $this->assertFileExists(Job::JOB_FILE);

        $data = json_decode(file_get_contents(Job::JOB_FILE), true);
        $this->assertSame($id, $data['id']);
        $this->assertSame(100, $data['data']);
    }

    /**
     * @expectedException \Spiral\Jobs\Exception\JobException
     */
    public function testError(): void
    {
        $c = new ContainerRegistry(new Container());
        $jobs = new ShortCircuit($c, $c);
        $jobs->push(ErrorJob::class);
    }

    public function testLocalDelay(): void
    {
        $c = new ContainerRegistry(new Container());
        $jobs = new ShortCircuit($c, $c);

        $id = $jobs->push(Job::class, ['data' => 100], Options::delayed(1));
        $this->assertNotEmpty($id);

        $this->assertFileExists(Job::JOB_FILE);

        $data = json_decode(file_get_contents(Job::JOB_FILE), true);
        $this->assertSame($id, $data['id']);
        $this->assertSame(100, $data['data']);
    }
}