Completed
Push — master ( dcb869...651674 )
by Dmitry
02:44
created

ServiceTest::testEntityTriggers()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 1
eloc 16
nc 1
nop 0
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([
18
            'services' => []
19
        ]);
20
21
        $this->mock('event.subscribe')->willReturn([
22
            'success' => true
23
        ]);
24
25
        $this->mock('web.register')->willDo(function($params) use ($context) {
26
            foreach ($params['routes'] as $route) {
27
                $context->routes[] = (object) ['route' => $route, 'service' => $params['service']];
28
            }
29
        });
30
31
        // internaly it should analyze routes and call web.register
32
        // then context will be updated and we should validate it
33
        $this->dispatch('module.register');
34
35
        $service = $this->app->get(Service::class);
36
        $serviceRoutes = [];
37
        foreach ($context->routes as $route) {
38
            if ($route->service == $service->getName()) {
39
                $serviceRoutes[] = $route->route;
40
            }
41
        }
42
43
        $this->assertCount(2, $serviceRoutes);
44
        $this->assertContains('index/index', $serviceRoutes);
45
        $this->assertContains('dynamic/*', $serviceRoutes);
46
    }
47
48
    public function testName()
49
    {
50
        $this->assertSame('test', $this->get(Service::class)->getName());
51
    }
52
53
    public function testServices()
54
    {
55
        $service = $this->get(Service::class);
56
57
        $mock = $this->mock('web.services')->willReturn([
58
            'services' => ['gateway', 'audit']
59
        ]);
60
61
        $this->assertSame($service->listServices(), ['gateway', 'audit']);
62
        $this->assertSame($service->listServices(), ['gateway', 'audit']);
63
64
        // service list is cached
65
        $this->assertSame(1, $mock->calls);
66
    }
67
68
    public function testEvents()
69
    {
70
        $service = $this->get(Service::class);
71
72
        $this->mock('web.services')->willReturn(['services' => ['web']]);
73
74
        $this->mock('event.subscribe')->willReturn(['success' => true]);
75
76
        $this->get(Pool::class)->registerResolver(function($name) {
77
            if ($name == 'event') {
78
                return new class {
79
                    public function find() {
80
                        return [
81
                            (object) ['nick' => 'test.*.*'],
82
                            (object) ['nick' => 'web.service.updated'],
83
                        ];
84
                    }
85
                };
86
            }
87
        });
88
89
        $service->subscribe('test.*.*');
90
91
        // equals
92
        $this->assertTrue($service->eventExists('web.service.updated'));
93
        $this->assertFalse($service->eventExists('guard.session.created'));
94
95
        // wildcard
96
        $this->assertTrue($service->eventExists('test.post.updated'));
97
98
        // wildcard
99
        $this->assertTrue($service->eventMatch('test.post.created', '*.post.*'));
100
        $this->assertFalse($service->eventMatch('test.post.created', '*.posts.*'));
101
    }
102
103
    public function testEntityTriggers()
104
    {
105
        $bazyaba = $this->create('post', ['text' => 'bazyaba']);
106
107
        // afterCreate was triggered
108
        $this->assertSame($bazyaba->text, 'bazyaba!');
0 ignored issues
show
Bug introduced by
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...
109
110
        $this->dispatch('module.trigger', [
111
            'space' => 'post',
112
            'id' => $bazyaba->id,
0 ignored issues
show
Bug introduced by
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...
113
            'type' => 'create',
114
        ]);
115
116
        // afterCreate + afterUpdate
117
        $this->assertSame($bazyaba->text, 'bazyaba!!.');
118
119
        $bazyaba->text = 'test';
120
        $bazyaba->save();
121
122
        $this->assertSame($bazyaba->text, 'test.');
123
124
        $this->dispatch('module.trigger', [
125
            'space' => 'post',
126
            'id' => $bazyaba->id,
127
            'type' => 'update',
128
        ]);
129
130
        $this->assertSame($bazyaba->text, 'test..');
131
    }
132
}
133