Skip to content

Commit

Permalink
Update API tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com>
  • Loading branch information
sampoyigi committed Nov 3, 2024
1 parent 1632fe0 commit 5b5485a
Show file tree
Hide file tree
Showing 26 changed files with 795 additions and 35 deletions.
14 changes: 7 additions & 7 deletions src/ApiResources/Requests/MenuItemOptionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class MenuItemOptionRequest extends FormRequest
public function attributes()
{
return [
'menu_id', lang('igniter.cart::default.menus.label_option'),
'option_id', lang('igniter.cart::default.menus.label_option_id'),
'priority', lang('igniter.cart::default.menus.label_option'),
'required', lang('igniter.cart::default.menus.label_option_required'),
'min_selected', lang('igniter.cart::default.menus.label_min_selected'),
'max_selected', lang('igniter.cart::default.menus.label_max_selected'),
'menu_option_values.*', lang('admin::lang.label_option_value_id'),
'menu_id' => lang('igniter.cart::default.menus.label_option'),
'option_id' => lang('igniter.cart::default.menus.label_option_id'),
'priority' => lang('igniter.cart::default.menus.label_option'),
'required' => lang('igniter.cart::default.menus.label_option_required'),
'min_selected' => lang('igniter.cart::default.menus.label_min_selected'),
'max_selected' => lang('igniter.cart::default.menus.label_max_selected'),
'menu_option_values.*' => lang('admin::lang.label_option_value_id'),
];
}

Expand Down
7 changes: 2 additions & 5 deletions src/ApiResources/Requests/OrderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Igniter\Api\ApiResources\Requests;

use Igniter\System\Classes\FormRequest;
use Illuminate\Support\Facades\Request;

class OrderRequest extends FormRequest
{
Expand All @@ -30,8 +29,6 @@ public function attributes()

public function rules()
{
$method = Request::method();

$rules = [
'first_name' => ['between:1,48'],
'last_name' => ['between:1,48'],
Expand All @@ -47,14 +44,14 @@ public function rules()
'is_processed' => ['integer'],
];

if ($method == 'post') {
if ($this->method() == 'post') {
$rules['first_name'][] = 'required';
$rules['last_name'][] = 'required';
$rules['order_type'][] = 'required';
$rules['customer_id'][] = 'required';
}

if (Request::input('order_type', 'collection') == 'delivery') {
if ($this->input('order_type', 'collection') == 'delivery') {
$rules['address_id'] = ['integer'];
$rules['address.address_1'] = ['required', 'min:3', 'max:128'];
$rules['address.address_2'] = ['sometimes', 'min:3', 'max:128'];
Expand Down
21 changes: 0 additions & 21 deletions src/Classes/ApiManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Igniter\Api\Models\Resource;
use Igniter\Flame\Igniter;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -46,26 +45,6 @@ public function getCurrentAction()
return Str::afterLast(Route::currentRouteAction(), '@');
}

public function buildResource($name, $model, $meta = [])
{
$controller = $this->parseName($name);
$singularController = str_singular($controller);
$namespace = '\\Igniter\\Api\\ApiResources';

Artisan::call('create:apiresource', [
'extension' => 'Igniter.Api',
'controller' => $controller,
'--model' => $model,
'--meta' => $meta,
]);

if (!class_exists($controllerName = $namespace."\\{$controller}")) {
return [null, null];
}

return [$controllerName, $namespace."\\Transformers\\{$singularController}Transformer"];
}

protected function loadResources()
{
Resource::syncAll();
Expand Down
29 changes: 29 additions & 0 deletions src/Database/Factories/ResourceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Igniter\Api\Database\Factories;

use Igniter\Flame\Database\Factories\Factory;

class ResourceFactory extends Factory
{
protected $model = \Igniter\Api\Models\Resource::class;

public function definition(): array
{
return [
'name' => $this->faker->sentence(2),
'endpoint' => $this->faker->word(),
'description' => $this->faker->paragraph(),
'meta' => [
'actions' => ['index', 'show', 'store', 'update', 'destroy'],
'authorization' => [
'index' => 'all',
'show' => 'admin',
'store' => 'admin',
'update' => 'admin',
'destroy' => 'admin',
],
],
];
}
}
21 changes: 21 additions & 0 deletions src/Database/Factories/TokenFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Igniter\Api\Database\Factories;

use Igniter\Flame\Database\Factories\Factory;

class TokenFactory extends Factory
{
protected $model = \Igniter\Api\Models\Token::class;

public function definition(): array
{
return [
'tokenable_type' => 'users',
'tokenable_id' => 1,
'name' => $this->faker->sentence(2),
'token' => $this->faker->sha256,
'abilities' => ['*'],
];
}
}
13 changes: 12 additions & 1 deletion src/Http/Requests/ResourceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@

class ResourceRequest extends FormRequest
{
public function rules()
public function attributes(): array
{
return [
'name' => lang('igniter.api::default.label_name'),
'description' => lang('igniter.api::default.label_description'),
'endpoint' => lang('igniter.api::default.label_endpoint'),
'meta.actions' => lang('igniter.api::default.label_actions'),
'meta.authorization' => lang('igniter.api::default.label_authorization'),
];
}

public function rules(): array
{
return [
'name' => ['required', 'min:2', 'max:128', 'string'],
Expand Down
2 changes: 2 additions & 0 deletions src/Models/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Igniter\Api\Models;

use Igniter\Flame\Database\Factories\HasFactory;
use Igniter\Flame\Database\Model;
use Igniter\Flame\Database\Traits\HasPermalink;
use Igniter\Flame\Mail\Markdown;
Expand All @@ -13,6 +14,7 @@
*/
class Resource extends Model
{
use HasFactory;
use HasPermalink;

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Models/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Igniter\Api\Models;

use Igniter\Flame\Database\Factories\HasFactory;
use Igniter\User\Models\Customer;
use Igniter\User\Models\User;
use Illuminate\Support\Str;
Expand All @@ -13,6 +14,8 @@
*/
class Token extends PersonalAccessToken
{
use HasFactory;

/**
* @var string The database table used by the model.
*/
Expand Down
40 changes: 40 additions & 0 deletions tests/ApiResources/Requests/MenuItemOptionRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Igniter\Api\Tests\ApiResources\Requests;

use Igniter\Api\ApiResources\Requests\MenuItemOptionRequest;

it('returns correct attribute labels', function() {
$request = new MenuItemOptionRequest();

$attributes = $request->attributes();

expect($attributes)->toHaveKey('menu_id', lang('igniter.cart::default.menus.label_option'))
->and($attributes)->toHaveKey('option_id', lang('igniter.cart::default.menus.label_option_id'))
->and($attributes)->toHaveKey('priority', lang('igniter.cart::default.menus.label_option'))
->and($attributes)->toHaveKey('required', lang('igniter.cart::default.menus.label_option_required'))
->and($attributes)->toHaveKey('min_selected', lang('igniter.cart::default.menus.label_min_selected'))
->and($attributes)->toHaveKey('max_selected', lang('igniter.cart::default.menus.label_max_selected'))
->and($attributes)->toHaveKey('menu_option_values.*', lang('admin::lang.label_option_value_id'));
});

it('returns correct validation rules', function() {
$request = new MenuItemOptionRequest();

$rules = $request->rules();

expect($rules)->toHaveKey('menu_id')
->and($rules)->toHaveKey('option_id')
->and($rules)->toHaveKey('priority')
->and($rules)->toHaveKey('required')
->and($rules)->toHaveKey('min_selected')
->and($rules)->toHaveKey('max_selected')
->and($rules)->toHaveKey('menu_option_values.*')
->and($rules['menu_id'])->toContain('nullable', 'integer')
->and($rules['option_id'])->toContain('required', 'integer')
->and($rules['priority'])->toContain('integer')
->and($rules['required'])->toContain('boolean')
->and($rules['min_selected'])->toContain('integer', 'lte:max_selected')
->and($rules['max_selected'])->toContain('integer', 'gte:min_selected')
->and($rules['menu_option_values.*'])->toContain('array');
});
49 changes: 49 additions & 0 deletions tests/ApiResources/Requests/MenuOptionRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Igniter\Api\Tests\ApiResources\Requests;

use Igniter\Api\ApiResources\Requests\MenuOptionRequest;

it('returns correct attribute labels', function() {
$request = new MenuOptionRequest();

$attributes = $request->attributes();

expect($attributes)->toHaveKey('option_name', lang('admin::lang.menu_options.label_option_group_name'))
->and($attributes)->toHaveKey('display_type', lang('admin::lang.menu_options.label_display_type'))
->and($attributes)->toHaveKey('priority', lang('admin::lang.menu_options.label_priority'))
->and($attributes)->toHaveKey('locations.*', lang('admin::lang.label_location'))
->and($attributes)->toHaveKey('option_values.*.option_value_id', lang('admin::lang.label_option_value_id'))
->and($attributes)->toHaveKey('option_values.*.option_id', lang('admin::lang.label_option_id'))
->and($attributes)->toHaveKey('option_values.*.value', lang('admin::lang.menu_options.label_option_value'))
->and($attributes)->toHaveKey('option_values.*.price', lang('admin::lang.menu_options.label_option_price'))
->and($attributes)->toHaveKey('option_values.*.priority', lang('admin::lang.menu_options.label_option_price'))
->and($attributes)->toHaveKey('option_values.*.allergens.*', lang('igniter.cart::default.menus.label_allergens'));
});

it('returns correct validation rules', function() {
$request = new MenuOptionRequest();

$rules = $request->rules();

expect($rules)->toHaveKey('option_name')
->and($rules)->toHaveKey('display_type')
->and($rules)->toHaveKey('priority')
->and($rules)->toHaveKey('locations.*')
->and($rules)->toHaveKey('option_values.*.option_value_id')
->and($rules)->toHaveKey('option_values.*.option_id')
->and($rules)->toHaveKey('option_values.*.value')
->and($rules)->toHaveKey('option_values.*.price')
->and($rules)->toHaveKey('option_values.*.priority')
->and($rules)->toHaveKey('option_values.*.allergens.*')
->and($rules['option_name'])->toContain('required', 'min:2', 'max:32')
->and($rules['display_type'])->toContain('required', 'alpha')
->and($rules['priority'])->toContain('integer')
->and($rules['locations.*'])->toContain('integer')
->and($rules['option_values.*.option_value_id'])->toContain('integer')
->and($rules['option_values.*.option_id'])->toContain('integer')
->and($rules['option_values.*.value'])->toContain('min:2', 'max:128')
->and($rules['option_values.*.price'])->toContain('numeric', 'min:0')
->and($rules['option_values.*.priority'])->toContain('integer')
->and($rules['option_values.*.allergens.*'])->toContain('integer');
});
68 changes: 68 additions & 0 deletions tests/ApiResources/Requests/OrderRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Igniter\Api\Tests\ApiResources\Requests;

use Igniter\Api\ApiResources\Requests\OrderRequest;

it('returns correct attribute labels', function() {
$request = new OrderRequest();

$attributes = $request->attributes();

expect($attributes)->toHaveKey('first_name', lang('igniter.cart::default.checkout.label_first_name'))
->and($attributes)->toHaveKey('last_name', lang('igniter.cart::default.checkout.label_last_name'))
->and($attributes)->toHaveKey('email', lang('igniter.cart::default.checkout.label_email'))
->and($attributes)->toHaveKey('telephone', lang('igniter.cart::default.checkout.label_telephone'))
->and($attributes)->toHaveKey('comment', lang('igniter.cart::default.checkout.label_comment'))
->and($attributes)->toHaveKey('payment', lang('igniter.cart::default.checkout.label_payment_method'))
->and($attributes)->toHaveKey('order_type', lang('igniter.cart::default.checkout.label_order_type'))
->and($attributes)->toHaveKey('address_id', lang('igniter.cart::default.checkout.label_address'))
->and($attributes)->toHaveKey('address.address_1', lang('igniter.cart::default.checkout.label_address_1'))
->and($attributes)->toHaveKey('address.address_2', lang('igniter.cart::default.checkout.label_address_2'))
->and($attributes)->toHaveKey('address.city', lang('igniter.cart::default.checkout.label_city'))
->and($attributes)->toHaveKey('address.state', lang('igniter.cart::default.checkout.label_state'))
->and($attributes)->toHaveKey('address.postcode', lang('igniter.cart::default.checkout.label_postcode'))
->and($attributes)->toHaveKey('address.country_id', lang('igniter.cart::default.checkout.label_country'));
});

it('returns correct validation rules', function() {
$request = new OrderRequest();
$request->setMethod('post');
$request->merge([
'order_type' => 'delivery',
]);

$rules = $request->rules();

expect($rules)->toHaveKey('first_name')
->and($rules)->toHaveKey('last_name')
->and($rules)->toHaveKey('email')
->and($rules)->toHaveKey('telephone')
->and($rules)->toHaveKey('comment')
->and($rules)->toHaveKey('payment')
->and($rules)->toHaveKey('order_type')
->and($rules)->toHaveKey('address_id')
->and($rules)->toHaveKey('address.address_1')
->and($rules)->toHaveKey('address.address_2')
->and($rules)->toHaveKey('address.city')
->and($rules)->toHaveKey('address.state')
->and($rules)->toHaveKey('address.postcode')
->and($rules)->toHaveKey('address.country_id')
->and($rules)->toHaveKey('customer_id')
->and($rules)->toHaveKey('order_menus')
->and($rules)->toHaveKey('order_totals')
->and($rules)->toHaveKey('status_id')
->and($rules)->toHaveKey('is_processed')
->and($rules['first_name'])->toContain('between:1,48')
->and($rules['last_name'])->toContain('between:1,48')
->and($rules['email'])->toContain('sometimes', 'required', 'email:filter', 'max:96')
->and($rules['telephone'])->toContain('string')
->and($rules['comment'])->toContain('max:500')
->and($rules['order_type'])->toContain('alpha_dash')
->and($rules['payment'])->toContain('sometimes', 'required', 'alpha_dash')
->and($rules['customer_id'])->toContain('integer')
->and($rules['order_menus'])->toContain('array')
->and($rules['order_totals'])->toContain('array')
->and($rules['status_id'])->toContain('integer')
->and($rules['is_processed'])->toContain('integer');
});
Loading

0 comments on commit 5b5485a

Please sign in to comment.