ragboyjr / laravel-api-skeleton
The Laravel Framework.
Installs: 12
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^7.1.3
- doctrine/dbal: ^2.6
- fideloper/proxy: ^4.0
- krak/doctrine-oauth2: ^0.1.1
- krak/fn: ^0.1.7
- krak/marshal: ^0.2.3
- krak/validation: ^0.3.9
- laravel-doctrine/migrations: ^1.1
- laravel-doctrine/orm: 1.4.*
- laravel/framework: 5.6.*
- predis/predis: ^1.1
- symfony/psr-http-message-bridge: ^1.0
- zendframework/zend-diactoros: ^1.7
Requires (Dev)
- filp/whoops: ^2.0
- fzaninotto/faker: ^1.4
- mockery/mockery: ^1.0
- nunomaduro/collision: ^2.0
- phpunit/phpunit: ^7.0
README
Install via:
composer create-project ragboyjr/laravel-api-skeleton {name}
Once installed, you'll want to run the following:
mv .editor/project.sublime-project .editor/acme-svc.sublime-project
find . -type d \( -path ./vendor -o -path ./.git \) -prune -o -type f -print -exec sed -i '' -e 's/{project}/acme-svc/g' {} \;
find . -type d \( -path ./vendor -o -path ./.git \) -prune -o -type f -print -exec sed -i '' -e 's/{project_underscore}/acme_svc/g' {} \;
find . -type d \( -path ./vendor -o -path ./.git \) -prune -o -type f -print -exec sed -i '' -e 's/{project_title}/Acme Svc/g' {} \;
find . -type d \( -path ./vendor -o -path ./.git \) -prune -o -type f -print -exec sed -i '' -e 's/{project_doctrine_ns}/Acme/g' {} \;
find ./tests -type f -print -exec sed -i '' -e 's/namespace App\\/namespace Acme\\Svc\\/g' {} \;
./artisan app:name 'Acme\Svc'
make ignored-files
Once completed, start docker via docker-compose up
and run ./artisan doctrine:migrations:diff
to generate the initial migration.
Development Setup
- copy over all the example files:
make ignored-files
- Start the docker containers:
docker-compose up
- Exec into the
{project}
container viadocker exec -it {project} bash
- Run
make setup
to initialize the repo. This will initialize all aspects of the repo, you can see what this script does by viewing theMakefile
.
At the end of make setup
, it should have generated a long access token, you'll need to copy that into the .env and set the INTEGRATION_API_TOKEN
value.
To verify that you've been setup correctly, you need to run make test
and then make refresh-db test-integration
. These will run the entire test suite. If any fail, then there was an issue with setup that will need to be resolved.
Development
This app is built with Laravel and Doctrine.
Migrations
From inside of the {project}
container, run ./artisan doctrine:migrations:migrate
to load the database. You'll need to run this every time there is a new migration. You should be in a habit to just run migrations when you pull the latest code from stg
.
Generating Migrations
The best way to manage migrations is by configuring the metadata yaml files first, then simply running ./artisan doctrine:migrations:diff
. This will create a migration file that will take the diff of your current local db to the migration mapping files. Keep this in mind because if you forgot to run ./artisan doctrine:migrations:migrate
to bring your local db up to the latest, running the migrations-diff command could re-apply some database changes which will break the migrations.
Testing
Unit Testing
make test
Integration Testing
make test-integration
Make sure all tests pass before pushing up.
Workflow
The easiest way to develop with phpunit and testing is to create specific tests for whatever you are doing, and then run them individually with phpunit.
For example, if I'm working on the tests/Integration/RepApiTest
testApproveRepNotFound
method, I'll run inside of the docker container the following command to test only that method:
./vendor/bin/phpunit tests/Integration/RepAPITest.php --filter=testApproveRepNotFound
Playground
playground.php
is a file that allows you to quickly prototype/test code in the current app. It's just a simple console command, but the file is ignored, so that you don't have to worry about comitting those temp changes.
You can run the playground via: ./artisan playground
.
Trouble Shooting
Here are some common errors you can get when developing and here's how to fix them.
Status 500 in Tests
When asserting a status code in an integration test, and you get a status 500, the easiest way to start debugging is to just check the storage/logs/laravel.log and find the exception that occurred.
Exception: Unresolvable dependency resolving [Parameter #0 [ $entityName ]] in class Doctrine\ORM\Mapping\ClassMetadataInfo
This can occur when you create a Repository class for an entity, and then type hint for the specific repository class but don't register the repository class in a service provider. To resolve this issue, you need to register the repository in the module's service provider like this:
addEntityRepo($this->app, Rep::class);
The addEntityRepo
expects the laravel app instance and the class name of the entity not the repository class.
You can see examples of register repositories in the service provider in the different modules in the app/Model
folder.