Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions tests/Common/StandardizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,65 @@ public function testToStd()
$expected = json_decode(file_get_contents($this->fixturesPath . 'txt/nfe_4.0.json'));
$this->assertEquals($expected, $std);
}

public function testToString()
{
$xml = file_get_contents($this->fixturesPath . 'xml/2017nfe_antiga_v310.xml');
$st = new Standardize($xml);
$st->whichIs();
$result = (string)$st;
$this->assertNotEmpty($result);
$this->assertStringContainsString('<NFe', $result);
}

public function testSimpleXml()
{
$xml = file_get_contents($this->fixturesPath . 'xml/2017nfe_antiga_v310.xml');
$st = new Standardize($xml);
$sxml = $st->simpleXml();
$this->assertInstanceOf(\SimpleXMLElement::class, $sxml);
}

public function testToStdWithXmlParameter()
{
$st = new Standardize();
$xml = file_get_contents($this->fixturesPath . 'xml/nfe_4.0.xml');
$std = $st->toStd($xml);
$this->assertIsObject($std);
}

public function testToJsonWithXmlParameter()
{
$st = new Standardize();
$xml = file_get_contents($this->fixturesPath . 'xml/2017nfe_antiga_v310.xml');
$json = $st->toJson($xml);
$this->assertJson($json);
}

public function testToArrayWithXmlParameter()
{
$st = new Standardize();
$xml = file_get_contents($this->fixturesPath . 'xml/2017nfe_antiga_v310.xml');
$arr = $st->toArray($xml);
$this->assertIsArray($arr);
$this->assertNotEmpty($arr);
}

public function testWhichIsWithConstructorXml()
{
$xml = file_get_contents($this->fixturesPath . 'xml/nfe_4.0.xml');
$st = new Standardize($xml);
$result = $st->whichIs();
$this->assertTrue(in_array($result, $st->rootTagList));
}

public function testNfceXml()
{
$xml = file_get_contents($this->fixturesPath . 'xml/nfce.xml');
$st = new Standardize($xml);
$resp = $st->whichIs();
// nfce.xml is a processed NFe (nfeProc)
$this->assertNotEmpty($resp);
$this->assertTrue(in_array($resp, $st->rootTagList));
}
}
211 changes: 211 additions & 0 deletions tests/Factories/ParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?php

declare(strict_types=1);

namespace NFePHP\NFe\Tests\Factories;

use NFePHP\NFe\Factories\Parser;
use PHPUnit\Framework\TestCase;

class ParserTest extends TestCase
{
private string $fixturesPath;

protected function setUp(): void
{
$this->fixturesPath = dirname(__DIR__) . '/fixtures/txt/';
}

// =========================================================================
// Constructor
// =========================================================================

public function test_constructor_default(): void
{
$parser = new Parser();
$this->assertInstanceOf(Parser::class, $parser);
}

public function test_constructor_with_version(): void
{
$parser = new Parser('4.00');
$this->assertInstanceOf(Parser::class, $parser);
}

public function test_constructor_with_sebrae_layout(): void
{
$parser = new Parser('4.00', Parser::SEBRAE);
$this->assertInstanceOf(Parser::class, $parser);
}

public function test_constructor_with_local_v12_layout(): void
{
$parser = new Parser('4.00', Parser::LOCAL_V12);
$this->assertInstanceOf(Parser::class, $parser);
}

public function test_constructor_with_local_v13_layout(): void
{
$parser = new Parser('4.00', Parser::LOCAL_V13);
$this->assertInstanceOf(Parser::class, $parser);
}

// =========================================================================
// toXml - LOCAL_V12 layout
// =========================================================================

public function test_toXml_local_v12_returns_valid_xml(): void
{
$txt = file_get_contents($this->fixturesPath . 'nfe_4.00_local_01.txt');
$notas = $this->parseTxt($txt);

$parser = new Parser('4.00', Parser::LOCAL_V12);
$xml = $parser->toXml($notas[0]);

$this->assertNotNull($xml);
$this->assertStringContainsString('<NFe', $xml);
$this->assertStringContainsString('<infNFe', $xml);
$this->assertStringContainsString('<ide>', $xml);
$this->assertStringContainsString('<emit>', $xml);
$this->assertStringContainsString('<dest>', $xml);
$this->assertStringContainsString('<det ', $xml);
$this->assertStringContainsString('<total>', $xml);
}

public function test_toXml_local_v12_valid_xml_structure(): void
{
$txt = file_get_contents($this->fixturesPath . 'nfe_4.00_local_01.txt');
$notas = $this->parseTxt($txt);

$parser = new Parser('4.00', Parser::LOCAL_V12);
$xml = $parser->toXml($notas[0]);

$nfe = new \SimpleXMLElement($xml);
// Check basic structure
$this->assertNotEmpty((string)$nfe->infNFe->ide->cUF);
$this->assertNotEmpty((string)$nfe->infNFe->emit->CNPJ);
$this->assertNotEmpty((string)$nfe->infNFe->dest->CNPJ);
$this->assertGreaterThan(0, count($nfe->infNFe->det));
}

// =========================================================================
// toXml - SEBRAE layout
// =========================================================================

public function test_toXml_sebrae_returns_valid_xml(): void
{
$txt = file_get_contents($this->fixturesPath . 'nota_4.00_sebrae.txt');
$notas = $this->parseTxt($txt);

$parser = new Parser('4.00', Parser::SEBRAE);
$xml = $parser->toXml($notas[0]);

$this->assertNotNull($xml);
$this->assertStringContainsString('<NFe', $xml);
$this->assertStringContainsString('<infNFe', $xml);
}

public function test_toXml_sebrae_valid_xml_structure(): void
{
$txt = file_get_contents($this->fixturesPath . 'nota_4.00_sebrae.txt');
$notas = $this->parseTxt($txt);

$parser = new Parser('4.00', Parser::SEBRAE);
$xml = $parser->toXml($notas[0]);

$nfe = new \SimpleXMLElement($xml);
$this->assertEquals('52', (string)$nfe->infNFe->ide->cUF);
$this->assertEquals('55', (string)$nfe->infNFe->ide->mod);
}

// =========================================================================
// dump - returns stdClass array
// =========================================================================

public function test_dump_returns_array_of_stdclass(): void
{
$txt = file_get_contents($this->fixturesPath . 'nfe_4.00_local_01.txt');
$notas = $this->parseTxt($txt);

$parser = new Parser('4.00', Parser::LOCAL_V12);
$result = $parser->dump($notas[0]);

$this->assertIsArray($result);
$this->assertNotEmpty($result);
// First element should be the infNFe std object
$this->assertInstanceOf(\stdClass::class, $result[0]);
}

public function test_dump_contains_nfe_id(): void
{
$txt = file_get_contents($this->fixturesPath . 'nfe_4.00_local_01.txt');
$notas = $this->parseTxt($txt);

$parser = new Parser('4.00', Parser::LOCAL_V12);
$result = $parser->dump($notas[0]);

$this->assertStringContainsString('NFe', $result[0]->Id);
}

// =========================================================================
// getErrors
// =========================================================================

public function test_getErrors_returns_empty_on_success(): void
{
$txt = file_get_contents($this->fixturesPath . 'nfe_4.00_local_01.txt');
$notas = $this->parseTxt($txt);

$parser = new Parser('4.00', Parser::LOCAL_V12);
$parser->toXml($notas[0]);

$errors = $parser->getErrors();
$this->assertIsArray($errors);
}

// =========================================================================
// LOCAL layout (v3.10 format)
// =========================================================================

public function test_toXml_local_layout_nfe_txt(): void
{
$txt = file_get_contents($this->fixturesPath . 'NFe.txt');
$notas = $this->parseTxt($txt);

$parser = new Parser('3.10', Parser::LOCAL);
$xml = $parser->toXml($notas[0]);

$this->assertNotNull($xml);
$this->assertStringContainsString('<NFe', $xml);
}

// =========================================================================
// Helper to split TXT into notes arrays (mimicking Convert logic)
// =========================================================================

private function parseTxt(string $txt): array
{
$txt = str_replace("\r\n", "\n", $txt);
$lines = explode("\n", $txt);
$notas = [];
$current = [];
foreach ($lines as $line) {
$line = trim($line);
if (empty($line)) {
continue;
}
if (stripos($line, 'NOTAFISCAL') === 0) {
if (!empty($current)) {
$notas[] = $current;
}
$current = [];
continue;
}
$current[] = $line;
}
if (!empty($current)) {
$notas[] = $current;
}
return $notas;
}
}
Loading
Loading