ManagerTest::testFlushCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace JobQueue\Tests\Application;
4
5
use JobQueue\Application\Console\ManagerApplication;
6
use JobQueue\Domain\Task\Profile;
7
use JobQueue\Domain\Task\Queue;
8
use JobQueue\Domain\Task\Status;
9
use JobQueue\Domain\Task\Task;
10
use JobQueue\Infrastructure\ServiceContainer;
11
use JobQueue\Tests\Domain\Job\DummyJob;
12
use PHPUnit\Framework\TestCase;
13
use Ramsey\Uuid\Uuid;
14
use Symfony\Component\Console\Tester\CommandTester;
15
16
final class ManagerTest extends TestCase
17
{
18
    /**
19
     *
20
     * @var Queue
21
     */
22
    private static $queue;
23
24
    /**
25
     *
26
     * @var
27
     */
28
    private static $manager;
29
30
    public static function setUpBeforeClass()
31
    {
32
        self::$queue = ServiceContainer::getInstance()->queue;
33
        self::$manager = new ManagerApplication(self::$queue);
34
    }
35
36
    /**
37
     *
38
     * @return string
39
     */
40
    public function testAddCommand(): string
41
    {
42
        $commandTester = new CommandTester($command = self::$manager->get('add'));
43
        $commandTester->execute([
44
            'command' => $command->getName(),
45
            'profile' => 'foo',
46
            'job' => 'JobQueue\Tests\Domain\Job\DummyJob',
47
            'parameters' => [
48
                'param1:value1',
49
                'param2:value2',
50
            ],
51
            '--tags' => ['tag1', 'tag2'],
52
        ], ['decorated' => false]);
53
54
        $uuidPattern = rtrim(ltrim(Uuid::VALID_PATTERN, '^'), '$');
55
56
        $this->assertEquals(1, preg_match("/Identifier +: ($uuidPattern)/", $commandTester->getDisplay(), $matches));
57
58
        return $matches[1];
59
    }
60
61
    /**
62
     *
63
     * @depends testAddCommand
64
     * @param string $identifier
65
     */
66
    public function testShowCommand(string $identifier)
67
    {
68
        $commandTester = new CommandTester($command = self::$manager->get('show'));
69
        $commandTester->execute([
70
            'command' => $command->getName(),
71
            'identifier' => $identifier,
72
        ], ['decorated' => false]);
73
74
        $this->assertEquals(1, preg_match("/Identifier +: $identifier/", $commandTester->getDisplay()), 'Missing identifier');
75
        $this->assertEquals(1, preg_match('/Parameters +: 1\) param1: value1/', $commandTester->getDisplay()), 'Missing parameter');
76
        $this->assertEquals(1, preg_match('/Tags +: tag1/', $commandTester->getDisplay()), 'Missing tag');
77
    }
78
79
    /**
80
     *
81
     * @depends testAddCommand
82
     * @param string $identifier
83
     */
84
    public function testListCommand(string $identifier)
85
    {
86
        $commandTester = new CommandTester($command = self::$manager->get('list'));
87
        $commandTester->execute([
88
            'command' => $command->getName(),
89
        ], ['decorated' => false]);
90
91
        $this->assertEquals(1, preg_match("/$identifier/", $commandTester->getDisplay()), 'Missing identifier');
92
    }
93
94
    /**
95
     *
96
     * @depends testAddCommand
97
     * @param string $identifier
98
     */
99
    public function testListCommandWithTagFilter(string $identifier): string
100
    {
101
        self::$queue->add(new Task(
102
            new Profile('test'),
103
            new DummyJob,
104
            [], ['foo', 'bar']
105
        ), $task2 = new Task(
106
            new Profile('test2'),
107
            new DummyJob,
108
            [], ['bar', 'baz']
109
        ));
110
111
        $commandTester = new CommandTester($command = self::$manager->get('list'));
112
        $commandTester->execute([
113
            'command' => $command->getName(),
114
            '--tags' => ['tag1'],
115
            '--legend' => true,
116
        ], ['decorated' => false]);
117
118
        $this->assertEquals(1, preg_match("/$identifier/", $commandTester->getDisplay()), 'Missing identifier');
119
        $this->assertEquals(1, preg_match('/T1: tag "tag1"/', $commandTester->getDisplay()), 'Missing tag legend');
120
121
        return (string) $task2->getIdentifier();
122
    }
123
124
    /**
125
     *
126
     * @depends testListCommandWithTagFilter
127
     * @param string $identifier
128
     */
129
    public function testListCommandWithProfileFilter(string $identifier)
130
    {
131
        $commandTester = new CommandTester($command = self::$manager->get('list'));
132
        $commandTester->execute([
133
            'command' => $command->getName(),
134
            '--profile' => 'test2',
135
            '--legend' => true,
136
        ], ['decorated' => false]);
137
138
        $this->assertEquals(1, preg_match("/$identifier/", $commandTester->getDisplay()), 'Missing identifier');
139
    }
140
141
    /**
142
     *
143
     * @depends testAddCommand
144
     * @param string $identifier
145
     */
146
    public function testEditCommand(string $identifier)
147
    {
148
        $commandTester = new CommandTester($command = self::$manager->get('edit'));
149
        $commandTester->execute([
150
            'command' => $command->getName(),
151
            'identifier' => $identifier,
152
            'status' => $status = Status::FINISHED,
153
        ], ['decorated' => false]);
154
155
        $this->assertEquals(1, preg_match("/Identifier +: $identifier/", $commandTester->getDisplay()), 'Missing identifier');
156
        $this->assertEquals(1, preg_match("/Status +: $status/", $commandTester->getDisplay()), 'Missing status');
157
    }
158
159
    /**
160
     *
161
     * @depends testEditCommand
162
     */
163
    public function testRestoreCommand()
164
    {
165
        $commandTester = new CommandTester($command = self::$manager->get('restore'));
166
        $commandTester->execute([
167
            'command' => $command->getName(),
168
            '--force' => true,
169
        ], ['decorated' => false]);
170
171
        $tasks = self::$queue->search();
172
173
        foreach ($tasks as $task) {
174
            $this->assertTrue(in_array((string) $task->getStatus(), [
175
                Status::WAITING,
176
                Status::FINISHED,
177
            ]));
178
        }
179
    }
180
181
    /**
182
     *
183
     * @depends testRestoreCommand
184
     */
185
    public function testFlushCommand()
186
    {
187
        $commandTester = new CommandTester($command = self::$manager->get('flush'));
188
        $commandTester->execute([
189
            'command' => $command->getName(),
190
            '--force' => true,
191
        ], ['decorated' => false]);
192
193
        $tasks = self::$queue->search();
194
195
        $this->assertTrue(empty($tasks));
196
    }
197
}
198