/** * @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)); } }
/** * @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 ::getCurrentAndOriginal */ public function testGetCurrentAndOriginal() { $models = [new City(), new City()]; $link = new LinkMany(new Country(), Country::getRepo()->getRel('cities'), $models); $model1 = new City(); $model2 = new City(); $link->add($model1)->add($model2)->remove($models[0]); $result = $link->getCurrentAndOriginal(); $this->assertInstanceOf('Harp\\Harp\\Model\\Models', $result); $this->assertCount(4, $result); $this->assertTrue($result->has($model1)); $this->assertTrue($result->has($model2)); $this->assertTrue($result->has($models[0])); $this->assertTrue($result->has($models[1])); }