Skip to content

Commit

Permalink
Casting XML to string (#1475)
Browse files Browse the repository at this point in the history
* Failing example for Xml insert

* Add test on DOMDocument

* Update to use actual domdocuments

* Add example of casting to string

* Add another test for xml writing of dom document

* Lint

* Update to use loadXml

* Unified inconsistent behavior of casting xml to string

* Added test for saving also xml element entry to database through doctrine

---------

Co-authored-by: John Mortlock <jmortlock@zenglobal.au>
  • Loading branch information
norberttech and jmortlock authored Feb 13, 2025
1 parent 5dcd26e commit 53d7c61
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,96 @@ public function test_inserts_new_rows_or_updates_already_existed_based_on_primar
);
}

public function test_inserts_xml_element_entry() : void
{
$this->pgsqlDatabaseContext->createTable((new Table(
$table = 'flow_doctrine_bulk_test',
[
new Column('id', Type::getType(Types::INTEGER), ['notnull' => true]),
new Column('name', Type::getType(Types::STRING), ['notnull' => true, 'length' => 255]),
new Column('description', Type::getType(Types::STRING), ['notnull' => true, 'length' => 255]),
],
))
->setPrimaryKey(['id']));

$loader = to_dbal_table_insert($this->connectionParams(), $table);

$documentA = new \DOMDocument();
$documentA->loadXml('<xml>Description One</xml>');

$documentB = new \DOMDocument();
$documentB->loadXml('<xml>Description Two</xml>');

$documentC = new \DOMDocument();
$documentC->loadXml('<xml>Description Three</xml>');

(data_frame())
->read(
from_array([
['id' => 1, 'name' => 'Name One', 'description' => $documentA->getElementsByTagName('xml')[0]],
['id' => 2, 'name' => 'Name Two', 'description' => $documentB->getElementsByTagName('xml')[0]],
['id' => 3, 'name' => 'Name Three', 'description' => $documentC->getElementsByTagName('xml')[0]],
]),
)
->load($loader)
->run();

self::assertEquals(3, $this->pgsqlDatabaseContext->tableCount($table));
self::assertEquals(
[
['id' => 1, 'name' => 'Name One', 'description' => '<xml>Description One</xml>'],
['id' => 2, 'name' => 'Name Two', 'description' => '<xml>Description Two</xml>'],
['id' => 3, 'name' => 'Name Three', 'description' => '<xml>Description Three</xml>'],
],
$this->pgsqlDatabaseContext->selectAll($table)
);
}

public function test_inserts_xml_entry() : void
{
$this->pgsqlDatabaseContext->createTable((new Table(
$table = 'flow_doctrine_bulk_test',
[
new Column('id', Type::getType(Types::INTEGER), ['notnull' => true]),
new Column('name', Type::getType(Types::STRING), ['notnull' => true, 'length' => 255]),
new Column('description', Type::getType(Types::STRING), ['notnull' => true, 'length' => 255]),
],
))
->setPrimaryKey(['id']));

$loader = to_dbal_table_insert($this->connectionParams(), $table);

$documentA = new \DOMDocument();
$documentA->loadXml('<xml>Description One</xml>');

$documentB = new \DOMDocument();
$documentB->loadXml('<xml>Description Two</xml>');

$documentC = new \DOMDocument();
$documentC->loadXml('<b>Description Three</b>');

(data_frame())
->read(
from_array([
['id' => 1, 'name' => 'Name One', 'description' => $documentA],
['id' => 2, 'name' => 'Name Two', 'description' => $documentB],
['id' => 3, 'name' => 'Name Three', 'description' => $documentC],
]),
)
->load($loader)
->run();

self::assertEquals(3, $this->pgsqlDatabaseContext->tableCount($table));
self::assertEquals(
[
['id' => 1, 'name' => 'Name One', 'description' => '<xml>Description One</xml>'],
['id' => 2, 'name' => 'Name Two', 'description' => '<xml>Description Two</xml>'],
['id' => 3, 'name' => 'Name Three', 'description' => '<b>Description Three</b>'],
],
$this->pgsqlDatabaseContext->selectAll($table)
);
}

public function test_update_multiple_rows_at_once() : void
{
$this->pgsqlDatabaseContext->createTable((new Table(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,34 @@
use function Flow\ETL\DSL\{average, df, from_array, overwrite, ref};
use function Flow\ETL\DSL\{config, flow_context, rows};
use function Flow\Filesystem\DSL\path;

use Flow\ETL\Adapter\JSON\JsonLoader;
use Flow\ETL\Tests\Double\FakeExtractor;
use Flow\ETL\{Tests\FlowTestCase};

final class JsonTest extends FlowTestCase
{
public function test_domdocument_json_file() : void
{
$domDocument = new \DOMDocument();
$domDocument->loadXml('<b>red</b>');

df()
->read(from_array([
['id' => 1, 'descriptionHtml' => $domDocument, 'size' => 'small'],
]))
->saveMode(overwrite())
->write(to_json($path = __DIR__ . '/var/test_domdocument.json'))
->run();

self::assertStringContainsString(
<<<'JSON'
[{"id":1,"descriptionHtml":"<b>red<\/b>","size":"small"}]
JSON,
\file_get_contents($path)
);
}

public function test_json_loader() : void
{
$path = __DIR__ . '/var/test_json_loader.json';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function value(mixed $value, Type $type, Caster $caster, Options $options
}

if ($value instanceof \DOMDocument) {
return $value->saveXML() ?: '';
return $value->saveXML($value->documentElement) ?: '';
}

if ($value instanceof \DOMElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,37 @@

namespace Flow\ETL\Tests\Integration\Function;

use function Flow\ETL\DSL\{df, from_rows, ref, row, rows, xml_element_entry, xml_entry};
use function Flow\ETL\DSL\{df, from_rows, ref, row, rows, type_string, xml_element_entry, xml_entry};

use Flow\ETL\Tests\FlowTestCase;

final class DOMElementValueTest extends FlowTestCase
{
public function test_dom_element_cast_as_string() : void
{
$document = new \DOMDocument();
$document->loadXml('<b>User Name 01</b>');

$rows = df()
->read(from_rows(
rows(
row(
xml_entry('html_raw', $document)
)
)
))
->withEntry('html', ref('html_raw')->cast(type_string()))
->drop('html_raw')
->fetch();

self::assertSame(
[
['html' => '<b>User Name 01</b>'],
],
$rows->toArray()
);
}

public function test_dom_element_value() : void
{
$rows = df()
Expand Down Expand Up @@ -53,6 +79,31 @@ public function test_dom_element_value_from_dom_document() : void
);
}

public function test_dom_element_value_on_dom_document() : void
{
$document = new \DOMDocument();
$document->loadXml('<b>User Name 01</b>');

$rows = df()
->read(from_rows(
rows(
row(
xml_entry('html_raw', $document)
)
)
))
->withEntry('html', ref('html_raw')->domElementValue())
->drop('html_raw')
->fetch();

self::assertSame(
[
['html' => 'User Name 01'],
],
$rows->toArray()
);
}

public function test_dom_element_value_on_xpath_result() : void
{
$rows = df()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function cast_provider() : array
'json_pretty' => [[1], 'json_pretty', "[\n 1\n]"],
'xml_to_array' => [$xml, 'array', ['root' => ['foo' => ['@attributes' => ['baz' => 'buz'], '@value' => 'bar']]]],
'string_to_xml' => [$xmlString, 'xml', $xml],
'xml_to_string' => [$xml, 'string', $fullXMLString],
'xml_to_string' => [$xml, 'string', '<root><foo baz="buz">bar</foo></root>'],
'datetime' => [new \DateTimeImmutable('2023-01-01 00:00:00 UTC'), 'string', '2023-01-01T00:00:00+00:00'],
'datetime_to_date' => [new \DateTimeImmutable('2023-01-01 00:01:00 UTC'), 'date', new \DateTimeImmutable('2023-01-01T00:00:00+00:00')],
'uuid' => [Uuid::fromString('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'), 'string', 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function __toString() : string
}
}, 'stringable'];
yield 'DOMDocument' => [new \DOMDocument(), '<?xml version="1.0"?>'];

$xml = (new \DOMDocument());
$xml->loadXML('<xml>Some Happy XML</xml>');

yield 'Not Empty DOMDocument' => [$xml, '<xml>Some Happy XML</xml>'];
yield 'DOMElement' => [new \DOMElement('element'), '<element/>'];
}

Expand Down
13 changes: 12 additions & 1 deletion src/lib/doctrine-dbal-bulk/src/Flow/Doctrine/Bulk/BulkData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Flow\Doctrine\Bulk;

use function Flow\ETL\DSL\dom_element_to_string;
use Doctrine\DBAL\Types\{Type, Types};
use Flow\Doctrine\Bulk\Exception\RuntimeException;

Expand Down Expand Up @@ -131,7 +132,7 @@ public function toSqlParameters(TableDefinition $table) : array
default => $entry,
},
'array' => match (Type::getTypeRegistry()->lookupName($table->dbalColumn($column)->getType())) {
Types::TEXT => \json_encode($entry, JSON_THROW_ON_ERROR),
Types::TEXT, Types::STRING => \json_encode($entry, JSON_THROW_ON_ERROR),
default => $entry,
},
'object' => match ($entry::class) {
Expand All @@ -143,6 +144,16 @@ public function toSqlParameters(TableDefinition $table) : array
Types::DATETIME_IMMUTABLE => \DateTimeImmutable::createFromMutable($entry),
default => $entry,
},
\DOMDocument::class => match (Type::getTypeRegistry()->lookupName($table->dbalColumn($column)->getType())) {
Types::TEXT,
Types::STRING => $entry->saveXML($entry->documentElement),
default => $entry,
},
\DOMElement::class => match (Type::getTypeRegistry()->lookupName($table->dbalColumn($column)->getType())) {
Types::TEXT,
Types::STRING => (string) dom_element_to_string($entry),
default => $entry,
},
default => $entry,
},
default => $entry,
Expand Down

0 comments on commit 53d7c61

Please sign in to comment.