Files
oficial/app/tests/unit/src/Service/Venta/MediosPago/Toku/CustomerTest.php
Juan Pablo Vial fb7177fd65 Reorden Toku
2025-05-10 12:38:14 -04:00

199 lines
8.0 KiB
PHP

<?php
namespace Tests\Unit\Service\Venta\MediosPago\Toku;
use Faker;
use GuzzleHttp\Client;
use Incoviba\Common\Define;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
use PDO;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
class CustomerTest extends TestCase
{
protected Client $client;
protected Model\Venta\MediosPago\Toku\Customer $customer;
protected Repository\Venta\MediosPago\Toku\Customer $customerRepository;
protected array $getData;
protected array $postData;
protected function setUp(): void
{
$dsn = "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']}";
$pdo = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD']);
$connection = $this->getMockBuilder(Define\Connection::class)
->disableOriginalConstructor()->getMock();
$connection->method('getPDO')->willReturn($pdo);
$faker = Faker\Factory::create();
$datos = $this->getMockBuilder(Model\Persona\Datos::class)
->disableOriginalConstructor()->getMock();
$datos->email = $faker->email();
$datos->telefono = $faker->randomNumber(8);
$persona = $this->getMockBuilder(Model\Persona::class)
->disableOriginalConstructor()->getMock();
$persona->rut = $faker->randomNumber();
$persona->digito = $faker->randomNumber();
$persona->nombres = $faker->firstName();
$persona->apellidoPaterno = $faker->lastName();
$persona->apellidoMaterno = $faker->lastName();
$persona->method('datos')->willReturn($datos);
$persona->method('nombreCompleto')->willReturn(implode(' ', [
$persona->nombres,
$persona->apellidoPaterno,
$persona->apellidoMaterno
]));
$this->customer = $this->getMockBuilder(Model\Venta\MediosPago\Toku\Customer::class)
->disableOriginalConstructor()->getMock();
$this->customer->id = $faker->randomNumber();
$this->customer->persona = $persona;
$this->customer->toku_id = $faker->ean13();
$this->customer->method('rut')->willReturn(implode('', [
$persona->rut,
strtoupper($persona->digito)
]));
$this->customer->method('jsonSerialize')->willReturn([
'id' => $this->customer->id,
'rut' => $this->customer->rut(),
'toku_id' => $this->customer->toku_id
]);
$this->customerRepository = $this->getMockBuilder(Repository\Venta\MediosPago\Toku\Customer::class)
->disableOriginalConstructor()->getMock();
$this->customerRepository->method('fetchById')->willReturn($this->customer);
$this->customerRepository->method('fetchByRut')->willReturn($this->customer);
$this->customerRepository->method('fetchByTokuId')->willReturn($this->customer);
$this->getData = [
'id' => $this->customer->id,
'external_id' => $this->customer->rut(),
'government_id' => $this->customer->rut(),
'email' => $persona->datos()->email,
'name' => $persona->nombreCompleto(),
'phone_number' => $persona->datos()->telefono,
'silenced_until' => null,
'default_agent' => null,
'agent_phone_number' => null,
'pac_mandate_id' => null,
'send_mail' => false,
'default_receipt_type' => 'bill',
'rfc' => null,
'tax_zip_code' => null,
'fiscal_regime' => null,
'secondary_emails' => [],
'metadata' => []
];
$getBody = $this->getMockBuilder(StreamInterface::class)
->disableOriginalConstructor()->getMock();
$getBody->method('getContents')->willReturn(json_encode($this->getData));
$getResponse = $this->getMockBuilder(ResponseInterface::class)
->disableOriginalConstructor()->getMock();
$getResponse->method('getStatusCode')->willReturn(200);
$getResponse->method('getBody')->willReturn($getBody);
$this->postData = [
'id' => $this->customer->id,
'external_id' => $this->customer->rut(),
'government_id' => $this->customer->rut(),
'email' => $persona->datos()->email,
'name' => $persona->nombreCompleto(),
'phone_number' => $persona->datos()->telefono,
'silenced_until' => null,
'default_agent' => null,
'agent_phone_number' => null,
'pac_mandate_id' => null,
'send_mail' => false,
'default_receipt_type' => null,
'rfc' => null,
'tax_zip_code' => null,
'fiscal_regime' => null,
'secondary_emails' => [],
'metadata' => []
];
$postBody = $this->getMockBuilder(StreamInterface::class)
->disableOriginalConstructor()->getMock();
$postBody->method('getContents')->willReturn(json_encode($this->postData));
$postResponse = $this->getMockBuilder(ResponseInterface::class)
->disableOriginalConstructor()->getMock();
$postResponse->method('getStatusCode')->willReturn(200);
$postResponse->method('getBody')->willReturn($postBody);
$deleteBody = $this->getMockBuilder(StreamInterface::class)
->disableOriginalConstructor()->getMock();
$deleteBody->method('getContents')->willReturn('Customer Deleted');
$deleteResponse = $this->getMockBuilder(ResponseInterface::class)
->disableOriginalConstructor()->getMock();
$deleteResponse->method('getStatusCode')->willReturn(204);
$deleteResponse->method('getBody')->willReturn($deleteBody);
$this->client = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()->getMock();
$this->client->method('get')->willReturn($getResponse);
$this->client->method('post')->willReturn($postResponse);
$this->client->method('put')->willReturn($postResponse);
$this->client->method('delete')->willReturn($deleteResponse);
}
public function testGetById(): void
{
$service = new Service\Venta\MediosPago\Toku\Customer($this->client, $this->customerRepository);
$this->assertEquals(json_decode(json_encode($this->customer), true), $service->getById($this->customer->id));
}
public function testGetByExternalId(): void
{
$service = new Service\Venta\MediosPago\Toku\Customer($this->client, $this->customerRepository);
$this->assertEquals(json_decode(json_encode($this->customer), true), $service->getByExternalId($this->customer->rut()));
}
public function testGet(): void
{
$service = new Service\Venta\MediosPago\Toku\Customer($this->client, $this->customerRepository);
$this->assertArrayIsEqualToArrayIgnoringListOfKeys($this->getData, $service->get($this->customer->toku_id), []);
}
public function testAdd(): void
{
$service = new Service\Venta\MediosPago\Toku\Customer($this->client, $this->customerRepository);
$sendData = [
'rut' => $this->postData['government_id'],
'nombreCompleto' => $this->postData['name'],
'email' => $this->postData['email'],
'telefono' => $this->postData['phone_number']
];
$this->assertTrue($service->add($sendData));
}
public function testEdit(): void
{
$service = new Service\Venta\MediosPago\Toku\Customer($this->client, $this->customerRepository);
$sendData = [
'rut' => $this->postData['government_id'],
'nombreCompleto' => $this->postData['name'],
'email' => $this->postData['email'],
'telefono' => $this->postData['phone_number']
];
$this->assertTrue($service->edit($this->customer->toku_id, $sendData));
}
public function testDelete(): void
{
$service = new Service\Venta\MediosPago\Toku\Customer($this->client, $this->customerRepository);
$this->expectNotToPerformAssertions();
$service->delete($this->customer->toku_id);
}
}