From c8e024981e948c8d1f0d0ec977c262cdd4feaca8 Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sun, 8 Dec 2024 10:27:14 +0330 Subject: [PATCH] create cmms machine service with tests --- .env.example | 4 + .../Commands/GetMachinesInfoListCommand.php | 63 ++++++++++++ app/Models/CMMSMachine.php | 15 +++ .../CMMS/GetMachinesInfoListService.php | 63 ++++++++++++ config/cmms_web_services.php | 9 ++ config/logging.php | 6 ++ database/factories/CMMSMachineFactory.php | 30 ++++++ ...2_07_102436_create_cmms_machines_table.php | 35 +++++++ database/seeders/CMMSMachineSeeder.php | 18 ++++ .../Console/CMMS/GetMachinesInfoListTest.php | 99 +++++++++++++++++++ .../Services/CMMS/GetMachinesInfoListTest.php | 78 +++++++++++++++ 11 files changed, 420 insertions(+) create mode 100644 app/Console/Commands/GetMachinesInfoListCommand.php create mode 100644 app/Models/CMMSMachine.php create mode 100644 app/Services/CMMS/GetMachinesInfoListService.php create mode 100644 config/cmms_web_services.php create mode 100644 database/factories/CMMSMachineFactory.php create mode 100644 database/migrations/2024_12_07_102436_create_cmms_machines_table.php create mode 100644 database/seeders/CMMSMachineSeeder.php create mode 100644 tests/Feature/Console/CMMS/GetMachinesInfoListTest.php create mode 100644 tests/Feature/Services/CMMS/GetMachinesInfoListTest.php diff --git a/.env.example b/.env.example index e5b61c61..dcd498a5 100644 --- a/.env.example +++ b/.env.example @@ -58,3 +58,7 @@ TELESCOPE_RESPONSE_SIZE_LIMIT=128 DEBUGBAR_ENABLED=false IS_DEVELOPMENT_ENV= + +CMMS_MACHINE_INFO_URL= +CMMS_MACHINE_INFO_PASSWORD= +CMMS_MACHINE_INFO_VIEW_NAME= \ No newline at end of file diff --git a/app/Console/Commands/GetMachinesInfoListCommand.php b/app/Console/Commands/GetMachinesInfoListCommand.php new file mode 100644 index 00000000..d37738d0 --- /dev/null +++ b/app/Console/Commands/GetMachinesInfoListCommand.php @@ -0,0 +1,63 @@ +run(); + + $updatedRows = 0; + + if (isset($response['vw_MachBankFull'])) { + + foreach ($response['vw_MachBankFull'] as $entry) { + // Insert data into the database + CMMSMachine::query()->updateOrCreate( + [ + 'car_id' => $entry['Id'], + 'machine_code' => $entry['MachineCode'], + ], + [ + 'car_name' => $entry['CarName'] ?? null, + 'car_group' => $entry['CarGroup'], + 'machine_code' => $entry['MachineCode'], + 'unit_name' => $entry['UnitName'], + 'car_status' => $entry['Status'], + 'car_id' => $entry['Id'], + 'unit_group' => $entry['UnitGroup'], + 'plak_number' => $entry['PlakNumber'] ?? null, + ]); + $updatedRows += 1; + } + } + + $this->info("{$updatedRows} row's updated successfully"); + } +} diff --git a/app/Models/CMMSMachine.php b/app/Models/CMMSMachine.php new file mode 100644 index 00000000..d3cd8424 --- /dev/null +++ b/app/Models/CMMSMachine.php @@ -0,0 +1,15 @@ +url = config('cmms_web_services.MACHINE_INFO.url'); + $this->password = config('cmms_web_services.MACHINE_INFO.password'); + $this->viewName = config('cmms_web_services.MACHINE_INFO.ViewName'); + $this->channelName = 'cmms_machines_info'; + ini_set('memory_limit', '512M'); + } + + /** + * @throws Exception + */ + public function run(): array + { + try { + $xmlResponse = $this->sendRequest(); + + return $this->xmlToArray($xmlResponse); + } + catch (Exception $e) { + Log::channel($this->channelName) + ->error(get_class($this), + [ + 'message' => $e->getMessage(), + ] + ); + + throw $e; + } + } + + public function sendRequest() + { + $client = new SoapClient($this->url); + + return $client->GetView([ + 'ViewName' => $this->viewName, + 'Password' => $this->password, + ])->GetViewResult; + } + + public function xmlToArray($xmlString) + { + $xmlObject = simplexml_load_string($xmlString); + $json = json_encode($xmlObject); + return json_decode($json, true); + } +} diff --git a/config/cmms_web_services.php b/config/cmms_web_services.php new file mode 100644 index 00000000..ab122229 --- /dev/null +++ b/config/cmms_web_services.php @@ -0,0 +1,9 @@ + [ + 'url' => env('CMMS_MACHINE_INFO_URL'), + 'password' => env('CMMS_MACHINE_INFO_PASSWORD'), + 'ViewName' => env('CMMS_MACHINE_INFO_VIEW_NAME'), + ], +]; \ No newline at end of file diff --git a/config/logging.php b/config/logging.php index 088c204e..2ac89fca 100644 --- a/config/logging.php +++ b/config/logging.php @@ -99,6 +99,12 @@ return [ 'emergency' => [ 'path' => storage_path('logs/laravel.log'), ], + + 'cmms_machines_info' => [ + 'driver' => 'single', + 'path' => storage_path('logs/cmms/machines_info_list_service.log'), + 'level' => 'info', + ], ], ]; diff --git a/database/factories/CMMSMachineFactory.php b/database/factories/CMMSMachineFactory.php new file mode 100644 index 00000000..3103f7d5 --- /dev/null +++ b/database/factories/CMMSMachineFactory.php @@ -0,0 +1,30 @@ + + */ +class CMMSMachineFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'car_name' => $this->faker->name, + 'car_group' => $this->faker->name, + 'car_id' => $this->faker->numberBetween(1, 10), + 'car_status' => $this->faker->lexify, + 'machine_code' => $this->faker->numerify, + 'unit_name' => $this->faker->name, + 'unit_group' => $this->faker->name, + 'plak_number' => $this->faker->numerify, + ]; + } +} diff --git a/database/migrations/2024_12_07_102436_create_cmms_machines_table.php b/database/migrations/2024_12_07_102436_create_cmms_machines_table.php new file mode 100644 index 00000000..cd2e698b --- /dev/null +++ b/database/migrations/2024_12_07_102436_create_cmms_machines_table.php @@ -0,0 +1,35 @@ +id(); + $table->string('car_name')->nullable(); + $table->string('car_group')->nullable(); + $table->unsignedBigInteger('car_id')->nullable(); + $table->string('car_status')->nullable(); + $table->string('machine_code')->nullable(); + $table->string('unit_name')->nullable(); + $table->string('unit_group')->nullable(); + $table->string('plak_number')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('cmms_machines'); + } +}; diff --git a/database/seeders/CMMSMachineSeeder.php b/database/seeders/CMMSMachineSeeder.php new file mode 100644 index 00000000..a259f7fd --- /dev/null +++ b/database/seeders/CMMSMachineSeeder.php @@ -0,0 +1,18 @@ +create(); + } +} diff --git a/tests/Feature/Console/CMMS/GetMachinesInfoListTest.php b/tests/Feature/Console/CMMS/GetMachinesInfoListTest.php new file mode 100644 index 00000000..2e35767c --- /dev/null +++ b/tests/Feature/Console/CMMS/GetMachinesInfoListTest.php @@ -0,0 +1,99 @@ +create(); + + + } + + public function test_get_machines_info_list_console_command_work_properly(): void + { + $this->withoutExceptionHandling(); + + $data = [ + "Id" => $this->faker->numberBetween(1, 10), + "PlakNumber" => $this->faker->numerify, + "CarName" => $this->faker->name, + "CarGroup" => $this->faker->name, + "MachineCode" => $this->faker->numerify, + "UnitName" => $this->faker->name, + "Status" => $this->faker->numberBetween(1, 10), + "UnitGroup" => $this->faker->name, + ]; + + $cmmsMachine = CMMSMachine::factory()->create([ + "Id" => $data['Id'], + ]); + + $serviceResult = [ + 'vw_MachBankFull' => [ + [ + "Id" => $data['Id'], + "PlakNumber" => $data['PlakNumber'], + "CarName" => $data['CarName'], + "CarGroup" => $data['CarGroup'], + "MachineCode" => $data['MachineCode'], + "UnitName" => $data['UnitName'], + "Status" => $data['Status'], + "UnitGroup" => $data['UnitGroup'], + ], + [ + "Id" => 3232, + "PlakNumber" => $data['PlakNumber'], + "CarName" => $data['CarName'], + "CarGroup" => $data['CarGroup'], + "MachineCode" => $data['MachineCode'], + "UnitName" => $data['UnitName'], + "Status" => $data['Status'], + "UnitGroup" => $data['UnitGroup'], + ], + ] + ]; + + $this->mock(GetMachinesInfoListService::class, function (MockInterface $mock) use($serviceResult) { + $mock->shouldReceive('run') + ->andReturn($serviceResult); + }); + + $this->artisan('cmms_machines_info:get_list'); + + $this->assertDatabaseHas('cmms_machines', [ + 'car_name' => $data['CarName'], + 'car_group' => $data['CarGroup'], + 'machine_code' => $data['MachineCode'], + 'unit_name' => $data['UnitName'], + 'car_status' => $data['Status'], + 'car_id' => $data['Id'], + 'unit_group' => $data['UnitGroup'], + 'plak_number' => $data['PlakNumber'], + ]); + $this->assertDatabaseHas('cmms_machines', [ + 'car_name' => $data['CarName'], + 'car_group' => $data['CarGroup'], + 'machine_code' => $data['MachineCode'], + 'unit_name' => $data['UnitName'], + 'car_status' => $data['Status'], + 'car_id' => 3232, + 'unit_group' => $data['UnitGroup'], + 'plak_number' => $data['PlakNumber'], + ]); + } +} diff --git a/tests/Feature/Services/CMMS/GetMachinesInfoListTest.php b/tests/Feature/Services/CMMS/GetMachinesInfoListTest.php new file mode 100644 index 00000000..f3ec230c --- /dev/null +++ b/tests/Feature/Services/CMMS/GetMachinesInfoListTest.php @@ -0,0 +1,78 @@ + $this->faker->numberBetween(1, 10), + "PlakNumber" => $this->faker->numerify, + "CarName" => $this->faker->name, + "CarGroup" => $this->faker->name, + "MachineCode" => $this->faker->numerify, + "UnitName" => $this->faker->name, + "Status" => $this->faker->numberBetween(1, 10), + "UnitGroup" => $this->faker->name, + ]; + $expected_response = [ + 'vw_MachBankFull' => [ + [ + "Id" => $data['Id'], + "PlakNumber" => $data['PlakNumber'], + "CarName" => $data['CarName'], + "CarGroup" => $data['CarGroup'], + "MachineCode" => $data['MachineCode'], + "UnitName" => $data['UnitName'], + "Status" => $data['Status'], + "UnitGroup" => $data['UnitGroup'], + ], + [ + "Id" => $data['Id'], + "PlakNumber" => $data['PlakNumber'], + "CarName" => $data['CarName'], + "CarGroup" => $data['CarGroup'], + "MachineCode" => $data['MachineCode'], + "UnitName" => $data['UnitName'], + "Status" => $data['Status'], + "UnitGroup" => $data['UnitGroup'], + ], + ] + ]; + + $mockStdClass = Mockery::mock('stdClass'); + $mockResultObject = $mockStdClass->GetViewResult->NewDataSet->vw_MachBankFull; + $mockResultObject->CarName = $data['CarName']; + $mockResultObject->CarGroup = $data['CarGroup']; + $mockResultObject->PlakNumber = $data['PlakNumber']; + $mockResultObject->MachineCode = $data['MachineCode']; + $mockResultObject->UnitName = $data['UnitName']; + $mockResultObject->Status = $data['Status']; + $mockResultObject->Id = $data['Id']; + $mockResultObject->UnitGroup = $data['UnitGroup']; + + + $this->mock(SoapClient::class, function (MockInterface $mock) use ($mockStdClass) { + $mock->shouldReceive('GetView') + ->andReturn($mockStdClass); + }); + + $service = new GetMachinesInfoListService(); + $actual_response = $service->run(); + + $this->assertEquals($expected_response, $actual_response); + } +}