From 487c2c07c8a8b1e7d2ccd28a630f2789ede6bd62 Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 5 Oct 2022 15:02:23 +0200 Subject: [PATCH] 6 fientries update connector (#7) Added feature to have the type of entity to create, update or delete be different from the connector type. This fixes an issue with providing data for the UpdateConnector 'FiEntries', whose type of entity to create must be 'FiEntryPar'. Co-authored-by: Peter Kok Co-authored-by: MegaChriz --- src/Core/Query/Delete.php | 6 +- src/Core/Query/Insert.php | 6 +- src/Core/Query/Update.php | 6 +- src/Core/Query/UpdateBase.php | 14 ++++- src/Core/Server.php | 12 ++-- src/Core/ServerInterface.php | 12 +++- tests/src/Core/ServerTest.php | 114 ++++++++++++++++++++++++++++++++++ 7 files changed, 153 insertions(+), 17 deletions(-) diff --git a/src/Core/Query/Delete.php b/src/Core/Query/Delete.php index 94ee1d3..771a21f 100644 --- a/src/Core/Query/Delete.php +++ b/src/Core/Query/Delete.php @@ -25,9 +25,11 @@ class Delete extends UpdateBase implements DeleteInterface { * The data to delete. * @param array $attribute_keys * (optional) The keys belonging to attributes. + * @param string $entity_type_id + * (optional) The type of entity to delete. */ - public function __construct(ServerInterface $server, $connector_id, array $data, array $attribute_keys = []) { - parent::__construct($server, $connector_id, $data, $attribute_keys); + public function __construct(ServerInterface $server, $connector_id, array $data, array $attribute_keys = [], string $entity_type_id = '') { + parent::__construct($server, $connector_id, $data, $attribute_keys, $entity_type_id); $this->entityContainer->setAction(EntityInterface::FIELDS_DELETE); $this->entityContainer->fromArray($data); } diff --git a/src/Core/Query/Insert.php b/src/Core/Query/Insert.php index 6a4338b..fda62e7 100644 --- a/src/Core/Query/Insert.php +++ b/src/Core/Query/Insert.php @@ -20,9 +20,11 @@ class Insert extends UpdateBase implements InsertInterface { * The data to insert. * @param array $attribute_keys * (optional) The keys belonging to attributes. + * @param string $entity_type_id + * (optional) The type of entity to insert. */ - public function __construct(ServerInterface $server, $connector_id, array $data, array $attribute_keys = []) { - parent::__construct($server, $connector_id, $data, $attribute_keys); + public function __construct(ServerInterface $server, $connector_id, array $data, array $attribute_keys = [], string $entity_type_id = '') { + parent::__construct($server, $connector_id, $data, $attribute_keys, $entity_type_id); $this->entityContainer->fromArray($data); } diff --git a/src/Core/Query/Update.php b/src/Core/Query/Update.php index cf4d554..1c439f7 100644 --- a/src/Core/Query/Update.php +++ b/src/Core/Query/Update.php @@ -25,9 +25,11 @@ class Update extends UpdateBase implements UpdateInterface { * The data to update. * @param array $attribute_keys * (optional) The keys belonging to attributes. + * @param string $entity_type_id + * (optional) The type of entity to update. */ - public function __construct(ServerInterface $server, $connector_id, array $data, array $attribute_keys = []) { - parent::__construct($server, $connector_id, $data, $attribute_keys); + public function __construct(ServerInterface $server, $connector_id, array $data, array $attribute_keys = [], string $entity_type_id = '') { + parent::__construct($server, $connector_id, $data, $attribute_keys, $entity_type_id); $this->entityContainer->setAction(EntityInterface::FIELDS_UPDATE); $this->entityContainer->fromArray($data); } diff --git a/src/Core/Query/UpdateBase.php b/src/Core/Query/UpdateBase.php index a4a76d1..b05f1f5 100644 --- a/src/Core/Query/UpdateBase.php +++ b/src/Core/Query/UpdateBase.php @@ -19,6 +19,13 @@ class UpdateBase extends Query implements UpdateBaseInterface { */ protected $connectorId; + /** + * The type of entity to insert, update or delete. + * + * @var string + */ + protected $entityTypeId; + /** * An entity container. * @@ -41,11 +48,14 @@ class UpdateBase extends Query implements UpdateBaseInterface { * The data to update. * @param array $attribute_keys * (optional) The keys belonging to attributes. + * @param string $entity_type_id + * (optional) The type of entity to insert, update or delete. */ - public function __construct(ServerInterface $server, $connector_id, array &$data, array $attribute_keys = []) { + public function __construct(ServerInterface $server, $connector_id, array &$data, array $attribute_keys = [], string $entity_type_id = '') { parent::__construct($server); $this->connectorId = $connector_id; - $this->entityContainer = new EntityContainer($connector_id); + $this->entityTypeId = $entity_type_id ?: $connector_id; + $this->entityContainer = new EntityContainer($this->entityTypeId); if (!empty($attribute_keys)) { if (ArrayHelper::isAssociative($data)) { diff --git a/src/Core/Server.php b/src/Core/Server.php index 580b787..e34f3e2 100644 --- a/src/Core/Server.php +++ b/src/Core/Server.php @@ -73,22 +73,22 @@ public function get($connector_id) { /** * {@inheritdoc} */ - public function insert($connector_id, array $data, array $attribute_keys = []) { - return new Insert($this, $connector_id, $data, $attribute_keys); + public function insert($connector_id, array $data, array $attribute_keys = [], string $entity_type_id = '') { + return new Insert($this, $connector_id, $data, $attribute_keys, $entity_type_id); } /** * {@inheritdoc} */ - public function update($connector_id, array $data, array $attribute_keys = []) { - return new Update($this, $connector_id, $data, $attribute_keys); + public function update($connector_id, array $data, array $attribute_keys = [], string $entity_type_id = '') { + return new Update($this, $connector_id, $data, $attribute_keys, $entity_type_id); } /** * {@inheritdoc} */ - public function delete($connector_id, array $data, array $attribute_keys = []) { - return new Delete($this, $connector_id, $data, $attribute_keys); + public function delete($connector_id, array $data, array $attribute_keys = [], string $entity_type_id = '') { + return new Delete($this, $connector_id, $data, $attribute_keys, $entity_type_id); } // -------------------------------------------------------------- diff --git a/src/Core/ServerInterface.php b/src/Core/ServerInterface.php index e6d5e6f..25801f8 100644 --- a/src/Core/ServerInterface.php +++ b/src/Core/ServerInterface.php @@ -31,11 +31,13 @@ public function get($connector_id); * The data to insert. * @param array $attribute_keys * (optional) The keys belonging to attributes. + * @param string $entity_type_id + * (optional) The type of entity that needs to be inserted. * * @return \Afas\Core\Query\Insert * An insert query. */ - public function insert($connector_id, array $data, array $attribute_keys = []); + public function insert($connector_id, array $data, array $attribute_keys = [], string $entity_type_id = ''); /** * Returns an update query object. @@ -46,11 +48,13 @@ public function insert($connector_id, array $data, array $attribute_keys = []); * The data to update. * @param array $attribute_keys * (optional) The keys belonging to attributes. + * @param string $entity_type_id + * (optional) The type of entity that needs to be updated. * * @return \Afas\Core\Query\Update * An update query. */ - public function update($connector_id, array $data, array $attribute_keys = []); + public function update($connector_id, array $data, array $attribute_keys = [], string $entity_type_id = ''); /** * Returns an update query object. @@ -61,11 +65,13 @@ public function update($connector_id, array $data, array $attribute_keys = []); * The data to delete. * @param array $attribute_keys * (optional) The keys belonging to attributes. + * @param string $entity_type_id + * (optional) The type of entity that needs to be deleted. * * @return \Afas\Core\Query\Delete * A delete query. */ - public function delete($connector_id, array $data, array $attribute_keys = []); + public function delete($connector_id, array $data, array $attribute_keys = [], string $entity_type_id = ''); // -------------------------------------------------------------- // GETTERS diff --git a/tests/src/Core/ServerTest.php b/tests/src/Core/ServerTest.php index ca33911..412e801 100644 --- a/tests/src/Core/ServerTest.php +++ b/tests/src/Core/ServerTest.php @@ -83,6 +83,44 @@ public function testInsertWithAttributes() { $this->assertXmlStringEqualsXmlString($expected, $insert->getEntityContainer()->compile()); } + /** + * @covers ::insert + */ + public function testInsertWithEntityTypeId() { + $insert = $this->server->insert('Dummy', [ + 'DbId' => 12345, + ], [], 'Dummy2'); + + $expected = ' + + + 12345 + + + '; + $this->assertXmlStringEqualsXmlString($expected, $insert->getEntityContainer()->compile()); + } + + /** + * @covers ::insert + */ + public function testInsertWithAttributesAndEntityTypeId() { + $insert = $this->server->insert('Dummy', [ + 'DbId' => 12345, + 'CdId' => 10001, + 'Foo' => 'Bar', + ], ['DbId', 'CdId'], 'Dummy2'); + + $expected = ' + + + Bar + + + '; + $this->assertXmlStringEqualsXmlString($expected, $insert->getEntityContainer()->compile()); + } + /** * @covers ::update */ @@ -128,6 +166,44 @@ public function testUpdateWithAttributes() { $this->assertXmlStringEqualsXmlString($expected, $update->getEntityContainer()->compile()); } + /** + * @covers ::update + */ + public function testUpdateWithEntityTypeId() { + $update = $this->server->update('Dummy', [ + 'DbId' => 12345, + ], [], 'Dummy2'); + + $expected = ' + + + 12345 + + + '; + $this->assertXmlStringEqualsXmlString($expected, $update->getEntityContainer()->compile()); + } + + /** + * @covers ::update + */ + public function testUpdateWithAttributesAndEntityTypeId() { + $update = $this->server->update('Dummy', [ + 'DbId' => 12345, + 'CdId' => 10001, + 'Foo' => 'Bar', + ], ['DbId', 'CdId'], 'Dummy2'); + + $expected = ' + + + Bar + + + '; + $this->assertXmlStringEqualsXmlString($expected, $update->getEntityContainer()->compile()); + } + /** * @covers ::delete */ @@ -173,6 +249,44 @@ public function testDeleteWithAttributes() { $this->assertXmlStringEqualsXmlString($expected, $delete->getEntityContainer()->compile()); } + /** + * @covers ::delete + */ + public function testDeleteWithEntityTypeId() { + $delete = $this->server->delete('Dummy', [ + 'DbId' => 12345, + ], [], 'Dummy2'); + + $expected = ' + + + 12345 + + + '; + $this->assertXmlStringEqualsXmlString($expected, $delete->getEntityContainer()->compile()); + } + + /** + * @covers ::delete + */ + public function testDeleteWithAttributesAndEntityTypeId() { + $delete = $this->server->delete('Dummy', [ + 'DbId' => 12345, + 'CdId' => 10001, + 'Foo' => 'Bar', + ], ['DbId', 'CdId'], 'Dummy2'); + + $expected = ' + + + Bar + + + '; + $this->assertXmlStringEqualsXmlString($expected, $delete->getEntityContainer()->compile()); + } + /** * @covers ::getBaseUrl * @covers ::__construct