ldc-crm/tests/Feature/ApiV2/CustomerTest.php
2021-07-28 14:18:47 +02:00

269 lines
11 KiB
PHP

<?php
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;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\CreatesApplication;
class CustomerTest extends ApiBase
{
use CreatesApplication;
use DatabaseTransactions;
public function testCustomerRelations()
{
extract($this->getCustomer());
$infos = $customer->infos;
$logins = $customer->logins;
$brands = $customer->brands;
$optInHistory = $customer->optInHistory;
$this->assertInstanceOf(Collection::class, $infos);
$this->assertInstanceOf(Collection::class, $logins);
$this->assertInstanceOf(Collection::class, $brands);
$this->assertInstanceOf(Collection::class, $optInHistory);
$this->assertInstanceOf(CustomerInfo::class, $customer->info($brands->first())->first());
$this->assertInstanceOf(CustomerLogin::class, $customer->login($brands->first())->first());
}
/**
* route: apiV2.customer.authenticate
*/
public function testApiAuthenticateCustomer()
{
extract($this->getCustomer());
$response = $this->callApiWithToken(
$service,
'post',
route('apiV2.customer.authenticate', compact('customer')),
[
'email' => $customer->email,
'password' => 'testPassword'
]
);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$this->assertNotEmpty($json->json('data'));
$this->assertNotEmpty($json->json('data.crm_id'));
$this->assertNotEmpty($json->json('data.firstname'));
$this->assertNotEmpty($json->json('data.lastname'));
$this->assertNotEmpty($json->json('data.birth_date'));
$this->assertNotEmpty($json->json('data.address'));
$this->assertNotEmpty($json->json('data.phone'));
$this->assertNotEmpty($json->json('data.country'));
$this->assertNotEmpty($json->json('data.city'));
$this->assertNotEmpty($json->json('data.postcode'));
$this->assertNotEmpty($json->json('data.hashcode'));
}
/**
* route: apiV2.customer.store
*/
public function testApiStoreCustomer()
{
extract($this->getCustomer());
$data = [
'firstname' => 'Jean<html></html>',
'lastname' => 'Bond',
'birth_date' => \date('1992-03-08'),
'address' => '10 impasse Maryse Bastié',
'email' => 'warthogdj@hotmail.fr',
'password' => '123.Mot.De.P4sse',
'phone' => '+33654821562',
'country' => (string)Country::france(),
'city' => 'Connerré',
'postcode' => '72160',
'opt_in' => true
];
$response = $this->callApiWithToken($service, 'post', route('apiV2.customer.store'), $data);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$this->assertNotEmpty($json->json('data.crm_id'));
$this->assertNotEmpty($json->json('data.firstname'));
$this->assertNotEmpty($json->json('data.lastname'));
$this->assertNotEmpty($json->json('data.birth_date'));
$this->assertNotEmpty($json->json('data.address'));
$this->assertNotEmpty($json->json('data.phone'));
$this->assertNotEmpty($json->json('data.country'));
$this->assertNotEmpty($json->json('data.city'));
$this->assertNotEmpty($json->json('data.postcode'));
$this->assertNotEmpty($json->json('data.hashcode'));
$this->assertEquals(\strip_tags($data['firstname']), $json->json('data.firstname'));
$this->assertEquals($data['lastname'], $json->json('data.lastname'));
$this->assertEquals($data['birth_date'], $json->json('data.birth_date'));
$this->assertEquals($data['address'], $json->json('data.address'));
$this->assertEquals($data['phone'], $json->json('data.phone'));
$this->assertEquals($data['country'], $json->json('data.country'));
$this->assertEquals($data['city'], $json->json('data.city'));
$this->assertEquals($data['postcode'], $json->json('data.postcode'));
$response = $this->callApiWithToken($service, 'post', route('apiV2.customer.store'), $data);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
$this->assertEquals(ApiError::customerExists()->value, $json->json('data.code'));
$this->assertEquals(ApiError::customerExists()->label, $json->json('data.message'));
$data['password'] = 'password';
$response = $this->callApiWithToken($service, 'post', route('apiV2.customer.store'), $data);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
$this->assertEquals(ApiError::validationFailed()->value, $json->json('data.code'));
$this->assertEquals(
ApiError::validationFailed()->label . ', The given data was invalid.',
$json->json('data.message')
);
}
/**
* route: apiV2.customer.activate
*/
public function testActivateCustomer()
{
/** @var \App\Models\Costumer $customer */
$customer = Customer::whereHas('logins', function ($q) {
$q->where('status', false);
})->inRandomOrder()->firstOrFail();
/** @var \App\Models\Brand $brand */
$brand = $customer->logins()->where('status', false)->firstOrFail()->brand;
/** @var \App\Models\Service $service */
$service = $brand->services()->firstOrFail();
$response = $this->callApiWithToken($service, 'post', route(
'apiV2.customer.activate',
compact('customer')
), []);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$response = $this->callApiWithToken($service, 'post', route(
'apiV2.customer.activate',
compact('customer')
), []);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
$this->assertEquals(ApiError::customerAlreadyActive()->value, $json->json('data.code'));
$this->assertEquals(ApiError::customerAlreadyActive()->label, $json->json('data.message'));
}
/**
* route: apiV2.customer.update
*/
public function testApiUpdateCustomer()
{
extract($this->getCustomer());
$data = [
'firstname' => 'Jean2',
'lastname' => 'Bond2',
'birth_date' => \date('1992-03-09'),
'address' => '10 impasse Maryse Bastié2',
'email' => 'warthogdj2@hotmail.fr',
'password' => '123.Mot.De.P4sseNew',
'phone' => '+33654821563',
'country' => (string)Country::bahamas(),
'city' => 'Connerré2',
'postcode' => '72161',
'opt_in' => 0
];
foreach ($data as $rowName => $row) {
$response = $this->callApiWithToken($service, 'patch', route(
'apiV2.customer.update',
compact('customer')
), [$rowName => $row]);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
if ($rowName != 'password') {
$this->assertNotNull($json->json('data.' . $rowName), "Failed asserting $rowName is not null");
$this->assertEquals(
$row,
$json->json('data.' . $rowName),
"Failed asserting $rowName is equal to $row"
);
} else {
$this->assertNull($json->json('data.' . $rowName), "Failed asserting $rowName is null");
$this->assertEquals(
null,
$json->json('data.' . $rowName),
"Failed asserting $rowName is equal to $row"
);
}
}
$response = $this->callApiWithToken($service, 'patch', route('apiV2.customer.update', compact('customer')), [
'address2' => 'anything'
]);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
$this->assertEquals(ApiError::validationFailed()->value, $json->json('data.code'));
$this->assertEquals(
ApiError::validationFailed()->label . ', The given data was invalid.',
$json->json('data.message')
);
}
/**
* route: apiV2.customer.show
*/
public function testApiGetCustomer()
{
extract($this->getCustomer());
$response = $this->callApiWithToken($service, 'get', route('apiV2.customer.show', compact('customer')));
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$this->assertNotEmpty($json->json('data.crm_id'));
$this->assertNotEmpty($json->json('data.firstname'));
$this->assertNotEmpty($json->json('data.lastname'));
$this->assertNotEmpty($json->json('data.birth_date'));
$this->assertNotEmpty($json->json('data.address'));
$this->assertNotEmpty($json->json('data.phone'));
$this->assertNotEmpty($json->json('data.country'));
$this->assertNotEmpty($json->json('data.city'));
$this->assertNotEmpty($json->json('data.postcode'));
$this->assertNotEmpty($json->json('data.hashcode'));
$csClassName = \array_slice(explode('\\', get_class($customer)), -1)[0];
$customer = 9999999;
$response = $this->callApiWithToken($service, 'get', route('apiV2.customer.show', compact('customer')));
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
$this->assertEquals(ApiError::modelNotFound()->value, $json->json('data.code'));
$this->assertEquals(
sprintf(ApiError::modelNotFound()->label, $csClassName),
$json->json('data.message')
);
}
/**
* route: apiV2.customer.destroy
*/
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',
compact('customer')
));
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$this->assertEmpty($json->json('data'));
}
}
}