ServiceTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
dl 0
loc 142
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ find() 0 6 1
A testHostResolver() 0 17 2
A testBootstrap() 0 36 4
A testName() 0 4 1
A testServices() 0 14 1
A testEvents() 0 34 2
A testEntityTriggers() 0 29 1
1
<?php
2
3
namespace Test;
4
5
use Basis\Test;
6
use Basis\Service;
7
use Tarantool\Mapper\Pool;
8
9
class ServiceTest extends Test
10
{
11
    public function testHostResolver()
12
    {
13
        $service = $this->get(Service::class);
14
        $result = [];
15
        foreach ([1, 2] as $attempt) {
16
            $start = microtime(1);
17
            $host = $service->getHost('basis.company')->address;
18
            $timer = microtime(1) - $start;
19
            $result[] = [
20
                'host' => $host,
21
                'timer' => $timer,
22
            ];
23
        }
24
25
        $this->assertGreaterThan($result[1]['timer'], $result[0]['timer']);
26
        $this->assertEquals($result[0]['host'], $result[1]['host']);
27
    }
28
29
    public function testBootstrap()
30
    {
31
        $context = (object) [
32
            'routes' => [],
33
        ];
34
35
        $this->mock('web.services')->willReturn([
36
            'services' => []
37
        ]);
38
39
        $this->mock('event.subscribe')->willReturn([
40
            'success' => true
41
        ]);
42
43
        $this->mock('web.register')->willDo(function($params) use ($context) {
44
            foreach ($params['routes'] as $route) {
45
                $context->routes[] = (object) ['route' => $route, 'service' => $params['service']];
46
            }
47
        });
48
49
        // internaly it should analyze routes and call web.register
50
        // then context will be updated and we should validate it
51
        $this->dispatch('module.register');
52
53
        $service = $this->app->get(Service::class);
54
        $serviceRoutes = [];
55
        foreach ($context->routes as $route) {
56
            if ($route->service == $service->getName()) {
57
                $serviceRoutes[] = $route->route;
58
            }
59
        }
60
61
        $this->assertCount(3, $serviceRoutes);
0 ignored issues
show
Documentation introduced by
$serviceRoutes is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
        $this->assertContains('index/index', $serviceRoutes);
63
        $this->assertContains('dynamic/*', $serviceRoutes);
64
    }
65
66
    public function testName()
67
    {
68
        $this->assertSame('test', $this->get(Service::class)->getName());
69
    }
70
71
    public function testServices()
72
    {
73
        $service = $this->get(Service::class);
74
75
        $mock = $this->mock('web.services')->willReturn([
76
            'services' => ['gateway', 'audit']
77
        ]);
78
79
        $this->assertSame($service->listServices(), ['gateway', 'audit']);
80
        $this->assertSame($service->listServices(), ['gateway', 'audit']);
81
82
        // service list is cached
83
        $this->assertSame(1, $mock->calls);
84
    }
85
86
    public function testEvents()
87
    {
88
        $service = $this->get(Service::class);
89
90
        $this->mock('web.services')->willReturn(['services' => ['web']]);
91
92
        $this->mock('event.subscribe')->willReturn(['success' => true]);
93
94
        $this->get(Pool::class)->registerResolver(function($name) {
95
            if ($name == 'event') {
96
                return new class {
97
                    public function find() {
98
                        return [
99
                            (object) ['nick' => 'test.*.*', 'ignore' => false],
100
                            (object) ['nick' => 'web.service.updated', 'ignore' => false],
101
                        ];
102
                    }
103
                };
104
            }
105
        });
106
107
        $service->subscribe('test.*.*');
108
109
        // equals
110
        $this->assertTrue($service->eventExists('web.service.updated'));
111
        $this->assertFalse($service->eventExists('guard.session.created'));
112
113
        // wildcard
114
        $this->assertTrue($service->eventExists('test.post.updated'));
115
116
        // wildcard
117
        $this->assertTrue($service->eventMatch('test.post.created', '*.post.*'));
118
        $this->assertFalse($service->eventMatch('test.post.created', '*.posts.*'));
119
    }
120
121
    public function testEntityTriggers()
122
    {
123
        $bazyaba = $this->create('post', ['text' => 'bazyaba']);
124
125
        // afterCreate was triggered
126
        $this->assertSame($bazyaba->text, 'bazyaba!');
127
128
        $this->dispatch('module.trigger', [
129
            'space' => 'post',
130
            'id' => $bazyaba->id,
131
            'type' => 'create',
132
        ]);
133
134
        // afterCreate + afterUpdate
135
        $this->assertSame($bazyaba->text, 'bazyaba!!.');
136
137
        $bazyaba->text = 'test';
138
        $bazyaba->save();
139
140
        $this->assertSame($bazyaba->text, 'test.');
141
142
        $this->dispatch('module.trigger', [
143
            'space' => 'post',
144
            'id' => $bazyaba->id,
145
            'type' => 'update',
146
        ]);
147
148
        $this->assertSame($bazyaba->text, 'test..');
149
    }
150
}
151