Completed
Push — master ( 2705ab...006cbd )
by Dmitry
04:32
created

TarantoolTest::testJobShortcuts()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 33
rs 8.8571
cc 1
eloc 20
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B TarantoolTest.php$0 ➔ run() 0 26 1
1
<?php
2
3
namespace Test;
4
5
use Basis\Filesystem;
6
use Basis\Job;
7
use Exception;
8
use ReflectionClass;
9
use Repository\Note;
10
use Tarantool\Mapper\Bootstrap;
11
use Tarantool\Mapper\Mapper;
12
13
class TarantoolTest extends TestSuite
14
{
15
    public function setup()
16
    {
17
        parent::setup();
18
        $this->app->dispatch('tarantool.clear');
19
20
        parent::setup();
21
22
        $fs = $this->app->get(Filesystem::class);
23
        $classes = $fs->listClasses('Migration');
24
25
        $dirs = [];
26
        foreach ($classes as $class) {
27
            $filename = $fs->getPath('php/'.str_replace('\\', '/', $class).'.php');
28
            unlink($filename);
29
            $dirs[dirname($filename)] = true;
30
        }
31
        foreach ($dirs as $dir => $_) {
32
            rmdir($dir);
33
        }
34
35
        $this->app->dispatch('tarantool.migrate');
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
                $mapper = $this->get(Mapper::class);
127
                $mapper->getRepository('note')->truncate();
128
129
                $test->assertCount(0, $this->find('note'));
130
                $note = $this->create('note', ['message' => 'hello world']);
131
                $test->assertCount(1, $this->find('note'));
132
                $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...
133
                $test->assertSame([$note], $this->find('note'));
134
135
                $testing = $this->findOrCreate('note', ['id' => $note->id]);
136
                $test->assertSame($note, $testing);
137
138
                $testing = $this->findOrCreate('note', ['id' => $note->id+1]);
139
                $test->assertCount(2, $this->find('note'));
140
141
                $this->findOrFail('note', $testing->id);
142
143
                $this->remove('note', ['id' => $testing->id]);
144
145
                $test->assertNull($this->findOne('note', ['id' => $testing->id]));
146
147
                $test->expectException(Exception::class);
148
                $this->findOrFail('note', $testing->id);
149
            }
150
        };
151
152
        $job->run($this);
153
    }
154
}
155