Passed
Pull Request — master (#277)
by Kirill
03:11
created

SchemaTest::testSyncDebug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 21
rs 9.7998
c 0
b 0
f 0
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\Boot\FinalizerInterface;
19
use Spiral\Console\ConsoleDispatcher;
20
use Spiral\Core\CoreInterface;
21
use Spiral\App\Controller\SelectController;
22
use Spiral\App\User\User;
23
use Spiral\App\User\UserRepository;
24
use Spiral\Tests\Framework\ConsoleTest;
25
use Symfony\Component\Console\Input\ArrayInput;
26
use Symfony\Component\Console\Output\BufferedOutput;
27
28
class SchemaTest extends ConsoleTest
29
{
30
    public function setUp(): void
31
    {
32
        $this->app = $this->makeApp([
33
            'SAFE_MIGRATIONS' => true
34
        ]);
35
    }
36
37
    public function testGetSchema(): void
38
    {
39
        $app = $this->app;
40
        $app->console()->run('cycle');
41
42
        $schema = $app->get(SchemaInterface::class);
43
        $this->assertSame(User::class, $schema->define('user', Schema::ENTITY));
44
    }
45
46
    public function testMigrate(): void
47
    {
48
        $app = $this->app;
49
        $this->runCommandDebug('migrate:init', ['-vvv' => true]);
50
51
        $output = $this->runCommandDebug('cycle:migrate', ['-r' => true]);
52
        $this->assertStringContainsString('default.users', $output);
53
54
        $u = new User('Antony');
55
        $app->get(Transaction::class)->persist($u)->run();
56
57
        $this->assertSame(1, $u->id);
58
    }
59
60
    public function testSync(): void
61
    {
62
        $output = $this->runCommand('cycle:sync');
63
        $this->assertStringContainsString('default.users', $output);
64
65
        $u = new User('Antony');
66
        $this->app->get(Transaction::class)->persist($u)->run();
67
68
        $this->assertSame(1, $u->id);
69
    }
70
71
    public function testSyncDebug(): void
72
    {
73
        $output = new BufferedOutput();
74
        $output->setVerbosity(BufferedOutput::VERBOSITY_VERY_VERBOSE);
75
76
        $app = $this->app;
77
        $app->get(ConsoleDispatcher::class)->serve(new ArrayInput([
78
            'command' => 'cycle:sync'
79
        ]), $output);
80
81
        $this->assertStringContainsString('Begin transaction', $out = $output->fetch());
82
83
        $this->assertStringContainsString('default.users', $out);
84
        $this->assertStringContainsString('create table', $out);
85
        $this->assertStringContainsString('add column', $out);
86
        $this->assertStringContainsString('add index', $out);
87
88
        $u = new User('Antony');
89
        $app->get(Transaction::class)->persist($u)->run();
90
91
        $this->assertSame(1, $u->id);
92
    }
93
94
    public function testGetRepository(): void
95
    {
96
        $app = $this->app;
97
        $this->runCommandDebug('cycle:sync');
98
99
        $u = new User('Antony');
100
        $app->get(Transaction::class)->persist($u)->run();
101
        $this->assertSame(1, $u->id);
102
103
        /** @var UserRepository $r */
104
        $r = $app->get(UserRepository::class);
105
106
        $this->assertInstanceOf(UserRepository::class, $r);
107
        $this->assertSame($u, $r->findOne());
108
    }
109
110
    public function testInjectedSelect(): void
111
    {
112
        $app = $this->app;
113
        $this->runCommandDebug('cycle:sync');
114
115
        $u = new User('Antony');
116
        $app->get(Transaction::class)->persist($u)->run();
117
        $this->assertSame(1, $u->id);
118
119
        /** @var CoreInterface $c */
120
        $c = $app->get(CoreInterface::class);
121
122
        $this->assertSame(1, $c->callAction(
123
            SelectController::class,
124
            'select'
125
        ));
126
    }
127
128
    public function testHeapReset(): void
129
    {
130
        $app = $this->app;
131
        $this->runCommandDebug('cycle:sync');
132
133
        $u = new User('Antony');
134
        $app->get(Transaction::class)->persist($u)->run();
135
        $this->assertSame(1, $u->id);
136
137
        /** @var ORMInterface $orm */
138
        $orm = $app->get(ORMInterface::class);
139
140
        $heap = $orm->getHeap();
141
        $this->assertTrue($heap->has($u));
142
143
        $this->app->get(FinalizerInterface::class)->finalize();
144
145
        $this->assertFalse($heap->has($u));
146
    }
147
}
148