/** * @covers ::getIdentityKey */ public function testGetIdentityKey() { $city = new City(); $this->assertNull($city->getIdentityKey()); $city = new City(['id' => 10]); $this->assertNull($city->getIdentityKey()); $city = new City(['id' => 10], State::SAVED); $this->assertEquals(10, $city->getIdentityKey()); }
/** * @covers ::__construct * @covers ::getModel * @covers ::getRel */ public function testConstruct() { $city = new City(); $rel = new BelongsTo('test', City::getRepo()->getConfig(), City::getRepo()); $link = $this->getMockForAbstractClass('Harp\\Harp\\Repo\\AbstractLink', [$city, $rel]); $this->assertSame($rel, $link->getRel()); $this->assertSame($city, $link->getModel()); }
/** * @covers ::executeModels */ public function testExecuteModels() { $models = new Models([new City(['id' => 5]), new City(['id' => 12])]); $delete = $this->getMock('Harp\\Harp\\Query\\Delete', ['models', 'execute'], [City::getRepo()]); $delete->expects($this->at(0))->method('models')->with($this->identicalTo($models))->will($this->returnSelf()); $delete->expects($this->at(1))->method('execute'); $delete->executeModels($models); }
/** * @covers ::__construct * @covers ::getRepo */ public function testConstruct() { $repo = City::getRepo(); $select = new Select($repo); $this->assertSame($repo, $select->getRepo()); $this->assertEquals([new SQL\Aliased('City')], $select->getFrom()); $this->assertEquals([new SQL\Aliased(new SQL\SQL('`City`.*'))], $select->getColumns()); }
/** * @covers Harp\Harp\Model\UnmappedPropertiesTrait */ public function testAll() { $object = new City(); $this->assertEmpty($object->getUnmapped()); $this->assertFalse(isset($object->test3)); $object->name = 'val1'; $object->id = 20; $this->assertEmpty($object->getUnmapped()); $this->assertFalse(isset($object->test3)); $this->assertFalse(isset($object->test4)); $object->test3 = 'val3'; $object->test4 = 'val4'; $expected = ['test3' => 'val3', 'test4' => 'val4']; $this->assertEquals($expected, $object->getUnmapped()); $this->assertEquals('val3', $object->test3); $this->assertEquals('val4', $object->test4); $this->assertTrue(isset($object->test4)); $this->assertTrue(isset($object->test4)); }
/** * @covers ::executeModels */ public function testExecuteModels() { $models1 = new Models([new City(), new City()]); $models2 = new Models([new City()]); $update = $this->getMock('Harp\\Harp\\Query\\Update', ['models', 'model', 'execute'], [City::getRepo()]); $update->expects($this->at(0))->method('models')->with($this->identicalTo($models1))->will($this->returnSelf()); $update->expects($this->at(1))->method('execute'); $update->expects($this->at(2))->method('model')->with($this->identicalTo($models2->getFirst()))->will($this->returnSelf()); $update->executeModels($models1); $update->executeModels($models2); }
/** * @covers ::getKey * @covers ::getForeignKey */ public function testConstruct() { $rel = new HasOne('test', Country::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\City'); $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('countryId', $rel->getForeignKey()); $rel = new HasOne('test', Country::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\City', array('foreignKey' => 'test')); $this->assertSame('test', $rel->getForeignKey()); }
/** * @covers ::executeModels */ public function testExecuteModels() { $models = new Models([new City(), new City()]); $insert = $this->getMock('Harp\\Harp\\Query\\Insert', ['models', 'execute', 'getLastInsertId'], [City::getRepo()]); $insert->expects($this->at(0))->method('models')->with($this->identicalTo($models))->will($this->returnSelf()); $insert->expects($this->at(1))->method('execute'); $insert->expects($this->at(2))->method('getLastInsertId')->will($this->returnValue(32)); $insert->executeModels($models); $this->assertEquals(32, $models->getFirst()->getId()); $this->assertEquals(33, $models->getNext()->getId()); }
/** * @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()); }
public function getMockForMethod($method, $arguments) { $find = $this->getMock('Harp\\Harp\\Find', ['getSelect'], [City::getRepo()]); $select = $this->getMock('Harp\\Harp\\Query\\Select', [$method], [City::getRepo()]); $methodMock = $select->expects($this->once())->method($method)->will($this->returnSelf()); if ($arguments) { $argumentConstraints = array_map(function ($argument) { return $this->equalTo($argument); }, $arguments); call_user_func_array([$methodMock, 'with'], $argumentConstraints); } $find->expects($this->once())->method('getSelect')->will($this->returnValue($select)); return $find; }
/** * @covers ::getCurrentAndOriginal */ public function testGetCurrentAndOriginal() { $model = new Country(); $model2 = new Country(); $link = new LinkOne(new City(), City::getRepo()->getRel('country'), $model); $result = $link->getCurrentAndOriginal(); $this->assertTrue($result->has($model)); $this->assertCount(1, $result); $link->set($model2); $result = $link->getCurrentAndOriginal(); $this->assertTrue($result->has($model)); $this->assertTrue($result->has($model2)); $this->assertCount(2, $result); }
/** * @covers ::resetOriginals */ public function testResetOriginals() { $model = new City(['id' => 1, 'name' => 'test 2']); $expected = ['id' => 1, 'name' => 'test 2', 'countryId' => null]; $model->name = 'test 3'; $model->id = 4; $this->assertEquals($expected, $model->getOriginals()); $model->resetOriginals(); $expected = ['id' => 4, 'name' => 'test 3', 'countryId' => null]; $this->assertEquals($expected, $model->getOriginals()); }
/** * @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 ::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'); }
public function getRel() { return $this->getMockForAbstractClass('Harp\\Harp\\Rel\\AbstractRelMany', ['test name', City::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\City']); }
/** * @covers ::delete * @expectedException LogicException */ public function testDeletePending() { $model = new City(null, State::PENDING); $model->delete(); }
/** * @coversNothing */ public function testLoadCount() { $count = City::where('countryId', 2)->loadCount(); $this->assertSame(2, $count); $this->assertQueries(['SELECT COUNT(`City`.`id`) AS `countAll` FROM `City` WHERE (`countryId` = 2)']); }
/** * @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 ::join */ public function testJoin() { $rel = new HasManyAs('users', City::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\User', 'location'); $select = new Select(City::getRepo()); $rel->join($select, 'City'); $this->assertEquals('SELECT `City`.* FROM `City` JOIN `User` AS `users` ON `users`.`locationId` = `City`.`id` AND `users`.`locationClass` = "Harp\\Harp\\Test\\TestModel\\City" AND `users`.`deletedAt` IS NULL', $select->humanize()); }
/** * @covers ::initialize */ public function testInitialize() { Container::clear(); $reflection = new ReflectionModel('Harp\\Harp\\Test\\TestModel\\City'); $reflection->initialize(City::getRepo()->getConfig()); }
/** * @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 ::get * @expectedException InvalidArgumentException */ public function testInvalidModel() { $map = new LinkMap(City::getRepo()); $model = new Country(); $map->get($model); }
/** * @covers ::getSoftDeleteConditions */ public function testGetSoftDeleteConditions() { $rel = $this->getMockForAbstractClass('Harp\\Harp\\Rel\\AbstractRel', ['test', City::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\City']); $this->assertEquals([], $rel->getSoftDeleteConditions()); $rel = $this->getMockForAbstractClass('Harp\\Harp\\Rel\\AbstractRel', ['test', User::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\User']); $this->assertEquals(['test.deletedAt' => new SQL('IS NULL')], $rel->getSoftDeleteConditions()); }
/** * @covers ::join */ public function testJoin() { $rel = new BelongsTo('country', City::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\Country'); $select = new Select(City::getRepo()); $rel->join($select, 'City'); $this->assertEquals('SELECT `City`.* FROM `City` JOIN `Country` AS `country` ON `country`.`id` = `City`.`countryId`', $select->humanize()); }
/** * @covers ::add */ public function testAdd() { $models = new RepoModels(City::getRepo()); $model = new City(); $models->add($model); $this->assertSame([$model], Objects::toArray($models->all())); }