|
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\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\User\User; |
|
20
|
|
|
use Spiral\App\User\UserRepository; |
|
21
|
|
|
use Spiral\Boot\FinalizerInterface; |
|
22
|
|
|
use Spiral\Console\ConsoleDispatcher; |
|
23
|
|
|
use Spiral\Core\CoreInterface; |
|
24
|
|
|
use Spiral\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->assertContains('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->assertContains('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->assertContains('Begin transaction', $out = $output->fetch()); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertContains('default.users', $out); |
|
84
|
|
|
$this->assertContains('create table', $out); |
|
85
|
|
|
$this->assertContains('add column', $out); |
|
86
|
|
|
$this->assertContains('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
|
|
|
|