An open API service indexing awesome lists of open source software.

https://github.com/jan-krcal/rest-api-example

REST API Example (ORM, Apitte, Codeception, Redis, Elasticsearh, RabbitMQ, Nette)
https://github.com/jan-krcal/rest-api-example

apitte codeception elasticsearch jwt nette orm php rabbitmq redis

Last synced: 9 days ago
JSON representation

REST API Example (ORM, Apitte, Codeception, Redis, Elasticsearh, RabbitMQ, Nette)

Awesome Lists containing this project

README

          

REST API Example (ORM, Apitte, Codeception, Redis, Elasticsearh, RabbitMQ, Nette)
=================

Sample REST API for working with two entities โ€” **articles** and **users**, with authorization support.

## ๐Ÿณ Docker Setup

This project uses the following basic environment setup:

- **PHP 8.2 CLI** with extensions: `bcmath`, `sockets`, `mysqli`
- MySQL 8.0 database with user, password, and database name as defined in `docker-compose.yml`

For detailed configuration, please see the `Dockerfile` and `docker-compose.yml` files.

### Important

If you are **not running the project via Docker**, you need to configure your environment to match these settings as closely as possible. This includes:

- Installing the required PHP version and extensions
- Setting up a MySQL 8.0 database
- Mounting or placing project files in the correct path

Additionally, **you might need to adjust URLs or paths in tests** to reflect your local environment if it differs from the Docker setup.

Keeping environments aligned ensures consistent behavior and successful test runs.

## โš™๏ธ Installed Extensions and Framework

This project runs on the **Nette Framework** and uses the following key Composer packages:

## Installed Packages

| Package | Purpose |
|---------------------------------------|-----------------------------------------|
| `codeception/codeception` | Testing framework (dev) |
| `codeception/module-asserts` | Codeception module for assertions (dev) |
| `codeception/module-phpbrowser` | Codeception module for browser testing (dev) |
| `codeception/module-rest` | Codeception module for REST API testing (dev) |
| `contributte/apitte` | API framework integration |
| `contributte/console` | Console commands integration |
| `elastic/elasticsearch` | Elasticsearch client |
| `firebase/php-jwt` | JWT (JSON Web Token) authentication |
| `nettrine/migrations` | Database migrations support |
| `nettrine/orm` | ORM integration for Nette |
| `php-amqplib/php-amqplib` | AMQP (RabbitMQ) client |
| `predis/predis` | Redis client |

## ๐Ÿš€ Startup Instructions

1. **Clone the repository:**

```bash
git clone https://github.com/jan-krcal/rest-api-example.git
```

2. **Install dependencies using Composer:**

```bash
composer install
```

3. **Start the Docker containers:**

```bash
docker-compose up --build
```

4. **Check and run migrations:**

```bash
php bin/console.php migrations:status
php bin/console.php migrations:migrate -n
```

## โœ… Tests

The project contains tests.

**Run tests with:**

```bash
php vendor/bin/codecept run
```

**List of tests**

| Path | Testing framework | Description |
|--------------------------|-----------------|------------------------------------------------------------------------------------------------|
| `tests/EmailTest.php` | Unit Test | ValueObject |
| `tests/AuthCest.php` | API Test | Tests authentication endpoints (login, registration, token validation) |
| `tests/OpenApiCest.php` | API Test | Tests OpenAPI endpoints |

## ๐Ÿงน Code Quality and Static Analysis

```bash
php vendor/bin/phpcbf --standard=PSR12
```

```bash
php vendor/bin/phpstan analyse --memory-limit=512M
```

## ๐Ÿ“š API Endpoints with Request Body Fields

### Authentication

| Method | Endpoint | Description | Request Body Fields |
|--------|------------------|---------------------------------------|--------------------------------|
| POST | `/auth/register` | Register a new user | `email`, `password`, `role`, `name` |
| POST | `/auth/login` | Login and receive authorization token | `email`, `password` |

---

### User Management (admin only)

| Method | Endpoint | Description | Request Body Fields |
|--------|------------------|-------------------------------------|------------------------------------------------------|
| GET | `/users` | List all users | โ€” |
| GET | `/users/{id}` | Get data for a specific user | โ€” |
| POST | `/users` | Create a new user | `email`, `password`, `role`, `name` |
| PUT | `/users/{id}` | Update user data | `email` (optional), `name` (optional), `password` (optional), `role` (optional) |
| DELETE | `/users/{id}` | Delete a user | โ€” |

---

### Article Management

| Method | Endpoint | Description | Request Body Fields |
|--------|--------------------|--------------------------------|-------------------------------|
| GET | `/articles` | List all articles | โ€” |
| GET | `/articles/{id}` | Get article details by ID | โ€” |
| POST | `/articles` | Create an article | `title`, `content` |
| PUT | `/articles/{id}` | Update an article | `title` (optional), `content` (optional) |

---

## ๐Ÿ“„ License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.