295 lines
11 KiB
PHP
295 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 Illuminate\Support\Str;
|
|
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()));
|
|
$this->assertInstanceOf(CustomerLogin::class, $customer->login($brands->first()));
|
|
}
|
|
|
|
/**
|
|
* route: apiV2.customers.authenticate
|
|
*/
|
|
public function testApiAuthenticateCustomer()
|
|
{
|
|
extract($this->getCustomer());
|
|
$response = $this->callApiWithToken(
|
|
$service,
|
|
'post',
|
|
route('apiV2.customers.authenticate', []),
|
|
[
|
|
'email' => $customer->email,
|
|
'password' => 'testPassword'
|
|
]
|
|
);
|
|
|
|
$json = $response->decodeResponseJson();
|
|
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
|
|
$this->assertNotEmpty($json->json('data'));
|
|
$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.customers.authenticate
|
|
*/
|
|
public function testApiAuthenticateCustomerShouldFail()
|
|
{
|
|
extract($this->getCustomer());
|
|
$response = $this->callApiWithToken(
|
|
$service,
|
|
'post',
|
|
route('apiV2.customers.authenticate', []),
|
|
[
|
|
'email' => $customer->email,
|
|
'password' => 'testPasswords'
|
|
]
|
|
);
|
|
|
|
$json = $response->decodeResponseJson();
|
|
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
|
|
$this->assertNotEmpty($json->json('data'));
|
|
$this->assertNotEmpty($json->json('data.message'));
|
|
$this->assertNotEmpty($json->json('data.code'));
|
|
$this->assertEquals(ApiError::authFailed()->value, $json->json('data.code'));
|
|
$this->assertEquals(ApiError::authFailed()->label, $json->json('data.message'));
|
|
}
|
|
|
|
/**
|
|
* route: apiV2.customers.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' => Country::france()->value,
|
|
'city' => 'Connerré',
|
|
'postcode' => '72160',
|
|
'opt_in' => true
|
|
];
|
|
$response = $this->callApiWithToken($service, 'post', route('apiV2.customers.store'), $data);
|
|
$json = $response->decodeResponseJson();
|
|
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
|
|
$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.customers.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.customers.store'), $data);
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
|
|
$this->assertEquals(ApiError::validationFailed()->value, $json->json('data.code'));
|
|
}
|
|
|
|
/**
|
|
* route: apiV2.customers.activate
|
|
*/
|
|
public function testActivateCustomer()
|
|
{
|
|
extract($this->getCustomer(false));
|
|
|
|
$response = $this->callApiWithToken($service, 'post', route(
|
|
'apiV2.customers.activate',
|
|
['customer' => $info->hashcode]
|
|
), []);
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
|
|
|
|
$response = $this->callApiWithToken($service, 'post', route(
|
|
'apiV2.customers.activate',
|
|
['customer' => $info->hashcode]
|
|
), []);
|
|
$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.customers.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' => Country::bahamas()->value,
|
|
'city' => 'Connerré2',
|
|
'postcode' => '72161',
|
|
'opt_in' => 0
|
|
];
|
|
|
|
foreach ($data as $rowName => $row) {
|
|
$response = $this->callApiWithToken($service, 'patch', route(
|
|
'apiV2.customers.update',
|
|
['customer' => $info->hashcode]
|
|
), [$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.customers.update',
|
|
['customer' => $info->hashcode]
|
|
), ['address2' => 'anything']);
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
|
|
$this->assertEquals(ApiError::validationFailed()->value, $json->json('data.code'));
|
|
}
|
|
|
|
/**
|
|
* route: apiV2.customers.show
|
|
*/
|
|
public function testApiGetCustomer()
|
|
{
|
|
extract($this->getCustomer());
|
|
|
|
$response = $this->callApiWithToken($service, 'get', route(
|
|
'apiV2.customers.show',
|
|
['customer' => $info->hashcode]
|
|
));
|
|
$json = $response->decodeResponseJson();
|
|
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
|
|
$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];
|
|
|
|
$uuid = Str::uuid();
|
|
|
|
$response = $this->callApiWithToken($service, 'get', route(
|
|
'apiV2.customers.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(
|
|
sprintf(ApiError::modelNotFound()->label, $csClassName),
|
|
$json->json('data.message')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* route: apiV2.customer.destroy
|
|
*/
|
|
public function testApiDestroyCustomer()
|
|
{
|
|
extract($this->getCustomer());
|
|
$service = $brand->services()->firstOrFail();
|
|
$response = $this->callApiWithToken($service, 'delete', route(
|
|
'apiV2.customers.destroy',
|
|
['customer' => $info->hashcode]
|
|
));
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
|
|
$this->assertEmpty($json->json('data'));
|
|
|
|
$response = $this->callApiWithToken($service, 'get', route(
|
|
'apiV2.customers.show',
|
|
['customer' => $info->hashcode]
|
|
));
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
|
|
}
|
|
}
|