new: pkg: add test cases to back-end w/ real database connection in it #10
This commit is contained in:
108
tests/README.md
Normal file
108
tests/README.md
Normal file
@@ -0,0 +1,108 @@
|
||||
# Tests Directory
|
||||
|
||||
This directory contains all test files for the MineSeeker project.
|
||||
|
||||
## Quick Start
|
||||
|
||||
```bash
|
||||
# Run all tests (recommended)
|
||||
make test
|
||||
|
||||
# Or with PHPUnit directly:
|
||||
vendor/bin/phpunit
|
||||
|
||||
# Run specific test file
|
||||
vendor/bin/phpunit tests/Controller/ProfileControllerTest.php
|
||||
|
||||
# Run with filter
|
||||
vendor/bin/phpunit --filter testCreateUser
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
For comprehensive testing documentation, see:
|
||||
|
||||
- **[Testing Guide](../docs/testing/TESTING.md)** - Complete testing setup, best practices, troubleshooting
|
||||
- **[Factory Documentation](../docs/testing/FACTORIES.md)** - Detailed factory API reference
|
||||
|
||||
## Directory Structure
|
||||
|
||||
```
|
||||
tests/
|
||||
├── Controller/ # HTTP endpoint tests
|
||||
├── Dto/ # Data Transfer Object tests
|
||||
├── Entity/ # Entity logic tests
|
||||
├── Service/ # Service layer tests
|
||||
├── Integration/ # Integration tests (example: FactoryExampleTest.php)
|
||||
├── Factory/ # Foundry factory classes
|
||||
│ ├── UserFactory.php
|
||||
│ ├── GamerFactory.php
|
||||
│ ├── PlayedGameFactory.php
|
||||
│ ├── StepFactory.php
|
||||
│ ├── GridFactory.php
|
||||
│ ├── GridRowFactory.php
|
||||
│ ├── WebAuthnCredentialFactory.php
|
||||
│ └── ContactMessageFactory.php
|
||||
├── WebTestCase.php # Base test class (extends this!)
|
||||
├── bootstrap.php # PHPUnit bootstrap
|
||||
└── README.md # This file
|
||||
```
|
||||
|
||||
## Quick Factory Examples
|
||||
|
||||
```php
|
||||
use App\Tests\Factory\UserFactory;
|
||||
use App\Tests\Factory\PlayedGameFactory;
|
||||
use App\Tests\WebTestCase;
|
||||
|
||||
class MyTest extends WebTestCase
|
||||
{
|
||||
public function testExample(): void
|
||||
{
|
||||
/** Create user */
|
||||
$user = UserFactory::createOne();
|
||||
|
||||
/** Create game with registered players */
|
||||
$game = PlayedGameFactory::new()
|
||||
->withRegisteredPlayers()
|
||||
->redWins()
|
||||
->create();
|
||||
|
||||
/** Create multiple entities */
|
||||
UserFactory::createMany(5);
|
||||
|
||||
/** Access repository */
|
||||
$users = UserFactory::repository()->findAll();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Important Notes
|
||||
|
||||
- **Always extend `App\Tests\WebTestCase`** - provides database isolation
|
||||
- **Use factories** - don't manually create entities with `new Entity()`
|
||||
- **Test database** - Tests run on `mineseeker_test`, never production
|
||||
- **Automatic rollback** - Each test is wrapped in a transaction
|
||||
|
||||
## Test Database Setup (One-time)
|
||||
|
||||
```bash
|
||||
# Create test database
|
||||
bin/console dbal:run-sql "CREATE DATABASE mineseeker_test"
|
||||
|
||||
# Run migrations
|
||||
bin/console doctrine:migrations:migrate --env=test --no-interaction
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Tests interfering with each other?
|
||||
→ Make sure your test extends `App\Tests\WebTestCase`
|
||||
|
||||
### Database schema out of sync?
|
||||
→ Run `bin/console doctrine:migrations:migrate --env=test`
|
||||
|
||||
### Memory limit errors?
|
||||
→ Run `php -d memory_limit=512M vendor/bin/phpunit`
|
||||
|
||||
For more troubleshooting, see [Testing Guide](../docs/testing/TESTING.md#troubleshooting).
|
||||
Reference in New Issue
Block a user