Ejemplo n.º 1
0
 /**
  * @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);
 }
Ejemplo n.º 2
0
 /**
  * @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());
 }
Ejemplo n.º 3
0
 /**
  * @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());
 }
Ejemplo n.º 4
0
 /**
  * @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());
 }
Ejemplo n.º 5
0
 /**
  * @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());
 }
Ejemplo n.º 6
0
 /**
  * @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);
 }
Ejemplo n.º 7
0
 /**
  * @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());
 }
Ejemplo n.º 8
0
 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;
 }
Ejemplo n.º 9
0
 /**
  * @covers ::getValidationAsserts
  */
 public function testGetValidationAsserts()
 {
     $city = new City();
     $this->assertSame(City::getRepo()->getAsserts(), $city->getValidationAsserts());
 }
Ejemplo n.º 10
0
 public function getRel()
 {
     return $this->getMockForAbstractClass('Harp\\Harp\\Rel\\AbstractRelMany', ['test name', City::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\City']);
 }
Ejemplo n.º 11
0
 /**
  * @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));
 }
Ejemplo n.º 12
0
 /**
  * @covers ::get
  * @expectedException InvalidArgumentException
  */
 public function testInvalidModel()
 {
     $map = new LinkMap(City::getRepo());
     $model = new Country();
     $map->get($model);
 }
Ejemplo n.º 13
0
 /**
  * @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());
 }
Ejemplo n.º 14
0
 /**
  * @covers ::initialize
  */
 public function testInitialize()
 {
     Container::clear();
     $reflection = new ReflectionModel('Harp\\Harp\\Test\\TestModel\\City');
     $reflection->initialize(City::getRepo()->getConfig());
 }
Ejemplo n.º 15
0
 /**
  * @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++;
     });
 }
Ejemplo n.º 16
0
 /**
  * @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());
 }
Ejemplo n.º 17
0
 /**
  * @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);
 }
Ejemplo n.º 18
0
 /**
  * @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());
 }
Ejemplo n.º 19
0
 /**
  * @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());
 }
Ejemplo n.º 20
0
 /**
  * @covers ::add
  */
 public function testAdd()
 {
     $models = new RepoModels(City::getRepo());
     $model = new City();
     $models->add($model);
     $this->assertSame([$model], Objects::toArray($models->all()));
 }