From 3594858e146a24bc110464aaf285beecd36eded7 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Thu, 2 May 2019 14:19:52 +0200 Subject: [PATCH 1/2] New Transit::attach() method to merge an entity to the handler storage --- src/Transit.php | 18 ++++++++++++++++++ tests/TransitTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/Transit.php b/src/Transit.php index f3d163a..234f996 100644 --- a/src/Transit.php +++ b/src/Transit.php @@ -68,6 +68,24 @@ public function select(string $domainClass, array $whereEquals = []) : TransitSe ); } + /** + * Attach a domain object to plan. + * + * @param \Atlas\Transit\object $domain + * + * @return \Atlas\Transit\Transit + * @throws \Atlas\Transit\Exception + */ + public function attach(object $domain): Transit + { + $handler = $this->handlerLocator->get($domain); + /** @var \Atlas\Mapper\Record $record */ + $record = $handler->updateSource($domain, $this->plan); + $record->getRow()->init(''); + + return $this; + } + // PLAN TO insert/update public function store(object $domain) : void { diff --git a/tests/TransitTest.php b/tests/TransitTest.php index b552215..be90bd0 100644 --- a/tests/TransitTest.php +++ b/tests/TransitTest.php @@ -533,4 +533,29 @@ public function testStore() $this->assertSame(101, $actual['responses'][0]['responseId']); $this->assertSame(14, $actual['responses'][0]['author']['authorId']); } + + public function testAttach() + { + // Creation of author in first context + $entity = new Author('Author'); + $this->transit->store($entity); + $this->transit->persist(); + + // New transit class for new context + $secondTransit = FakeTransit::new( + $this->atlas, + 'Atlas\\Testing\\DataSource\\' + ); + $secondTransit->attach($entity); + + // Modify entity after attach to context + $entity->setName('Arthur 2123 fdgfd'); + + $secondTransit->store($entity); + $secondTransit->persist(); + + /** @var \Atlas\Mapper\Record $record */ + $record = $secondTransit->getStorage()->offsetGet($entity); + $this->assertEquals('UPDATED', $record->getRow()->getStatus()); + } } From f2909d079add7f46a0fc49b73294e71b144e3d38 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Thu, 2 May 2019 15:01:31 +0200 Subject: [PATCH 2/2] Fix PhpDoc --- src/Transit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Transit.php b/src/Transit.php index 234f996..d654653 100644 --- a/src/Transit.php +++ b/src/Transit.php @@ -71,7 +71,7 @@ public function select(string $domainClass, array $whereEquals = []) : TransitSe /** * Attach a domain object to plan. * - * @param \Atlas\Transit\object $domain + * @param object $domain * * @return \Atlas\Transit\Transit * @throws \Atlas\Transit\Exception