Passed
Pull Request — master (#298)
by Valentin
04:51
created

SchemaTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 81
dl 0
loc 161
rs 10
c 0
b 0
f 0
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testInjectedSelect() 0 15 1
A testRegenerateEmptySchema() 0 29 1
A testMigrate() 0 12 1
A testSync() 0 9 1
A setUp() 0 4 1
A testRegenerateSchema() 0 9 1
A testSyncDebug() 0 21 1
A testGetSchema() 0 8 1
A testGetRepository() 0 14 1
A testHeapReset() 0 18 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Framework\Cycle;
13
14
use Cycle\ORM\ORMInterface;
15
use Cycle\ORM\Schema;
16
use Cycle\ORM\SchemaInterface;
17
use Cycle\ORM\Transaction;
18
use Spiral\App\Controller\SelectController;
19
use Spiral\App\TestApp;
20
use Spiral\App\User\User;
21
use Spiral\App\User\UserRepository;
22
use Spiral\Boot\Environment;
23
use Spiral\Boot\FinalizerInterface;
24
use Spiral\Boot\MemoryInterface;
25
use Spiral\Console\ConsoleDispatcher;
26
use Spiral\Core\CoreInterface;
27
use Spiral\Tests\Framework\ConsoleTest;
28
use Symfony\Component\Console\Input\ArrayInput;
29
use Symfony\Component\Console\Output\BufferedOutput;
30
31
class SchemaTest extends ConsoleTest
32
{
33
    public function setUp(): void
34
    {
35
        $this->app = $this->makeApp([
36
            'SAFE_MIGRATIONS' => true
37
        ]);
38
    }
39
40
    public function testGetSchema(): void
41
    {
42
        $app = $this->app;
43
        $app->console()->run('cycle');
44
45
        /** @var SchemaInterface $schema */
46
        $schema = $app->get(SchemaInterface::class);
47
        $this->assertSame(User::class, $schema->define('user', Schema::ENTITY));
48
    }
49
50
    public function testRegenerateSchema(): void
51
    {
52
        $app = $this->app;
53
54
        /** @var SchemaInterface $schema */
55
        $schema = $app->get(SchemaInterface::class);
56
57
        $this->assertTrue($schema->defines('user'));
58
        $this->assertSame(User::class, $schema->define('user', Schema::ENTITY));
59
    }
60
61
    public function testRegenerateEmptySchema(): void
62
    {
63
        $app = TestApp::init(
64
            [
65
                'root'    => __DIR__ . '/../../..',
66
                'app'     => __DIR__ . '/../../emptyApp',
67
                'runtime' => sys_get_temp_dir() . '/spiral',
68
                'cache'   => sys_get_temp_dir() . '/spiral',
69
            ],
70
            new Environment(['SAFE_MIGRATIONS' => true]),
71
            false
72
        );
73
74
        $app->getContainer()->bind(
75
            MemoryInterface::class,
76
            new TrackedMemory($app->get(MemoryInterface::class))
0 ignored issues
show
Bug introduced by
It seems like $app->get(Spiral\Boot\MemoryInterface::class) can also be of type null; however, parameter $memory of Spiral\Tests\Framework\C...edMemory::__construct() does only seem to accept Spiral\Boot\Memory, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            new TrackedMemory(/** @scrutinizer ignore-type */ $app->get(MemoryInterface::class))
Loading history...
Bug introduced by
new Spiral\Tests\Framewo...emoryInterface::class)) of type Spiral\Tests\Framework\Cycle\TrackedMemory is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            /** @scrutinizer ignore-type */ new TrackedMemory($app->get(MemoryInterface::class))
Loading history...
77
        );
78
        /** @var TrackedMemory $memory */
79
        $memory = $app->get(MemoryInterface::class);
80
81
        //Emulate multiple re-generations for empty schemas
82
        $app->get(SchemaInterface::class);
83
        $app->get(SchemaInterface::class);
84
        $app->get(SchemaInterface::class);
85
86
        /** @var SchemaInterface $schema */
87
        $schema = $app->get(SchemaInterface::class);
88
        $this->assertSame(1, $memory->saveCount);
89
        $this->assertFalse($schema->defines('user'));
90
    }
91
92
    public function testMigrate(): void
93
    {
94
        $app = $this->app;
95
        $this->runCommandDebug('migrate:init', ['-vvv' => true]);
96
97
        $output = $this->runCommandDebug('cycle:migrate', ['-r' => true]);
98
        $this->assertStringContainsString('default.users', $output);
99
100
        $u = new User('Antony');
101
        $app->get(Transaction::class)->persist($u)->run();
102
103
        $this->assertSame(1, $u->id);
104
    }
105
106
    public function testSync(): void
107
    {
108
        $output = $this->runCommand('cycle:sync');
109
        $this->assertStringContainsString('default.users', $output);
110
111
        $u = new User('Antony');
112
        $this->app->get(Transaction::class)->persist($u)->run();
113
114
        $this->assertSame(1, $u->id);
115
    }
116
117
    public function testSyncDebug(): void
118
    {
119
        $output = new BufferedOutput();
120
        $output->setVerbosity(BufferedOutput::VERBOSITY_VERY_VERBOSE);
121
122
        $app = $this->app;
123
        $app->get(ConsoleDispatcher::class)->serve(new ArrayInput([
124
            'command' => 'cycle:sync'
125
        ]), $output);
126
127
        $this->assertStringContainsString('Begin transaction', $out = $output->fetch());
128
129
        $this->assertStringContainsString('default.users', $out);
130
        $this->assertStringContainsString('create table', $out);
131
        $this->assertStringContainsString('add column', $out);
132
        $this->assertStringContainsString('add index', $out);
133
134
        $u = new User('Antony');
135
        $app->get(Transaction::class)->persist($u)->run();
136
137
        $this->assertSame(1, $u->id);
138
    }
139
140
    public function testGetRepository(): void
141
    {
142
        $app = $this->app;
143
        $this->runCommandDebug('cycle:sync');
144
145
        $u = new User('Antony');
146
        $app->get(Transaction::class)->persist($u)->run();
147
        $this->assertSame(1, $u->id);
148
149
        /** @var UserRepository $r */
150
        $r = $app->get(UserRepository::class);
151
152
        $this->assertInstanceOf(UserRepository::class, $r);
153
        $this->assertSame($u, $r->findOne());
154
    }
155
156
    public function testInjectedSelect(): void
157
    {
158
        $app = $this->app;
159
        $this->runCommandDebug('cycle:sync');
160
161
        $u = new User('Antony');
162
        $app->get(Transaction::class)->persist($u)->run();
163
        $this->assertSame(1, $u->id);
164
165
        /** @var CoreInterface $c */
166
        $c = $app->get(CoreInterface::class);
167
168
        $this->assertSame(1, $c->callAction(
169
            SelectController::class,
170
            'select'
171
        ));
172
    }
173
174
    public function testHeapReset(): void
175
    {
176
        $app = $this->app;
177
        $this->runCommandDebug('cycle:sync');
178
179
        $u = new User('Antony');
180
        $app->get(Transaction::class)->persist($u)->run();
181
        $this->assertSame(1, $u->id);
182
183
        /** @var ORMInterface $orm */
184
        $orm = $app->get(ORMInterface::class);
185
186
        $heap = $orm->getHeap();
187
        $this->assertTrue($heap->has($u));
188
189
        $this->app->get(FinalizerInterface::class)->finalize();
190
191
        $this->assertFalse($heap->has($u));
192
    }
193
}
194