- Use
docker
for portability/reusability and multi platform maintenance - Use industry standards (Docker, PHP-FPM, Nginx, Postgresql, Symfony, PHPUnit, etc) to comply with industry standards
- Use proper
test
environment forautomated-testing
- Learn a bit more about swagger :)
the main dependency is Docker and Docker Compose
After docker
and docker compose
are installed the following commands can be used;
# spin up `docker` containers
docker-compose up --build
# run migrations
docker-compose exec -w "/var/www/html/customer-api" php-fpm php bin/console doctrine:migrations:migrate
# run migrations on the `test_db`
docker-compose exec -w "/var/www/html/customer-api" php-fpm php bin/console doctrine:migrations:migrate --env test
# test all the api's
docker-compose exec -w "/var/www/html/customer-api" php-fpm ./vendor/bin/phpunit
# api runs at
[localhost:8080](http://localhost:8080)
# swagger documentation
[localhost:8081](http://localhost:8081)
# peek `test_db` -> customers
docker-compose exec test_db psql -U user -d symfony_test -c "SELECT * FROM customers;"
# peek `db` -> customers`
docker-compose exec db psql -U user -d symfony -c "SELECT * FROM customers;"
- I added a very basic documentation and swagger implementation -> this should be improved
- Since i opted for UUID i had to add a
/get-random-[CUSTOMER]
endpoint, the test suite should add, then update the same user for normalcy - I got away from making different Interfaces/Validators for private/business with :
<?php
private function getBusinessContent(Request $request) {
$dataString = $request->getContent();
$originalData = json_decode($dataString, true);
// Create a new array with 'type' as the first key
$data = ['type' => 'business'];
// Copy other elements from the original array to the new array
foreach ($originalData as $key => $value) {
// Skip 'type' to avoid overwriting it if it's already set in original data
if ($key !== 'type') {
$data[$key] = $value;
}
}
return json_encode($data);
}
(using symfony Entity validator by ensuring type is set before a dependency is validated) -> innovation or cheatcode, whatever your perspective ;)
- Since 2 endpoints were specified I assumed the
Private customer endpoint
will ALWAYS beprivate
thus not needing atype
- Add more documentation for implementation ->
Curl
or popularfetch
request examples
the symfony scaffolder wants to make it a git, so to let it set git config
for the running container so we can scaffold a symfony
project
docker-compose exec php-fpm bash
# within container
git config --global user.email "[email protected]"
git config --global user.name "Fuentastic"
docker-compose exec php-fpm symfony new --webapp "/var/www/html/customer-api"
rm -rf customer-api/.git # since we already have a repo
docker-compose exec -w "/var/www/html/customer-api" php-fpm composer require symfony/uid
docker-compose exec -w "/var/www/html/customer-api" php-fpm php bin/console doctrine:migrations:diff
docker-compose exec -w "/var/www/html/customer-api" php-fpm php bin/console doctrine:migrations:migrate
docker-compose exec db psql -U user -d symfony -c "\dt"
docker-compose exec db psql -U user -d symfony -c "SELECT * FROM customers;"
# for test
docker-compose exec test_db psql -U user -d symfony_test -c "\dt"
docker-compose exec test_db psql -U user -d symfony_test -c "SELECT * FROM customers;"
docker-compose exec -w "/var/www/html/customer-api" php-fpm ./vendor/bin/phpunit
#specific tests
docker-compose exec -w "/var/www/html/customer-api" php-fpm ./vendor/bin/phpunit --filter testFailCreatePrivateCustomer
docker-compose exec -w "/var/www/html/customer-api" php-fpm php bin/console debug:router | grep doc