Passed
Pull Request — master (#277)
by Kirill
03:11
created

DispatcherTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 17
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Framework\GRPC;
13
14
use Mockery as m;
15
use Spiral\Files\Files;
16
use Spiral\GRPC\GRPCDispatcher;
17
use Spiral\RoadRunner\Worker;
18
use Spiral\App\Service\Sub\Message;
0 ignored issues
show
Bug introduced by
The type Spiral\App\Service\Sub\Message was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Spiral\Tests\Framework\ConsoleTest;
20
21
class DispatcherTest extends ConsoleTest
22
{
23
    public function setUp(): void
24
    {
25
        exec('protoc 2>&1', $out);
26
        if (strpos(implode("\n", $out), '--php_out') === false) {
27
            $this->markTestSkipped('Protoc binary is missing');
28
        }
29
30
        parent::setUp();
31
32
        $fs = new Files();
33
34
        $this->runCommandDebug('grpc:generate', [
35
            'proto' => realpath($fs->normalizePath($this->app->dir('app') . 'proto/service.proto'))
36
        ]);
37
38
        $output = $this->app->dir('app') . 'src/Service/EchoService.php';
39
        file_put_contents($output, GenerateTest::SERVICE);
40
    }
41
42
    public function tearDown(): void
43
    {
44
        parent::tearDown();
45
46
        $fs = new Files();
47
48
        if ($fs->isDirectory($this->app->dir('app') . 'src/Service')) {
49
            $fs->deleteDirectory($this->app->dir('app') . 'src/Service');
50
        }
51
52
        if ($fs->isDirectory($this->app->dir('app') . 'src/GPBMetadata')) {
53
            $fs->deleteDirectory($this->app->dir('app') . 'src/GPBMetadata');
54
        }
55
    }
56
57
    public function testCanServe(): void
58
    {
59
        $this->assertFalse($this->app->get(GRPCDispatcher::class)->canServe());
60
    }
61
62
    public function testCanServe2(): void
63
    {
64
        $this->app->getEnvironment()->set('RR_GRPC', true);
65
        $this->assertTrue($this->app->get(GRPCDispatcher::class)->canServe());
66
    }
67
68
    public function testServe(): void
69
    {
70
        $w = m::mock(Worker::class);
71
72
        $this->app->getEnvironment()->set('RR_GRPC', true);
73
        $this->app->getContainer()->bind(Worker::class, $w);
0 ignored issues
show
Bug introduced by
$w of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        $this->app->getContainer()->bind(Worker::class, /** @scrutinizer ignore-type */ $w);
Loading history...
74
75
        $msg = new Message();
76
        $msg->setMsg('hello');
77
78
        $w->shouldReceive('receive')->once()->with(
79
            \Mockery::on(function (&$context) {
80
                $context = '{
81
                  "service": "service.Echo",
82
                  "method": "Ping",
83
                  "context": {}
84
                }';
85
86
                return true;
87
            })
88
        )->andReturn($msg->serializeToString());
89
90
        $w->shouldReceive('send')->once()->with(
91
            \Mockery::on(function ($out) {
92
                $msg = new Message();
93
                $msg->mergeFromString($out);
94
                $this->assertSame('hello', $msg->getMsg());
95
96
                return true;
97
            }),
98
            '{}'
99
        )->andReturn(true);
100
101
        // one command only
102
        $w->shouldReceive('receive')->once()->with(
103
            \Mockery::on(function (&$context) {
104
                $context = null;
105
                return true;
106
            })
107
        )->andReturn(null);
108
109
        $this->app->get(GRPCDispatcher::class)->serve();
110
    }
111
112
    public function testError(): void
113
    {
114
        $w = m::mock(Worker::class);
115
116
        $this->app->getEnvironment()->set('RR_GRPC', true);
117
        $this->app->getContainer()->bind(Worker::class, $w);
0 ignored issues
show
Bug introduced by
$w of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
        $this->app->getContainer()->bind(Worker::class, /** @scrutinizer ignore-type */ $w);
Loading history...
118
119
        $msg = new Message();
120
        $msg->setMsg('hello');
121
122
        $w->shouldReceive('receive')->once()->with(
123
            \Mockery::on(function (&$context) {
124
                $context = '{
125
                  "service": "service.Echo",
126
                  "method": "Invalid",
127
                  "context": {}
128
                }';
129
130
                return true;
131
            })
132
        )->andReturn($msg->serializeToString());
133
134
        $w->shouldReceive('error')->once()->with(
135
            \Mockery::on(function ($out) {
136
                $this->assertStringContainsString('Method `Invalid` not found', $out);
137
                return true;
138
            })
139
        )->andReturn(true);
140
141
        // one command only
142
        $w->shouldReceive('receive')->once()->with(
143
            \Mockery::on(function (&$context) {
144
                $context = null;
145
                return true;
146
            })
147
        )->andReturn(null);
148
149
        $this->app->get(GRPCDispatcher::class)->serve();
150
    }
151
}
152