Passed
Branch master (b26d17)
by Anton
02:18
created

DispatcherTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 20
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Spiral\Framework\GRPC;
11
12
use Mockery as m;
13
use Spiral\App\Service\Message;
0 ignored issues
show
Bug introduced by
The type Spiral\App\Service\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...
14
use Spiral\Files\Files;
15
use Spiral\Framework\ConsoleTest;
16
use Spiral\GRPC\GRPCDispatcher;
17
use Spiral\RoadRunner\Worker;
18
19
class DispatcherTest extends ConsoleTest
20
{
21
    public function setUp()
22
    {
23
        exec('protoc 2>&1', $out);
24
        if (strpos(join("\n", $out), '--php_out') === false) {
25
            $this->markTestSkipped('Protoc binary is missing');
26
        }
27
28
        parent::setUp();
29
30
        $fs = new Files();
31
        $proto = $fs->normalizePath($this->app->dir('app') . 'proto/service.proto');
32
33
        // protoc can't figure relative paths
34
        $proto = str_replace('Framework/../', '', $proto);
35
36
        $this->runCommandDebug('grpc:generate', [
37
            'proto' => $proto
38
        ]);
39
40
        file_put_contents($this->app->dir('app') . 'src/Service/EchoService.php', GenerateTest::SERVICE);
41
    }
42
43
    public function tearDown()
44
    {
45
        parent::tearDown();
46
47
        $fs = new Files();
48
49
        if ($fs->isDirectory($this->app->dir('app') . 'src/Service')) {
50
            $fs->deleteDirectory($this->app->dir('app') . 'src/Service');
51
        }
52
53
        if ($fs->isDirectory($this->app->dir('app') . 'src/GPBMetadata')) {
54
            $fs->deleteDirectory($this->app->dir('app') . 'src/GPBMetadata');
55
        }
56
    }
57
58
    public function testCanServe()
59
    {
60
        $this->assertFalse($this->app->get(GRPCDispatcher::class)->canServe());
61
    }
62
63
    public function testCanServe2()
64
    {
65
        $this->app->getEnvironment()->set('RR_GRPC', true);
66
        $this->assertTrue($this->app->get(GRPCDispatcher::class)->canServe());
67
    }
68
69
    public function testServe()
70
    {
71
        $w = m::mock(Worker::class);
72
73
        $this->app->getEnvironment()->set('RR_GRPC', true);
74
        $this->app->getContainer()->bind(Worker::class, $w);
0 ignored issues
show
Bug introduced by
$w of type 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

74
        $this->app->getContainer()->bind(Worker::class, /** @scrutinizer ignore-type */ $w);
Loading history...
75
76
        $msg = new Message();
77
        $msg->setMsg("hello");
78
79
        $w->shouldReceive('receive')->once()->with(
80
            \Mockery::on(function (&$context) {
81
                $context = '{
82
                  "service": "service.Echo",
83
                  "method": "Ping"               
84
                }';
85
86
                return true;
87
            })
88
        )->andReturn($msg->serializeToString());
89
90
        $w->shouldReceive('send')->once()->with(
91
            \Mockery::on(function ($out) {
92
93
                $msg = new Message();
94
                $msg->mergeFromString($out);
95
                $this->assertSame("hello", $msg->getMsg());
96
97
                return true;
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()
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\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
                }';
128
129
                return true;
130
            })
131
        )->andReturn($msg->serializeToString());
132
133
        $w->shouldReceive('error')->once()->with(
134
            \Mockery::on(function ($out) {
135
                $this->assertContains('Method `Invalid` not found', $out);
136
                return true;
137
            })
138
        )->andReturn(true);
139
140
        // one command only
141
        $w->shouldReceive('receive')->once()->with(
142
            \Mockery::on(function (&$context) {
143
                $context = null;
144
                return true;
145
            })
146
        )->andReturn(null);
147
148
        $this->app->get(GRPCDispatcher::class)->serve();
149
    }
150
}