-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from luttje/feature/add-test-suite
Add test suite
- Loading branch information
Showing
20 changed files
with
548 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
use Illuminate\Routing\Route as RoutingRoute; | ||
use Rupadana\ApiService\Tests\Fixtures\Database\Seeders\ProductsSeeder; | ||
use Rupadana\ApiService\Tests\Fixtures\Models\Product; | ||
|
||
it('can make routes for a product resource', function () { | ||
$routes = collect(app('router')->getRoutes())->map(function (RoutingRoute $route) { | ||
return implode('|', $route->methods()) . ' ' . $route->uri(); | ||
}); | ||
|
||
// The route name is customized to `our-products` in the `ProductApiService` class | ||
expect($routes)->toContain('POST api/our-products'); | ||
expect($routes)->toContain('PUT api/our-products/{id}'); | ||
expect($routes)->toContain('DELETE api/our-products/{id}'); | ||
expect($routes)->toContain('GET|HEAD api/our-products'); | ||
expect($routes)->toContain('GET|HEAD api/our-products/{id}'); | ||
}); | ||
|
||
it('can return a list of products with allowed attributes', function () { | ||
$this->seed(ProductsSeeder::class); | ||
|
||
$response = $this->get('/api/our-products') | ||
->assertStatus(200); | ||
|
||
$products = Product::all()->map(function ($product) { | ||
return [ | ||
'name' => $product['name'], | ||
'description' => $product['description'], | ||
'price' => $product['price'], | ||
]; | ||
})->toArray(); | ||
|
||
foreach ($products as $product) { | ||
$response->assertJsonFragment($product); | ||
} | ||
|
||
// Check that the slug (hidden) is not returned | ||
$response->assertJsonMissing([ | ||
'slug' => 't-shirt', | ||
]); | ||
}); | ||
|
||
it('can return a list of products with selected fields', function () { | ||
$this->seed(ProductsSeeder::class); | ||
|
||
$response = $this->get('/api/our-products?fields[products]=name,price') | ||
->assertStatus(200); | ||
|
||
$response->assertJsonFragment([ | ||
'name' => 'T-Shirt', | ||
'price' => 500, | ||
]); | ||
}); | ||
|
||
it('throws when selecting a field that is not allowed', function () { | ||
$this->seed(ProductsSeeder::class); | ||
|
||
$this->get('/api/our-products?fields[products]=name,slug,price') | ||
->assertStatus(400) | ||
->assertJsonFragment([ | ||
'message' => 'Requested field(s) `products.slug` are not allowed.', | ||
]); | ||
})->throws(\Spatie\QueryBuilder\Exceptions\InvalidFieldQuery::class); | ||
|
||
it('can return a list of products with selected sorts', function () { | ||
$this->seed(ProductsSeeder::class); | ||
|
||
$response = $this->get('/api/our-products?sort=-price') | ||
->assertStatus(200); | ||
|
||
$data = Product::all() | ||
->sortByDesc('price') | ||
->values() | ||
->map(function ($product) { | ||
return [ | ||
'name' => $product['name'], | ||
'price' => $product['price'], | ||
]; | ||
}) | ||
->toArray(); | ||
|
||
foreach ($data as $product) { | ||
$response->assertJsonFragment($product); | ||
} | ||
}); | ||
|
||
it('throws when sorting by a field that is not allowed', function () { | ||
$this->seed(ProductsSeeder::class); | ||
|
||
$this->get('/api/our-products?sort=-slug') | ||
->assertStatus(400) | ||
->assertJsonFragment([ | ||
'message' => 'Requested sort(s) `products.slug` are not allowed.', | ||
]); | ||
})->throws(\Spatie\QueryBuilder\Exceptions\InvalidSortQuery::class); | ||
|
||
it('can return a list of products with selected filters', function () { | ||
$this->seed(ProductsSeeder::class); | ||
|
||
$response = $this->get('/api/our-products?filter[name]=T-Shirt&filter[price]=500') | ||
->assertStatus(200); | ||
|
||
$response->assertJsonFragment([ | ||
'name' => 'T-Shirt', | ||
'price' => 500, | ||
]); | ||
}); | ||
|
||
it('throws when filtering by a field that is not allowed', function () { | ||
$this->seed(ProductsSeeder::class); | ||
|
||
$this->get('/api/our-products?filter[name]=T-Shirt&filter[slug]=t-shirt') | ||
->assertStatus(400) | ||
->assertJsonFragment([ | ||
'message' => 'Requested filter(s) `products.slug` are not allowed.', | ||
]); | ||
})->throws(\Spatie\QueryBuilder\Exceptions\InvalidFilterQuery::class); | ||
|
||
it('can return a list of products with a custom transformer', function () { | ||
$this->seed(ProductsSeeder::class); | ||
|
||
$response = $this->get('/api/our-products') | ||
->assertStatus(200); | ||
|
||
$product = Product::first(); | ||
|
||
$response->assertJsonFragment([ | ||
'hash' => md5($product['name']), | ||
]); | ||
}); |
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
tests/Fixtures/Database/Migrations/01_create_products_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class () extends Migration { | ||
public function up() | ||
{ | ||
Schema::create('products', function (Blueprint $table) { | ||
$table->id(); | ||
|
||
$table->string('name'); | ||
$table->string('slug')->unique(); | ||
|
||
$table->text('description')->nullable(); | ||
|
||
$table->decimal('price', 15, 2)->default(0); | ||
|
||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropIfExists('products'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Rupadana\ApiService\Tests\Fixtures\Database\Seeders; | ||
|
||
use Illuminate\Database\Seeder; | ||
use Rupadana\ApiService\Tests\Fixtures\Models\Product; | ||
|
||
class ProductsSeeder extends Seeder | ||
{ | ||
public function run() | ||
{ | ||
Product::create([ | ||
'name' => 'Jeans', | ||
'slug' => 'jeans', | ||
'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', | ||
'price' => 1000, | ||
]); | ||
|
||
Product::create([ | ||
'name' => 'T-Shirt', | ||
'slug' => 't-shirt', | ||
'description' => 'Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo.', | ||
'price' => 500, | ||
]); | ||
|
||
Product::create([ | ||
'name' => 'Shoes', | ||
'slug' => 'shoes', | ||
'description' => 'Maecenas faucibus mollis interdum. Nullam id dolor id nibh ultricies vehicula ut id elit.', | ||
'price' => 2000, | ||
]); | ||
|
||
Product::create([ | ||
'name' => 'Hat', | ||
'slug' => 'hat', | ||
'description' => 'Maecenas sed diam eget risus varius blandit sit amet non magna.', | ||
'price' => 100, | ||
]); | ||
|
||
Product::create([ | ||
'name' => 'Socks', | ||
'slug' => 'socks', | ||
'description' => 'Cras mattis consectetur purus sit amet fermentum.', | ||
'price' => 50, | ||
]); | ||
|
||
Product::create([ | ||
'name' => 'Gloves', | ||
'slug' => 'gloves', | ||
'description' => 'Maecenas sed diam eget risus varius blandit sit amet non magna.', | ||
'price' => 150, | ||
]); | ||
|
||
Product::create([ | ||
'name' => 'Jacket', | ||
'slug' => 'jacket', | ||
'description' => 'Maecenas sed diam eget risus varius blandit sit amet non magna.', | ||
'price' => 200, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Rupadana\ApiService\Tests\Fixtures\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Product extends Model | ||
{ | ||
public static array $allowedFields = [ | ||
'name', | ||
// 'slug' is not allowed | ||
'description', | ||
'price', | ||
'created_at', | ||
]; | ||
|
||
public static array $allowedSorts = [ | ||
'name', | ||
'price', | ||
'created_at', | ||
]; | ||
|
||
public static array $allowedFilters = [ | ||
'name', | ||
'price', | ||
'created_at', | ||
]; | ||
|
||
protected $guarded = []; | ||
|
||
protected $hidden = [ | ||
'slug', | ||
]; | ||
} |
Oops, something went wrong.