1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Spiral\Framework\Cycle; |
11
|
|
|
|
12
|
|
|
use Cycle\ORM\ORMInterface; |
13
|
|
|
use Cycle\ORM\Schema; |
14
|
|
|
use Cycle\ORM\SchemaInterface; |
15
|
|
|
use Cycle\ORM\Transaction; |
16
|
|
|
use Spiral\App\Controller\SelectController; |
17
|
|
|
use Spiral\App\User\User; |
18
|
|
|
use Spiral\App\User\UserRepository; |
19
|
|
|
use Spiral\Boot\FinalizerInterface; |
20
|
|
|
use Spiral\Console\ConsoleDispatcher; |
21
|
|
|
use Spiral\Core\CoreInterface; |
22
|
|
|
use Spiral\Framework\ConsoleTest; |
23
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
24
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
25
|
|
|
|
26
|
|
|
class SchemaTest extends ConsoleTest |
27
|
|
|
{ |
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
$this->app->getEnvironment()->set('SAFE_MIGRATIONS', true); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testGetSchema() |
36
|
|
|
{ |
37
|
|
|
$app = $this->app; |
38
|
|
|
$app->console()->run('cycle'); |
39
|
|
|
|
40
|
|
|
$schema = $app->get(SchemaInterface::class); |
41
|
|
|
$this->assertSame(User::class, $schema->define('user', Schema::ENTITY)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testMigrate() |
45
|
|
|
{ |
46
|
|
|
$app = $this->app; |
47
|
|
|
$this->runCommandDebug('migrate:init', ['-vvv' => true]); |
48
|
|
|
|
49
|
|
|
$output = $this->runCommandDebug('cycle:migrate', ['-r' => true]); |
50
|
|
|
$this->assertContains('default.users', $output); |
51
|
|
|
|
52
|
|
|
$u = new User('Antony'); |
53
|
|
|
$app->get(Transaction::class)->persist($u)->run(); |
54
|
|
|
|
55
|
|
|
$this->assertSame(1, $u->id); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testSync() |
59
|
|
|
{ |
60
|
|
|
$app = $this->app; |
61
|
|
|
$output = $app->console()->run('cycle:sync'); |
62
|
|
|
$this->assertContains('default.users', $output->getOutput()->fetch()); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$u = new User('Antony'); |
65
|
|
|
$app->get(Transaction::class)->persist($u)->run(); |
66
|
|
|
|
67
|
|
|
$this->assertSame(1, $u->id); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testSyncDebug() |
71
|
|
|
{ |
72
|
|
|
$output = new BufferedOutput(); |
73
|
|
|
$output->setVerbosity(BufferedOutput::VERBOSITY_VERY_VERBOSE); |
74
|
|
|
|
75
|
|
|
$app = $this->app; |
76
|
|
|
$app->get(ConsoleDispatcher::class)->serve(new ArrayInput([ |
77
|
|
|
'command' => 'cycle:sync' |
78
|
|
|
]), $output); |
79
|
|
|
|
80
|
|
|
$this->assertContains('Begin transaction', $out = $output->fetch()); |
81
|
|
|
|
82
|
|
|
$this->assertContains('default.users', $out); |
83
|
|
|
$this->assertContains('create table', $out); |
84
|
|
|
$this->assertContains('add column', $out); |
85
|
|
|
$this->assertContains('add index', $out); |
86
|
|
|
|
87
|
|
|
$u = new User('Antony'); |
88
|
|
|
$app->get(Transaction::class)->persist($u)->run(); |
89
|
|
|
|
90
|
|
|
$this->assertSame(1, $u->id); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @expectedException \Cycle\ORM\Exception\ORMException |
95
|
|
|
*/ |
96
|
|
|
public function testSchemaMissing() |
97
|
|
|
{ |
98
|
|
|
/** @var UserRepository $r */ |
99
|
|
|
$this->app->get(UserRepository::class); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testGetRepository() |
103
|
|
|
{ |
104
|
|
|
$app = $this->app; |
105
|
|
|
$this->runCommandDebug('cycle:sync'); |
106
|
|
|
|
107
|
|
|
$u = new User('Antony'); |
108
|
|
|
$app->get(Transaction::class)->persist($u)->run(); |
109
|
|
|
$this->assertSame(1, $u->id); |
110
|
|
|
|
111
|
|
|
/** @var UserRepository $r */ |
112
|
|
|
$r = $app->get(UserRepository::class); |
113
|
|
|
$this->assertInstanceOf(UserRepository::class, $r); |
114
|
|
|
|
115
|
|
|
// todo: need bugfix |
116
|
|
|
// $this->assertSame($u, $r->findOne()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testInjectedSelect() |
120
|
|
|
{ |
121
|
|
|
$app = $this->app; |
122
|
|
|
$this->runCommandDebug('cycle:sync'); |
123
|
|
|
|
124
|
|
|
$u = new User('Antony'); |
125
|
|
|
$app->get(Transaction::class)->persist($u)->run(); |
126
|
|
|
$this->assertSame(1, $u->id); |
127
|
|
|
|
128
|
|
|
/** @var CoreInterface $c */ |
129
|
|
|
$c = $app->get(CoreInterface::class); |
130
|
|
|
// $this->assertInstanceOf(UserRepository::class, $r); |
131
|
|
|
|
132
|
|
|
$this->assertSame(1, $c->callAction( |
133
|
|
|
SelectController::class, |
134
|
|
|
'select' |
135
|
|
|
)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testHeapReset() |
139
|
|
|
{ |
140
|
|
|
$app = $this->app; |
141
|
|
|
$this->runCommandDebug('cycle:sync'); |
142
|
|
|
|
143
|
|
|
$u = new User('Antony'); |
144
|
|
|
$app->get(Transaction::class)->persist($u)->run(); |
145
|
|
|
$this->assertSame(1, $u->id); |
146
|
|
|
|
147
|
|
|
/** @var ORMInterface $orm */ |
148
|
|
|
$orm = $app->get(ORMInterface::class); |
149
|
|
|
|
150
|
|
|
$heap = $orm->getHeap(); |
151
|
|
|
$this->assertTrue($heap->has($u)); |
152
|
|
|
|
153
|
|
|
$this->app->get(FinalizerInterface::class)->finalize(); |
154
|
|
|
|
155
|
|
|
$this->assertFalse($heap->has($u)); |
156
|
|
|
} |
157
|
|
|
} |