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

HttpTest::testListTasks()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
nc 1
nop 0
1
<?php
2
3
namespace JobQueue\Tests\Application;
4
5
use GuzzleHttp\Client;
6
use JobQueue\Domain\Task\Profile;
7
use JobQueue\Domain\Task\Status;
8
use JobQueue\Domain\Task\Task;
9
use JobQueue\Tests\Domain\Job\DummyJob;
10
use PHPUnit\Framework\TestCase;
11
use Ramsey\Uuid\Uuid;
12
use Symfony\Component\Process\PhpExecutableFinder;
13
use Symfony\Component\Process\Process;
14
15
final class HttpTest extends TestCase
16
{
17
    /**
18
     *
19
     * @var Process
20
     */
21
    public static $process;
22
23
    /**
24
     *
25
     * @var Client
26
     */
27
    public static $client;
28
29
    public static function setUpBeforeClass()
30
    {
31
        $command = sprintf('%1$s -S localhost:8085 -t %2$s/public %2$s/public/index.php',
32
            (new PhpExecutableFinder)->find() ?: 'php',
33
            dirname(dirname(dirname(realpath(__DIR__)))));
34
35
        self::$process = new Process($command);
36
        self::$process->start();
37
38
        self::$client = new Client([
39
            'base_uri' => 'http://localhost:8085',
40
        ]);
41
    }
42
43
    /**
44
     *
45
     * @return string
46
     */
47
    public function testAddTask(): string
48
    {
49
        $response = self::$client->post('/tasks', [
50
            'json' => new Task(
51
                new Profile('profile1'),
52
                new DummyJob,
53
                [
54
                    'param1' => 'value1',
55
                    'param2' => 'value2',
56
                ], [
57
                    'tag1', 'tag2'
58
                ]
59
            )
60
        ]);
61
62
        $this->assertEquals(200, $response->getStatusCode());
63
64
        $task = json_decode($response->getBody(), true);
65
66
        $uuidPattern = rtrim(ltrim(Uuid::VALID_PATTERN, '^'), '$');
67
        $this->assertEquals(1, preg_match("/$uuidPattern/", $task['identifier']), 'Missing identifier');
68
69
        $this->assertEquals(Status::WAITING, $task['status']);
70
71
        $this->assertEquals('profile1', $task['profile']);
72
73
        $this->assertTrue(isset($task['parameters']['param1']));
74
        $this->assertEquals('value1', $task['parameters']['param1']);
75
76
77
        $this->assertTrue(isset($task['tags']));
78
        $this->assertTrue(in_array('tag1', $task['tags']));
79
80
        return $task['identifier'];
81
    }
82
83
    /**
84
     *
85
     * @param string $identifier
86
     * @depends testAddTask
87
     */
88
    public function testShowTask(string $identifier)
89
    {
90
        $response = self::$client->get(sprintf('/task/%s', $identifier));
91
92
        $this->assertEquals(200, $response->getStatusCode());
93
94
        $task = json_decode($response->getBody(), true);
95
96
        $this->assertEquals(1, preg_match("/^$identifier$/", $task['identifier']), 'Missing identifier');
97
98
        $this->assertEquals(Status::WAITING, $task['status']);
99
100
        $this->assertEquals('profile1', $task['profile']);
101
102
        $this->assertTrue(isset($task['parameters']['param1']));
103
        $this->assertEquals('value1', $task['parameters']['param1']);
104
105
106
        $this->assertTrue(isset($task['tags']));
107
        $this->assertTrue(in_array('tag1', $task['tags']));
108
    }
109
110
    /**
111
     *
112
     * @depends testAddTask
113
     */
114
    public function testListTasks()
115
    {
116
        $task1Response = self::$client->post('/tasks', [
117
            'json' => new Task(
118
                new Profile('profile1'),
119
                new DummyJob,
120
                [
121
                    'param2' => 'value2',
122
                    'param31' => 'value3',
123
                ], [
124
                    'tag1', 'tag3'
125
                ]
126
            )
127
        ]);
128
        $this->assertEquals(200, $task1Response->getStatusCode());
129
130
        $task2Response = self::$client->post('/tasks', [
131
            'json' => new Task(
132
                new Profile('profile2'),
133
                new DummyJob,
134
                [
135
                    'param2' => 'value2',
136
                    'param31' => 'value3',
137
                ], [
138
                    'tag1', 'tag3'
139
                ]
140
            )
141
        ]);
142
        $this->assertEquals(200, $task2Response->getStatusCode());
143
144
        $tasksResponse = self::$client->get('/tasks');
145
        $this->assertEquals(200, $tasksResponse->getStatusCode());
146
        $tasks = json_decode($tasksResponse->getBody(), true);
147
148
        $this->assertEquals(3, count($tasks));
149
150
        $profiles = [
151
            'profile1',
152
            'profile2',
153
        ];
154
        $this->assertTrue(in_array($tasks[1]['profile'], $profiles));
155
        $this->assertTrue(in_array($tasks[2]['profile'], $profiles));
156
    }
157
158
    public static function tearDownAfterClass()
159
    {
160
        self::$process->stop();
161
    }
162
}
163