From 2ae1d09afcae770854a91c7d5e8350238121b96c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 19 Oct 2021 17:15:34 +0200 Subject: [PATCH] Correct arcadia Api V2 methods + unit tests --- .../Controllers/Api/V2/ArcadiaController.php | 16 +++- .../Api/V2/SendArchadiaDefaultMailRequest.php | 16 ++-- .../Api/V2/SendArchadiaGameMailRequest.php | 16 ++-- resources/documents/arcadia_default.xsd | 96 +++++++++++++++++++ resources/documents/arcadia_game.xsd | 95 ++++++++++++++++++ .../mail/arcadiaOrders/default.blade.php | 61 ++++++------ .../views/mail/arcadiaOrders/game.blade.php | 33 +++++++ tests/Feature/ApiV2/ArcadiaTest.php | 10 +- tests/Unit/CrmClientV1CustomerTest.php | 96 +++++++++++++++++++ tests/Unit/CrmClientV2ArcadiaTest.php | 72 ++++++++++++-- 10 files changed, 452 insertions(+), 59 deletions(-) create mode 100644 resources/documents/arcadia_default.xsd create mode 100644 resources/documents/arcadia_game.xsd create mode 100644 resources/views/mail/arcadiaOrders/game.blade.php diff --git a/app/Http/Controllers/Api/V2/ArcadiaController.php b/app/Http/Controllers/Api/V2/ArcadiaController.php index e6897f2..5d6b886 100644 --- a/app/Http/Controllers/Api/V2/ArcadiaController.php +++ b/app/Http/Controllers/Api/V2/ArcadiaController.php @@ -15,6 +15,7 @@ use DOMDocument; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; /** * TODO: séparer ça quand coté arcadia ils auront branché le cerveau @@ -43,7 +44,7 @@ class ArcadiaController extends Controller ArcadiaMailType $type ) { $request = $request->merge([ - 'order_number' => str_pad(\intval($request->order_number), 9, '0', STR_PAD_LEFT) + 'order_num' => str_pad(\intval($request->order_num), 9, '0', STR_PAD_LEFT) ]); $xml = 'none'; try { @@ -62,12 +63,18 @@ class ArcadiaController extends Controller // Format xml $doc = new DOMDocument('1.0', 'utf-8'); - $doc->preserveWhiteSpace = true; + $doc->xmlStandalone = true; + $doc->preserveWhiteSpace = false; $doc->formatOutput = true; $doc->loadXML($xml); $xml = $doc->saveXML(); - Storage::disk('arcadia')->put(date("Ymd_His") . ".xml", $xml); + Storage::disk('arcadia')->put(Str::slug(sprintf( + 'v2_%s_%s%s.xml', + $type->label, + date('Ymd_His'), + \microtime(true) + )), $xml); return response()->json(); } catch (\Exception $e) { Log::critical("ARCADIA XML CREATION FAILED : " . $e->getMessage()); @@ -102,7 +109,8 @@ class ArcadiaController extends Controller array $customerInfo ): string { return view( - 'mail.arcadiaOrders.default', + $type->equals(ArcadiaMailType::default()) ? + 'mail.arcadiaOrders.default' : 'mail.arcadiaOrders.game', [ 'request' => $request, 'type' => $type, diff --git a/app/Http/Requests/Api/V2/SendArchadiaDefaultMailRequest.php b/app/Http/Requests/Api/V2/SendArchadiaDefaultMailRequest.php index 510e8c4..a7c630f 100644 --- a/app/Http/Requests/Api/V2/SendArchadiaDefaultMailRequest.php +++ b/app/Http/Requests/Api/V2/SendArchadiaDefaultMailRequest.php @@ -48,14 +48,14 @@ class SendArchadiaDefaultMailRequest extends BaseApiRequest ], 'support_code' => 'required|int', 'deliver' => 'required|array', - 'deliver.firstname' => 'required|string|min:2', - 'deliver.lastname' => 'required|string|min:2', - 'deliver.email' => 'required|email:rfc,dns,spoof,filter', - 'deliver.address' => 'required|string|min:2', - 'deliver.address2' => 'sometimes|string|min:2', - 'deliver.city' => 'required|string|min:2', - 'deliver.postcode' => 'required|string|min:5', - 'deliver.phone' => 'required|string', + 'deliver.firstname' => 'required|string|min:1|max:255', + 'deliver.lastname' => 'required|string|min:1|max:255', + 'deliver.email' => 'required|email:rfc,dns,spoof,filter|max:255', + 'deliver.address' => 'required|string|min:2|max:255', + 'deliver.address2' => 'sometimes|string|min:2|max:255', + 'deliver.city' => 'required|string|min:2|max:255', + 'deliver.postcode' => 'required|string|min:5|max:255', + 'deliver.phone' => 'required|string|max:255', ]; } } diff --git a/app/Http/Requests/Api/V2/SendArchadiaGameMailRequest.php b/app/Http/Requests/Api/V2/SendArchadiaGameMailRequest.php index c30c71c..b232a42 100644 --- a/app/Http/Requests/Api/V2/SendArchadiaGameMailRequest.php +++ b/app/Http/Requests/Api/V2/SendArchadiaGameMailRequest.php @@ -42,14 +42,14 @@ class SendArchadiaGameMailRequest extends BaseApiRequest 'order_date' => 'required|date', 'support_code' => 'required|int', 'deliver' => 'required|array', - 'deliver.firstname' => 'required|string|min:2', - 'deliver.lastname' => 'required|string|min:2', - 'deliver.email' => 'required|email:rfc,dns,spoof,filter', - 'deliver.address' => 'required|string|min:2', - 'deliver.address2' => 'sometimes|string|min:2', - 'deliver.city' => 'required|string|min:2', - 'deliver.postcode' => 'required|digits:5', - 'deliver.phone' => 'required|string', + 'deliver.firstname' => 'required|string|min:1|max:255', + 'deliver.lastname' => 'required|string|min:1|max:255', + 'deliver.email' => 'required|email:rfc,dns,spoof,filter|max:255', + 'deliver.address' => 'required|string|min:2|max:255', + 'deliver.address2' => 'sometimes|string|min:2|max:255', + 'deliver.city' => 'required|string|min:2|max:255', + 'deliver.postcode' => 'required|string|min:5|max:255', + 'deliver.phone' => 'required|string|max:255', ]; } } diff --git a/resources/documents/arcadia_default.xsd b/resources/documents/arcadia_default.xsd new file mode 100644 index 0000000..4e5f487 --- /dev/null +++ b/resources/documents/arcadia_default.xsd @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/documents/arcadia_game.xsd b/resources/documents/arcadia_game.xsd new file mode 100644 index 0000000..b435c18 --- /dev/null +++ b/resources/documents/arcadia_game.xsd @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/views/mail/arcadiaOrders/default.blade.php b/resources/views/mail/arcadiaOrders/default.blade.php index cdbaabe..8d49ea1 100644 --- a/resources/views/mail/arcadiaOrders/default.blade.php +++ b/resources/views/mail/arcadiaOrders/default.blade.php @@ -1,41 +1,46 @@ - - - + + + + {{ $request->order_num }} + {{ Carbon::parse($request->order_date)->format('d/m/Y') }} + {{ $request->operation }} + @if($type->equals(ArcadiaMailType::default())) + @if($orderType->equals(ArcadiaOrderType::cb())) + 1 + Null + @elseif($orderType->equals(ArcadiaOrderType::check())) + Null + 1 + @endif + @endif - - + + + @if(strlen($customerInfo['address2'])) + @else + + @endif -@if($type->equals(ArcadiaMailType::default())) -@if($orderType->equals(ArcadiaOrderType::cb())) - 1 - Null@elseif($orderType->equals(ArcadiaOrderType::check())) - Null - 1@endif @endif - {{ $request->order_num }} - {{ Carbon::parse($request->order_date)->format('d/m/Y') }} - {{ $request->operation }} - {{ $request->support_code }} - {{ $request->label_type_support }} {{ $request->package_unit_weight }} -@if(!is_null($request->qty)) {{-- {{ $request->qty }} --}} {{-- Locked by Arcadia to quantity = 1 --}} 1 -@endif + {{ $request->support_code }} + {{ $request->label_type_support }} + @if($type->equals(ArcadiaMailType::game())) + @if(!is_null($request->code_on_pack)) + code_on_pack }}]]> + @endif + @if(!is_null($request->code_unique)) + code_unique }}]]> + @endif + @endif -@if($type->equals(ArcadiaMailType::game())) -@if(!is_null($request->code_on_pack)) - code_on_pack }}]]> -@endif -@if(!is_null($request->code_unique)) - code_unique }}]]> -@endif -@endif - - + + diff --git a/resources/views/mail/arcadiaOrders/game.blade.php b/resources/views/mail/arcadiaOrders/game.blade.php new file mode 100644 index 0000000..4b2dd61 --- /dev/null +++ b/resources/views/mail/arcadiaOrders/game.blade.php @@ -0,0 +1,33 @@ + + + + + {{ Carbon::parse($request->order_date)->format('d/m/Y') }} + {{ $request->operation }} + + + + + + @if(strlen($customerInfo['address2'])) + + @else + + @endif + + + + + {{-- {{ $request->qty }} --}} + {{-- Locked by Arcadia to quantity = 1 --}} + 1 + + {{ $request->support_code }} + {{ $request->label_type_support }} + code_on_pack }}]]> + @if(!is_null($request->code_unique)) + code_unique }}]]> + @endif + + + diff --git a/tests/Feature/ApiV2/ArcadiaTest.php b/tests/Feature/ApiV2/ArcadiaTest.php index cec79c1..73a7616 100644 --- a/tests/Feature/ApiV2/ArcadiaTest.php +++ b/tests/Feature/ApiV2/ArcadiaTest.php @@ -34,10 +34,10 @@ class ArcadiaTest extends ApiBase 'label_type_support' => 'Send Departaimants', 'order_date' => \date('Y/m/d H:i:s'), 'state_order' => ArcadiaOrderType::cb()->value, - 'support_code' => 32, + 'support_code' => 1, 'label_type_support' => 'Fond carte Aimanté', - 'order_num' => str_pad('23', 9, '0', STR_PAD_LEFT), - 'package_unit_weight' => 200, + 'order_num' => 23, + 'package_unit_weight' => 0, ]; $response = $this->callApiWithToken($service, 'post', route( 'apiV2.arcadia.outgoingMailDefault' @@ -65,8 +65,8 @@ class ArcadiaTest extends ApiBase 'order_date' => date('Y/m/d H:i:s'), 'support_code' => 32, 'label_type_support' => 'Jeux Reghalal', - 'code_unique' => Str::uuid(), - 'code_on_pack' => 'anystring' + 'code_unique' => \strtoupper(\str_replace('-', '', (string)Str::uuid())), + 'code_on_pack' => '2563547' ]; $response = $this->callApiWithToken($service, 'post', route( 'apiV2.arcadia.outgoingMailGame', diff --git a/tests/Unit/CrmClientV1CustomerTest.php b/tests/Unit/CrmClientV1CustomerTest.php index df98b1a..d3faa8d 100644 --- a/tests/Unit/CrmClientV1CustomerTest.php +++ b/tests/Unit/CrmClientV1CustomerTest.php @@ -350,4 +350,100 @@ class CrmClientV1CustomerTest extends CrmClientTester $this->assertEquals('OK', $reponse->status); } + + public function testCreateArcadiaOutgoingMailGameWithEan() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->ArcadiaOutgoingMail( + ModelsCustomer::where('email', 'v1toto.delarogerie@gmail.com') + ->firstOrFail()->id, + [ + 'root' => [ + [ + 'fieldName' => 'order_date', + 'fieldValue' => date('d/m/Y') + ], + [ + 'fieldName' => 'operation', + 'fieldValue' => '32' + ] + ], + 'orderLine' => [ + [ + 'fieldName' => 'code_unique', + 'fieldValue' => '2A5B416325414232' + ], + [ + 'fieldName' => 'code_on_pack', + 'fieldValue' => '2541632541423' + ], + [ + 'fieldName' => 'support_code', + 'fieldValue' => '16' + ], + [ + 'fieldName' => 'label_type_support', + 'fieldValue' => 'Gain du lot : ballon de foot en métal type "fracture"' + ] + ] + ], + 'game', + [ + "firstname" => 'toto', + "lastname" => 'delarogerie', + "address" => 'rue des toto', + "city" => 'toto ville', + "postcode" => '72000', + "phone" => '0673891160' + ] + ); + + $this->assertEquals('OK', $reponse->status); + } + + public function testCreateArcadiaOutgoingMailGameWithoutEan() + { + $api = new ApiV1(config('crm.endpoint'), config('crm.token')); + $reponse = $api->ArcadiaOutgoingMail( + ModelsCustomer::where('email', 'v1toto.delarogerie@gmail.com') + ->firstOrFail()->id, + [ + 'root' => [ + [ + 'fieldName' => 'order_date', + 'fieldValue' => date('d/m/Y') + ], + [ + 'fieldName' => 'operation', + 'fieldValue' => '32' + ] + ], + 'orderLine' => [ + [ + 'fieldName' => 'code_unique', + 'fieldValue' => '25416325414232' + ], + [ + 'fieldName' => 'support_code', + 'fieldValue' => '16' + ], + [ + 'fieldName' => 'label_type_support', + 'fieldValue' => 'Gain du lot : ballon de foot en métal type "fracture"' + ] + ] + ], + 'game', + [ + "firstname" => 'toto', + "lastname" => 'delarogerie', + "address" => 'rue des toto', + "city" => 'toto ville', + "postcode" => '72000', + "phone" => '0673891160' + ] + ); + + $this->assertEquals('OK', $reponse->status); + } } diff --git a/tests/Unit/CrmClientV2ArcadiaTest.php b/tests/Unit/CrmClientV2ArcadiaTest.php index 10cf626..a60d413 100644 --- a/tests/Unit/CrmClientV2ArcadiaTest.php +++ b/tests/Unit/CrmClientV2ArcadiaTest.php @@ -2,6 +2,8 @@ namespace Tests\Unit; +use DOMDocument; +use Illuminate\Support\Facades\File; use Illuminate\Support\Str; use LDC\LDCCRM\CrmApiV2; use LDC\LDCCRM\Enums\ArcadiaOrderType; @@ -14,7 +16,7 @@ class CrmClientV2ArcadiaTest extends CrmClientTester { $api = new CrmApiV2(); $customer = $api->getCustomerOrNew(static::$info->hashcode); - dump(static::$info->postcode); + $arcadiaOrderMail = new ArcadiaMailDefault([ 'email' => static::$customer->email, 'firstname' => static::$info->firstname, @@ -28,10 +30,10 @@ class CrmClientV2ArcadiaTest extends CrmClientTester 'label_type_support' => 'Send Departaimants', 'order_date' => \date('Y/m/d H:i:s'), 'state_order' => ArcadiaOrderType::cb()->value, - 'support_code' => 32, + 'support_code' => 1, 'label_type_support' => 'Fond carte Aimanté', 'order_num' => 23, - 'package_unit_weight' => 200, + 'package_unit_weight' => 0, ]); $this->assertTrue($arcadiaOrderMail->send($customer)); } @@ -40,7 +42,6 @@ class CrmClientV2ArcadiaTest extends CrmClientTester { $api = new CrmApiV2(); $customer = $api->getCustomerOrNew(static::$info->hashcode); - dump(static::$info->postcode); $arcadiaOrderMail = new ArcadiaMailGame([ 'email' => static::$customer->email, 'firstname' => static::$info->firstname, @@ -54,9 +55,68 @@ class CrmClientV2ArcadiaTest extends CrmClientTester 'order_date' => date('Y/m/d H:i:s'), 'support_code' => 32, 'label_type_support' => 'Jeux Reghalal', - 'code_unique' => (string)Str::uuid(), - 'code_on_pack' => 'anystring' + 'code_unique' => \strtoupper(\str_replace('-', '', (string)Str::uuid())), + 'code_on_pack' => '12365214587562' ]); $this->assertTrue($arcadiaOrderMail->send($customer)); } + + 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-game') === 0) { + $fileFound = true; + + // Enable user error handling + \libxml_use_internal_errors(true); + + $xml = new DOMDocument(); + $xml->load($file->getRealPath()); + + if (!$xml->schemaValidate(\resource_path('documents/arcadia_game.xsd'))) { + print 'DOMDocument::schemaValidate() Generated Errors!'; + $this->libxmlDisplayErrors(); + throw new \Exception('Could not validate xml file against xsd'); + } + } + } + $this->assertTrue($fileFound, 'Should have found a xml file to validate and pass validation'); + } + + private function libxmlDisplayError($error) + { + $return = "
\n"; + switch ($error->level) { + case LIBXML_ERR_WARNING: + $return .= "Warning $error->code: "; + break; + case LIBXML_ERR_ERROR: + $return .= "Error $error->code: "; + break; + case LIBXML_ERR_FATAL: + $return .= "Fatal Error $error->code: "; + break; + } + $return .= trim($error->message); + if ($error->file) { + $return .= " in $error->file"; + } + $return .= " on line $error->line\n"; + + return $return; + } + + private function libxmlDisplayErrors() + { + $errors = libxml_get_errors(); + foreach ($errors as $error) { + print $this->libxmlDisplayError($error); + } + libxml_clear_errors(); + } }