Completed
Push — master ( 95b8e4...b19727 )
by Dmitry
06:52
created

test/php/Test/ServiceTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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(2, $serviceRoutes);
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.*.*'],
100
                            (object) ['nick' => 'web.service.updated'],
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!');
0 ignored issues
show
The property text does not seem to exist in Tarantool\Mapper\Entity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
127
128
        $this->dispatch('module.trigger', [
129
            'space' => 'post',
130
            'id' => $bazyaba->id,
0 ignored issues
show
The property id does not seem to exist in Tarantool\Mapper\Entity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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