1 | <?php |
||
29 | abstract class BaseTest extends TestCase |
||
30 | { |
||
31 | // tests configuration |
||
32 | public static $config; |
||
33 | |||
34 | // currently active driver |
||
35 | public const DRIVER = null; |
||
36 | |||
37 | // cross test driver cache |
||
38 | public static $driverCache = []; |
||
39 | |||
40 | protected static $lastORM; |
||
41 | |||
42 | /** @var Driver */ |
||
43 | protected $driver; |
||
44 | |||
45 | /** @var DatabaseManager */ |
||
46 | protected $dbal; |
||
47 | |||
48 | /** @var ORM */ |
||
49 | protected $orm; |
||
50 | |||
51 | /** @var TestLogger */ |
||
52 | protected $logger; |
||
53 | |||
54 | /** @var ClassesInterface */ |
||
55 | protected $locator; |
||
56 | |||
57 | /** |
||
58 | * Init all we need. |
||
59 | */ |
||
60 | public function setUp() |
||
61 | { |
||
62 | parent::setUp(); |
||
63 | |||
64 | $this->dbal = new DatabaseManager(new DatabaseConfig(['default' => 'default'])); |
||
65 | $this->dbal->addDatabase(new Database( |
||
66 | 'default', |
||
67 | '', |
||
68 | $this->getDriver() |
||
69 | )); |
||
70 | |||
71 | $this->dbal->addDatabase(new Database( |
||
72 | 'secondary', |
||
73 | 'secondary_', |
||
74 | $this->getDriver() |
||
75 | )); |
||
76 | |||
77 | $this->logger = new TestLogger(); |
||
78 | $this->getDriver()->setLogger($this->logger); |
||
79 | |||
80 | if (self::$config['debug']) { |
||
81 | $this->logger->display(); |
||
82 | } |
||
83 | |||
84 | $this->logger = new TestLogger(); |
||
85 | $this->getDriver()->setLogger($this->logger); |
||
86 | |||
87 | if (self::$config['debug']) { |
||
88 | $this->logger->display(); |
||
89 | } |
||
90 | |||
91 | $this->orm = new ORM(new Factory( |
||
92 | $this->dbal, |
||
93 | RelationConfig::getDefault() |
||
94 | )); |
||
95 | |||
96 | $tokenizer = new Tokenizer(new TokenizerConfig([ |
||
97 | 'directories' => [__DIR__ . '/Fixtures'], |
||
98 | 'exclude' => [], |
||
99 | ])); |
||
100 | |||
101 | $this->locator = $tokenizer->classLocator(); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Cleanup. |
||
106 | */ |
||
107 | public function tearDown() |
||
108 | { |
||
109 | $this->disableProfiling(); |
||
110 | $this->dropDatabase($this->dbal->database('default')); |
||
111 | $this->orm = null; |
||
112 | $this->dbal = null; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Calculates missing parameters for typecasting. |
||
117 | * |
||
118 | * @param SchemaInterface $schema |
||
119 | * |
||
120 | * @return ORM|\Cycle\ORM\ORMInterface |
||
121 | */ |
||
122 | public function withSchema(SchemaInterface $schema) |
||
123 | { |
||
124 | $this->orm = $this->orm->withSchema($schema); |
||
125 | |||
126 | return $this->orm; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return Driver |
||
131 | */ |
||
132 | public function getDriver(): Driver |
||
133 | { |
||
134 | if (isset(static::$driverCache[static::DRIVER])) { |
||
135 | return static::$driverCache[static::DRIVER]; |
||
136 | } |
||
137 | |||
138 | $config = self::$config[static::DRIVER]; |
||
139 | if (!isset($this->driver)) { |
||
140 | $class = $config['driver']; |
||
141 | |||
142 | $this->driver = new $class([ |
||
143 | 'connection' => $config['conn'], |
||
144 | 'username' => $config['user'], |
||
145 | 'password' => $config['pass'], |
||
146 | 'options' => [] |
||
147 | ]); |
||
148 | } |
||
149 | |||
150 | $this->driver->setProfiling(true); |
||
151 | |||
152 | return static::$driverCache[static::DRIVER] = $this->driver; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @return Database |
||
157 | */ |
||
158 | protected function getDatabase(): Database |
||
159 | { |
||
160 | return $this->dbal->database('default'); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param Database|null $database |
||
165 | */ |
||
166 | protected function dropDatabase(Database $database = null) |
||
167 | { |
||
168 | if (empty($database)) { |
||
169 | return; |
||
170 | } |
||
171 | |||
172 | foreach ($database->getTables() as $table) { |
||
173 | $schema = $table->getSchema(); |
||
174 | |||
175 | foreach ($schema->getForeignKeys() as $foreign) { |
||
176 | $schema->dropForeignKey($foreign->getColumn()); |
||
177 | } |
||
178 | |||
179 | $schema->save(Handler::DROP_FOREIGN_KEYS); |
||
180 | } |
||
181 | |||
182 | foreach ($database->getTables() as $table) { |
||
183 | $schema = $table->getSchema(); |
||
184 | $schema->declareDropped(); |
||
185 | $schema->save(); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * For debug purposes only. |
||
191 | */ |
||
192 | protected function enableProfiling() |
||
193 | { |
||
194 | if (!is_null($this->logger)) { |
||
195 | $this->logger->display(); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * For debug purposes only. |
||
201 | */ |
||
202 | protected function disableProfiling() |
||
203 | { |
||
204 | if (!is_null($this->logger)) { |
||
205 | $this->logger->hide(); |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
305 | } |