/** * @param LinkMany $link */ public function update(LinkMany $link) { foreach ($link->getAdded() as $added) { $added->{$this->getForeignKey()} = $link->getModel()->{$this->getKey()}; } foreach ($link->getRemoved() as $added) { $added->{$this->getForeignKey()} = null; } }
/** * @covers ::update */ public function testUpdate() { $rel = new HasManyExclusive('cities', Country::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\City'); $model = new Country(['id' => 2]); $foreign1 = new City(['countryId' => 2]); $foreign2 = new City(['countryId' => 2]); $foreign3 = new City(['countryId' => 8]); $link = new LinkMany($model, $rel, [$foreign1, $foreign2]); $link->remove($foreign1); $link->add($foreign3); $rel->update($link); $this->assertEquals(2, $foreign2->countryId); $this->assertEquals(2, $foreign3->countryId); }
/** * @covers ::getModels */ public function testGetModels() { $model1 = new Address(); $model2 = new Post(); $model3 = new Post(); $links = new Links(new User()); $linkOne = new LinkOne(new User(), User::getRepo()->getRel('address'), new Address()); $linkMany = new LinkMany(new User(), User::getRepo()->getRel('posts'), []); $linkOne->set($model1); $linkMany->add($model2)->add($model3); $links->add($linkOne)->add($linkMany); $result = $links->getModels(); $this->assertInstanceOf('Harp\\Harp\\Model\\Models', $result); $this->assertTrue($result->has($model1)); $this->assertTrue($result->has($model2)); $this->assertTrue($result->has($model3)); }
/** * @covers ::add */ public function testAdd() { $save = new Save(); $models = [0 => new User(), 1 => new Address(), 2 => new Country(), 3 => new City(), 4 => new City(), 5 => new City(), 6 => new Address()]; $link1 = new LinkOne($models[0], User::getRepo()->getRel('address'), $models[1]); $link1->set($models[6]); $link2 = new LinkMany($models[2], Country::getRepo()->getRel('cities'), [$models[3], $models[4]]); $link2->remove($models[3]); $link2->add($models[5]); User::getRepo()->addLink($link1); Country::getRepo()->addLink($link2); $save->add($models[0])->add($models[2]); $this->assertCount(count($models), $save); foreach ($models as $model) { $this->assertTrue($save->has($model)); } }
public function insert(LinkMany $link) { $inserted = new Models(); if (count($link->getAdded()) > 0) { $through = $link->getModel()->getLink($this->through); $repo = $this->getThroughRepo(); foreach ($link->getAdded() as $added) { $inserted->add($repo->newModel([$this->getKey() => $link->getModel()->getId(), $this->getForeignKey() => $added->getId()])); } $through->get()->addAll($inserted); } return $inserted; }
/** * @covers ::delete * @covers ::insert */ public function testUpdate() { $rel = new HasManyThrough('tags', Post::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\Tag', 'postTags'); $model = new Post(['id' => 2]); $foreign1 = new Tag(['id' => 5]); $foreign2 = new Tag(['id' => 6]); $foreign3 = new Tag(['id' => 7]); $link1 = new PostTag(['tagId' => 5, 'postId' => 2], State::SAVED); $link2 = new PostTag(['tagId' => 6, 'postId' => 2], State::SAVED); $postTagsLink = new LinkMany($model, Post::getRepo()->getRel('postTags'), [$link1, $link2]); Post::getRepo()->addLink($postTagsLink); $link = new LinkMany($model, $rel, [$foreign1, $foreign2]); $link->remove($foreign1); $link->add($foreign3); $result = $rel->delete($link); $this->assertCount(1, $result); $this->assertSame($link1, $result->getFirst()); $this->assertTrue($result->getFirst()->isDeleted()); $this->assertFalse($postTagsLink->has($link1)); $this->assertTrue($postTagsLink->has($link2)); $result = $rel->insert($link); $this->assertCount(1, $result); $this->assertInstanceOf('Harp\\Harp\\Test\\TestModel\\PostTag', $result->getFirst()); $this->assertTrue($result->getFirst()->isPending()); $this->assertEquals(['postId' => 2, 'tagId' => 7, 'id' => null], $result->getFirst()->getProperties()); $this->assertTrue($postTagsLink->has($result->getFirst())); }
/** * @covers ::update */ public function testUpdate() { $rel = new HasManyAs('users', City::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\User', 'location'); $model = new City(['id' => 2]); $foreign1 = new User(['locationId' => 2, 'locationClass' => 'Harp\\Harp\\Test\\TestModel\\City']); $foreign2 = new User(['locationId' => 2, 'locationClass' => 'Harp\\Harp\\Test\\TestModel\\City']); $foreign3 = new User(['locationId' => 8, 'locationClass' => 'Harp\\Harp\\Test\\TestModel\\Country']); $link = new LinkMany($model, $rel, [$foreign1, $foreign2]); $link->remove($foreign1); $link->add($foreign3); $rel->update($link); $this->assertEquals(null, $foreign1->locationId); $this->assertEquals(null, $foreign1->locationClass); $this->assertEquals(2, $foreign2->locationId); $this->assertEquals('Harp\\Harp\\Test\\TestModel\\City', $foreign2->locationClass); $this->assertEquals(2, $foreign3->locationId); $this->assertEquals('Harp\\Harp\\Test\\TestModel\\City', $foreign3->locationClass); }
/** * @covers ::current * @covers ::key * @covers ::next * @covers ::rewind * @covers ::valid */ public function testIterator() { $models = [new City(), new City()]; $link = new LinkMany(new Country(), Country::getRepo()->getRel('cities'), $models); $key = $link->key(); foreach ($link as $i => $item) { $this->assertSame(current($models), $item); next($models); } }