-
-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathCreateDatabaseDoctrineTest.php
More file actions
84 lines (66 loc) · 2.76 KB
/
CreateDatabaseDoctrineTest.php
File metadata and controls
84 lines (66 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command;
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Tests\Polyfill\SymfonyApp;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\SchemaManagerFactory;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Container;
use function array_merge;
use function sys_get_temp_dir;
use function unlink;
/** @psalm-import-type Params from DriverManager */
class CreateDatabaseDoctrineTest extends TestCase
{
public function tearDown(): void
{
@unlink(sys_get_temp_dir() . '/test');
}
public function testExecute(): void
{
$connectionName = 'default';
$dbName = 'test';
$params = [
'path' => sys_get_temp_dir() . '/' . $dbName,
'driver' => 'pdo_sqlite',
];
$container = $this->getMockContainer($connectionName, $params);
$application = new SymfonyApp();
$application->addCommand(new CreateDatabaseDoctrineCommand($container->get('doctrine')));
$command = $application->find('doctrine:database:create');
$commandTester = new CommandTester($command);
$commandTester->execute(
array_merge(['command' => $command->getName()]),
);
$this->assertStringContainsString(
'Created database ' . sys_get_temp_dir() . '/' . $dbName . ' for connection named ' . $connectionName,
$commandTester->getDisplay(),
);
}
/**
* @param mixed[]|null $params Connection parameters
* @psalm-param Params $params
*
* @return Stub&Container
*/
private function getMockContainer(string $connectionName, array|null $params = null): Stub
{
// Mock the container and everything you'll need here
$mockDoctrine = $this->createStub(ManagerRegistry::class);
$mockDoctrine->method('getDefaultConnectionName')->willReturn($connectionName);
$config = (new Configuration())->setSchemaManagerFactory($this->createStub(SchemaManagerFactory::class));
$mockConnection = $this->createStub(Connection::class);
$mockConnection->method('getConfiguration')->willReturn($config);
$mockConnection->method('getParams')->willReturn($params);
$mockDoctrine->method('getConnection')->willReturn($mockConnection);
$mockContainer = $this->createStub(Container::class);
$mockContainer->method('get')->willReturnMap([['doctrine', $mockDoctrine]]);
return $mockContainer;
}
}