Merge branch 'feature/CMMSMachineCode' into 'develop'
create cmms machine service with tests See merge request witelgroup/rms_v2!40
This commit is contained in:
@@ -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=
|
||||
63
app/Console/Commands/GetMachinesInfoListCommand.php
Normal file
63
app/Console/Commands/GetMachinesInfoListCommand.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\CMMSMachine;
|
||||
use App\Services\CMMS\GetMachinesInfoListService;
|
||||
use Exception;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use SoapClient;
|
||||
|
||||
class GetMachinesInfoListCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'cmms_machines_info:get_list';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'receive cmms cars information list and update the corresponding tables';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
* @throws Exception
|
||||
*/
|
||||
public function handle(GetMachinesInfoListService $getMachinesInfoListService): void
|
||||
{
|
||||
$response = $getMachinesInfoListService->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");
|
||||
}
|
||||
}
|
||||
15
app/Models/CMMSMachine.php
Normal file
15
app/Models/CMMSMachine.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class CMMSMachine extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'cmms_machines';
|
||||
|
||||
protected $guarded = [];
|
||||
}
|
||||
63
app/Services/CMMS/GetMachinesInfoListService.php
Normal file
63
app/Services/CMMS/GetMachinesInfoListService.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\CMMS;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use SoapClient;
|
||||
|
||||
class GetMachinesInfoListService
|
||||
{
|
||||
protected string $url;
|
||||
protected string $channelName;
|
||||
protected string $password;
|
||||
protected string $viewName;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
9
config/cmms_web_services.php
Normal file
9
config/cmms_web_services.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'MACHINE_INFO' => [
|
||||
'url' => env('CMMS_MACHINE_INFO_URL'),
|
||||
'password' => env('CMMS_MACHINE_INFO_PASSWORD'),
|
||||
'ViewName' => env('CMMS_MACHINE_INFO_VIEW_NAME'),
|
||||
],
|
||||
];
|
||||
@@ -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',
|
||||
],
|
||||
],
|
||||
|
||||
];
|
||||
|
||||
30
database/factories/CMMSMachineFactory.php
Normal file
30
database/factories/CMMSMachineFactory.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\CMMSMachine>
|
||||
*/
|
||||
class CMMSMachineFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('cmms_machines', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
};
|
||||
18
database/seeders/CMMSMachineSeeder.php
Normal file
18
database/seeders/CMMSMachineSeeder.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\CMMSMachine;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class CMMSMachineSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
CMMSMachine::factory(5)->create();
|
||||
}
|
||||
}
|
||||
99
tests/Feature/Console/CMMS/GetMachinesInfoListTest.php
Normal file
99
tests/Feature/Console/CMMS/GetMachinesInfoListTest.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Console\CMMS;
|
||||
|
||||
use App\Models\CMMSMachine;
|
||||
use App\Services\CMMS\GetMachinesInfoListService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Mockery\MockInterface;
|
||||
use Tests\TestCase;
|
||||
|
||||
class GetMachinesInfoListTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, WithFaker;
|
||||
/**
|
||||
* A basic feature test example.
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
CMMSMachine::factory()->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'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
78
tests/Feature/Services/CMMS/GetMachinesInfoListTest.php
Normal file
78
tests/Feature/Services/CMMS/GetMachinesInfoListTest.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Services\CMMS;
|
||||
|
||||
use App\Services\CMMS\GetMachinesInfoListService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Mockery;
|
||||
use Mockery\MockInterface;
|
||||
use SoapClient;
|
||||
use Tests\TestCase;
|
||||
|
||||
class GetMachinesInfoListTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, WithFaker;
|
||||
/**
|
||||
* A basic feature test example.
|
||||
*/
|
||||
public function test_data_returned_properly(): void
|
||||
{
|
||||
$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,
|
||||
];
|
||||
$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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user