Completed
Push — master ( 6ab5d4...9d8998 )
by Dmitry
02:41
created

TarantoolTest::tearDown()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 10
nc 4
nop 0
1
<?php
2
3
namespace Test;
4
5
use Basis\Filesystem;
6
use Basis\Job;
7
use Exception;
8
use ReflectionClass;
9
use Procedure\Greet;
10
use Repository\Note;
11
use Tarantool\Mapper\Bootstrap;
12
use Tarantool\Mapper\Mapper;
13
use Basis\Test;
14
15
class TarantoolTest extends Test
16
{
17
    public function tearDown()
18
    {
19
        $fs = $this->app->get(Filesystem::class);
20
        $classes = $fs->listClasses('Migration');
21
22
        $dirs = [];
23
        foreach ($classes as $class) {
24
            $filename = $fs->getPath('php/'.str_replace('\\', '/', $class).'.php');
25
            unlink($filename);
26
            $dirs[dirname($filename)] = true;
27
        }
28
        foreach ($dirs as $dir => $_) {
29
            rmdir($dir);
30
        }
31
    }
32
33
    public function testProcedureRegistration()
34
    {
35
        $this->assertSame($this->app->get(Greet::class)('Dmitry'), 'Hello, Dmitry!');
36
    }
37
38
    public function testMigrationOrder()
39
    {
40
        $migration = $this->app->dispatch('generate.migration', [
41
            'name' => 'b',
42
        ]);
43
        $contents = file_get_contents($migration['filename']);
44
        $contents = str_replace('throw', '//throw', $contents);
45
        file_put_contents($migration['filename'], $contents);
46
47
        sleep(1);
48
49
        $migration = $this->app->dispatch('generate.migration', [
50
            'name' => 'a',
51
        ]);
52
53
        $contents = file_get_contents($migration['filename']);
54
        $contents = str_replace('throw', '//throw', $contents);
55
        file_put_contents($migration['filename'], $contents);
56
57
        $this->app->dispatch('tarantool.migrate');
58
59
        $bootstrap = $this->app->get(Bootstrap::class);
60
61
        $reflection = new ReflectionClass(Bootstrap::class);
62
        $property = $reflection->getProperty('migrations');
63
        $property->setAccessible(true);
64
65
        $migrations = $property->getValue($bootstrap);
66
67
        $this->assertCount(2, $migrations);
68
69
        $order = [];
70
        foreach ($migrations as $migration) {
71
            $order[] = substr($migration, -1);
72
        }
73
        $this->assertSame(['B', 'A'], $order);
74
    }
75
76
    public function testMigrationGenerator()
77
    {
78
        $fs = $this->app->get(Filesystem::class);
79
80
        $classes = $fs->listClasses('Migration');
81
        $this->assertCount(0, $classes);
82
83
        $this->app->dispatch('generate.migration', [
84
            'name' => 'my migration created at ' . time(),
85
        ]);
86
87
        $classes = $fs->listClasses('Migration');
88
        $this->assertCount(1, $classes);
89
    }
90
91
    public function testEntity()
92
    {
93
        $this->app->dispatch('tarantool.migrate');
94
95
        $mapper = $this->app->get(Mapper::class);
96
        $mapper->getRepository('note')->truncate();
97
        $note = $mapper->getRepository('note')->create('zzz');
98
        $this->assertSame($note->message, 'zzz');
99
100
        $note->message = 'test';
101
        $note->save();
102
103
        $this->assertNotNull($note->id);
104
        $this->assertSame($note->message, 'test');
105
106
        $this->assertSame($note->app, $this->app);
107
108
        ob_start();
109
        var_dump($note);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($note); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
110
        $contents = ob_get_clean();
111
112
        $this->assertNotContains("app", $contents);
113
    }
114
115
    public function testRepositoryRegistration()
116
    {
117
        $repository = $this->app->get(Note::class);
118
        $this->assertSame($this->app->get(Mapper::class), $repository->getMapper());
119
    }
120
121
    public function testJobShortcuts()
122
    {
123
        $job = new class($this->app) extends Job {
124
            public function run(TarantoolTest $test)
125
            {
126
                // dispatch shortcut
127
                $result = $this->dispatch('example.hello');
128
                $test->assertSame($result, ['message' => 'hello world!']);
129
130
                // get instance shortcut
131
                $mapper = $this->get(Mapper::class);
132
                $mapper->getRepository('note')->truncate();
133
134
                // find shortcut
135
                $test->assertCount(0, $this->find('note'));
136
                $note = $this->create('note', ['message' => 'hello world']);
137
                $test->assertCount(1, $this->find('note'));
138
                // find one shortcut
139
                $test->assertNotNull($this->findOne('note', ['id' => $note->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...
140
                $test->assertSame([$note], $this->find('note'));
141
142
                // find or create shortcut
143
                $testing = $this->findOrCreate('note', ['id' => $note->id]);
144
                $test->assertSame($note, $testing);
145
146
                $testing = $this->findOrCreate('note', ['id' => $note->id+1]);
147
                $test->assertCount(2, $this->find('note'));
148
149
                // find or fail shortcut
150
                $this->findOrFail('note', $testing->id);
151
152
                // remove shortcut
153
                $this->remove('note', ['id' => $testing->id]);
154
155
                $test->assertNull($this->findOne('note', ['id' => $testing->id]));
156
157
                $test->expectException(Exception::class);
158
                $this->findOrFail('note', $testing->id);
159
            }
160
        };
161
162
        $job->run($this);
163
164
        ob_start();
165
        var_dump($job);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($job); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
166
        $contents = ob_get_clean();
167
168
        $this->assertNotContains("app", $contents);
169
    }
170
}
171