A RESTful chat application backend built with PHP, Slim Framework, SQLite, and Redis. The application allows users to create chat groups, join them, and exchange messages.
- PHP 8.0+
- Slim Framework 4.0
- SQLite Database
- Redis (for caching and rate limiting)
- PHPUnit for testing
- PHP 8.0 or higher
- Composer
- SQLite
- Redis server
- Clone the repository
- Run
composer install - Configure environment variables in
.envor use default values: - Initialize database:
php src/Database/setup.php - Make sure redis server is running otherwise send message endpoint will not work:
redis-server - Start server:
php server.php
- POST
/users - Body:
{"username": "john_doe"} - Response: 201 Created
{ "id": 1, "username": "john_doe", "created_at": "2024-01-29 15:00:00" }
- GET
/users/{id} - Response: 200 OK
{ "id": 1, "username": "john_doe", "created_at": "2024-01-29 15:00:00" }
- POST
/groups - Body:
{"name": "General Chat"} - Response: 201 Created
{ "id": 1, "name": "General Chat", "created_at": "2024-01-29 15:00:00" }
- POST
/groups/{group_id}/join - Body:
{"user_id": 1} - Response: 200 OK
{ "message": "Successfully joined group" }
- GET
/groups/{group_id}/members - Response: 200 OK
[ { "id": 1, "username": "john_doe", "joined_at": "2024-01-29 15:00:00" } ]
- POST
/groups/{group_id}/messages - Body:
{"user_id": 1, "content": "Hello, everyone!"} - Response: 201 Created
- Rate Limit: 60 messages per minute per user enforced by redis
{ "id": 1, "group_id": 1, "user_id": 1, "content": "Hello, everyone!", "created_at": "2024-01-29 15:00:00" }
- GET
/groups/{group_id}/messages?user_id=1 - Response: 200 OK
[ { "id": 1, "content": "Hello, everyone!", "created_at": "2024-01-29 15:00:00", "user_id": 1, "username": "john_doe" } ]
- GET
/groups/{group_id}/messages/since/{timestamp}?user_id=1 - Response: 200 OK
[ { "id": 2, "content": "New message", "created_at": "2024-01-29 15:10:00", "user_id": 1, "username": "john_doe" } ]
- 400 Bad Request: Invalid input
- 401 Unauthorized: Authentication required
- 403 Forbidden: Not a group member
- 404 Not Found: Resource doesn't exist
- 409 Conflict: Resource already exists
- 429 Too Many Requests: Rate limit exceeded
- 500 Internal Server Error: Server error
- Message lists are cached in Redis for 5 minutes
- Cache is automatically invalidated when new messages are posted
- Rate limiting uses Redis for tracking message counts
- Install dependencies:
composer install - Run tests:
./vendor/bin/phpunit
- Unit Tests:
./vendor/bin/phpunit --testsuite Unit - Integration Tests:
./vendor/bin/phpunit --testsuite Integration - API Tests:
./vendor/bin/phpunit --testsuite API
- id (INTEGER PRIMARY KEY)
- username (TEXT UNIQUE)
- created_at (DATETIME)
- id (INTEGER PRIMARY KEY)
- name (TEXT)
- created_at (DATETIME)
- group_id (INTEGER)
- user_id (INTEGER)
- joined_at (DATETIME)
- PRIMARY KEY (group_id, user_id)
- FOREIGN KEY references to groups and users
- id (INTEGER PRIMARY KEY)
- group_id (INTEGER)
- user_id (INTEGER)
- content (TEXT)
- created_at (DATETIME)
- FOREIGN KEY references to groups and users
A Postman collection is available in docs/postman/Chat_API.postman_collection.json for testing all endpoints.
- Make sure Redis server is running
- No need for SQLite file as tests use in-memory database
./vendor/bin/phpunit# Run only unit tests
./vendor/bin/phpunit --testsuite Unit
# Run only integration tests
./vendor/bin/phpunit --testsuite Integration
# Run only API tests
./vendor/bin/phpunit --testsuite API# Run specific test file
./vendor/bin/phpunit tests/Unit/Models/UserTest.php
# Run specific test method
./vendor/bin/phpunit --filter testCreateUser tests/Unit/Models/UserTest.php- Unit Tests: Model methods and basic functionality
- Integration Tests: Cross-component interactions
- API Tests: HTTP endpoints and responses
- Creating/retrieving users
- Creating/joining groups
- Sending/retrieving messages
- Rate limiting
- Cache invalidation
- Error handling