TestCase.php 906 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests;
  4. use PHPUnit\Framework\TestCase as BaseTestCase;
  5. use App\Services\DB;
  6. use Tests\TestDatabase;
  7. abstract class TestCase extends BaseTestCase
  8. {
  9. protected bool $useDatabase = false;
  10. protected function setUp(): void
  11. {
  12. parent::setUp();
  13. if ($this->useDatabase) {
  14. $this->setUpDatabase();
  15. }
  16. }
  17. protected function tearDown(): void
  18. {
  19. parent::tearDown();
  20. }
  21. protected function setUpDatabase(): void
  22. {
  23. try {
  24. TestDatabase::init();
  25. $this->createTestTables();
  26. } catch (\Exception $e) {
  27. $this->markTestSkipped('Database connection not available: ' . $e->getMessage());
  28. }
  29. }
  30. protected function createTestTables(): void
  31. {
  32. // Override in test classes that need specific tables
  33. }
  34. }