TestCase::initQueue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of the tarantool/queue package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Queue\Tests\Integration;
15
16
use PHPUnitExtras\Annotation\AnnotationProcessorBuilder;
17
use Tarantool\Client\Client;
18
use Tarantool\PhpUnit\TestCase as BaseTestCase;
19
use Tarantool\Queue\Queue;
20
use Tarantool\Queue\Task;
21
use Tarantool\Queue\Tests\PhpUnitCompat;
22
23
abstract class TestCase extends BaseTestCase
24
{
25
    use PhpUnitCompat;
26
27
    /** @var Client|null */
28
    private $client;
29
30
    /** @var Queue */
31
    protected $queue;
32
33
    public function getQueueName() : string
34
    {
35
        $methodName = $this->getName(false);
36
        if (0 === strpos($methodName, 'test')) {
37
            $methodName = substr($methodName, 4);
38
        }
39
40
        return sprintf('t_%s_%s', $this->getQueueType(), strtolower($methodName));
41
    }
42
43
    public function getQueueType() : string
44
    {
45
        $class = new \ReflectionClass($this);
46
        $type = str_replace('QueueTest', '', $class->getShortName());
47
48
        return strtolower($type);
49
    }
50
51
    final protected function getClient() : Client
52
    {
53
        if ($this->client) {
54
            return $this->client;
55
        }
56
57
        if (false === $uri = getenv('TNT_LISTEN_URI')) {
58
            return $this->client = Client::fromDefaults();
59
        }
60
61
        if (0 === strpos($uri, '/')) {
62
            $dsn = 'unix://'.$uri;
63
        } elseif (0 === strpos($uri, 'unix/:')) {
64
            $dsn = 'unix://'.substr($uri, 6);
65
        } elseif (ctype_digit($uri)) {
66
            $dsn = 'tcp://127.0.0.1:'.$uri;
67
        } else {
68
            $dsn = 'tcp://'.$uri;
69
        }
70
71
        return $this->client = Client::fromDsn($dsn);
72
    }
73
74
    final protected function createAnnotationProcessorBuilder() : AnnotationProcessorBuilder
75
    {
76
        return parent::createAnnotationProcessorBuilder()
77
            ->addPlaceholderResolver(new TubePlaceholderResolver($this));
78
    }
79
80
    /**
81
     * @before
82
     */
83
    final protected function initQueue() : void
84
    {
85
        $this->queue = new Queue($this->getClient(), $this->getQueueName());
86
    }
87
88
    final protected function getQueue() : Queue
89
    {
90
        return $this->queue;
91
    }
92
93
    final protected static function assertTaskInstance($task) : void
94
    {
95
        self::assertInstanceOf(Task::class, $task);
96
    }
97
98
    final protected static function assertTask($task, int $expectedId, string $expectedState, $expectedData) : void
99
    {
100
        self::assertTaskInstance($task);
101
        self::assertSame($expectedId, $task->getId());
102
        self::assertSame($expectedState, $task->getState());
103
        self::assertSame($expectedData, $task->getData());
104
    }
105
}
106