ldc-crm/tests/Client/CrmClientV2ArcadiaTest.php

294 lines
9.8 KiB
PHP

<?php
namespace Tests\Client;
use DOMDocument;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use LDC\LDCCRM\CrmApiV2;
use LDC\LDCCRM\Enums\ArcadiaOrderType;
use LDC\LDCCRM\Models\ArcadiaMailDefault;
use LDC\LDCCRM\Models\ArcadiaMailGame;
class CrmClientV2ArcadiaTest extends CrmClientTester
{
/**
* Send arcadia order default test.
*
* @return void
*/
public function testArcadiaSendOrderDefault()
{
$api = new CrmApiV2();
$customer = $api->getCustomerOrNew(static::$info->hashcode);
$arcadiaOrderMail = new ArcadiaMailDefault([
'email' => static::$customer->email,
'firstname' => static::$info->firstname,
'lastname' => static::$info->lastname,
'phone' => static::$info->phone,
'address' => static::$info->address,
'address2' => static::$info->address2,
'city' => static::$info->city,
'postcode' => static::$info->postcode,
'operation' => 16,
'label_type_support' => 'Send Departaimants',
'order_date' => \date('Y/m/d H:i:s'),
'state_order' => ArcadiaOrderType::cb()->value,
'support_code' => 1,
'label_type_support' => 'Fond carte Aimanté',
'order_num' => 23,
'package_unit_weight' => 0,
]);
$this->assertTrue($arcadiaOrderMail->send($customer));
}
/**
* Test arcadia default mail can serialize to json
*
* @return void
*/
public function testArcadiaOrderDefaultJson()
{
$data = [
'email' => static::$customer->email,
'firstname' => static::$info->firstname,
'lastname' => static::$info->lastname,
'phone' => static::$info->phone,
'address' => static::$info->address,
'address2' => static::$info->address2,
'city' => static::$info->city,
'postcode' => static::$info->postcode,
'operation' => 16,
'label_type_support' => 'Send Departaimants',
'order_date' => \date('Y/m/d H:i:s'),
'state_order' => ArcadiaOrderType::cb()->value,
'support_code' => 1,
'label_type_support' => 'Fond carte Aimanté',
'order_num' => 23,
'package_unit_weight' => 0,
];
$arcadiaOrderMail = new ArcadiaMailDefault($data);
/** Mutator and accessor tests */
foreach ($data as $propName => $value) {
$arcadiaOrderMail->{$propName} = $value;
$arcadiaOrderMail->{$propName} = $arcadiaOrderMail->{$propName};
}
$jsonDecoded = \json_decode(\json_encode($arcadiaOrderMail), true);
$this->assertIsArray($jsonDecoded);
foreach ($data as $key => $value) {
if (isset($jsonDecoded['deliver'][$key])) {
$this->assertArrayHasKey($key, $jsonDecoded['deliver']);
$this->assertEquals($value, $jsonDecoded['deliver'][$key]);
} else {
$this->assertArrayHasKey($key, $jsonDecoded);
$this->assertEquals($value, $jsonDecoded[$key]);
}
}
}
/**
* Send arcadia order default game.
*
* @return void
*/
public function testArcadiaSendOrderGame()
{
$api = new CrmApiV2();
$customer = $api->getCustomerOrNew(static::$info->hashcode);
$arcadiaOrderMail = new ArcadiaMailGame([
'email' => static::$customer->email,
'firstname' => static::$info->firstname,
'lastname' => static::$info->lastname,
'phone' => static::$info->phone,
'address' => static::$info->address,
'address2' => static::$info->address2,
'city' => static::$info->city,
'postcode' => static::$info->postcode,
'operation' => 16,
'order_date' => date('Y/m/d H:i:s'),
'support_code' => 32,
'label_type_support' => 'Jeux Reghalal',
'code_unique' => \strtoupper(\str_replace('-', '', (string)Str::uuid())),
'code_on_pack' => '12365214587562'
]);
$this->assertTrue($arcadiaOrderMail->send($customer));
}
/**
* Send arcadia order default game (empty string does not convert to null).
*
* @return void
*/
public function testArcadiaSendOrderGameNullString()
{
$api = new CrmApiV2();
$customer = $api->getCustomerOrNew(static::$info->hashcode);
$arcadiaOrderMail = new ArcadiaMailGame([
'email' => 'toto@gmail.com',
'firstname' => 'Jean',
'lastname' => 'Pouet',
'phone' => '0678142536',
'address' => '30 rue des totots',
'address2' => null,
'city' => 'Le Mans',
'postcode' => '72000',
'operation' => 12,
'order_date' => '2022/03/17 11:24:55',
'support_code' => 16,
'label_type_support' => 'Gain du lot : Lot ',
'code_unique' => '11N311A1',
'code_on_pack' => '12345678'
]);
$this->assertTrue($arcadiaOrderMail->send($customer));
}
/**
* Test arcadia game mail can serialize to json
*
* @return void
*/
public function testArcadiaOrderGameJson()
{
$data = [
'email' => static::$customer->email,
'firstname' => static::$info->firstname,
'lastname' => static::$info->lastname,
'phone' => static::$info->phone,
'address' => static::$info->address,
'address2' => static::$info->address2,
'city' => static::$info->city,
'postcode' => static::$info->postcode,
'operation' => 16,
'order_date' => date('Y/m/d H:i:s'),
'support_code' => 32,
'label_type_support' => 'Jeux Reghalal',
'code_unique' => \strtoupper(\str_replace('-', '', (string)Str::uuid())),
'code_on_pack' => '12365214587562'
];
$arcadiaGameMail = new ArcadiaMailGame($data);
/** Mutator and accessor tests */
foreach ($data as $propName => $value) {
$arcadiaGameMail->{$propName} = $value;
$arcadiaGameMail->{$propName} = $arcadiaGameMail->{$propName};
}
$jsonDecoded = \json_decode(\json_encode($arcadiaGameMail), true);
$this->assertIsArray($jsonDecoded);
foreach ($data as $key => $value) {
if (isset($jsonDecoded['deliver'][$key])) {
$this->assertArrayHasKey($key, $jsonDecoded['deliver']);
$this->assertEquals($value, $jsonDecoded['deliver'][$key]);
} else {
$this->assertArrayHasKey($key, $jsonDecoded);
$this->assertEquals($value, $jsonDecoded[$key]);
}
}
}
/**
* Valdate xml generated files default.
*
* @return void
*/
public function testValidateGeneratedXmlDefault()
{
$fileFound = false;
$files = File::allFiles(\storage_path('export/arcadia'));
/** @var \Symfony\Component\Finder\SplFileInfo $file */
foreach ($files as $file) {
if (\strpos($file->getFilename(), 'v2-order-000000023-default-') === 0) {
$fileFound = true;
// Enable user error handling.
\libxml_use_internal_errors(true);
$xml = new DOMDocument();
$xml->load($file->getRealPath());
$this->assertTrue($xml->schemaValidate(\resource_path('documents/arcadia_default.xsd')));
$this->libxmlDisplayErrors();
}
}
$this->assertTrue($fileFound, 'Should have found a xml file to validate and pass validation');
}
/**
* Valdate xml generated files game.
*
* @return void
*/
public function testValidateGeneratedXmlGame()
{
$fileFound = false;
$files = File::allFiles(\storage_path('export/arcadia'));
/** @var \Symfony\Component\Finder\SplFileInfo $file */
foreach ($files as $file) {
if (\strpos($file->getFilename(), 'v2-order-000000000-game') === 0) {
$fileFound = true;
// Enable user error handling.
\libxml_use_internal_errors(true);
$xml = new DOMDocument();
$xml->load($file->getRealPath());
$this->assertTrue($xml->schemaValidate(\resource_path('documents/arcadia_game.xsd')));
$this->libxmlDisplayErrors();
}
}
$this->assertTrue($fileFound, 'Should have found a xml file to validate and pass validation');
}
/**
* Render a \LibXMLError
*
* @param \LibXMLError $error
* @return string
*/
private function libxmlDisplayError(\LibXMLError $error): string
{
$return = "<br/>\n";
switch ($error->level) {
case LIBXML_ERR_WARNING:
$return .= "<b>Warning $error->code</b>: ";
break;
case LIBXML_ERR_ERROR:
$return .= "<b>Error $error->code</b>: ";
break;
case LIBXML_ERR_FATAL:
$return .= "<b>Fatal Error $error->code</b>: ";
break;
}
$return .= trim($error->message);
if ($error->file) {
$return .= " in <b>$error->file</b>";
}
$return .= " on line <b>$error->line</b>\n";
return $return;
}
/**
* Display xml errors.
*
* @return void
*/
private function libxmlDisplayErrors()
{
$errors = libxml_get_errors();
foreach ($errors as $error) {
print $this->libxmlDisplayError($error);
}
libxml_clear_errors();
}
}