Fixes for tests

This commit is contained in:
David Bomba 2025-05-05 08:22:07 +10:00
parent 1dee59bafa
commit 41b2387643
4 changed files with 80 additions and 260 deletions

View File

@ -4,244 +4,30 @@ namespace App\Services\EDocument\Standards\Verifactu\Types;
use Symfony\Component\Serializer\Annotation\SerializedName;
/**
* ObligadoEmision represents a required entity with NombreRazon and NIF.
* Extends PersonaFisicaJuridicaES but enforces both properties to be required at construction time.
*/
class ObligadoEmision extends PersonaFisicaJuridicaES
{
/** @var string|null */
#[SerializedName('sum1:TipoPersona')]
protected $TipoPersona;
/** @var string|null */
#[SerializedName('sum1:RazonSocialCompleta')]
protected $RazonSocialCompleta;
/** @var string|null */
#[SerializedName('sum1:NombreComercial')]
protected $NombreComercial;
/** @var string|null */
#[SerializedName('sum1:CodigoPostal')]
protected $CodigoPostal;
/** @var string|null */
#[SerializedName('sum1:Direccion')]
protected $Direccion;
/** @var string|null */
#[SerializedName('sum1:Poblacion')]
protected $Poblacion;
/** @var string|null */
#[SerializedName('sum1:Provincia')]
protected $Provincia;
/** @var string|null */
#[SerializedName('sum1:Pais')]
protected $Pais;
/** @var string|null */
#[SerializedName('sum1:Telefono')]
protected $Telefono;
/** @var string|null */
#[SerializedName('sum1:Email')]
protected $Email;
/** @var string|null */
#[SerializedName('sum1:Web')]
protected $Web;
/** @var string */
#[SerializedName('sum1:NombreRazon')]
protected $NombreRazon;
/** @var string */
#[SerializedName('sum1:NIFRepresentante')]
protected $NIFRepresentante;
public function getTipoPersona(): ?string
public function __construct()
{
return $this->TipoPersona;
}
public function setTipoPersona(?string $tipoPersona): self
public function setNombreRazon(?string $nombreRazon): self
{
if ($tipoPersona !== null && !in_array($tipoPersona, ['F', 'J'])) {
throw new \InvalidArgumentException('TipoPersona must be either "F" (Física) or "J" (Jurídica)');
if (empty($nombreRazon)) {
throw new \InvalidArgumentException('NombreRazon is required for ObligadoEmision');
}
$this->TipoPersona = $tipoPersona;
return $this;
return parent::setNombreRazon($nombreRazon);
}
public function getRazonSocialCompleta(): ?string
public function setNIF(string $nif): self
{
return $this->RazonSocialCompleta;
}
public function setRazonSocialCompleta(?string $razonSocialCompleta): self
{
if ($razonSocialCompleta !== null && strlen($razonSocialCompleta) > 120) {
throw new \InvalidArgumentException('RazonSocialCompleta must not exceed 120 characters');
if (empty($nif)) {
throw new \InvalidArgumentException('NIF is required for ObligadoEmision');
}
$this->RazonSocialCompleta = $razonSocialCompleta;
return $this;
}
public function getNombreComercial(): ?string
{
return $this->NombreComercial;
}
public function setNombreComercial(?string $nombreComercial): self
{
if ($nombreComercial !== null && strlen($nombreComercial) > 120) {
throw new \InvalidArgumentException('NombreComercial must not exceed 120 characters');
}
$this->NombreComercial = $nombreComercial;
return $this;
}
public function getCodigoPostal(): ?string
{
return $this->CodigoPostal;
}
public function setCodigoPostal(?string $codigoPostal): self
{
if ($codigoPostal !== null && strlen($codigoPostal) > 10) {
throw new \InvalidArgumentException('CodigoPostal must not exceed 10 characters');
}
$this->CodigoPostal = $codigoPostal;
return $this;
}
public function getDireccion(): ?string
{
return $this->Direccion;
}
public function setDireccion(?string $direccion): self
{
if ($direccion !== null && strlen($direccion) > 250) {
throw new \InvalidArgumentException('Direccion must not exceed 250 characters');
}
$this->Direccion = $direccion;
return $this;
}
public function getPoblacion(): ?string
{
return $this->Poblacion;
}
public function setPoblacion(?string $poblacion): self
{
if ($poblacion !== null && strlen($poblacion) > 50) {
throw new \InvalidArgumentException('Poblacion must not exceed 50 characters');
}
$this->Poblacion = $poblacion;
return $this;
}
public function getProvincia(): ?string
{
return $this->Provincia;
}
public function setProvincia(?string $provincia): self
{
if ($provincia !== null && strlen($provincia) > 20) {
throw new \InvalidArgumentException('Provincia must not exceed 20 characters');
}
$this->Provincia = $provincia;
return $this;
}
public function getPais(): ?string
{
return $this->Pais;
}
public function setPais(?string $pais): self
{
if ($pais !== null && strlen($pais) > 20) {
throw new \InvalidArgumentException('Pais must not exceed 20 characters');
}
$this->Pais = $pais;
return $this;
}
public function getTelefono(): ?string
{
return $this->Telefono;
}
public function setTelefono(?string $telefono): self
{
if ($telefono !== null && strlen($telefono) > 20) {
throw new \InvalidArgumentException('Telefono must not exceed 20 characters');
}
$this->Telefono = $telefono;
return $this;
}
public function getEmail(): ?string
{
return $this->Email;
}
public function setEmail(?string $email): self
{
if ($email !== null) {
if (strlen($email) > 120) {
throw new \InvalidArgumentException('Email must not exceed 120 characters');
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('Invalid email format');
}
}
$this->Email = $email;
return $this;
}
public function getWeb(): ?string
{
return $this->Web;
}
public function setWeb(?string $web): self
{
if ($web !== null && strlen($web) > 250) {
throw new \InvalidArgumentException('Web must not exceed 250 characters');
}
$this->Web = $web;
return $this;
}
public function getNombreRazon(): string
{
return $this->NombreRazon;
}
public function setNombreRazon(string $nombreRazon): self
{
if (strlen($nombreRazon) > 120) {
throw new \InvalidArgumentException('NombreRazon must not exceed 120 characters');
}
$this->NombreRazon = $nombreRazon;
return $this;
}
public function getNIFRepresentante(): string
{
return $this->NIFRepresentante;
}
public function setNIFRepresentante(string $nifRepresentante): self
{
if (!preg_match('/^[A-Z0-9]{9}$/', $nifRepresentante)) {
throw new \InvalidArgumentException('NIFRepresentante must be a valid NIF (9 alphanumeric characters)');
}
$this->NIFRepresentante = $nifRepresentante;
return $this;
return parent::setNIF($nif);
}
}

View File

@ -495,4 +495,38 @@ class RegistroFacturacionAlta
$this->SistemaInformatico = $sistemaInformatico;
return $this;
}
public function toRegistroAlta(): RegistroAlta
{
$registroAlta = new RegistroAlta();
$registroAlta->setIDVersion($this->getIDVersion());
// Convert IDFacturaExpedida to IDFactura
$idFactura = new IDFactura();
$idFactura->setIDEmisorFactura($this->getIDFactura()->getIDEmisorFactura());
$idFactura->setNumSerieFactura($this->getIDFactura()->getNumSerieFactura());
$idFactura->setFechaExpedicionFactura($this->getIDFactura()->getFechaExpedicionFactura());
$registroAlta->setIDFactura($idFactura);
$registroAlta->setNombreRazonEmisor($this->getNombreRazonEmisor());
$registroAlta->setTipoFactura($this->getTipoFactura());
$registroAlta->setDescripcionOperacion($this->getDescripcionOperacion());
// Convert array of Destinatarios to Destinatarios object
$destinatarios = new Destinatarios();
foreach ($this->getDestinatarios() as $destinatario) {
$destinatarios->addDestinatario($destinatario);
}
$registroAlta->setDestinatarios($destinatarios);
$registroAlta->setDesglose($this->getDesglose());
$registroAlta->setCuotaTotal($this->getCuotaTotal());
$registroAlta->setImporteTotal($this->getImporteTotal());
$registroAlta->setSistemaInformatico($this->getSistemaInformatico());
$registroAlta->setFechaHoraHusoGenRegistro($this->getFechaHoraHusoGenRegistro()->format('Y-m-d\TH:i:sP'));
$registroAlta->setTipoHuella($this->getTipoHuella());
$registroAlta->setHuella($this->getHuella());
return $registroAlta;
}
}

View File

@ -116,11 +116,11 @@ class VerifactuClient
/**
* Send an invoice registration (alta) request
*
* @param RegistroFacturacionAlta $registro
* @param RegistroAlta $registro
* @return mixed The SOAP response
* @throws \SoapFault
*/
public function sendRegistroAlta(RegistroFacturacionAlta $registro)
public function sendRegistroAlta(RegistroAlta $registro)
{
$factura = new RegistroFactura();
$factura->setRegistroAlta($registro);

View File

@ -75,19 +75,6 @@ class VerifactuTest extends TestCase
$detalle->setTipoImpositivo(101.00); // Exceeds 100%
}
public function testPersonaFisicaJuridicaValidation()
{
$persona = new PersonaFisicaJuridica();
$persona->setNombreRazon('Empresa Test')
->setNIF('B12345678');
$this->assertEquals('Empresa Test', $persona->getNombreRazon());
$this->assertEquals('B12345678', $persona->getNIF());
$this->expectException(\InvalidArgumentException::class);
$persona->setNombreRazon(str_repeat('a', 121)); // Exceeds 120 chars
}
public function testOperacionExentaValidation()
{
@ -131,19 +118,32 @@ class VerifactuTest extends TestCase
public function testObligadoEmisionValidation()
{
$obligado = new ObligadoEmision();
$obligado->setNIF('B12345678')
->setTipoPersona('J')
->setRazonSocialCompleta('Empresa Test')
->setEmail('test@example.com');
$obligado->setNIF('B12345678');
$obligado->setNombreRazon('Empresa Test');
$this->assertEquals('B12345678', $obligado->getNIF());
$this->assertEquals('J', $obligado->getTipoPersona());
$this->assertEquals('Empresa Test', $obligado->getRazonSocialCompleta());
$this->assertEquals('test@example.com', $obligado->getEmail());
$this->assertEquals('Empresa Test', $obligado->getNombreRazon());
}
public function testObligadoEmisionEmptyNombreRazon()
{
$obligado = new ObligadoEmision();
$this->expectException(\InvalidArgumentException::class);
$obligado->setEmail('invalid-email'); // Invalid email format
$obligado->setNombreRazon('');
}
public function testObligadoEmisionEmptyNIF()
{
$obligado = new ObligadoEmision();
$this->expectException(\InvalidArgumentException::class);
$obligado->setNIF('');
}
public function testObligadoEmisionInvalidNIF()
{
$obligado = new ObligadoEmision();
$this->expectException(\InvalidArgumentException::class);
$obligado->setNIF('invalid');
}
public function testCreateCompleteRegistroFactura()
@ -151,18 +151,18 @@ class VerifactuTest extends TestCase
// Create ObligadoEmision
$obligadoEmision = new ObligadoEmision();
$obligadoEmision->setNombreRazon('XXXXX')
->setNIF('AAAA');
->setNIF('A12345678');
// Create IDFactura
$idFactura = new IDFactura();
$idFactura->setIDEmisorFactura('AAAA')
$idFactura->setIDEmisorFactura('B12345678')
->setNumSerieFactura('12345')
->setFechaExpedicionFactura('13-09-2024');
// Create Destinatario
$destinatario = new IDDestinatario();
$destinatario->setNombreRazon('YYYY')
->setNIF('BBBB');
->setNIF('C12345678');
// Create Destinatarios collection
$destinatarios = new Destinatarios();
@ -190,7 +190,7 @@ class VerifactuTest extends TestCase
// Create RegistroAnterior for Encadenamiento
$registroAnterior = new IDFacturaAR();
$registroAnterior->setIDEmisorFactura('AAAA')
$registroAnterior->setIDEmisorFactura('E12345678')
->setNumSerieFactura('44')
->setFechaExpedicionFactura('13-09-2024');
@ -202,7 +202,7 @@ class VerifactuTest extends TestCase
// Create SistemaInformatico
$sistemaInformatico = new SistemaInformatico();
$sistemaInformatico->setNombreRazon('SSSS')
->setNIF('NNNN')
->setNIF('D12345678')
->setNombreSistemaInformatico('NombreSistemaInformatico')
->setIdSistemaInformatico('77')
->setVersion('1.0.03')
@ -243,14 +243,14 @@ class VerifactuTest extends TestCase
$this->assertEquals('Huella', $registroAlta->getHuella());
// Test nested objects
$this->assertEquals('AAAA', $registroAlta->getIDFactura()->getIDEmisorFactura());
$this->assertEquals('B12345678', $registroAlta->getIDFactura()->getIDEmisorFactura());
$this->assertEquals('12345', $registroAlta->getIDFactura()->getNumSerieFactura());
$this->assertEquals('13-09-2024', $registroAlta->getIDFactura()->getFechaExpedicionFactura());
// Test Destinatarios
$destinatarios = $registroAlta->getDestinatarios();
$this->assertEquals('YYYY', $destinatarios->getIDDestinatario()[0]->getNombreRazon());
$this->assertEquals('BBBB', $destinatarios->getIDDestinatario()[0]->getNIF());
$this->assertEquals('C12345678', $destinatarios->getIDDestinatario()[0]->getNIF());
// Test Desglose
$detalles = $registroAlta->getDesglose()->getDetalleDesglose();
@ -263,7 +263,7 @@ class VerifactuTest extends TestCase
// Test SistemaInformatico
$this->assertEquals('SSSS', $registroAlta->getSistemaInformatico()->getNombreRazon());
$this->assertEquals('NNNN', $registroAlta->getSistemaInformatico()->getNIF());
$this->assertEquals('D12345678', $registroAlta->getSistemaInformatico()->getNIF());
$this->assertEquals('77', $registroAlta->getSistemaInformatico()->getIdSistemaInformatico());
}
}