Browse Source

fix test clear db

Alexander Musikhin 4 days ago
parent
commit
119ed5521d
2 changed files with 20 additions and 0 deletions
  1. 4 0
      Makefile
  2. 16 0
      tests/TestCase.php

+ 4 - 0
Makefile

@@ -154,15 +154,19 @@ scheduler-log: ## Лог планировщика
 	$(compose) logs app-schedule -f
 
 test: ## Run tests
+	$(application) php artisan config:clear
 	$(application) php artisan test
 
 test-unit: ## Run unit tests only
+	$(application) php artisan config:clear
 	$(application) php artisan test --testsuite=Unit
 
 test-feature: ## Run feature tests only
+	$(application) php artisan config:clear
 	$(application) php artisan test --testsuite=Feature
 
 test-coverage: ## Run tests with coverage report (requires Xdebug or PCOV)
+	$(application) php artisan config:clear
 	$(application) php artisan test --coverage
 
 test-setup: ## Create test database (crm_testing)

+ 16 - 0
tests/TestCase.php

@@ -4,6 +4,7 @@ namespace Tests;
 
 use Illuminate\Foundation\Http\Middleware\ValidateCsrfToken;
 use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
+use RuntimeException;
 
 abstract class TestCase extends BaseTestCase
 {
@@ -11,6 +12,21 @@ abstract class TestCase extends BaseTestCase
     {
         parent::setUp();
 
+        $this->guardAgainstProductionDatabase();
+
         $this->withoutMiddleware(ValidateCsrfToken::class);
     }
+
+    private function guardAgainstProductionDatabase(): void
+    {
+        $database = config('database.connections.'.config('database.default').'.database');
+
+        if ($database !== 'crm_testing') {
+            throw new RuntimeException(sprintf(
+                'Refusing to run tests against database "%s". Expected "crm_testing". '.
+                'Likely cause: stale bootstrap/cache/config.php. Run "php artisan config:clear".',
+                $database
+            ));
+        }
+    }
 }