Passed
Push — 1.x ( 5710fc...1fcc19 )
by Kevin
06:17
created

InteractsWithMessenger::messenger()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 5
nop 1
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Zenstruck\Messenger\Test;
4
5
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6
use Zenstruck\Messenger\Test\Transport\TestTransport;
7
use Zenstruck\Messenger\Test\Transport\TestTransportRegistry;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
trait InteractsWithMessenger
13
{
14
    /**
15
     * @after
16
     */
17
    final protected function resetTransports(): void
18
    {
19
        TestTransport::reset();
20
    }
21
22
    final protected function messenger(?string $transport = null): TestTransport
23
    {
24
        if (!$this instanceof KernelTestCase) {
25
            throw new \LogicException(\sprintf('The %s trait can only be used with %s.', __TRAIT__, KernelTestCase::class));
26
        }
27
28
        if (!self::$container) {
29
            self::bootKernel();
30
        }
31
32
        if (!self::$container->has(TestTransportRegistry::class)) {
33
            throw new \LogicException('Cannot access transport - is ZenstruckMessengerTestBundle enabled in your test environment?');
34
        }
35
36
        return self::$container->get(TestTransportRegistry::class)->get($transport);
37
    }
38
}
39