Total Complexity | 78 |
Total Lines | 804 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like OrmFunctionalTestCase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OrmFunctionalTestCase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | abstract class OrmFunctionalTestCase extends OrmTestCase |
||
50 | { |
||
51 | /** |
||
52 | * The metadata cache shared between all functional tests. |
||
53 | * |
||
54 | * @var Cache|null |
||
55 | */ |
||
56 | private static $metadataCacheImpl = null; |
||
57 | |||
58 | /** |
||
59 | * The query cache shared between all functional tests. |
||
60 | * |
||
61 | * @var Cache|null |
||
62 | */ |
||
63 | private static $queryCacheImpl = null; |
||
64 | |||
65 | /** |
||
66 | * Shared connection when a TestCase is run alone (outside of its functional suite). |
||
67 | * |
||
68 | * @var \Doctrine\DBAL\Connection|null |
||
69 | */ |
||
70 | protected static $sharedConn; |
||
71 | |||
72 | /** @var EntityManagerInterface */ |
||
73 | protected $em; |
||
74 | |||
75 | /** @var SchemaTool */ |
||
76 | protected $schemaTool; |
||
77 | |||
78 | /** @var DebugStack */ |
||
79 | protected $sqlLoggerStack; |
||
80 | |||
81 | /** |
||
82 | * The names of the model sets used in this testcase. |
||
83 | * |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $usedModelSets = []; |
||
87 | |||
88 | /** |
||
89 | * To be configured by the test that uses result set cache |
||
90 | * |
||
91 | * @var Cache|null |
||
92 | */ |
||
93 | protected $resultCacheImpl; |
||
94 | |||
95 | /** |
||
96 | * Whether the database schema has already been created. |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | protected static $tablesCreated = []; |
||
101 | |||
102 | /** |
||
103 | * Array of entity class name to their tables that were created. |
||
104 | * |
||
105 | * @var array |
||
106 | */ |
||
107 | protected static $entityTablesCreated = []; |
||
108 | |||
109 | /** |
||
110 | * List of model sets and their classes. |
||
111 | * |
||
112 | * @var array |
||
113 | */ |
||
114 | protected static $modelSets = [ |
||
115 | 'cms' => [ |
||
116 | Models\CMS\CmsUser::class, |
||
117 | Models\CMS\CmsPhonenumber::class, |
||
118 | Models\CMS\CmsAddress::class, |
||
119 | Models\CMS\CmsEmail::class, |
||
120 | Models\CMS\CmsGroup::class, |
||
121 | Models\CMS\CmsTag::class, |
||
122 | Models\CMS\CmsArticle::class, |
||
123 | Models\CMS\CmsComment::class, |
||
124 | ], |
||
125 | 'company' => [ |
||
126 | Models\Company\CompanyPerson::class, |
||
127 | Models\Company\CompanyEmployee::class, |
||
128 | Models\Company\CompanyManager::class, |
||
129 | Models\Company\CompanyOrganization::class, |
||
130 | Models\Company\CompanyEvent::class, |
||
131 | Models\Company\CompanyAuction::class, |
||
132 | Models\Company\CompanyRaffle::class, |
||
133 | Models\Company\CompanyCar::class, |
||
134 | Models\Company\CompanyContract::class, |
||
135 | ], |
||
136 | 'ecommerce' => [ |
||
137 | Models\ECommerce\ECommerceCart::class, |
||
138 | Models\ECommerce\ECommerceCustomer::class, |
||
139 | Models\ECommerce\ECommerceProduct::class, |
||
140 | Models\ECommerce\ECommerceShipping::class, |
||
141 | Models\ECommerce\ECommerceFeature::class, |
||
142 | Models\ECommerce\ECommerceCategory::class, |
||
143 | ], |
||
144 | 'generic' => [ |
||
145 | Models\Generic\BooleanModel::class, |
||
146 | Models\Generic\DateTimeModel::class, |
||
147 | Models\Generic\DecimalModel::class, |
||
148 | Models\Generic\SerializationModel::class, |
||
149 | ], |
||
150 | 'routing' => [ |
||
151 | Models\Routing\RoutingLeg::class, |
||
152 | Models\Routing\RoutingLocation::class, |
||
153 | Models\Routing\RoutingRoute::class, |
||
154 | Models\Routing\RoutingRouteBooking::class, |
||
155 | ], |
||
156 | 'navigation' => [ |
||
157 | Models\Navigation\NavUser::class, |
||
158 | Models\Navigation\NavCountry::class, |
||
159 | Models\Navigation\NavPhotos::class, |
||
160 | Models\Navigation\NavTour::class, |
||
161 | Models\Navigation\NavPointOfInterest::class, |
||
162 | ], |
||
163 | 'directorytree' => [ |
||
164 | Models\DirectoryTree\AbstractContentItem::class, |
||
165 | Models\DirectoryTree\File::class, |
||
166 | Models\DirectoryTree\Directory::class, |
||
167 | ], |
||
168 | 'ddc117' => [ |
||
169 | Models\DDC117\DDC117Article::class, |
||
170 | Models\DDC117\DDC117Reference::class, |
||
171 | Models\DDC117\DDC117Translation::class, |
||
172 | Models\DDC117\DDC117ArticleDetails::class, |
||
173 | Models\DDC117\DDC117ApproveChanges::class, |
||
174 | Models\DDC117\DDC117Editor::class, |
||
175 | Models\DDC117\DDC117Link::class, |
||
176 | ], |
||
177 | 'ddc3699' => [ |
||
178 | Models\DDC3699\DDC3699Parent::class, |
||
179 | Models\DDC3699\DDC3699RelationOne::class, |
||
180 | Models\DDC3699\DDC3699RelationMany::class, |
||
181 | Models\DDC3699\DDC3699Child::class, |
||
182 | ], |
||
183 | 'stockexchange' => [ |
||
184 | Models\StockExchange\Bond::class, |
||
185 | Models\StockExchange\Stock::class, |
||
186 | Models\StockExchange\Market::class, |
||
187 | ], |
||
188 | 'legacy' => [ |
||
189 | Models\Legacy\LegacyUser::class, |
||
190 | Models\Legacy\LegacyUserReference::class, |
||
191 | Models\Legacy\LegacyArticle::class, |
||
192 | Models\Legacy\LegacyCar::class, |
||
193 | ], |
||
194 | 'customtype' => [ |
||
195 | Models\CustomType\CustomTypeChild::class, |
||
196 | Models\CustomType\CustomTypeParent::class, |
||
197 | Models\CustomType\CustomTypeUpperCase::class, |
||
198 | ], |
||
199 | 'compositekeyinheritance' => [ |
||
200 | Models\CompositeKeyInheritance\JoinedRootClass::class, |
||
201 | Models\CompositeKeyInheritance\JoinedChildClass::class, |
||
202 | Models\CompositeKeyInheritance\SingleRootClass::class, |
||
203 | Models\CompositeKeyInheritance\SingleChildClass::class, |
||
204 | ], |
||
205 | 'taxi' => [ |
||
206 | Models\Taxi\PaidRide::class, |
||
207 | Models\Taxi\Ride::class, |
||
208 | Models\Taxi\Car::class, |
||
209 | Models\Taxi\Driver::class, |
||
210 | ], |
||
211 | 'cache' => [ |
||
212 | Models\Cache\Country::class, |
||
213 | Models\Cache\State::class, |
||
214 | Models\Cache\City::class, |
||
215 | Models\Cache\Traveler::class, |
||
216 | Models\Cache\TravelerProfileInfo::class, |
||
217 | Models\Cache\TravelerProfile::class, |
||
218 | Models\Cache\Travel::class, |
||
219 | Models\Cache\Attraction::class, |
||
220 | Models\Cache\Restaurant::class, |
||
221 | Models\Cache\Beach::class, |
||
222 | Models\Cache\Bar::class, |
||
223 | Models\Cache\Flight::class, |
||
224 | Models\Cache\Token::class, |
||
225 | Models\Cache\Login::class, |
||
226 | Models\Cache\Client::class, |
||
227 | Models\Cache\Person::class, |
||
228 | Models\Cache\Address::class, |
||
229 | Models\Cache\Action::class, |
||
230 | Models\Cache\ComplexAction::class, |
||
231 | Models\Cache\AttractionInfo::class, |
||
232 | Models\Cache\AttractionContactInfo::class, |
||
233 | Models\Cache\AttractionLocationInfo::class, |
||
234 | ], |
||
235 | 'tweet' => [ |
||
236 | Models\Tweet\User::class, |
||
237 | Models\Tweet\Tweet::class, |
||
238 | Models\Tweet\UserList::class, |
||
239 | ], |
||
240 | 'ddc2504' => [ |
||
241 | Models\DDC2504\DDC2504RootClass::class, |
||
242 | Models\DDC2504\DDC2504ChildClass::class, |
||
243 | Models\DDC2504\DDC2504OtherClass::class, |
||
244 | ], |
||
245 | 'ddc3346' => [ |
||
246 | Models\DDC3346\DDC3346Author::class, |
||
247 | Models\DDC3346\DDC3346Article::class, |
||
248 | ], |
||
249 | 'quote' => [ |
||
250 | Models\Quote\Address::class, |
||
251 | Models\Quote\City::class, |
||
252 | Models\Quote\FullAddress::class, |
||
253 | Models\Quote\Group::class, |
||
254 | Models\Quote\NumericEntity::class, |
||
255 | Models\Quote\Phone::class, |
||
256 | Models\Quote\User::class, |
||
257 | ], |
||
258 | 'vct_onetoone' => [ |
||
259 | Models\ValueConversionType\InversedOneToOneEntity::class, |
||
260 | Models\ValueConversionType\OwningOneToOneEntity::class, |
||
261 | ], |
||
262 | 'vct_onetoone_compositeid' => [ |
||
263 | Models\ValueConversionType\InversedOneToOneCompositeIdEntity::class, |
||
264 | Models\ValueConversionType\OwningOneToOneCompositeIdEntity::class, |
||
265 | ], |
||
266 | 'vct_onetoone_compositeid_foreignkey' => [ |
||
267 | Models\ValueConversionType\AuxiliaryEntity::class, |
||
268 | Models\ValueConversionType\InversedOneToOneCompositeIdForeignKeyEntity::class, |
||
269 | Models\ValueConversionType\OwningOneToOneCompositeIdForeignKeyEntity::class, |
||
270 | ], |
||
271 | 'vct_onetomany' => [ |
||
272 | Models\ValueConversionType\InversedOneToManyEntity::class, |
||
273 | Models\ValueConversionType\OwningManyToOneEntity::class, |
||
274 | ], |
||
275 | 'vct_onetomany_compositeid' => [ |
||
276 | Models\ValueConversionType\InversedOneToManyCompositeIdEntity::class, |
||
277 | Models\ValueConversionType\OwningManyToOneCompositeIdEntity::class, |
||
278 | ], |
||
279 | 'vct_onetomany_compositeid_foreignkey' => [ |
||
280 | Models\ValueConversionType\AuxiliaryEntity::class, |
||
281 | Models\ValueConversionType\InversedOneToManyCompositeIdForeignKeyEntity::class, |
||
282 | Models\ValueConversionType\OwningManyToOneCompositeIdForeignKeyEntity::class, |
||
283 | ], |
||
284 | 'vct_onetomany_extralazy' => [ |
||
285 | Models\ValueConversionType\InversedOneToManyExtraLazyEntity::class, |
||
286 | Models\ValueConversionType\OwningManyToOneExtraLazyEntity::class, |
||
287 | ], |
||
288 | 'vct_manytomany' => [ |
||
289 | Models\ValueConversionType\InversedManyToManyEntity::class, |
||
290 | Models\ValueConversionType\OwningManyToManyEntity::class, |
||
291 | ], |
||
292 | 'vct_manytomany_compositeid' => [ |
||
293 | Models\ValueConversionType\InversedManyToManyCompositeIdEntity::class, |
||
294 | Models\ValueConversionType\OwningManyToManyCompositeIdEntity::class, |
||
295 | ], |
||
296 | 'vct_manytomany_compositeid_foreignkey' => [ |
||
297 | Models\ValueConversionType\AuxiliaryEntity::class, |
||
298 | Models\ValueConversionType\InversedManyToManyCompositeIdForeignKeyEntity::class, |
||
299 | Models\ValueConversionType\OwningManyToManyCompositeIdForeignKeyEntity::class, |
||
300 | ], |
||
301 | 'vct_manytomany_extralazy' => [ |
||
302 | Models\ValueConversionType\InversedManyToManyExtraLazyEntity::class, |
||
303 | Models\ValueConversionType\OwningManyToManyExtraLazyEntity::class, |
||
304 | ], |
||
305 | 'geonames' => [ |
||
306 | Models\GeoNames\Country::class, |
||
307 | Models\GeoNames\Admin1::class, |
||
308 | Models\GeoNames\Admin1AlternateName::class, |
||
309 | Models\GeoNames\City::class, |
||
310 | ], |
||
311 | 'custom_id_object_type' => [ |
||
312 | Models\CustomType\CustomIdObjectTypeParent::class, |
||
313 | Models\CustomType\CustomIdObjectTypeChild::class, |
||
314 | ], |
||
315 | 'pagination' => [ |
||
316 | Models\Pagination\Company::class, |
||
317 | Models\Pagination\Logo::class, |
||
318 | Models\Pagination\Department::class, |
||
319 | Models\Pagination\User::class, |
||
320 | Models\Pagination\User1::class, |
||
321 | ], |
||
322 | 'versioned_many_to_one' => [ |
||
323 | Models\VersionedManyToOne\Category::class, |
||
324 | Models\VersionedManyToOne\Article::class, |
||
325 | ], |
||
326 | 'issue5989' => [ |
||
327 | Models\Issue5989\Issue5989Person::class, |
||
328 | Models\Issue5989\Issue5989Employee::class, |
||
329 | Models\Issue5989\Issue5989Manager::class, |
||
330 | ], |
||
331 | ]; |
||
332 | |||
333 | /** |
||
334 | * @param string $setName |
||
335 | */ |
||
336 | protected function useModelSet($setName) |
||
337 | { |
||
338 | $this->usedModelSets[$setName] = true; |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Sweeps the database tables and clears the EntityManager. |
||
343 | */ |
||
344 | protected function tearDown() : void |
||
345 | { |
||
346 | $conn = static::$sharedConn; |
||
347 | |||
348 | // In case test is skipped, tearDown is called, but no setup may have run |
||
349 | if (! $conn) { |
||
350 | return; |
||
351 | } |
||
352 | |||
353 | $platform = $conn->getDatabasePlatform(); |
||
354 | |||
355 | $this->sqlLoggerStack->enabled = false; |
||
356 | |||
357 | if (isset($this->usedModelSets['cms'])) { |
||
358 | $conn->executeUpdate('DELETE FROM cms_users_groups'); |
||
359 | $conn->executeUpdate('DELETE FROM cms_groups'); |
||
360 | $conn->executeUpdate('DELETE FROM cms_users_tags'); |
||
361 | $conn->executeUpdate('DELETE FROM cms_tags'); |
||
362 | $conn->executeUpdate('DELETE FROM cms_addresses'); |
||
363 | $conn->executeUpdate('DELETE FROM cms_phonenumbers'); |
||
364 | $conn->executeUpdate('DELETE FROM cms_comments'); |
||
365 | $conn->executeUpdate('DELETE FROM cms_articles'); |
||
366 | $conn->executeUpdate('DELETE FROM cms_users'); |
||
367 | $conn->executeUpdate('DELETE FROM cms_emails'); |
||
368 | } |
||
369 | |||
370 | if (isset($this->usedModelSets['ecommerce'])) { |
||
371 | $conn->executeUpdate('DELETE FROM ecommerce_carts_products'); |
||
372 | $conn->executeUpdate('DELETE FROM ecommerce_products_categories'); |
||
373 | $conn->executeUpdate('DELETE FROM ecommerce_products_related'); |
||
374 | $conn->executeUpdate('DELETE FROM ecommerce_carts'); |
||
375 | $conn->executeUpdate('DELETE FROM ecommerce_customers'); |
||
376 | $conn->executeUpdate('DELETE FROM ecommerce_features'); |
||
377 | $conn->executeUpdate('DELETE FROM ecommerce_products'); |
||
378 | $conn->executeUpdate('DELETE FROM ecommerce_shippings'); |
||
379 | $conn->executeUpdate('UPDATE ecommerce_categories SET parent_id = NULL'); |
||
380 | $conn->executeUpdate('DELETE FROM ecommerce_categories'); |
||
381 | } |
||
382 | |||
383 | if (isset($this->usedModelSets['company'])) { |
||
384 | $conn->executeUpdate('DELETE FROM company_contract_employees'); |
||
385 | $conn->executeUpdate('DELETE FROM company_contract_managers'); |
||
386 | $conn->executeUpdate('DELETE FROM company_contracts'); |
||
387 | $conn->executeUpdate('DELETE FROM company_persons_friends'); |
||
388 | $conn->executeUpdate('DELETE FROM company_managers'); |
||
389 | $conn->executeUpdate('DELETE FROM company_employees'); |
||
390 | $conn->executeUpdate('UPDATE company_persons SET spouse_id = NULL'); |
||
391 | $conn->executeUpdate('DELETE FROM company_persons'); |
||
392 | $conn->executeUpdate('DELETE FROM company_raffles'); |
||
393 | $conn->executeUpdate('DELETE FROM company_auctions'); |
||
394 | $conn->executeUpdate('UPDATE company_organizations SET main_event_id = NULL'); |
||
395 | $conn->executeUpdate('DELETE FROM company_events'); |
||
396 | $conn->executeUpdate('DELETE FROM company_organizations'); |
||
397 | } |
||
398 | |||
399 | if (isset($this->usedModelSets['generic'])) { |
||
400 | $conn->executeUpdate('DELETE FROM boolean_model'); |
||
401 | $conn->executeUpdate('DELETE FROM date_time_model'); |
||
402 | $conn->executeUpdate('DELETE FROM decimal_model'); |
||
403 | $conn->executeUpdate('DELETE FROM serialize_model'); |
||
404 | } |
||
405 | |||
406 | if (isset($this->usedModelSets['routing'])) { |
||
407 | $conn->executeUpdate('DELETE FROM RoutingRouteLegs'); |
||
408 | $conn->executeUpdate('DELETE FROM RoutingRouteBooking'); |
||
409 | $conn->executeUpdate('DELETE FROM RoutingRoute'); |
||
410 | $conn->executeUpdate('DELETE FROM RoutingLeg'); |
||
411 | $conn->executeUpdate('DELETE FROM RoutingLocation'); |
||
412 | } |
||
413 | |||
414 | if (isset($this->usedModelSets['navigation'])) { |
||
415 | $conn->executeUpdate('DELETE FROM navigation_tour_pois'); |
||
416 | $conn->executeUpdate('DELETE FROM navigation_photos'); |
||
417 | $conn->executeUpdate('DELETE FROM navigation_pois'); |
||
418 | $conn->executeUpdate('DELETE FROM navigation_tours'); |
||
419 | $conn->executeUpdate('DELETE FROM navigation_countries'); |
||
420 | } |
||
421 | |||
422 | if (isset($this->usedModelSets['directorytree'])) { |
||
423 | $conn->executeUpdate('DELETE FROM ' . $platform->quoteIdentifier('file')); |
||
424 | // MySQL doesn't know deferred deletions therefore only executing the second query gives errors. |
||
425 | $conn->executeUpdate('DELETE FROM Directory WHERE parentDirectory_id IS NOT NULL'); |
||
426 | $conn->executeUpdate('DELETE FROM Directory'); |
||
427 | } |
||
428 | |||
429 | if (isset($this->usedModelSets['ddc117'])) { |
||
430 | $conn->executeUpdate('DELETE FROM ddc117editor_ddc117translation'); |
||
431 | $conn->executeUpdate('DELETE FROM DDC117Editor'); |
||
432 | $conn->executeUpdate('DELETE FROM DDC117ApproveChanges'); |
||
433 | $conn->executeUpdate('DELETE FROM DDC117Link'); |
||
434 | $conn->executeUpdate('DELETE FROM DDC117Reference'); |
||
435 | $conn->executeUpdate('DELETE FROM DDC117ArticleDetails'); |
||
436 | $conn->executeUpdate('DELETE FROM DDC117Translation'); |
||
437 | $conn->executeUpdate('DELETE FROM DDC117Article'); |
||
438 | } |
||
439 | |||
440 | if (isset($this->usedModelSets['stockexchange'])) { |
||
441 | $conn->executeUpdate('DELETE FROM exchange_bonds_stocks'); |
||
442 | $conn->executeUpdate('DELETE FROM exchange_bonds'); |
||
443 | $conn->executeUpdate('DELETE FROM exchange_stocks'); |
||
444 | $conn->executeUpdate('DELETE FROM exchange_markets'); |
||
445 | } |
||
446 | |||
447 | if (isset($this->usedModelSets['legacy'])) { |
||
448 | $conn->executeUpdate('DELETE FROM legacy_users_cars'); |
||
449 | $conn->executeUpdate('DELETE FROM legacy_users_reference'); |
||
450 | $conn->executeUpdate('DELETE FROM legacy_articles'); |
||
451 | $conn->executeUpdate('DELETE FROM legacy_cars'); |
||
452 | $conn->executeUpdate('DELETE FROM legacy_users'); |
||
453 | } |
||
454 | |||
455 | if (isset($this->usedModelSets['customtype'])) { |
||
456 | $conn->executeUpdate('DELETE FROM customtype_parent_friends'); |
||
457 | $conn->executeUpdate('DELETE FROM customtype_parents'); |
||
458 | $conn->executeUpdate('DELETE FROM customtype_children'); |
||
459 | $conn->executeUpdate('DELETE FROM customtype_uppercases'); |
||
460 | } |
||
461 | |||
462 | if (isset($this->usedModelSets['compositekeyinheritance'])) { |
||
463 | $conn->executeUpdate('DELETE FROM JoinedChildClass'); |
||
464 | $conn->executeUpdate('DELETE FROM JoinedRootClass'); |
||
465 | $conn->executeUpdate('DELETE FROM SingleRootClass'); |
||
466 | } |
||
467 | |||
468 | if (isset($this->usedModelSets['taxi'])) { |
||
469 | $conn->executeUpdate('DELETE FROM taxi_paid_ride'); |
||
470 | $conn->executeUpdate('DELETE FROM taxi_ride'); |
||
471 | $conn->executeUpdate('DELETE FROM taxi_car'); |
||
472 | $conn->executeUpdate('DELETE FROM taxi_driver'); |
||
473 | } |
||
474 | |||
475 | if (isset($this->usedModelSets['tweet'])) { |
||
476 | $conn->executeUpdate('DELETE FROM tweet_tweet'); |
||
477 | $conn->executeUpdate('DELETE FROM tweet_user_list'); |
||
478 | $conn->executeUpdate('DELETE FROM tweet_user'); |
||
479 | } |
||
480 | |||
481 | if (isset($this->usedModelSets['cache'])) { |
||
482 | $conn->executeUpdate('DELETE FROM cache_attraction_location_info'); |
||
483 | $conn->executeUpdate('DELETE FROM cache_attraction_contact_info'); |
||
484 | $conn->executeUpdate('DELETE FROM cache_attraction_info'); |
||
485 | $conn->executeUpdate('DELETE FROM cache_visited_cities'); |
||
486 | $conn->executeUpdate('DELETE FROM cache_flight'); |
||
487 | $conn->executeUpdate('DELETE FROM cache_attraction'); |
||
488 | $conn->executeUpdate('DELETE FROM cache_travel'); |
||
489 | $conn->executeUpdate('DELETE FROM cache_traveler'); |
||
490 | $conn->executeUpdate('DELETE FROM cache_traveler_profile_info'); |
||
491 | $conn->executeUpdate('DELETE FROM cache_traveler_profile'); |
||
492 | $conn->executeUpdate('DELETE FROM cache_city'); |
||
493 | $conn->executeUpdate('DELETE FROM cache_state'); |
||
494 | $conn->executeUpdate('DELETE FROM cache_country'); |
||
495 | $conn->executeUpdate('DELETE FROM cache_login'); |
||
496 | $conn->executeUpdate('DELETE FROM cache_token'); |
||
497 | $conn->executeUpdate('DELETE FROM cache_complex_action'); |
||
498 | $conn->executeUpdate('DELETE FROM cache_action'); |
||
499 | $conn->executeUpdate('DELETE FROM cache_client'); |
||
500 | } |
||
501 | |||
502 | if (isset($this->usedModelSets['ddc3346'])) { |
||
503 | $conn->executeUpdate('DELETE FROM ddc3346_articles'); |
||
504 | $conn->executeUpdate('DELETE FROM ddc3346_users'); |
||
505 | } |
||
506 | |||
507 | if (isset($this->usedModelSets['quote'])) { |
||
508 | $conn->executeUpdate( |
||
509 | sprintf( |
||
510 | 'UPDATE %s SET %s = NULL', |
||
511 | $platform->quoteIdentifier('quote-address'), |
||
512 | $platform->quoteIdentifier('user-id') |
||
513 | ) |
||
514 | ); |
||
515 | |||
516 | $conn->executeUpdate('DELETE FROM ' . $platform->quoteIdentifier('quote-users-groups')); |
||
517 | $conn->executeUpdate('DELETE FROM ' . $platform->quoteIdentifier('quote-group')); |
||
518 | $conn->executeUpdate('DELETE FROM ' . $platform->quoteIdentifier('quote-phone')); |
||
519 | $conn->executeUpdate('DELETE FROM ' . $platform->quoteIdentifier('quote-user')); |
||
520 | $conn->executeUpdate('DELETE FROM ' . $platform->quoteIdentifier('quote-address')); |
||
521 | $conn->executeUpdate('DELETE FROM ' . $platform->quoteIdentifier('quote-city')); |
||
522 | } |
||
523 | |||
524 | if (isset($this->usedModelSets['vct_onetoone'])) { |
||
525 | $conn->executeUpdate('DELETE FROM vct_owning_onetoone'); |
||
526 | $conn->executeUpdate('DELETE FROM vct_inversed_onetoone'); |
||
527 | } |
||
528 | |||
529 | if (isset($this->usedModelSets['vct_onetoone_compositeid'])) { |
||
530 | $conn->executeUpdate('DELETE FROM vct_owning_onetoone_compositeid'); |
||
531 | $conn->executeUpdate('DELETE FROM vct_inversed_onetoone_compositeid'); |
||
532 | } |
||
533 | |||
534 | if (isset($this->usedModelSets['vct_onetoone_compositeid_foreignkey'])) { |
||
535 | $conn->executeUpdate('DELETE FROM vct_owning_onetoone_compositeid_foreignkey'); |
||
536 | $conn->executeUpdate('DELETE FROM vct_inversed_onetoone_compositeid_foreignkey'); |
||
537 | $conn->executeUpdate('DELETE FROM vct_auxiliary'); |
||
538 | } |
||
539 | |||
540 | if (isset($this->usedModelSets['vct_onetomany'])) { |
||
541 | $conn->executeUpdate('DELETE FROM vct_owning_manytoone'); |
||
542 | $conn->executeUpdate('DELETE FROM vct_inversed_onetomany'); |
||
543 | } |
||
544 | |||
545 | if (isset($this->usedModelSets['vct_onetomany_compositeid'])) { |
||
546 | $conn->executeUpdate('DELETE FROM vct_owning_manytoone_compositeid'); |
||
547 | $conn->executeUpdate('DELETE FROM vct_inversed_onetomany_compositeid'); |
||
548 | } |
||
549 | |||
550 | if (isset($this->usedModelSets['vct_onetomany_compositeid_foreignkey'])) { |
||
551 | $conn->executeUpdate('DELETE FROM vct_owning_manytoone_compositeid_foreignkey'); |
||
552 | $conn->executeUpdate('DELETE FROM vct_inversed_onetomany_compositeid_foreignkey'); |
||
553 | $conn->executeUpdate('DELETE FROM vct_auxiliary'); |
||
554 | } |
||
555 | |||
556 | if (isset($this->usedModelSets['vct_onetomany_extralazy'])) { |
||
557 | $conn->executeUpdate('DELETE FROM vct_owning_manytoone_extralazy'); |
||
558 | $conn->executeUpdate('DELETE FROM vct_inversed_onetomany_extralazy'); |
||
559 | } |
||
560 | |||
561 | if (isset($this->usedModelSets['vct_manytomany'])) { |
||
562 | $conn->executeUpdate('DELETE FROM vct_xref_manytomany'); |
||
563 | $conn->executeUpdate('DELETE FROM vct_owning_manytomany'); |
||
564 | $conn->executeUpdate('DELETE FROM vct_inversed_manytomany'); |
||
565 | } |
||
566 | |||
567 | if (isset($this->usedModelSets['vct_manytomany_compositeid'])) { |
||
568 | $conn->executeUpdate('DELETE FROM vct_xref_manytomany_compositeid'); |
||
569 | $conn->executeUpdate('DELETE FROM vct_owning_manytomany_compositeid'); |
||
570 | $conn->executeUpdate('DELETE FROM vct_inversed_manytomany_compositeid'); |
||
571 | } |
||
572 | |||
573 | if (isset($this->usedModelSets['vct_manytomany_compositeid_foreignkey'])) { |
||
574 | $conn->executeUpdate('DELETE FROM vct_xref_manytomany_compositeid_foreignkey'); |
||
575 | $conn->executeUpdate('DELETE FROM vct_owning_manytomany_compositeid_foreignkey'); |
||
576 | $conn->executeUpdate('DELETE FROM vct_inversed_manytomany_compositeid_foreignkey'); |
||
577 | $conn->executeUpdate('DELETE FROM vct_auxiliary'); |
||
578 | } |
||
579 | |||
580 | if (isset($this->usedModelSets['vct_manytomany_extralazy'])) { |
||
581 | $conn->executeUpdate('DELETE FROM vct_xref_manytomany_extralazy'); |
||
582 | $conn->executeUpdate('DELETE FROM vct_owning_manytomany_extralazy'); |
||
583 | $conn->executeUpdate('DELETE FROM vct_inversed_manytomany_extralazy'); |
||
584 | } |
||
585 | |||
586 | if (isset($this->usedModelSets['geonames'])) { |
||
587 | $conn->executeUpdate('DELETE FROM geonames_admin1_alternate_name'); |
||
588 | $conn->executeUpdate('DELETE FROM geonames_admin1'); |
||
589 | $conn->executeUpdate('DELETE FROM geonames_city'); |
||
590 | $conn->executeUpdate('DELETE FROM geonames_country'); |
||
591 | } |
||
592 | |||
593 | if (isset($this->usedModelSets['custom_id_object_type'])) { |
||
594 | $conn->executeUpdate('DELETE FROM custom_id_type_child'); |
||
595 | $conn->executeUpdate('DELETE FROM custom_id_type_parent'); |
||
596 | } |
||
597 | |||
598 | if (isset($this->usedModelSets['pagination'])) { |
||
599 | $conn->executeUpdate('DELETE FROM pagination_logo'); |
||
600 | $conn->executeUpdate('DELETE FROM pagination_department'); |
||
601 | $conn->executeUpdate('DELETE FROM pagination_company'); |
||
602 | $conn->executeUpdate('DELETE FROM pagination_user'); |
||
603 | } |
||
604 | |||
605 | if (isset($this->usedModelSets['versioned_many_to_one'])) { |
||
606 | $conn->executeUpdate('DELETE FROM versioned_many_to_one_article'); |
||
607 | $conn->executeUpdate('DELETE FROM versioned_many_to_one_category'); |
||
608 | } |
||
609 | |||
610 | if (isset($this->usedModelSets['issue5989'])) { |
||
611 | $conn->executeUpdate('DELETE FROM issue5989_persons'); |
||
612 | $conn->executeUpdate('DELETE FROM issue5989_employees'); |
||
613 | $conn->executeUpdate('DELETE FROM issue5989_managers'); |
||
614 | } |
||
615 | |||
616 | $this->em->clear(); |
||
617 | } |
||
618 | |||
619 | /** |
||
620 | * @param array $classNames |
||
621 | * |
||
622 | * @throws RuntimeException |
||
623 | */ |
||
624 | protected function setUpEntitySchema(array $classNames) |
||
625 | { |
||
626 | if ($this->em === null) { |
||
627 | throw new RuntimeException('EntityManager not set, you have to call parent::setUp() before invoking this method.'); |
||
628 | } |
||
629 | |||
630 | $classes = []; |
||
631 | |||
632 | foreach ($classNames as $className) { |
||
633 | if (! isset(static::$entityTablesCreated[$className])) { |
||
634 | static::$entityTablesCreated[$className] = true; |
||
635 | $classes[] = $this->em->getClassMetadata($className); |
||
636 | } |
||
637 | } |
||
638 | |||
639 | if ($classes) { |
||
640 | $this->schemaTool->createSchema($classes); |
||
641 | } |
||
642 | } |
||
643 | |||
644 | /** |
||
645 | * Creates a connection to the test database, if there is none yet, and |
||
646 | * creates the necessary tables. |
||
647 | */ |
||
648 | protected function setUp() : void |
||
649 | { |
||
650 | $this->setUpDBALTypes(); |
||
651 | |||
652 | if (! isset(static::$sharedConn)) { |
||
653 | static::$sharedConn = TestUtil::getConnection(); |
||
654 | } |
||
655 | |||
656 | if (isset($GLOBALS['DOCTRINE_MARK_SQL_LOGS'])) { |
||
657 | if (in_array(static::$sharedConn->getDatabasePlatform()->getName(), ['mysql', 'postgresql'], true)) { |
||
658 | static::$sharedConn->executeQuery('SELECT 1 /*' . static::class . '*/'); |
||
659 | } elseif (static::$sharedConn->getDatabasePlatform()->getName() === 'oracle') { |
||
660 | static::$sharedConn->executeQuery('SELECT 1 /*' . static::class . '*/ FROM dual'); |
||
661 | } |
||
662 | } |
||
663 | |||
664 | if (! $this->em) { |
||
665 | $this->em = $this->getEntityManager(); |
||
666 | $this->schemaTool = new SchemaTool($this->em); |
||
667 | } |
||
668 | |||
669 | foreach ($this->usedModelSets as $setName => $bool) { |
||
670 | if (! isset(static::$tablesCreated[$setName])) { |
||
671 | $this->setUpEntitySchema(static::$modelSets[$setName]); |
||
672 | |||
673 | static::$tablesCreated[$setName] = true; |
||
674 | } |
||
675 | } |
||
676 | |||
677 | $this->sqlLoggerStack->enabled = true; |
||
678 | } |
||
679 | |||
680 | /** |
||
681 | * Gets an EntityManager for testing purposes. |
||
682 | * |
||
683 | * @return EntityManagerInterface |
||
684 | * |
||
685 | * @throws ORMException |
||
686 | */ |
||
687 | protected function getEntityManager(?Connection $connection = null, ?MappingDriver $mappingDriver = null) |
||
688 | { |
||
689 | // NOTE: Functional tests use their own shared metadata cache, because |
||
690 | // the actual database platform used during execution has effect on some |
||
691 | // metadata mapping behaviors (like the choice of the ID generation). |
||
692 | if (self::$metadataCacheImpl === null) { |
||
693 | if (isset($GLOBALS['DOCTRINE_CACHE_IMPL'])) { |
||
694 | self::$metadataCacheImpl = new $GLOBALS['DOCTRINE_CACHE_IMPL'](); |
||
695 | } else { |
||
696 | self::$metadataCacheImpl = new ArrayCache(); |
||
697 | } |
||
698 | } |
||
699 | |||
700 | if (self::$queryCacheImpl === null) { |
||
701 | self::$queryCacheImpl = new ArrayCache(); |
||
702 | } |
||
703 | |||
704 | $this->sqlLoggerStack = new DebugStack(); |
||
705 | $this->sqlLoggerStack->enabled = false; |
||
706 | |||
707 | //FIXME: two different configs! $conn and the created entity manager have |
||
708 | // different configs. |
||
709 | $config = new Configuration(); |
||
710 | |||
711 | $config->setMetadataCacheImpl(self::$metadataCacheImpl); |
||
712 | $config->setQueryCacheImpl(self::$queryCacheImpl); |
||
713 | $config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL); |
||
714 | $config->setProxyNamespace('Doctrine\Tests\Proxies'); |
||
715 | |||
716 | if ($this->resultCacheImpl !== null) { |
||
717 | $config->setResultCacheImpl($this->resultCacheImpl); |
||
718 | } |
||
719 | |||
720 | $enableSecondLevelCache = getenv('ENABLE_SECOND_LEVEL_CACHE'); |
||
721 | |||
722 | if ($this->isSecondLevelCacheEnabled || $enableSecondLevelCache) { |
||
723 | $cacheConfig = new CacheConfiguration(); |
||
724 | $cache = $this->getSharedSecondLevelCacheDriverImpl(); |
||
725 | $factory = new DefaultCacheFactory($cacheConfig->getRegionsConfiguration(), $cache); |
||
726 | |||
727 | $this->secondLevelCacheFactory = $factory; |
||
728 | |||
729 | if ($this->isSecondLevelCacheLogEnabled) { |
||
730 | $this->secondLevelCacheLogger = new StatisticsCacheLogger(); |
||
731 | $cacheConfig->setCacheLogger($this->secondLevelCacheLogger); |
||
732 | } |
||
733 | |||
734 | $cacheConfig->setCacheFactory($factory); |
||
735 | $config->setSecondLevelCacheEnabled(true); |
||
736 | $config->setSecondLevelCacheConfiguration($cacheConfig); |
||
737 | |||
738 | $this->isSecondLevelCacheEnabled = true; |
||
739 | } |
||
740 | |||
741 | $conn = $connection ?: static::$sharedConn; |
||
742 | |||
743 | $config->setMetadataDriverImpl( |
||
744 | $mappingDriver ?? $config->newDefaultAnnotationDriver([ |
||
745 | realpath(__DIR__ . '/Models/Cache'), |
||
746 | realpath(__DIR__ . '/Models/GeoNames'), |
||
747 | ]) |
||
748 | ); |
||
749 | |||
750 | $conn->getConfiguration()->setSQLLogger($this->sqlLoggerStack); |
||
751 | |||
752 | // get rid of more global state |
||
753 | $evm = $conn->getEventManager(); |
||
754 | |||
755 | foreach ($evm->getListeners() as $event => $listeners) { |
||
756 | foreach ($listeners as $listener) { |
||
757 | $evm->removeEventListener([$event], $listener); |
||
758 | } |
||
759 | } |
||
760 | |||
761 | if ($enableSecondLevelCache) { |
||
762 | $evm->addEventListener('loadClassMetadata', new CacheMetadataListener()); |
||
763 | } |
||
764 | |||
765 | if (isset($GLOBALS['db_event_subscribers'])) { |
||
766 | foreach (explode(',', $GLOBALS['db_event_subscribers']) as $subscriberClass) { |
||
767 | $subscriberInstance = new $subscriberClass(); |
||
768 | $evm->addEventSubscriber($subscriberInstance); |
||
769 | } |
||
770 | } |
||
771 | |||
772 | if (isset($GLOBALS['debug_uow_listener'])) { |
||
773 | $evm->addEventListener(['onFlush'], new DebugUnitOfWorkListener()); |
||
774 | } |
||
775 | |||
776 | return EntityManager::create($conn, $config); |
||
777 | } |
||
778 | |||
779 | /** |
||
780 | * @throws Throwable |
||
781 | */ |
||
782 | protected function onNotSuccessfulTest(Throwable $e) |
||
783 | { |
||
784 | if ($e instanceof AssertionFailedError) { |
||
785 | throw $e; |
||
786 | } |
||
787 | |||
788 | if (isset($this->sqlLoggerStack->queries) && count($this->sqlLoggerStack->queries)) { |
||
789 | $queries = ''; |
||
790 | $last25queries = array_slice(array_reverse($this->sqlLoggerStack->queries, true), 0, 25, true); |
||
791 | |||
792 | foreach ($last25queries as $i => $query) { |
||
793 | $params = array_map( |
||
794 | static function ($p) { |
||
795 | return is_object($p) ? get_class($p) : var_export($p, true); |
||
796 | }, |
||
797 | $query['params'] ?: [] |
||
798 | ); |
||
799 | |||
800 | $queries .= $i . ". SQL: '" . $query['sql'] . "' Params: " . implode(', ', $params) . PHP_EOL; |
||
801 | } |
||
802 | |||
803 | $trace = $e->getTrace(); |
||
804 | $traceMsg = ''; |
||
805 | |||
806 | foreach ($trace as $part) { |
||
807 | if (isset($part['file'])) { |
||
808 | if (strpos($part['file'], 'PHPUnit/') !== false) { |
||
809 | // Beginning with PHPUnit files we don't print the trace anymore. |
||
810 | break; |
||
811 | } |
||
812 | |||
813 | $traceMsg .= $part['file'] . ':' . $part['line'] . PHP_EOL; |
||
814 | } |
||
815 | } |
||
816 | |||
817 | $message = '[' . get_class($e) . '] ' . $e->getMessage() . PHP_EOL . PHP_EOL . 'With queries:' . PHP_EOL . $queries . PHP_EOL . 'Trace:' . PHP_EOL . $traceMsg; |
||
818 | |||
819 | throw new Exception($message, (int) $e->getCode(), $e); |
||
820 | } |
||
821 | |||
822 | throw $e; |
||
823 | } |
||
824 | |||
825 | public static function assertSQLEquals($expectedSql, $actualSql) |
||
826 | { |
||
827 | self::assertEquals( |
||
828 | strtolower((string) $expectedSql), |
||
829 | strtolower((string) $actualSql), |
||
830 | 'Lowercase comparison of SQL statements failed.' |
||
831 | ); |
||
832 | } |
||
833 | |||
834 | /** |
||
835 | * Using the SQL Logger Stack this method retrieves the current query count executed in this test. |
||
836 | * |
||
837 | * @return int |
||
838 | */ |
||
839 | protected function getCurrentQueryCount() |
||
842 | } |
||
843 | |||
844 | /** |
||
845 | * Configures DBAL types required in tests |
||
846 | */ |
||
847 | protected function setUpDBALTypes() |
||
853 | } |
||
854 | } |
||
855 | } |
||
856 |