-
Notifications
You must be signed in to change notification settings - Fork 7
Laravel Example
The following is an example Laravel application that is tested using Dredd and dredd-hooks-php.
It shows how dredd-hooks-php can be utilized to seed a database for API endpoints being tested by Dredd.
##Important details
All of the files referenced in this section assume the root directory is examples/laravel
The laravel.apib file
# My Api
## GET /user
+ Response 200 (application/json;charset=utf-8)
{
"user": {
"name": "John Doe",
"age": 22
}
}
## GET /users
+ Response 200 (application/json;charset=utf-8)
{
"users": [
{
"name": "Dom",
"email": "[email protected]"
}
]
}
The laravel routes file (app/Http/routes.php)
<?php
use App\User;
use Illuminate\Http\Response;
Route::get('/user', function () {
$data = json_encode([
'user' => [
'name' => 'John Doe',
'age' => 22
]
]);
return (new Response($data, 200))->header('Content-Type', 'application/json;charset=utf-8');
});
Route::get('/users', function() {
$users = User::all();
return (new Response(['users' => $users], 200))->header('Content-Type', 'application/json;charset=utf-8');
});
This file defines the two routes expected in the api blueprint file. As you can see, the /user
endpoint has the output hardcoded. The /users
endpoint however retrieves data from the database. In order to seed the database before Dredd hits the endpoint, we can use a before hook.
The following hookfile uses Laravel's database manager along with another composer package laracasts/testdummy
that allows for easily creating fixtures. dredd-hooks-php does not lock you into using any specific package. If you wanted to use something like Doctrine ORM you would just need to instantiate the necessary class with the proper database configuration and then seed the database in which ever hook you need.
Here is the hookfile (tests/dredd/hooks/hookfile.php)
<?php
use App\User;
use Dredd\Hooks;
use Illuminate\Database\Capsule\Manager;
use Laracasts\TestDummy\Factory;
// instantiate class that will interact with the database
$manager = new Manager();
// get database configuration. SQLite was used for this and lives at storage/database.sqlite
$config = require __DIR__ . "/../../../config/database.php";
// Add database connection, set global for Laracasts\TestDummy\Factory, and begin transaction
$manager->addConnection($config['connections']['sqlite']);
$manager->bootEloquent();
$manager->setAsGlobal();
$manager->getConnection()->beginTransaction();
$factory = new Factory(__DIR__ . "/../../../tests/factories");
Hooks::before('/users > GET', function(&$transaction) {
// seed the database with a user
Factory::create(User::class, [
'name' => 'Dom',
'email' => '[email protected]'
]);
});
Hooks::after('/users > GET', function(&$transaction) use($manager) {
// rollback database so data is not persisted
$manager->getConnection()->rollback();
});
##Running the example
-
Install dredd globally
npm install -g dredd
-
Clone the repository
git clone https://github.com/ddelnano/dredd-hooks-php.git
-
Change into the
examples/laravel
directory. -
Install the dependencies with composer
composer install
-
Run dredd with dredd-hooks-php
dredd ./laravel.apib http://localhost:8001 --server "php -S 0.0.0.0:8001 -t public/" --language vendor/bin/dredd-hooks-php --hookfiles tests/dredd/hooks/hookfile.php