Completed
Push — master ( 9d8998...346ad6 )
by Dmitry
03:39
created

ServiceTest.php$0 ➔ find()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 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 testBootstrap()
12
    {
13
        $context = (object) [
14
            'routes' => [],
15
        ];
16
17
        $this->mock('web.services')->willReturn(['names' => []]);
18
19
        $this->mock('event.subscribe')
20
            ->willReturn(['success' => true]);
21
22
        $this->mock('web.register')
23
            ->willReturn(function($params) use ($context) {
24
                foreach ($params['routes'] as $route) {
25
                    $context->routes[] = (object) ['route' => $route, 'service' => $params['service']];
26
                }
27
                return ['success' => true];
28
            });
29
30
        // internaly it should analyze routes and call web.register
31
        // then context will be updated and we should validate it
32
        $this->dispatch('module.register');
33
34
        $service = $this->app->get(Service::class);
35
        $serviceRoutes = [];
36
        foreach ($context->routes as $route) {
37
            if ($route->service == $service->getName()) {
38
                $serviceRoutes[] = $route->route;
39
            }
40
        }
41
42
        $this->assertCount(2, $serviceRoutes);
43
        $this->assertContains('index/index', $serviceRoutes);
44
        $this->assertContains('dynamic/*', $serviceRoutes);
45
    }
46
47
    public function testName()
48
    {
49
        $this->assertSame('test', $this->get(Service::class)->getName());
50
    }
51
52
    public function testServices()
53
    {
54
        $service = $this->get(Service::class);
55
56
        $calls = 0;
57
        $this->mock('web.services')
58
            ->willReturn(function() use (&$calls) {
59
                $calls++;
60
                return [
61
                    'names' => ['gateway', 'audit']
62
                ];
63
            });
64
65
        $this->assertSame($service->listServices(), ['gateway', 'audit']);
66
        $this->assertSame($service->listServices(), ['gateway', 'audit']);
67
        // service list is cached
68
        $this->assertSame(1, $calls);
69
    }
70
71
    public function testEvents()
72
    {
73
        $service = $this->get(Service::class);
74
75
        $this->mock('web.services')->willReturn(['names' => ['web']]);
76
77
        $this->mock('event.subscribe')->willReturn(['success' => true]);
78
79
        $this->get(Pool::class)->registerResolver(function($name) {
80
            if ($name == 'event') {
81
                return new class {
82
                    public function find() {
83
                        return [
84
                            (object) ['nick' => 'test.*.*'],
85
                            (object) ['nick' => 'web.service.updated'],
86
                        ];
87
                    }
88
                };
89
            }
90
        });
91
92
        $service->subscribe('test.*.*');
93
94
        // equals
95
        $this->assertTrue($service->eventExists('web.service.updated'));
96
        $this->assertFalse($service->eventExists('guard.session.created'));
97
98
        // wildcard
99
        $this->assertTrue($service->eventExists('test.post.updated'));
100
101
        // wildcard
102
        $this->assertTrue($service->eventMatch('test.post.created', '*.post.*'));
103
        $this->assertFalse($service->eventMatch('test.post.created', '*.posts.*'));
104
    }
105
}
106