|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Db; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Connection; |
|
7
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Prophecy\Argument; |
|
10
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
11
|
|
|
use Shlinkio\Shlink\CLI\Command\Db\CreateDatabaseCommand; |
|
12
|
|
|
use Symfony\Component\Console\Application; |
|
13
|
|
|
use Symfony\Component\Console\Helper\ProcessHelper; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
16
|
|
|
use Symfony\Component\Lock\Factory as Locker; |
|
17
|
|
|
use Symfony\Component\Lock\LockInterface; |
|
18
|
|
|
use Symfony\Component\Process\PhpExecutableFinder; |
|
19
|
|
|
|
|
20
|
|
|
class CreateDatabaseCommandTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var CommandTester */ |
|
23
|
|
|
private $commandTester; |
|
24
|
|
|
/** @var ObjectProphecy */ |
|
25
|
|
|
private $processHelper; |
|
26
|
|
|
/** @var ObjectProphecy */ |
|
27
|
|
|
private $regularConn; |
|
28
|
|
|
/** @var ObjectProphecy */ |
|
29
|
|
|
private $noDbNameConn; |
|
30
|
|
|
/** @var ObjectProphecy */ |
|
31
|
|
|
private $schemaManager; |
|
32
|
|
|
|
|
33
|
|
|
public function setUp(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$locker = $this->prophesize(Locker::class); |
|
36
|
|
|
$lock = $this->prophesize(LockInterface::class); |
|
37
|
|
|
$lock->acquire(Argument::any())->willReturn(true); |
|
38
|
|
|
$lock->release()->will(function () { |
|
39
|
|
|
}); |
|
40
|
|
|
$locker->createLock(Argument::cetera())->willReturn($lock->reveal()); |
|
41
|
|
|
|
|
42
|
|
|
$phpExecutableFinder = $this->prophesize(PhpExecutableFinder::class); |
|
43
|
|
|
$phpExecutableFinder->find(false)->willReturn('/usr/local/bin/php'); |
|
44
|
|
|
|
|
45
|
|
|
$this->processHelper = $this->prophesize(ProcessHelper::class); |
|
46
|
|
|
$this->schemaManager = $this->prophesize(AbstractSchemaManager::class); |
|
47
|
|
|
|
|
48
|
|
|
$this->regularConn = $this->prophesize(Connection::class); |
|
49
|
|
|
$this->regularConn->getSchemaManager()->willReturn($this->schemaManager->reveal()); |
|
50
|
|
|
$this->noDbNameConn = $this->prophesize(Connection::class); |
|
51
|
|
|
$this->noDbNameConn->getSchemaManager()->willReturn($this->schemaManager->reveal()); |
|
52
|
|
|
|
|
53
|
|
|
$command = new CreateDatabaseCommand( |
|
54
|
|
|
$locker->reveal(), |
|
55
|
|
|
$this->processHelper->reveal(), |
|
56
|
|
|
$phpExecutableFinder->reveal(), |
|
57
|
|
|
$this->regularConn->reveal(), |
|
58
|
|
|
$this->noDbNameConn->reveal() |
|
59
|
|
|
); |
|
60
|
|
|
$app = new Application(); |
|
61
|
|
|
$app->add($command); |
|
62
|
|
|
|
|
63
|
|
|
$this->commandTester = new CommandTester($command); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** @test */ |
|
67
|
|
|
public function successMessageIsPrintedIfDatabaseAlreadyExists(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$shlinkDatabase = 'shlink_database'; |
|
70
|
|
|
$getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase); |
|
71
|
|
|
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', $shlinkDatabase, 'bar']); |
|
72
|
|
|
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function () { |
|
73
|
|
|
}); |
|
74
|
|
|
$listTables = $this->schemaManager->listTableNames()->willReturn(['foo_table', 'bar_table']); |
|
75
|
|
|
|
|
76
|
|
|
$this->commandTester->execute([]); |
|
77
|
|
|
$output = $this->commandTester->getDisplay(); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertStringContainsString('Database already exists. Run "db:migrate" command', $output); |
|
80
|
|
|
$getDatabase->shouldHaveBeenCalledOnce(); |
|
81
|
|
|
$listDatabases->shouldHaveBeenCalledOnce(); |
|
82
|
|
|
$createDatabase->shouldNotHaveBeenCalled(); |
|
83
|
|
|
$listTables->shouldHaveBeenCalledOnce(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** @test */ |
|
87
|
|
|
public function databaseIsCreatedIfItDoesNotExist(): void |
|
88
|
|
|
{ |
|
89
|
|
|
$shlinkDatabase = 'shlink_database'; |
|
90
|
|
|
$getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase); |
|
91
|
|
|
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', 'bar']); |
|
92
|
|
|
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function () { |
|
93
|
|
|
}); |
|
94
|
|
|
$listTables = $this->schemaManager->listTableNames()->willReturn(['foo_table', 'bar_table']); |
|
95
|
|
|
|
|
96
|
|
|
$this->commandTester->execute([]); |
|
97
|
|
|
|
|
98
|
|
|
$getDatabase->shouldHaveBeenCalledOnce(); |
|
99
|
|
|
$listDatabases->shouldHaveBeenCalledOnce(); |
|
100
|
|
|
$createDatabase->shouldHaveBeenCalledOnce(); |
|
101
|
|
|
$listTables->shouldHaveBeenCalledOnce(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** @test */ |
|
105
|
|
|
public function tablesAreCreatedIfDatabaseIsEMpty(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$shlinkDatabase = 'shlink_database'; |
|
108
|
|
|
$getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase); |
|
109
|
|
|
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', $shlinkDatabase, 'bar']); |
|
110
|
|
|
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function () { |
|
111
|
|
|
}); |
|
112
|
|
|
$listTables = $this->schemaManager->listTableNames()->willReturn([]); |
|
113
|
|
|
$runCommand = $this->processHelper->run(Argument::type(OutputInterface::class), [ |
|
114
|
|
|
'/usr/local/bin/php', |
|
115
|
|
|
CreateDatabaseCommand::DOCTRINE_HELPER_SCRIPT, |
|
116
|
|
|
CreateDatabaseCommand::DOCTRINE_HELPER_COMMAND, |
|
117
|
|
|
]); |
|
118
|
|
|
|
|
119
|
|
|
$this->commandTester->execute([]); |
|
120
|
|
|
$output = $this->commandTester->getDisplay(); |
|
121
|
|
|
|
|
122
|
|
|
$this->assertStringContainsString('Creating database tables...', $output); |
|
123
|
|
|
$this->assertStringContainsString('Database properly created!', $output); |
|
124
|
|
|
$getDatabase->shouldHaveBeenCalledOnce(); |
|
125
|
|
|
$listDatabases->shouldHaveBeenCalledOnce(); |
|
126
|
|
|
$createDatabase->shouldNotHaveBeenCalled(); |
|
127
|
|
|
$listTables->shouldHaveBeenCalledOnce(); |
|
128
|
|
|
$runCommand->shouldHaveBeenCalledOnce(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|