/** * @covers ::initializeModel */ public function testInitializeModel() { $user = new User(); $user->object = 'C:41:"Harp\\Harp\\Test\\Integration\\SaveableObject":22:{a:1:{i:0;s:5:"test1";}}'; $expected = new SaveableObject(); $expected->setVar('test1'); User::getRepo()->initializeModel($user); $this->assertEquals($expected, $user->object); $post = new BlogPost(['class' => null]); BlogPost::getRepo()->initializeModel($post); $this->assertEquals('Harp\\Harp\\Test\\TestModel\\BlogPost', $post->class); }
/** * @covers ::findAllWhereIn */ public function testFindAllWhereIn() { $rel = $this->getMockForAbstractClass('Harp\\Harp\\Rel\\AbstractRel', ['test', User::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\User']); $find = $rel->findAllWhereIn('name', [10, 13], State::DELETED); $expected = 'SELECT `User`.* FROM `User` WHERE (`name` IN (10, 13)) AND (`User`.`deletedAt` IS NOT NULL)'; $this->assertEquals($expected, $find->applyFlags()->humanize()); }
/** * @covers ::get */ public function testGet() { $links = new Links(new User()); $linkOne = new LinkOne(new User(), User::getRepo()->getRel('address'), new Address()); $this->assertNull($links->get('address')); $links->add($linkOne); $this->assertEquals($linkOne, $links->get('address')); }
/** * @covers ::join * @expectedException BadMethodCallException */ public function testJoin() { $rel = new BelongsToPolymorphic('location', User::getRepo()->getConfig(), 'Harp\\Harp\\Test\\TestModel\\Country'); $select = new Select(User::getRepo()); $rel->join($select, 'City'); }
/** * @covers ::joinNestedRels * @expectedException InvalidArgumentException */ public function testJoinRelsError() { $repo = User::getRepo(); $select = new Select($repo); $select->joinRels(['unknown address']); }
/** * @covers ::onlyDeleted * @covers ::onlySaved * @covers ::deletedAndSaved */ public function testFlagSetters() { $repo = User::getRepo(); $find = new Find($repo); $find->onlyDeleted(); $this->assertSame(State::DELETED, $find->getFlags()); $find->onlySaved(); $this->assertSame(State::SAVED, $find->getFlags()); $find->deletedAndSaved(); $this->assertSame(State::DELETED | State::SAVED, $find->getFlags()); }
/** * @covers ::saveArray */ public function testSaveArray() { $posts = Post::findAll()->load()->toArray(); $posts[0]->name = 'new post name 1'; $posts[1]->name = 'new post name 2'; Post::saveArray($posts); User::getRepo()->getIdentityMap()->clear(); $posts = Post::findAll()->load(); $this->assertEquals('new post name 1', $posts->getFirst()->name); $this->assertEquals('new post name 2', $posts->getNext()->name); }
/** * @covers ::eachLink */ public function testEachLink() { $save = new Save(); $model1 = new User(); $model2 = new Country(); $model3 = new City(); $link1 = new LinkOne($model1, User::getRepo()->getRel('location'), $model2); $link2 = new LinkMany($model2, Country::getRepo()->getRel('cities'), []); User::getRepo()->addLink($link1); Country::getRepo()->addLink($link2); $save->add($model1); $i = 0; $expected = [[$model1, $link1], [$model2, $link2]]; $save->eachLink(function (AbstractLink $link) use($expected, $model3, &$i) { $this->assertSame($expected[$i][0], $link->getModel()); $this->assertSame($expected[$i][1], $link); $i++; return new Models([$model3]); }); $this->assertTrue($save->has($model3)); }