160 lines
5.3 KiB
PHP
160 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\CRM\ApiV4;
|
|
|
|
use App\Enums\ApiError;
|
|
use App\Enums\ApiStatus;
|
|
use App\Lib\Helpers\FileHelper;
|
|
use App\Models\Claim;
|
|
use App\Models\ClaimSubject;
|
|
use App\Models\Customer;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class ClaimTest extends ApiBase
|
|
{
|
|
/** @var array<string, mixed> */
|
|
private array $skeleton;
|
|
|
|
/**
|
|
* Prepare data.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$image = UploadedFile::fake()->image('photo.jpg');
|
|
$this->skeleton = [
|
|
'first_name' => 'Jean',
|
|
'last_name' => 'Bond',
|
|
'email' => 'claim@hotmail.fr',
|
|
'message' => 'Ceci est un message.',
|
|
'address' => '10 rue des totos',
|
|
'city' => 'Le Mans',
|
|
'postcode' => '72100',
|
|
'file' => base64_encode(file_get_contents($image->getRealPath())),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Test save claim should work.
|
|
* route: apiV4.claim.store
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testSendClaimShouldWork(): void
|
|
{
|
|
extract($this->getCustomer(true, false));
|
|
$subject = ClaimSubject::query()->firstOrNew();
|
|
$customer = Customer::query()->firstOrNew();
|
|
|
|
$response = $this->callApiWithToken($service, 'post', route('apiV4.claim.store'), \array_merge(
|
|
$this->skeleton,
|
|
[
|
|
'brand_id' => $brand->getKey(),
|
|
'subject_id' => $subject->getKey(),
|
|
]
|
|
));
|
|
$claimModel = Claim::query()->where([
|
|
['email', 'claim@hotmail.fr'],
|
|
['brand_id', $brand->getKey()],
|
|
['subject_id', $subject->getKey()],
|
|
['hashcode', $customer->hashcode],
|
|
])->exists();
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
|
$this->assertTrue($claimModel, 'Claim should have been stored.');
|
|
|
|
// Assert file stored.
|
|
$claimModel = Claim::query()->where('email', 'claim@hotmail.fr')->firstOrFail();
|
|
$this->assertNotEmpty($claimModel->file, 'The "file" field is empty on the Claim model.');
|
|
$this->assertTrue(
|
|
Storage::disk('private')->exists(FileHelper::getPrivateFilePath($claimModel->file)),
|
|
"The file does not exist: {$claimModel->file}"
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Test save claim with inexistent models should fail.
|
|
* route: apiV4.claim.store
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testSendClaimWithInexistentModelsShouldFail(): void
|
|
{
|
|
extract($this->getCustomer(true, false));
|
|
|
|
$response = $this->callApiWithToken($service, 'post', route('apiV4.claim.store'), \array_merge(
|
|
$this->skeleton,
|
|
[
|
|
'brand_id' => 0,
|
|
'subject_id' => 0,
|
|
'hashcode' => null,
|
|
]
|
|
));
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
|
|
$this->assertEquals(ApiError::validationFailed->value(), $json->json('data.code'));
|
|
}
|
|
|
|
/**
|
|
* Test save claim without optionnal fields should work.
|
|
* route: apiV4.claim.store
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testSendClaimWithoutOptionnalFieldsShouldWork(): void
|
|
{
|
|
extract($this->getCustomer(true, false));
|
|
$subject = ClaimSubject::query()->firstOrNew();
|
|
|
|
$response = $this->callApiWithToken($service, 'post', route('apiV4.claim.store'), \array_merge(
|
|
Arr::except($this->skeleton, ['address', 'city', 'postcode', 'file']),
|
|
[
|
|
'brand_id' => $brand->getKey(),
|
|
'subject_id' => $subject->getKey(),
|
|
]
|
|
));
|
|
$claimModel = Claim::query()->where([
|
|
['email', 'claim@hotmail.fr'],
|
|
['brand_id', $brand->getKey()],
|
|
['subject_id', $subject->getKey()],
|
|
['hashcode', null],
|
|
])->exists();
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
|
$this->assertTrue($claimModel, 'Claim should have been stored.');
|
|
}
|
|
|
|
/**
|
|
* Test save claim with wrong file format should fail.
|
|
* route: apiV4.claim.store
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testSendClaimWithWrongFileFormatShouldFail(): void
|
|
{
|
|
extract($this->getCustomer(true, false));
|
|
$subject = ClaimSubject::query()->firstOrNew();
|
|
$customer = Customer::query()->firstOrNew();
|
|
|
|
// Create fake .gif base64.
|
|
$base64Gif = 'R0lGODlhAQABAIAAAAUEBA==';
|
|
|
|
$response = $this->callApiWithToken($service, 'post', route('apiV4.claim.store'), \array_merge(
|
|
Arr::except($this->skeleton, ['address', 'city', 'postcode', 'file']),
|
|
[
|
|
'file' => $base64Gif,
|
|
'brand_id' => $brand->getKey(),
|
|
'subject_id' => $subject->getKey(),
|
|
'hashcode' => $customer->hashcode,
|
|
]
|
|
));
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
|
|
$this->assertEquals(ApiError::validationFailed->value(), $json->json('data.code'));
|
|
}
|
|
}
|