fix customer bind route

fix customer test
This commit is contained in:
Jérémy Munsch 2021-08-04 13:00:23 +02:00
parent 3619593485
commit 89c5f11dc4
4 changed files with 50 additions and 29 deletions

View file

@ -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());
});
}
}

View file

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
@ -21,8 +21,8 @@
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<!-- <server name="DB_CONNECTION" value="sqlite"/> -->
<!-- <server name="DB_DATABASE" value=":memory:"/> -->
<server name="DB_CONNECTION" value="mysql"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>

View file

@ -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');
}
}

View file

@ -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());
}
}