Skip to content

Commit

Permalink
Release/3.0.0 (#7)
Browse files Browse the repository at this point in the history
* feat: Updates PHP version, refactor models, add event emission, and other adjustments.

* feat: Adds integration test execution to the pipeline.
  • Loading branch information
gustavofreze authored Mar 17, 2024
1 parent 81d3a60 commit 77aea7e
Show file tree
Hide file tree
Showing 224 changed files with 4,707 additions and 2,065 deletions.
22 changes: 14 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: CI

on:
push:
pull_request:

permissions:
Expand All @@ -16,14 +15,16 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Install dependencies
run: composer update --no-progress --optimize-autoloader

- name: Run phpcs
run: composer phpcs

- name: Run phpmd
run: composer phpmd
- name: Run review
run: composer review

tests:
name: Tests
Expand All @@ -33,10 +34,15 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Install dependencies
run: composer update --no-progress --optimize-autoloader

- name: Run unit tests
- name: Run tests
env:
XDEBUG_MODE: coverage
run: composer test
run: composer unit-test
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea

/vendor/
/report
composer.lock
.phpunit.result.cache
*.lock
.phpunit.*
28 changes: 23 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
FROM gustavofreze/php:8.2-fpm

RUN apk update \
&& apk add libressl-dev \
&& pecl install mongodb \
&& docker-php-ext-enable mongodb \
&& rm -rf /var/cache/apk/*
LABEL author="Gustavo Freze" \
maintainer="Gustavo Freze" \
org.label-schema.name="gustavofreze/cheap-delivery" \
org.label-schema.vcs-url="https://github.com/gustavofreze/cheap-delivery/blob/main/Dockerfile" \
org.label-schema.schema-version="1.0"

ARG FLYWAY_VERSION=10.10.0

RUN apk --no-cache add tar curl mysql-client openjdk21-jre \
&& mkdir -p /opt/flyway \
&& curl -L "https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/${FLYWAY_VERSION}/flyway-commandline-${FLYWAY_VERSION}-linux-x64.tar.gz" | tar -xz --strip-components=1 -C /opt/flyway \
&& rm -f /opt/flyway/jre/bin/java \
&& ln -s /usr/lib/jvm/java-11-openjdk/jre/bin/java /opt/flyway/jre/bin/java \
&& ln -s /opt/flyway/flyway /usr/local/bin/flyway \
&& apk del curl tar

WORKDIR /var/www/html

COPY ./db /db

RUN chmod +x /db/entrypoint.sh

ENTRYPOINT ["bash", "/db/entrypoint.sh"]
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021-2023 Gustavo Freze
Copyright (c) 2021-2024 Gustavo Freze

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
69 changes: 51 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,27 +1,60 @@
DOCKER_RUN = docker run --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/cheap-delivery
DOCKER_EXEC = docker exec -it cheap-delivery
PWD := $(shell pwd -L)
PHP_IMAGE := gustavofreze/php:8.2
APP_RUN := docker run -u root --rm -it --network=host -v ${PWD}:/app -w /app ${PHP_IMAGE}
APP_TEST_RUN := docker run -u root --rm -it --name cheap-delivery-integration-test --link cheap-delivery-adm --network=cheap-delivery_default -v ${PWD}:/app -w /app ${PHP_IMAGE}

.PHONY: configure run test test-no-coverage review show-reports clean
FLYWAY_IMAGE := flyway/flyway:10.10.0
FLYWAY_RUN := docker run --rm -v ${PWD}/db/mysql/migrations:/flyway/sql --env-file=config/local.env --link cheap-delivery-adm --network=cheap-delivery_default ${FLYWAY_IMAGE}
MIGRATE_DB := ${FLYWAY_RUN} -locations=filesystem:/flyway/sql -schemas=cheap_delivery_adm
MIGRATE_TEST_DB := ${FLYWAY_RUN} -locations=filesystem:/flyway/sql -schemas=cheap_delivery_adm_test

configure:
.DEFAULT_GOAL := help

.PHONY: start configure test unit-test integration-test review fix-style show-coverage clean migrate-database clean-database migrate-test-database help

start: ## Start Docker compose services
@docker-compose up -d --build
@${DOCKER_EXEC} composer update --optimize-autoloader

run:
@${DOCKER_RUN} composer update --optimize-autoloader
configure: ## Configure development environment
@${APP_RUN} composer update --optimize-autoloader

test: migrate-test-database ## Run all tests
@${APP_TEST_RUN} composer run tests

unit-test: ## Run unit tests
@${APP_RUN} composer run unit-test

integration-test: migrate-test-database ## Run integration tests
@${APP_TEST_RUN} composer run integration-test

review: ## Run code review
@${APP_RUN} composer review

fix-style: ## Fix code style
@${APP_RUN} composer fix-style

test: run review
@${DOCKER_RUN} composer tests
show-coverage: ## Open code coverage reports in browser
@sensible-browser report/coverage/coverage-html/index.html report/coverage/mutation-report.html

test-no-coverage: run review
@${DOCKER_RUN} composer tests-no-coverage
migrate-database: ## Run database migrations
@${MIGRATE_DB} migrate

review:
@${DOCKER_RUN} composer review
clean-database: ## Clean database
@${MIGRATE_DB} clean

show-reports:
@sensible-browser report/coverage/coverage-html/index.html
migrate-test-database: ## Run test database migrations
@${MIGRATE_TEST_DB} clean
@${MIGRATE_TEST_DB} migrate

clean:
@sudo chown -R ${USER}:${USER} ${PWD}
@rm -rf report vendor
help: ## Display this help message
@echo "Usage: make [target]"
@echo ""
@echo "Setup and run"
@grep -E '^(configure|start|migrate-database|clean-database):.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@echo ""
@echo "Testing"
@grep -E '^(test|unit-test|integration-test|show-coverage|migrate-test-database):.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@echo ""
@echo "Code review "
@grep -E '^(review|fix-style):.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@echo ""
137 changes: 81 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,123 @@
[![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)

* [Overview](#overview)
* [Instalação](#installation)
- [Repositório](#repository)
- [Configuração](#settings)
* [Endpoints](#endpoints)
- [Use cases](#use_cases)
- [Queries](#queries)
* [Installation](#installation)
- [Repository](#repository)
- [Configuration](#configuration)
- [Tests](#tests)
- [Review](#review)
* [Environment setup](#environment_setup)

<div id="overview"></div>

## Overview

A empresa XPTO realizou um sorteio entre jogadores do Brasil inteiro, porém, os brindes precisam chegar a seus
ganhadores de alguma maneira. Para tanto, é necessário implementar um sistema para calcular o menor custo para o envio
de cada brinde de acordo com a distância do ganhador, e o peso do brinde. Ao entrar em contato com as transportadoras,
recebemos as seguintes condições para o transporte:
Company XPTO held a drawing among players from all over Brazil. However, the prizes need to be delivered to the winners.
To achieve this, it is necessary to implement a system that calculates the lowest shipping cost for each award based on
the distance to the winner and the weight of the prize. When contacting carriers, we received the following
transportation conditions:

| Empresa | Valor fixo | Valor km/kg |
|:----------------|-----------:|------------:|
| DHL | R$ 10,00 | R$ 0,05 |
| FedEx | R$ 4,30 | R$ 0,12 |
| Loggi (até 5kg) | R$ 2,10 | R$ 1,10 |
| Loggi (+ 5kg) | R$ 10,00 | R$ 0,01 |
| Carrier | Fixed value | Value per km/kg |
|:-------------------|------------:|----------------:|
| DHL | R$ 10,00 | R$ 0,05 |
| FedEx | R$ 4,30 | R$ 0,12 |
| Loggi (up to 5kg) | R$ 2,10 | R$ 1,10 |
| Loggi (5kg and up) | R$ 10,00 | R$ 0,01 |

<div id='use_cases'></div>

### Use cases

- [Dispatch with lowest cost](docs/USE_CASES.md#dispatch-with-lowest-cost)

<div id='queries'></div>

### Queries

- [Query dispatch with lowest cost](docs/QUERIES.md#query-dispatches-with-lowest-cost)

<div id='installation'></div>

## Instalação
## Installation

<div id='repository'></div>

### Repositório
### Repository

Para clonar o repositório usando a linha de comando, execute:
To clone the repository using the command line, run:

```bash
git clone https://github.com/gustavofreze/cheap-delivery.git
```

<div id='settings'></div>
<div id='configuration'></div>

### Configuration

### Configuração
To install project dependencies locally, run:

```bash
make configure
```

<div id='endpoints'></div>
To start the application containers, run:

## Endpoints

URLs de acesso:
```bash
make start
```

| Ambiente | DNS |
|:---------|:-----------------------------------|
| `Local` | http://cheap-delivery.localhost:81 |
<div id='tests'></div>

<div id="tests"></div>
### Tests

### Shipment
Run only unit tests:

###### Realiza o cálculo do menor custo de envio disponível.
```bash
make unit-test
```

**POST** `{{dns}}/shipment`
Run only integration tests:

**Request**
```bash
make integration-test
```

| Parâmetro | Tipo | Descrição | Obrigatório |
|:------------------|:------:|:---------------------------|:-----------:|
| `person.name` | String | Nome do destinatário. | Sim |
| `person.distance` | float | Distância do destinatário. | Sim |
| `product.name` | String | Nome do produto. | Sim |
| `product.weight` | float | Peso do produto. | Sim |
Run all tests:

```json
{
"person": {
"name": "Gustavo",
"distance": 150.00
},
"product": {
"name": "Notebook",
"weight": 3.70
}
}
```bash
make test
```

**Response**
Displays coverage reports in the browser:

```bash
make show-coverage
```
HTTP/1.1 200 OK
```

```json
{
"carrier": "DHL",
"cost": 37.75
}
<div id='review'></div>

### Review

Run static code analysis:

```bash
make review
```

> You can check other available commands by running `make help`.
## Environment Setup

### Access URLs

| Environment | DNS |
|:------------|:--------------------------------|
| `Local` | http://cheap-delivery.localhost |

### Database

| Environment | URL | Port |
|:------------|:----------------------------|:----:|
| `Local` | jdbc:mysql://localhost:3307 | 3307 |
Loading

0 comments on commit 77aea7e

Please sign in to comment.