-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBaseTestCase.php
More file actions
171 lines (140 loc) · 4.34 KB
/
BaseTestCase.php
File metadata and controls
171 lines (140 loc) · 4.34 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
declare(strict_types=1);
namespace Cycle\Annotated\Tests\Functional\Driver\Common;
use Cycle\Annotated\Tests\Fixtures\Fixtures1\TestLogger;
use Cycle\ORM\Config\RelationConfig;
use Cycle\ORM\Factory;
use Cycle\ORM\ORM;
use Cycle\ORM\Schema;
use PHPUnit\Framework\TestCase;
use Spiral\Attributes\AnnotationReader;
use Spiral\Attributes\AttributeReader;
use Spiral\Attributes\Composite\SelectiveReader;
use Cycle\Database\Config\DatabaseConfig;
use Cycle\Database\Database;
use Cycle\Database\DatabaseManager;
use Cycle\Database\Driver\Driver;
use Cycle\Database\Driver\Handler;
use Spiral\Tokenizer\ClassesInterface;
use Spiral\Tokenizer\Config\TokenizerConfig;
use Spiral\Tokenizer\Tokenizer;
abstract class BaseTestCase extends TestCase
{
// currently active driver
public const DRIVER = null;
// tests configuration
public static array $config;
// cross test driver cache
public static array $driverCache = [];
protected Driver $driver;
protected DatabaseManager $dbal;
protected ORM $orm;
protected TestLogger $logger;
protected ClassesInterface $locator;
/**
* Init all we need.
*/
public function setUp(): void
{
parent::setUp();
$this->dbal = new DatabaseManager(new DatabaseConfig(['default' => 'default']));
$this->dbal->addDatabase(new Database(
'default',
'',
$this->getDriver()
));
$this->dbal->addDatabase(new Database(
'secondary',
'secondary_',
$this->getDriver()
));
$this->logger = new TestLogger();
$this->getDriver()->setLogger($this->logger);
if (self::$config['debug']) {
$this->logger->display();
}
$this->logger = new TestLogger();
$this->getDriver()->setLogger($this->logger);
if (self::$config['debug']) {
$this->logger->display();
}
$this->orm = new ORM(new Factory(
$this->dbal,
RelationConfig::getDefault()
), new Schema([]));
$tokenizer = new Tokenizer(new TokenizerConfig([
'directories' => [__DIR__ . '/../../../Fixtures/Fixtures1'],
'exclude' => [],
]));
$this->locator = $tokenizer->classLocator();
}
/**
* Cleanup.
*/
public function tearDown(): void
{
$this->disableProfiling();
$this->dropDatabase($this->dbal->database('default'));
}
/**
* @return Driver
*/
public function getDriver(): Driver
{
if (isset(static::$driverCache[static::DRIVER])) {
return static::$driverCache[static::DRIVER];
}
$config = self::$config[static::DRIVER];
if (!isset($this->driver)) {
$this->driver = $config->driver::create($config);
}
$this->driver->setProfiling(true);
return static::$driverCache[static::DRIVER] = $this->driver;
}
public static function singularReadersProvider(): \Traversable
{
yield 'Annotation reader' => [new AnnotationReader()];
yield 'Attribute reader' => [new AttributeReader()];
}
public static function allReadersProvider(): \Traversable
{
yield from static::singularReadersProvider();
yield 'Selective reader' => [new SelectiveReader([new AttributeReader(), new AnnotationReader()])];
}
protected function getDatabase(): Database
{
return $this->dbal->database('default');
}
protected function dropDatabase(Database $database = null): void
{
if (empty($database)) {
return;
}
foreach ($database->getTables() as $table) {
$schema = $table->getSchema();
foreach ($schema->getForeignKeys() as $foreign) {
$schema->dropForeignKey($foreign->getColumns());
}
$schema->save(Handler::DROP_FOREIGN_KEYS);
}
foreach ($database->getTables() as $table) {
$schema = $table->getSchema();
$schema->declareDropped();
$schema->save();
}
}
/**
* For debug purposes only.
*/
protected function enableProfiling(): void
{
$this->logger->display();
}
/**
* For debug purposes only.
*/
protected function disableProfiling(): void
{
$this->logger->hide();
}
}