From 89c5f11dc44ba88d89cfb483d0ec478215e24399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Wed, 4 Aug 2021 13:00:23 +0200 Subject: [PATCH] fix customer bind route fix customer test --- app/Providers/RouteServiceProvider.php | 8 ++--- phpunit.xml | 10 +++--- tests/Feature/ApiV2/ApiBase.php | 15 ++++++--- tests/Feature/ApiV2/CustomerTest.php | 46 ++++++++++++++++++-------- 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index b9f1b72..67d2595 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -38,10 +38,8 @@ class RouteServiceProvider extends ServiceProvider Route::bind('customer', function ($value) { return Customer::whereHas('infos', function ($q) use ($value) { $q->where('customer_infos.hashcode', $value); - }) - ->orWhere('id', $value) - ->orWhere('email', $value)->firstOrFail() - ->firstOrFail(); + })->first() ?? (\ctype_digit($value) ? Customer::where('id', '=', $value)->first() : null) ?? + Customer::where('email', '=', $value)->firstOrFail(); }); } @@ -53,7 +51,7 @@ class RouteServiceProvider extends ServiceProvider protected function configureRateLimiting() { RateLimiter::for('api', function (Request $request) { - return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip()); + return Limit::perMinute(config('api.apiRequestRatePerMinute'))->by(optional($request->user())->id ?: $request->ip()); }); } } diff --git a/phpunit.xml b/phpunit.xml index 5a77d2a..b754467 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,8 +1,8 @@ @@ -21,8 +21,8 @@ - - + + diff --git a/tests/Feature/ApiV2/ApiBase.php b/tests/Feature/ApiV2/ApiBase.php index 5b1fdbb..493d74b 100644 --- a/tests/Feature/ApiV2/ApiBase.php +++ b/tests/Feature/ApiV2/ApiBase.php @@ -64,16 +64,21 @@ class ApiBase extends BaseTestCase } /** + * Get Customer models with given status (active or not) + * + * @param boolean $status the customer status for a brand * @return array */ - protected function getCustomer() + protected function getCustomer(bool $status = true): array { /** @var \App\Models\Costumer $customer */ - $customer = Customer::whereHas('logins', function ($q) { - $q->where('status', true); + $customer = Customer::whereHas('logins', function ($q) use ($status) { + $q->where('status', $status); })->inRandomOrder()->firstOrFail(); + /** @var \App\Models\Login $login */ + $login = $customer->logins()->where('status', $status)->firstOrFail(); /** @var \App\Models\Brand $brand */ - $brand = $customer->logins()->where('status', true)->firstOrFail()->brand; + $brand = $login->brand; /** @var \App\Models\Service $service */ $service = $brand->services()->firstOrFail(); @@ -86,6 +91,6 @@ class ApiBase extends BaseTestCase // get customer info $info = $customer->info($brand)->firstOrFail(); - return compact('customer', 'service', 'brand', 'info'); + return compact('customer', 'service', 'login', 'brand', 'info'); } } diff --git a/tests/Feature/ApiV2/CustomerTest.php b/tests/Feature/ApiV2/CustomerTest.php index c27f907..ed36117 100644 --- a/tests/Feature/ApiV2/CustomerTest.php +++ b/tests/Feature/ApiV2/CustomerTest.php @@ -5,6 +5,7 @@ namespace Tests\Feature\ApiV2; use App\Enums\ApiError; use App\Enums\ApiStatus; use App\Enums\Country; +use App\Models\Customer; use App\Models\CustomerInfo; use App\Models\CustomerLogin; use Illuminate\Database\Eloquent\Collection; @@ -129,7 +130,7 @@ class CustomerTest extends ApiBase */ public function testActivateCustomer() { - extract($this->getCustomer()); + extract($this->getCustomer(false)); $response = $this->callApiWithToken($service, 'post', route( 'apiV2.customer.activate', @@ -210,7 +211,10 @@ class CustomerTest extends ApiBase { extract($this->getCustomer()); - $response = $this->callApiWithToken($service, 'get', route('apiV2.customer.show', ['customer' => $info->hashcode])); + $response = $this->callApiWithToken($service, 'get', route( + 'apiV2.customer.show', + ['customer' => $info->hashcode] + )); $json = $response->decodeResponseJson(); $this->assertApiResponseStatusIs($json, ApiStatus::OK()); @@ -226,9 +230,19 @@ class CustomerTest extends ApiBase $csClassName = \array_slice(explode('\\', get_class($customer)), -1)[0]; - $response = $this->callApiWithToken($service, 'get', route('apiV2.customer.show', ['customer' => Str::uuid()])); + $uuid = Str::uuid(); + + $response = $this->callApiWithToken($service, 'get', route( + 'apiV2.customer.show', + ['customer' => $uuid] + )); $json = $response->decodeResponseJson(); + /** @var \App\Models\Customer */ + $customer = Customer::whereHas('infos', function ($q) use ($uuid) { + $q->where('hashcode', $uuid); + })->first(); + $this->assertApiResponseStatusIs($json, ApiStatus::KO()); $this->assertEquals(ApiError::modelNotFound()->value, $json->json('data.code')); $this->assertEquals( @@ -243,16 +257,20 @@ class CustomerTest extends ApiBase public function testApiDestroyCustomer() { extract($this->getCustomer()); - $brands = $customer->brands()->get(); - foreach ($brands as $brand) { - $service = $brand->services()->firstOrFail(); - $response = $this->callApiWithToken($service, 'delete', route( - 'apiV2.customer.destroy', - ['customer' => $info->hashcode] - )); - $json = $response->decodeResponseJson(); - $this->assertApiResponseStatusIs($json, ApiStatus::OK()); - $this->assertEmpty($json->json('data')); - } + $service = $brand->services()->firstOrFail(); + $response = $this->callApiWithToken($service, 'delete', route( + 'apiV2.customer.destroy', + ['customer' => $info->hashcode] + )); + $json = $response->decodeResponseJson(); + $this->assertApiResponseStatusIs($json, ApiStatus::OK()); + $this->assertEmpty($json->json('data')); + + $response = $this->callApiWithToken($service, 'get', route( + 'apiV2.customer.show', + ['customer' => $info->hashcode] + )); + $json = $response->decodeResponseJson(); + $this->assertApiResponseStatusIs($json, ApiStatus::KO()); } }