/** * @covers ::models */ public function testModels() { $repo = Country::getRepo(); $insert = new Insert($repo); $models = new Models([new Country(['name' => 'test']), new City(['name' => 'test2'])]); $insert->models($models); $this->assertEquals('INSERT INTO `Country` (`id`, `name`) VALUES (NULL, "test"), (NULL, "test2")', $insert->humanize()); }
/** * @covers ::model */ public function testModel() { $repo = Country::getRepo(); $update = new Update($repo); $model = new Country(['id' => 1], State::SAVED); $model->name = 'test'; $update->model($model); $this->assertEquals('UPDATE `Country` SET `name` = "test" WHERE (`id` = 1)', $update->humanize()); }
/** * @covers ::__construct * @covers ::getKey * @covers ::getForeignKey */ public function testConstruct() { $rel = new BelongsTo('test', City::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\Country'); $this->assertSame('test', $rel->getName()); $this->assertSame(City::getRepo()->getConfig(), $rel->getConfig()); $this->assertSame(Country::getRepo(), $rel->getRepo()); $this->assertSame('testId', $rel->getKey()); $this->assertSame('id', $rel->getForeignKey()); $rel = new BelongsTo('test', City::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\Country', array('key' => 'test')); $this->assertSame('test', $rel->getKey()); }
/** * @covers ::__construct * @covers ::getKey * @covers ::getForeignKey * @covers ::getForeignClassKey */ public function testConstruct() { $rel = new HasManyAs('test', Country::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\City', 'parent'); $this->assertSame('test', $rel->getName()); $this->assertSame(Country::getRepo()->getConfig(), $rel->getConfig()); $this->assertSame(City::getRepo(), $rel->getRepo()); $this->assertSame('id', $rel->getKey()); $this->assertSame('parentId', $rel->getForeignKey()); $this->assertSame('parentClass', $rel->getForeignClassKey()); $rel = new HasManyAs('test', City::getRepo()->getConfig(), Country::getRepo(), 'parent', ['foreignKey' => 'test', 'foreignClassKey' => 'testClass']); $this->assertSame('test', $rel->getForeignKey()); $this->assertSame('testClass', $rel->getForeignClassKey()); }
/** * @covers ::__construct * @covers ::getKey * @covers ::getClassKey * @covers ::getForeignKey */ public function testConstruct() { $rel = new BelongsToPolymorphic('test', User::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\Country'); $this->assertSame('test', $rel->getName()); $this->assertSame(User::getRepo()->getConfig(), $rel->getConfig()); $this->assertSame(Country::getRepo(), $rel->getRepo()); $this->assertSame('testId', $rel->getKey()); $this->assertSame('testClass', $rel->getClassKey()); $this->assertSame('id', $rel->getForeignKey()); $rel = new BelongsToPolymorphic('test', City::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\Country', ['key' => 'test', 'classKey' => 'testClass']); $this->assertSame('test', $rel->getKey()); $this->assertSame('testClass', $rel->getClassKey()); }
/** * @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 ::update */ public function testNoUpdate() { $rel = $this->getMock(__NAMESPACE__ . '\\TestBelongsTo', ['update'], ['test', City::getRepo()->getConfig(), Country::getRepo()]); $link = new LinkOne(new City(), $rel, new Country()); $result = $link->update(); $this->assertNull($result); }
/** * @covers ::join */ public function testJoin() { $rel = new HasOne('city', Country::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\City'); $select = new Select(Country::getRepo()); $rel->join($select, 'Country'); $this->assertEquals('SELECT `Country`.* FROM `Country` JOIN `City` AS `city` ON `city`.`countryId` = `Country`.`id`', $select->humanize()); }
/** * @covers ::byRepo */ public function testByRepo() { $source = [0 => new City(), 1 => new City(), 2 => new Country(), 3 => new City(), 4 => new Country()]; $models = new Models($source); $expected = [[City::getRepo(), [$source[0], $source[1], $source[3]]], [Country::getRepo(), [$source[2], $source[4]]]]; $i = 0; $models->byRepo(function ($repo, Models $repoModels) use($expected, &$i) { $this->assertSame($expected[$i][0], $repo); $this->assertSame($expected[$i][1], Objects::toArray($repoModels->all())); $i++; }); }
/** * @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); } }
/** * @covers ::loadRelFor */ public function testLoadRelFor() { $citiesArray = [new City(), new City()]; $countriesArray = [new Country(), new Country()]; $cities = new Models($citiesArray); $countries = new Models($countriesArray); $mockRel = $this->getMock('Harp\\Harp\\Rel\\BelongsTo', ['loadModelsIfAvailable', 'areLinked'], ['country', City::getRepo()->getConfig(), Country::getRepo()]); City::getRepo()->getConfig()->addRel($mockRel); $mockRel->expects($this->once())->method('loadModelsIfAvailable')->with($this->identicalTo($cities), $this->equalTo(State::DELETED))->will($this->returnValue($countries)); $map = [[$citiesArray[0], $countriesArray[0], true], [$citiesArray[1], $countriesArray[0], false], [$citiesArray[0], $countriesArray[1], false], [$citiesArray[1], $countriesArray[1], true]]; $mockRel->expects($this->exactly(4))->method('areLinked')->will($this->returnValueMap($map)); $result = City::getRepo()->loadRelFor($cities, 'country', State::DELETED); $this->assertSame($countries, $result); $link1 = City::getRepo()->loadLink($citiesArray[0], 'country'); $link2 = City::getRepo()->loadLink($citiesArray[1], 'country'); $this->assertSame($countriesArray[0], $link1->get()); $this->assertSame($countriesArray[1], $link2->get()); }
/** * @covers ::loadFirst */ public function testLoadFirst() { $repo = Country::getRepo(); $find = $this->getMock('Harp\\Harp\\Find', ['limit', 'load'], [$repo]); $model = new Country(['id' => 300]); $models = new RepoModels($repo, [$model]); $emptyModels = new RepoModels($repo); $find->expects($this->exactly(2))->method('limit')->with($this->equalTo(1))->will($this->returnSelf()); $find->expects($this->exactly(2))->method('load')->will($this->onConsecutiveCalls($models, $emptyModels)); $result = $find->loadFirst(); $this->assertSame($model, $result); $result = $find->loadFirst(); $this->assertInstanceOf('Harp\\Harp\\Test\\TestModel\\Country', $result); $this->assertTrue($result->isVoid()); }
/** * @covers ::all */ public function testGetLinkMany() { $country = Country::find(1); $city = City::find(1); $cities = $country->all('cities'); $this->assertSame($cities, Country::getRepo()->loadLink($country, 'cities')); $this->setExpectedException('LogicException', 'Rel country in Harp\\Harp\\Test\\TestModel\\City must be a link of a HasMany, HasManyThrough or other AbstractRelMany'); $city->all('country'); }
/** * @dataProvider dataRelModifiers * @covers ::addFromDeleteRels * @covers ::addFromInsertRels * @covers ::addFromUpdateRels */ public function testRelModifiers($method, $trigger) { $save = new Save(); $model1 = new City(); $model2 = new City(); $model3 = new Country(); $model4 = new Country(); $model5 = new City(); $rel = $this->getMock(__NAMESPACE__ . '\\TestBelongsTo', [$method], ['country', City::getRepo()->getConfig(), Country::getRepo()]); City::getRepo()->getConfig()->addRel($rel); $link1 = new LinkOne($model1, $rel, $model3); $link2 = new LinkOne($model2, $rel, $model4); City::getRepo()->addLink($link1)->addLink($link2); $save->add($model1)->add($model2); $rel->expects($this->at(0))->method($method)->with($this->identicalTo($link1))->will($this->returnValue(new Models([$model5]))); $rel->expects($this->at(1))->method($method)->with($this->identicalTo($link2))->will($this->returnValue(null)); $this->assertFalse($save->has($model5)); $save->{$trigger}(); $this->assertTrue($save->has($model5)); }
/** * @covers ::add * @expectedException InvalidArgumentException */ public function testAddInvalid() { $models = new RepoModels(Country::getRepo()); $models->add(new City()); }