Passed
Branch master (aecce8)
by Brice
05:03
created

ManagerTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 174
rs 10
c 0
b 0
f 0

9 Methods

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